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:
parent
04b0ec782b
commit
d97b10eecf
@ -11,6 +11,8 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
|
|||||||
protected $expected_doing_it_wrong = array();
|
protected $expected_doing_it_wrong = array();
|
||||||
protected $caught_doing_it_wrong = array();
|
protected $caught_doing_it_wrong = array();
|
||||||
|
|
||||||
|
protected static $ignore_files;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var WP_UnitTest_Factory
|
* @var WP_UnitTest_Factory
|
||||||
*/
|
*/
|
||||||
@ -19,6 +21,10 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
|
|||||||
function setUp() {
|
function setUp() {
|
||||||
set_time_limit(0);
|
set_time_limit(0);
|
||||||
|
|
||||||
|
if ( ! self::$ignore_files ) {
|
||||||
|
self::$ignore_files = $this->scan_user_uploads();
|
||||||
|
}
|
||||||
|
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$wpdb->suppress_errors = false;
|
$wpdb->suppress_errors = false;
|
||||||
$wpdb->show_errors = true;
|
$wpdb->show_errors = true;
|
||||||
@ -325,4 +331,54 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
|
|||||||
$message .= implode( $not_false, ', ' ) . ' should be false.';
|
$message .= implode( $not_false, ', ' ) . ' should be false.';
|
||||||
$this->assertTrue( $passed, $message );
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -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.
|
* Removes the post type and its taxonomy associations.
|
||||||
*/
|
*/
|
||||||
|
@ -36,10 +36,6 @@ class Tests_Ajax_MediaEdit extends WP_Ajax_UnitTestCase {
|
|||||||
wp_delete_attachment( $id, true );
|
wp_delete_attachment( $id, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
$uploads = wp_upload_dir();
|
|
||||||
foreach ( scandir( $uploads['basedir'] ) as $file )
|
|
||||||
_rmdir( $uploads['basedir'] . '/' . $file );
|
|
||||||
|
|
||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ class Test_Functions_Deprecated extends WP_UnitTestCase {
|
|||||||
$img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' );
|
$img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' );
|
||||||
wp_save_image_file( $file, $img, 'image/jpeg', 1 );
|
wp_save_image_file( $file, $img, 'image/jpeg', 1 );
|
||||||
imagedestroy( $img );
|
imagedestroy( $img );
|
||||||
@unlink($file);
|
unlink( $file );
|
||||||
|
|
||||||
// Check if the arg was deprecated
|
// Check if the arg was deprecated
|
||||||
$check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
|
$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' );
|
$img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
|
||||||
wp_save_image_file( $file, $img, 'image/jpeg', 1 );
|
wp_save_image_file( $file, $img, 'image/jpeg', 1 );
|
||||||
unset( $img );
|
unset( $img );
|
||||||
@unlink($file);
|
unlink( $file );
|
||||||
|
|
||||||
// Check if the arg was deprecated
|
// Check if the arg was deprecated
|
||||||
$check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
|
$check = $this->was_deprecated( 'argument', 'wp_save_image_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
|
* Set the image editor engine according to the unit test's specification
|
||||||
*/
|
*/
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
if ( ! call_user_func( array( $this->editor_engine, 'test' ) ) ) {
|
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) );
|
$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
|
* Undo the image editor override
|
||||||
*/
|
*/
|
||||||
public function tearDown() {
|
public function tearDown() {
|
||||||
|
parent::tearDown();
|
||||||
|
|
||||||
remove_filter( 'wp_image_editors', array( $this, 'setEngine' ), 10, 2 );
|
remove_filter( 'wp_image_editors', array( $this, 'setEngine' ), 10, 2 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
* @group media
|
* @group media
|
||||||
* @group wp-image-editor-gd
|
* @group wp-image-editor-gd
|
||||||
*/
|
*/
|
||||||
|
require_once( dirname( __FILE__ ) . '/base.php' );
|
||||||
|
|
||||||
class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
|
class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
|
||||||
|
|
||||||
@ -18,14 +19,16 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shutDown() {
|
public function tearDown() {
|
||||||
$folder = DIR_TESTDATA . '/images/waffles-*.jpg';
|
$folder = DIR_TESTDATA . '/images/waffles-*.jpg';
|
||||||
|
|
||||||
foreach ( glob( $folder ) as $file ) {
|
foreach ( glob( $folder ) as $file ) {
|
||||||
unlink( $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 );
|
$editor->save( $save_to_file );
|
||||||
|
|
||||||
$this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
|
$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 );
|
$editor->save( $save_to_file );
|
||||||
|
|
||||||
$this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
|
$this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
|
||||||
|
|
||||||
|
unlink( $save_to_file );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
* @group media
|
* @group media
|
||||||
* @group wp-image-editor-imagick
|
* @group wp-image-editor-imagick
|
||||||
*/
|
*/
|
||||||
|
require_once( dirname( __FILE__ ) . '/base.php' );
|
||||||
|
|
||||||
class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
|
class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
|
||||||
|
|
||||||
@ -18,14 +19,16 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shutDown() {
|
public function tearDown() {
|
||||||
$folder = DIR_TESTDATA . '/images/waffles-*.jpg';
|
$folder = DIR_TESTDATA . '/images/waffles-*.jpg';
|
||||||
|
|
||||||
foreach ( glob( $folder ) as $file ) {
|
foreach ( glob( $folder ) as $file ) {
|
||||||
unlink( $file );
|
unlink( $file );
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::shutDown();
|
$this->remove_added_uploads();
|
||||||
|
|
||||||
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -467,6 +470,8 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
|
|||||||
$editor->save( $save_to_file );
|
$editor->save( $save_to_file );
|
||||||
|
|
||||||
$this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
|
$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 );
|
$editor->save( $save_to_file );
|
||||||
|
|
||||||
$this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
|
$this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
|
||||||
|
|
||||||
|
unlink( $save_to_file );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -144,8 +144,8 @@ class Tests_Image_Functions extends WP_UnitTestCase {
|
|||||||
$this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
|
$this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
@unlink( $file );
|
unlink( $file );
|
||||||
@unlink( $ret['path'] );
|
unlink( $ret['path'] );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
@ -185,8 +185,8 @@ class Tests_Image_Functions extends WP_UnitTestCase {
|
|||||||
$this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
|
$this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
@unlink( $file );
|
unlink( $file );
|
||||||
@unlink( $ret['path'] );
|
unlink( $ret['path'] );
|
||||||
unset( $img );
|
unset( $img );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -231,8 +231,7 @@ class Tests_Image_Functions extends WP_UnitTestCase {
|
|||||||
$this->assertNotEmpty( $ret );
|
$this->assertNotEmpty( $ret );
|
||||||
$this->assertNotInstanceOf( 'WP_Error', $ret );
|
$this->assertNotInstanceOf( 'WP_Error', $ret );
|
||||||
$this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
|
$this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
|
||||||
@unlink( $file );
|
unlink( $ret['path'] );
|
||||||
@unlink( $ret['path'] );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group image
|
* @group image
|
||||||
* @group media
|
* @group media
|
||||||
* @group upload
|
* @group upload
|
||||||
*/
|
*/
|
||||||
class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
|
class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
|
||||||
|
function tearDown() {
|
||||||
|
$this->remove_added_uploads();
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
function test_make_intermediate_size_no_size() {
|
function test_make_intermediate_size_no_size() {
|
||||||
$image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false );
|
$image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false );
|
||||||
|
|
||||||
|
@ -4,7 +4,10 @@
|
|||||||
* @group image
|
* @group image
|
||||||
* @group media
|
* @group media
|
||||||
* @group upload
|
* @group upload
|
||||||
|
* @group resize
|
||||||
*/
|
*/
|
||||||
|
require_once( dirname( __FILE__ ) . '/base.php' );
|
||||||
|
|
||||||
abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase {
|
abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase {
|
||||||
|
|
||||||
function test_resize_jpg() {
|
function test_resize_jpg() {
|
||||||
|
@ -4,7 +4,10 @@
|
|||||||
* @group image
|
* @group image
|
||||||
* @group media
|
* @group media
|
||||||
* @group upload
|
* @group upload
|
||||||
|
* @group resize
|
||||||
*/
|
*/
|
||||||
|
require_once( dirname( __FILE__ ) . '/resize.php' );
|
||||||
|
|
||||||
class Test_Image_Resize_GD extends WP_Tests_Image_Resize_UnitTestCase {
|
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
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $editor_engine = 'WP_Image_Editor_GD';
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,7 +4,10 @@
|
|||||||
* @group image
|
* @group image
|
||||||
* @group media
|
* @group media
|
||||||
* @group upload
|
* @group upload
|
||||||
|
* @group resize
|
||||||
*/
|
*/
|
||||||
|
require_once( dirname( __FILE__ ) . '/resize.php' );
|
||||||
|
|
||||||
class Test_Image_Resize_Imagick extends WP_Tests_Image_Resize_UnitTestCase {
|
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
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $editor_engine = 'WP_Image_Editor_Imagick';
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
@ -9,10 +9,7 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
|
|||||||
|
|
||||||
function tearDown() {
|
function tearDown() {
|
||||||
// Remove all uploads.
|
// Remove all uploads.
|
||||||
$uploads = wp_upload_dir();
|
$this->remove_added_uploads();
|
||||||
foreach ( scandir( $uploads['basedir'] ) as $file )
|
|
||||||
_rmdir( $uploads['basedir'] . '/' . $file );
|
|
||||||
|
|
||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,13 +22,9 @@ class Tests_Upload extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function tearDown() {
|
function tearDown() {
|
||||||
parent::tearDown();
|
$this->remove_added_uploads();
|
||||||
|
|
||||||
// Remove year/month folders created by wp_upload_dir().
|
parent::tearDown();
|
||||||
$uploads = wp_upload_dir();
|
|
||||||
foreach ( scandir( $uploads['basedir'] ) as $file )
|
|
||||||
_rmdir( $uploads['basedir'] . '/' . $file );
|
|
||||||
_rmdir( ABSPATH . 'foo/' );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_upload_dir_default() {
|
function test_upload_dir_default() {
|
||||||
|
Loading…
Reference in New Issue
Block a user