Make WordPress Core

Changeset 29120


Ignore:
Timestamp:
07/12/2014 07:08:15 AM (10 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk/tests/phpunit
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r28943 r29120  
    1212    protected $caught_doing_it_wrong = array();
    1313
     14    protected static $ignore_files;
     15
    1416    /**
    1517     * @var WP_UnitTest_Factory
     
    1921    function setUp() {
    2022        set_time_limit(0);
     23
     24        if ( ! self::$ignore_files ) {
     25            self::$ignore_files = $this->scan_user_uploads();
     26        }
    2127
    2228        global $wpdb;
     
    326332        $this->assertTrue( $passed, $message );
    327333    }
     334
     335    function unlink( $file ) {
     336        $exists = is_file( $file );
     337        if ( $exists && ! in_array( $file, self::$ignore_files ) ) {
     338            //error_log( $file );
     339            unlink( $file );
     340        } elseif ( ! $exists ) {
     341            $this->fail( "Trying to delete a file that doesn't exist: $file" );
     342        }
     343    }
     344
     345    function rmdir( $path ) {
     346        $files = $this->files_in_dir( $path );
     347        foreach ( $files as $file ) {
     348            if ( ! in_array( $file, self::$ignore_files ) ) {
     349                $this->unlink( $file );
     350            }
     351        }
     352    }
     353
     354    function remove_added_uploads() {
     355        // Remove all uploads.
     356        $uploads = wp_upload_dir();
     357        $this->rmdir( $uploads['basedir'] );
     358    }
     359
     360    function files_in_dir( $dir ) {
     361        $files = array();
     362
     363        $iterator = new RecursiveDirectoryIterator( $dir );
     364        $objects = new RecursiveIteratorIterator( $iterator );
     365        foreach ( $objects as $name => $object ) {
     366            if ( is_file( $name ) ) {
     367                $files[] = $name;
     368            }
     369        }
     370
     371        return $files;
     372    }
     373
     374    function scan_user_uploads() {
     375        static $files = array();
     376        if ( ! empty( $files ) ) {
     377            return $files;
     378        }
     379
     380        $uploads = wp_upload_dir();
     381        $files = $this->files_in_dir( $uploads['basedir'] );
     382        return $files;
     383    }
    328384}
  • trunk/tests/phpunit/includes/utils.php

    r28797 r29120  
    314314}
    315315
    316 function _rmdir( $path ) {
    317     if ( in_array(basename( $path ), array( '.', '..' ) ) ) {
    318         return;
    319     } elseif ( is_file( $path ) ) {
    320         unlink( $path );
    321     } elseif ( is_dir( $path ) ) {
    322         foreach ( scandir( $path ) as $file )
    323             _rmdir( $path . '/' . $file );
    324         rmdir( $path );
    325     }
    326 }
    327 
    328316/**
    329317 * Removes the post type and its taxonomy associations.
  • trunk/tests/phpunit/tests/ajax/MediaEdit.php

    r26088 r29120  
    3333    public function tearDown() {
    3434        // Cleanup
    35         foreach ($this->_ids as $id){
    36             wp_delete_attachment($id, true);
     35        foreach ( $this->_ids as $id ) {
     36            wp_delete_attachment( $id, true );
    3737        }
    38 
    39         $uploads = wp_upload_dir();
    40         foreach ( scandir( $uploads['basedir'] ) as $file )
    41             _rmdir( $uploads['basedir'] . '/' . $file );
    4238
    4339        parent::tearDown();
  • trunk/tests/phpunit/tests/functions/deprecated.php

    r25409 r29120  
    149149        wp_save_image_file( $file, $img, 'image/jpeg', 1 );
    150150        imagedestroy( $img );
    151         @unlink($file);
     151        unlink( $file );
    152152
    153153        // Check if the arg was deprecated
     
    170170        wp_save_image_file( $file, $img, 'image/jpeg', 1 );
    171171        unset( $img );
    172         @unlink($file);
     172        unlink( $file );
    173173
    174174        // Check if the arg was deprecated
  • trunk/tests/phpunit/tests/http/base.php

    r25224 r29120  
    285285        $this->assertTrue( ! is_wp_error( $res ), print_r( $res, true ) );
    286286    }
    287    
    288    
     287
     288
    289289}
  • trunk/tests/phpunit/tests/image/base.php

    r27794 r29120  
    1010     */
    1111    public function setUp() {
     12        parent::setUp();
     13
    1214        if ( ! call_user_func( array( $this->editor_engine, 'test' ) ) ) {
    1315            $this->markTestSkipped( sprintf('The image editor engine %s is not supported on this system', $this->editor_engine) );
     
    2123     */
    2224    public function tearDown() {
     25        parent::tearDown();
     26
    2327        remove_filter( 'wp_image_editors', array( $this, 'setEngine' ), 10, 2 );
    2428    }
     
    3438    /**
    3539     * Helper assertion for testing alpha on images
    36      * 
     40     *
    3741     * @param  string $image_path
    3842     * @param  array $point      array(x,y)
  • trunk/tests/phpunit/tests/image/editor_gd.php

    r27794 r29120  
    77 * @group wp-image-editor-gd
    88 */
     9require_once( dirname( __FILE__ ) . '/base.php' );
    910
    1011class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
     
    1920    }
    2021
    21     public function shutDown() {
     22    public function tearDown() {
    2223        $folder = DIR_TESTDATA . '/images/waffles-*.jpg';
    2324
     
    2627        }
    2728
    28         parent::shutDown();
     29        $this->remove_added_uploads();
     30
     31        parent::tearDown();
    2932    }
    3033
     
    468471
    469472        $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
     473
     474        unlink( $save_to_file );
    470475    }
    471476
     
    486491
    487492        $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
     493
     494        unlink( $save_to_file );
    488495    }
    489496}
  • trunk/tests/phpunit/tests/image/editor_imagick.php

    r27794 r29120  
    77 * @group wp-image-editor-imagick
    88 */
     9require_once( dirname( __FILE__ ) . '/base.php' );
    910
    1011class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
     
    1920    }
    2021
    21     public function shutDown() {
     22    public function tearDown() {
    2223        $folder = DIR_TESTDATA . '/images/waffles-*.jpg';
    2324
     
    2627        }
    2728
    28         parent::shutDown();
     29        $this->remove_added_uploads();
     30
     31        parent::tearDown();
    2932    }
    3033
     
    464467        $editor->resize( 5, 5 );
    465468        $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    466        
     469
    467470        $editor->save( $save_to_file );
    468471
    469472        $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
     473
     474        unlink( $save_to_file );
    470475    }
    471476
     
    486491
    487492        $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
     493
     494        unlink( $save_to_file );
    488495    }
    489496}
  • trunk/tests/phpunit/tests/image/functions.php

    r28603 r29120  
    8989        $files = array(
    9090            // 'test-image-cmyk.jpg', Allowed in r9727
    91             // 'test-image.bmp', Allowed in r28589 
     91            // 'test-image.bmp', Allowed in r28589
    9292            // 'test-image-grayscale.jpg', Allowed in r9727
    9393            'test-image.pct',
     
    145145
    146146                // Clean up
    147                 @unlink( $file );
    148                 @unlink( $ret['path'] );
     147                unlink( $file );
     148                unlink( $ret['path'] );
    149149            }
    150150
     
    186186
    187187            // Clean up
    188             @unlink( $file );
    189             @unlink( $ret['path'] );
     188            unlink( $file );
     189            unlink( $ret['path'] );
    190190            unset( $img );
    191191        }
     
    232232                $this->assertNotInstanceOf( 'WP_Error', $ret );
    233233                $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) );
    234                 @unlink( $file );
    235                 @unlink( $ret['path'] );
     234                unlink( $ret['path'] );
    236235            }
    237236
  • trunk/tests/phpunit/tests/image/intermediate_size.php

    r25507 r29120  
    11<?php
    2 
    32/**
    43 * @group image
     
    76 */
    87class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
     8    function tearDown() {
     9        $this->remove_added_uploads();
     10        parent::tearDown();
     11    }
     12
    913    function test_make_intermediate_size_no_size() {
    1014        $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false );
  • trunk/tests/phpunit/tests/image/resize.php

    r25002 r29120  
    55 * @group media
    66 * @group upload
     7 * @group resize
    78 */
     9require_once( dirname( __FILE__ ) . '/base.php' );
     10
    811abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase {
    912
     
    109112        $this->assertEquals( IMAGETYPE_JPEG, $type );
    110113
    111         unlink($image);
     114        unlink( $image );
    112115    }
    113116
  • trunk/tests/phpunit/tests/image/resize_gd.php

    r25002 r29120  
    55 * @group media
    66 * @group upload
     7 * @group resize
    78 */
     9require_once( dirname( __FILE__ ) . '/resize.php' );
     10
    811class Test_Image_Resize_GD extends WP_Tests_Image_Resize_UnitTestCase {
    912
     
    1316     */
    1417    public $editor_engine = 'WP_Image_Editor_GD';
     18
     19    public function setUp() {
     20        require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );
     21        require_once( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' );
     22
     23        parent::setUp();
     24    }
    1525}
  • trunk/tests/phpunit/tests/image/resize_imagick.php

    r25002 r29120  
    55 * @group media
    66 * @group upload
     7 * @group resize
    78 */
     9require_once( dirname( __FILE__ ) . '/resize.php' );
     10
    811class Test_Image_Resize_Imagick extends WP_Tests_Image_Resize_UnitTestCase {
    912
     
    1316     */
    1417    public $editor_engine = 'WP_Image_Editor_Imagick';
     18
     19    public function setUp() {
     20        require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );
     21        require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );
     22
     23        parent::setUp();
     24    }
    1525}
  • trunk/tests/phpunit/tests/post/attachments.php

    r28788 r29120  
    1010    function tearDown() {
    1111        // Remove all uploads.
    12         $uploads = wp_upload_dir();
    13         foreach ( scandir( $uploads['basedir'] ) as $file )
    14             _rmdir( $uploads['basedir'] . '/' . $file );
    15 
     12        $this->remove_added_uploads();
    1613        parent::tearDown();
    1714    }
  • trunk/tests/phpunit/tests/upload.php

    r26552 r29120  
    2323
    2424    function tearDown() {
     25        $this->remove_added_uploads();
     26
    2527        parent::tearDown();
    26 
    27         // Remove year/month folders created by wp_upload_dir().
    28         $uploads = wp_upload_dir();
    29         foreach ( scandir( $uploads['basedir'] ) as $file )
    30             _rmdir( $uploads['basedir'] . '/' . $file );
    31         _rmdir( ABSPATH . 'foo/' );
    3228    }
    3329
Note: See TracChangeset for help on using the changeset viewer.