wp_upload_dir() has a (little-known?) side effect: if you call it, it will attempt to create an uploads directory for the current month. As such, tearDown() and cleanup routines have to be in sync with this behavior when deleting bogus directories used in unit tests.

Examples: if you clean up directories in a test, or a test fails before the directories are cleaned, or a test fails before the `'upload_path'` option is reset, the next call to `wp_upload_dir()` will recreate the directories you just tried to delete.

These changes ensure that `src/foo` and `/tmp/wp-unit-test` directories are deleted immediately after `wp_upload_dir()` is fired in the tests. 
 
Fixes #30513.


git-svn-id: https://develop.svn.wordpress.org/trunk@30658 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-11-30 19:05:52 +00:00
parent 2f3fed5a52
commit 653485e87f
2 changed files with 32 additions and 10 deletions

View File

@ -483,6 +483,28 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
return $files; return $files;
} }
function delete_folders( $path ) {
$this->matched_dirs = array();
if ( ! is_dir( $path ) ) {
return;
}
$this->scandir( $path );
foreach ( array_reverse( $this->matched_dirs ) as $dir ) {
rmdir( $dir );
}
rmdir( $path );
}
function scandir( $dir ) {
foreach ( scandir( $dir ) as $path ) {
if ( 0 !== strpos( $path, '.' ) && is_dir( $dir . '/' . $path ) ) {
$this->matched_dirs[] = $dir . '/' . $path;
$this->scandir( $dir . '/' . $path );
}
}
}
/** /**
* Helper to Convert a microtime string into a float * Helper to Convert a microtime string into a float
*/ */

View File

@ -1,6 +1,4 @@
<?php <?php
/** /**
* @group upload * @group upload
* @group media * @group media
@ -10,23 +8,21 @@ class Tests_Upload extends WP_UnitTestCase {
var $siteurl; var $siteurl;
function setUp() { function setUp() {
if ( is_multisite() ) if ( is_multisite() ) {
$this->knownUTBug( 35 ); $this->knownUTBug( 35 );
}
$this->_reset_options();
parent::setUp(); parent::setUp();
return; }
function _reset_options() {
// system defaults // system defaults
update_option( 'upload_path', 'wp-content/uploads' ); update_option( 'upload_path', 'wp-content/uploads' );
update_option( 'upload_url_path', '' ); update_option( 'upload_url_path', '' );
update_option( 'uploads_use_yearmonth_folders', 1 ); update_option( 'uploads_use_yearmonth_folders', 1 );
} }
function tearDown() {
$this->remove_added_uploads();
parent::tearDown();
}
function test_upload_dir_default() { function test_upload_dir_default() {
// wp_upload_dir() with default parameters // wp_upload_dir() with default parameters
$info = wp_upload_dir(); $info = wp_upload_dir();
@ -40,6 +36,8 @@ class Tests_Upload extends WP_UnitTestCase {
// wp_upload_dir() with a relative upload path that is not 'wp-content/uploads' // wp_upload_dir() with a relative upload path that is not 'wp-content/uploads'
update_option( 'upload_path', 'foo/bar' ); update_option( 'upload_path', 'foo/bar' );
$info = wp_upload_dir(); $info = wp_upload_dir();
$this->delete_folders( ABSPATH . 'foo' );
$this->assertEquals( get_option( 'siteurl' ) . '/foo/bar/' . gmstrftime('%Y/%m'), $info['url'] ); $this->assertEquals( get_option( 'siteurl' ) . '/foo/bar/' . gmstrftime('%Y/%m'), $info['url'] );
$this->assertEquals( ABSPATH . 'foo/bar/' . gmstrftime('%Y/%m'), $info['path'] ); $this->assertEquals( ABSPATH . 'foo/bar/' . gmstrftime('%Y/%m'), $info['path'] );
$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] ); $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
@ -56,6 +54,8 @@ class Tests_Upload extends WP_UnitTestCase {
// doesn't make sense to use an absolute file path without setting the url path // doesn't make sense to use an absolute file path without setting the url path
update_option( 'upload_url_path', '/baz' ); update_option( 'upload_url_path', '/baz' );
$info = wp_upload_dir(); $info = wp_upload_dir();
$this->delete_folders( $path );
$this->assertEquals( '/baz/' . gmstrftime('%Y/%m'), $info['url'] ); $this->assertEquals( '/baz/' . gmstrftime('%Y/%m'), $info['url'] );
$this->assertEquals( "$path/" . gmstrftime('%Y/%m'), $info['path'] ); $this->assertEquals( "$path/" . gmstrftime('%Y/%m'), $info['path'] );
$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] ); $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );