WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/12/2014 07:08:15 AM (7 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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.