Make WordPress Core


Ignore:
Timestamp:
02/20/2020 05:04:42 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Build/Test Tools: Fix the Travis CI build for the 3.9 branch.

Among other fixes, this backports [28943], [28961], [28964-28968], [28988], [29120], [29251], [29503], [29860], [29869], [29954], [30001], [30160], [30282], [30285], [30289-30291], [30513-30514], [30516-30521], [30523-30524], [30526], [30529-30530], [31253-31254], [31257-31259], [31622], [33374], [40255], [40257], [40259], [40269], [40271], [40446], [40449], [40457], [40604], [40538], [40833], [41082], [41303], [41306], [44993].

See #49485.

Location:
branches/3.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.9

  • branches/3.9/tests/phpunit/includes/testcase.php

    r42066 r47329  
    1212    protected $caught_doing_it_wrong = array();
    1313
     14    protected static $hooks_saved = array();
     15    protected static $ignore_files;
     16
    1417    /**
    1518     * @var WP_UnitTest_Factory
     
    1922    function setUp() {
    2023        set_time_limit(0);
     24
     25        if ( ! self::$ignore_files ) {
     26            self::$ignore_files = $this->scan_user_uploads();
     27        }
     28
     29        if ( ! self::$hooks_saved ) {
     30            $this->_backup_hooks();
     31        }
    2132
    2233        global $wpdb;
     
    2738        $this->factory = new WP_UnitTest_Factory;
    2839        $this->clean_up_global_scope();
     40
     41        /*
     42         * When running core tests, ensure that post types and taxonomies
     43         * are reset for each test. We skip this step for non-core tests,
     44         * given the large number of plugins that register post types and
     45         * taxonomies at 'init'.
     46         */
     47        if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
     48            $this->reset_post_types();
     49            $this->reset_taxonomies();
     50        }
     51
    2952        $this->start_transaction();
    3053        $this->expectDeprecated();
    3154        add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
     55    }
     56
     57    /**
     58     * Unregister existing post types and register defaults.
     59     *
     60     * Run before each test in order to clean up the global scope, in case
     61     * a test forgets to unregister a post type on its own, or fails before
     62     * it has a chance to do so.
     63     */
     64    protected function reset_post_types() {
     65        foreach ( get_post_types() as $pt ) {
     66            _unregister_post_type( $pt );
     67        }
     68        create_initial_post_types();
     69    }
     70
     71    /**
     72     * Unregister existing taxonomies and register defaults.
     73     *
     74     * Run before each test in order to clean up the global scope, in case
     75     * a test forgets to unregister a taxonomy on its own, or fails before
     76     * it has a chance to do so.
     77     */
     78    protected function reset_taxonomies() {
     79        foreach ( get_taxonomies() as $tax ) {
     80            _unregister_taxonomy( $tax );
     81        }
     82        create_initial_taxonomies();
    3283    }
    3384
     
    3687        $this->expectedDeprecated();
    3788        $wpdb->query( 'ROLLBACK' );
     89        if ( is_multisite() ) {
     90            while ( ms_is_switched() ) {
     91                restore_current_blog();
     92            }
     93        }
    3894        $wp_query = new WP_Query();
    3995        $post = null;
     
    4298        remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
    4399        remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
     100        $this->_restore_hooks();
     101        wp_set_current_user( 0 );
    44102    }
    45103
     
    71129    }
    72130
     131    /**
     132     * Saves the action and filter-related globals so they can be restored later.
     133     *
     134     * Stores $merged_filters, $wp_actions, $wp_current_filter, and $wp_filter
     135     * on a class variable so they can be restored on tearDown() using _restore_hooks().
     136     *
     137     * @global array $merged_filters
     138     * @global array $wp_actions
     139     * @global array $wp_current_filter
     140     * @global array $wp_filter
     141     * @return void
     142     */
     143    protected function _backup_hooks() {
     144        $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
     145        foreach ( $globals as $key ) {
     146            self::$hooks_saved[ $key ] = $GLOBALS[ $key ];
     147        }
     148    }
     149
     150    /**
     151     * Restores the hook-related globals to their state at setUp()
     152     * so that future tests aren't affected by hooks set during this last test.
     153     *
     154     * @global array $merged_filters
     155     * @global array $wp_actions
     156     * @global array $wp_current_filter
     157     * @global array $wp_filter
     158     * @return void
     159     */
     160    protected function _restore_hooks() {
     161        $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
     162        foreach ( $globals as $key ) {
     163            if ( isset( self::$hooks_saved[ $key ] ) ) {
     164                $GLOBALS[ $key ] = self::$hooks_saved[ $key ];
     165            }
     166        }
     167    }
     168   
    73169    function flush_cache() {
    74170        global $wp_object_cache;
     
    232328    protected function checkRequirements() {
    233329        parent::checkRequirements();
     330
     331        // Core tests no longer check against open Trac tickets, but others using WP_UnitTestCase may do so.
     332        if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
     333            return;
     334        }
     335
    234336        if ( WP_TESTS_FORCE_KNOWN_BUGS )
    235337            return;
     
    354456        $this->assertTrue( $passed, $message );
    355457    }
     458
     459    function unlink( $file ) {
     460        $exists = is_file( $file );
     461        if ( $exists && ! in_array( $file, self::$ignore_files ) ) {
     462            //error_log( $file );
     463            unlink( $file );
     464        } elseif ( ! $exists ) {
     465            $this->fail( "Trying to delete a file that doesn't exist: $file" );
     466        }
     467    }
     468
     469    function rmdir( $path ) {
     470        $files = $this->files_in_dir( $path );
     471        foreach ( $files as $file ) {
     472            if ( ! in_array( $file, self::$ignore_files ) ) {
     473                $this->unlink( $file );
     474            }
     475        }
     476    }
     477
     478    function remove_added_uploads() {
     479        // Remove all uploads.
     480        $uploads = wp_upload_dir();
     481        $this->rmdir( $uploads['basedir'] );
     482    }
     483
     484    function files_in_dir( $dir ) {
     485        $files = array();
     486
     487        $iterator = new RecursiveDirectoryIterator( $dir );
     488        $objects = new RecursiveIteratorIterator( $iterator );
     489        foreach ( $objects as $name => $object ) {
     490            if ( is_file( $name ) ) {
     491                $files[] = $name;
     492            }
     493        }
     494
     495        return $files;
     496    }
     497
     498    function scan_user_uploads() {
     499        static $files = array();
     500        if ( ! empty( $files ) ) {
     501            return $files;
     502        }
     503
     504        $uploads = wp_upload_dir();
     505        $files = $this->files_in_dir( $uploads['basedir'] );
     506        return $files;
     507    }
    356508}
Note: See TracChangeset for help on using the changeset viewer.