Wouldn't it be incredible if you could run Unit Tests without all of your uploads being indiscriminately blown away and your upload folder permissions being destroyed?

The Future Is Now.

Fixes #28847.


git-svn-id: https://develop.svn.wordpress.org/trunk@29120 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-07-12 07:08:15 +00:00
parent 04b0ec782b
commit d97b10eecf
15 changed files with 125 additions and 48 deletions

View File

@ -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;
}
}

View File

@ -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.
*/

View File

@ -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();
}

View File

@ -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' );

View File

@ -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 ) );
}
}

View File

@ -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

View File

@ -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 );
}
}

View File

@ -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 );
}
}

View File

@ -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

View File

@ -1,11 +1,15 @@
<?php
/**
* @group image
* @group media
* @group upload
*/
class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
function tearDown() {
$this->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 );

View File

@ -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() {

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -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() {