diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index b7149fcbb2..1f6a29f91d 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -11,6 +11,8 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { protected $expected_doing_it_wrong = array(); protected $caught_doing_it_wrong = array(); + protected static $ignore_files; + /** * @var WP_UnitTest_Factory */ @@ -19,6 +21,10 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { function setUp() { set_time_limit(0); + if ( ! self::$ignore_files ) { + self::$ignore_files = $this->scan_user_uploads(); + } + global $wpdb; $wpdb->suppress_errors = false; $wpdb->show_errors = true; @@ -325,4 +331,54 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $message .= implode( $not_false, ', ' ) . ' should be false.'; $this->assertTrue( $passed, $message ); } -} + + function unlink( $file ) { + $exists = is_file( $file ); + if ( $exists && ! in_array( $file, self::$ignore_files ) ) { + //error_log( $file ); + unlink( $file ); + } elseif ( ! $exists ) { + $this->fail( "Trying to delete a file that doesn't exist: $file" ); + } + } + + function rmdir( $path ) { + $files = $this->files_in_dir( $path ); + foreach ( $files as $file ) { + if ( ! in_array( $file, self::$ignore_files ) ) { + $this->unlink( $file ); + } + } + } + + function remove_added_uploads() { + // Remove all uploads. + $uploads = wp_upload_dir(); + $this->rmdir( $uploads['basedir'] ); + } + + function files_in_dir( $dir ) { + $files = array(); + + $iterator = new RecursiveDirectoryIterator( $dir ); + $objects = new RecursiveIteratorIterator( $iterator ); + foreach ( $objects as $name => $object ) { + if ( is_file( $name ) ) { + $files[] = $name; + } + } + + return $files; + } + + function scan_user_uploads() { + static $files = array(); + if ( ! empty( $files ) ) { + return $files; + } + + $uploads = wp_upload_dir(); + $files = $this->files_in_dir( $uploads['basedir'] ); + return $files; + } +} \ No newline at end of file diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index ae8abe7678..d6c79e9e6b 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -313,18 +313,6 @@ if ( !function_exists( 'str_getcsv' ) ) { } } -function _rmdir( $path ) { - if ( in_array(basename( $path ), array( '.', '..' ) ) ) { - return; - } elseif ( is_file( $path ) ) { - unlink( $path ); - } elseif ( is_dir( $path ) ) { - foreach ( scandir( $path ) as $file ) - _rmdir( $path . '/' . $file ); - rmdir( $path ); - } -} - /** * Removes the post type and its taxonomy associations. */ diff --git a/tests/phpunit/tests/ajax/MediaEdit.php b/tests/phpunit/tests/ajax/MediaEdit.php index 5115d9c3eb..d5c99ad00d 100644 --- a/tests/phpunit/tests/ajax/MediaEdit.php +++ b/tests/phpunit/tests/ajax/MediaEdit.php @@ -32,14 +32,10 @@ class Tests_Ajax_MediaEdit extends WP_Ajax_UnitTestCase { */ public function tearDown() { // Cleanup - foreach ($this->_ids as $id){ - wp_delete_attachment($id, true); + foreach ( $this->_ids as $id ) { + wp_delete_attachment( $id, true ); } - $uploads = wp_upload_dir(); - foreach ( scandir( $uploads['basedir'] ) as $file ) - _rmdir( $uploads['basedir'] . '/' . $file ); - parent::tearDown(); } diff --git a/tests/phpunit/tests/functions/deprecated.php b/tests/phpunit/tests/functions/deprecated.php index fd2f375da0..542ea392bb 100644 --- a/tests/phpunit/tests/functions/deprecated.php +++ b/tests/phpunit/tests/functions/deprecated.php @@ -148,7 +148,7 @@ class Test_Functions_Deprecated extends WP_UnitTestCase { $img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' ); wp_save_image_file( $file, $img, 'image/jpeg', 1 ); imagedestroy( $img ); - @unlink($file); + unlink( $file ); // Check if the arg was deprecated $check = $this->was_deprecated( 'argument', 'wp_save_image_file' ); @@ -169,7 +169,7 @@ class Test_Functions_Deprecated extends WP_UnitTestCase { $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' ); wp_save_image_file( $file, $img, 'image/jpeg', 1 ); unset( $img ); - @unlink($file); + unlink( $file ); // Check if the arg was deprecated $check = $this->was_deprecated( 'argument', 'wp_save_image_file' ); diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index a0415e9c4e..bc57ad22dd 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -284,6 +284,6 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { $res = wp_remote_get( 'https://wordpress.org/' ); $this->assertTrue( ! is_wp_error( $res ), print_r( $res, true ) ); } - - + + } diff --git a/tests/phpunit/tests/image/base.php b/tests/phpunit/tests/image/base.php index be9ab67350..7ea54883c6 100644 --- a/tests/phpunit/tests/image/base.php +++ b/tests/phpunit/tests/image/base.php @@ -9,6 +9,8 @@ abstract class WP_Image_UnitTestCase extends WP_UnitTestCase { * Set the image editor engine according to the unit test's specification */ public function setUp() { + parent::setUp(); + if ( ! call_user_func( array( $this->editor_engine, 'test' ) ) ) { $this->markTestSkipped( sprintf('The image editor engine %s is not supported on this system', $this->editor_engine) ); } @@ -20,6 +22,8 @@ abstract class WP_Image_UnitTestCase extends WP_UnitTestCase { * Undo the image editor override */ public function tearDown() { + parent::tearDown(); + remove_filter( 'wp_image_editors', array( $this, 'setEngine' ), 10, 2 ); } @@ -33,7 +37,7 @@ abstract class WP_Image_UnitTestCase extends WP_UnitTestCase { /** * Helper assertion for testing alpha on images - * + * * @param string $image_path * @param array $point array(x,y) * @param int $alpha diff --git a/tests/phpunit/tests/image/editor_gd.php b/tests/phpunit/tests/image/editor_gd.php index ca134bdb39..0e1fe608ce 100644 --- a/tests/phpunit/tests/image/editor_gd.php +++ b/tests/phpunit/tests/image/editor_gd.php @@ -6,6 +6,7 @@ * @group media * @group wp-image-editor-gd */ +require_once( dirname( __FILE__ ) . '/base.php' ); class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { @@ -18,14 +19,16 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { parent::setUp(); } - public function shutDown() { + public function tearDown() { $folder = DIR_TESTDATA . '/images/waffles-*.jpg'; foreach ( glob( $folder ) as $file ) { unlink( $file ); } - parent::shutDown(); + $this->remove_added_uploads(); + + parent::tearDown(); } /** @@ -467,6 +470,8 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { $editor->save( $save_to_file ); $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); + + unlink( $save_to_file ); } /** @@ -485,5 +490,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { $editor->save( $save_to_file ); $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); + + unlink( $save_to_file ); } } diff --git a/tests/phpunit/tests/image/editor_imagick.php b/tests/phpunit/tests/image/editor_imagick.php index ec0cd23dec..d997b9c837 100644 --- a/tests/phpunit/tests/image/editor_imagick.php +++ b/tests/phpunit/tests/image/editor_imagick.php @@ -6,6 +6,7 @@ * @group media * @group wp-image-editor-imagick */ +require_once( dirname( __FILE__ ) . '/base.php' ); class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { @@ -18,14 +19,16 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { parent::setUp(); } - public function shutDown() { + public function tearDown() { $folder = DIR_TESTDATA . '/images/waffles-*.jpg'; foreach ( glob( $folder ) as $file ) { unlink( $file ); } - parent::shutDown(); + $this->remove_added_uploads(); + + parent::tearDown(); } /** @@ -463,10 +466,12 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { $editor->load(); $editor->resize( 5, 5 ); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; - + $editor->save( $save_to_file ); $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); + + unlink( $save_to_file ); } /** @@ -485,5 +490,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { $editor->save( $save_to_file ); $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); + + unlink( $save_to_file ); } } diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index d3515fbc78..8fd7b81118 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -88,7 +88,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { // these are image files but aren't suitable for web pages because of compatibility or size issues $files = array( // 'test-image-cmyk.jpg', Allowed in r9727 - // 'test-image.bmp', Allowed in r28589 + // 'test-image.bmp', Allowed in r28589 // 'test-image-grayscale.jpg', Allowed in r9727 'test-image.pct', 'test-image.tga', @@ -144,8 +144,8 @@ class Tests_Image_Functions extends WP_UnitTestCase { $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) ); // Clean up - @unlink( $file ); - @unlink( $ret['path'] ); + unlink( $file ); + unlink( $ret['path'] ); } // Clean up @@ -185,8 +185,8 @@ class Tests_Image_Functions extends WP_UnitTestCase { $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) ); // Clean up - @unlink( $file ); - @unlink( $ret['path'] ); + unlink( $file ); + unlink( $ret['path'] ); unset( $img ); } } @@ -231,8 +231,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { $this->assertNotEmpty( $ret ); $this->assertNotInstanceOf( 'WP_Error', $ret ); $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) ); - @unlink( $file ); - @unlink( $ret['path'] ); + unlink( $ret['path'] ); } // Clean up diff --git a/tests/phpunit/tests/image/intermediate_size.php b/tests/phpunit/tests/image/intermediate_size.php index 36942e9b9f..73728e53a0 100644 --- a/tests/phpunit/tests/image/intermediate_size.php +++ b/tests/phpunit/tests/image/intermediate_size.php @@ -1,11 +1,15 @@ remove_added_uploads(); + parent::tearDown(); + } + function test_make_intermediate_size_no_size() { $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false ); diff --git a/tests/phpunit/tests/image/resize.php b/tests/phpunit/tests/image/resize.php index a4a6aaab2b..3bd4ef17b1 100644 --- a/tests/phpunit/tests/image/resize.php +++ b/tests/phpunit/tests/image/resize.php @@ -4,7 +4,10 @@ * @group image * @group media * @group upload + * @group resize */ +require_once( dirname( __FILE__ ) . '/base.php' ); + abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase { function test_resize_jpg() { @@ -108,7 +111,7 @@ abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase $this->assertEquals( 100, $h ); $this->assertEquals( IMAGETYPE_JPEG, $type ); - unlink($image); + unlink( $image ); } function test_resize_thumb_50x150_crop() { diff --git a/tests/phpunit/tests/image/resize_gd.php b/tests/phpunit/tests/image/resize_gd.php index 7fe144b012..53350ff368 100644 --- a/tests/phpunit/tests/image/resize_gd.php +++ b/tests/phpunit/tests/image/resize_gd.php @@ -4,7 +4,10 @@ * @group image * @group media * @group upload + * @group resize */ +require_once( dirname( __FILE__ ) . '/resize.php' ); + class Test_Image_Resize_GD extends WP_Tests_Image_Resize_UnitTestCase { /** @@ -12,4 +15,11 @@ class Test_Image_Resize_GD extends WP_Tests_Image_Resize_UnitTestCase { * @var string */ public $editor_engine = 'WP_Image_Editor_GD'; + + public function setUp() { + require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' ); + require_once( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' ); + + parent::setUp(); + } } \ No newline at end of file diff --git a/tests/phpunit/tests/image/resize_imagick.php b/tests/phpunit/tests/image/resize_imagick.php index 18c5f3c0ad..01a31e40c2 100644 --- a/tests/phpunit/tests/image/resize_imagick.php +++ b/tests/phpunit/tests/image/resize_imagick.php @@ -4,7 +4,10 @@ * @group image * @group media * @group upload + * @group resize */ +require_once( dirname( __FILE__ ) . '/resize.php' ); + class Test_Image_Resize_Imagick extends WP_Tests_Image_Resize_UnitTestCase { /** @@ -12,4 +15,11 @@ class Test_Image_Resize_Imagick extends WP_Tests_Image_Resize_UnitTestCase { * @var string */ public $editor_engine = 'WP_Image_Editor_Imagick'; + + public function setUp() { + require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' ); + require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' ); + + parent::setUp(); + } } \ No newline at end of file diff --git a/tests/phpunit/tests/post/attachments.php b/tests/phpunit/tests/post/attachments.php index d05c5e8ead..9548af48e5 100644 --- a/tests/phpunit/tests/post/attachments.php +++ b/tests/phpunit/tests/post/attachments.php @@ -9,10 +9,7 @@ class Tests_Post_Attachments extends WP_UnitTestCase { function tearDown() { // Remove all uploads. - $uploads = wp_upload_dir(); - foreach ( scandir( $uploads['basedir'] ) as $file ) - _rmdir( $uploads['basedir'] . '/' . $file ); - + $this->remove_added_uploads(); parent::tearDown(); } diff --git a/tests/phpunit/tests/upload.php b/tests/phpunit/tests/upload.php index 48947d8678..685976fa78 100644 --- a/tests/phpunit/tests/upload.php +++ b/tests/phpunit/tests/upload.php @@ -22,13 +22,9 @@ class Tests_Upload extends WP_UnitTestCase { } function tearDown() { - parent::tearDown(); + $this->remove_added_uploads(); - // Remove year/month folders created by wp_upload_dir(). - $uploads = wp_upload_dir(); - foreach ( scandir( $uploads['basedir'] ) as $file ) - _rmdir( $uploads['basedir'] . '/' . $file ); - _rmdir( ABSPATH . 'foo/' ); + parent::tearDown(); } function test_upload_dir_default() {