Make WordPress Core


Ignore:
Timestamp:
02/22/2020 12:05:12 PM (5 years ago)
Author:
SergeyBiryukov
Message:

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

Among other fixes, this backports [26087], [26091], [26095], [26252], [26307], [26318], [26512], [26705], [26871], [26909-26910], [26940], [27086], [27168], [28799], [28873], [28943], [28961], [28964], [28966-28967], [29120], [29251], [29503], [29860], [29869], [29954], [30001], [30282], [30285], [30289-30291], [30513-30514], [30516-30521], [30524], [30526], [30529-30530], [31253-31254], [31257-31259], [31622], [40241], [40255], [40257], [40259], [40269], [40271], [40446], [40449], [40457], [40604], [40538], [40833], [41082], [41303], [41306], [44993].

See #49485.

Location:
branches/3.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.7

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

    r42068 r47343  
    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' );
    38         remove_filter( 'dbdelta_create_queries', array( $this, '_create_temporary_tables' ) );
     89        if ( is_multisite() ) {
     90            while ( ms_is_switched() ) {
     91                restore_current_blog();
     92            }
     93        }
     94        remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
    3995        remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
    4096        remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
     97        $this->_restore_hooks();
     98        wp_set_current_user( 0 );
    4199    }
    42100
     
    47105    }
    48106
     107    /**
     108     * Allow tests to be skipped on some automated runs
     109     *
     110     * For test runs on Travis for something other than trunk/master
     111     * we want to skip tests that only need to run for master.
     112     */
     113    public function skipOnAutomatedBranches() {
     114        // gentenv can be disabled
     115        if ( ! function_exists( 'getenv' ) ) {
     116            return false;
     117        }
     118
     119        // https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
     120        $travis_branch       = getenv( 'TRAVIS_BRANCH' );
     121        $travis_pull_request = getenv( 'TRAVIS_PULL_REQUEST' );
     122
     123        if ( false !== $travis_pull_request && 'master' !== $travis_branch ) {
     124            $this->markTestSkipped( 'For automated test runs, this test is only run on trunk/master' );
     125        }
     126    }
     127
     128    /**
     129     * Saves the action and filter-related globals so they can be restored later.
     130     *
     131     * Stores $merged_filters, $wp_actions, $wp_current_filter, and $wp_filter
     132     * on a class variable so they can be restored on tearDown() using _restore_hooks().
     133     *
     134     * @global array $merged_filters
     135     * @global array $wp_actions
     136     * @global array $wp_current_filter
     137     * @global array $wp_filter
     138     * @return void
     139     */
     140    protected function _backup_hooks() {
     141        $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
     142        foreach ( $globals as $key ) {
     143            self::$hooks_saved[ $key ] = $GLOBALS[ $key ];
     144        }
     145    }
     146
     147    /**
     148     * Restores the hook-related globals to their state at setUp()
     149     * so that future tests aren't affected by hooks set during this last test.
     150     *
     151     * @global array $merged_filters
     152     * @global array $wp_actions
     153     * @global array $wp_current_filter
     154     * @global array $wp_filter
     155     * @return void
     156     */
     157    protected function _restore_hooks() {
     158        $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
     159        foreach ( $globals as $key ) {
     160            if ( isset( self::$hooks_saved[ $key ] ) ) {
     161                $GLOBALS[ $key ] = self::$hooks_saved[ $key ];
     162            }
     163        }
     164    }
     165   
    49166    function flush_cache() {
    50167        global $wp_object_cache;
     
    65182        $wpdb->query( 'SET autocommit = 0;' );
    66183        $wpdb->query( 'START TRANSACTION;' );
    67         add_filter( 'dbdelta_create_queries', array( $this, '_create_temporary_tables' ) );
     184        add_filter( 'query', array( $this, '_create_temporary_tables' ) );
    68185        add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
    69186    }
    70187
    71     function _create_temporary_tables( $queries ) {
    72         return str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $queries );
     188    function _create_temporary_tables( $query ) {
     189        if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) )
     190            return substr_replace( trim( $query ), 'CREATE TEMPORARY TABLE', 0, 12 );
     191        return $query;
    73192    }
    74193
    75194    function _drop_temporary_tables( $query ) {
    76         if ( 'DROP TABLE' === substr( $query, 0, 10 ) )
    77             return 'DROP TEMPORARY TABLE ' . substr( $query, 10 );
     195        if ( 'DROP TABLE' === substr( trim( $query ), 0, 10 ) )
     196            return substr_replace( trim( $query ), 'DROP TEMPORARY TABLE', 0, 10 );
    78197        return $query;
    79198    }
     
    213332    protected function checkRequirements() {
    214333        parent::checkRequirements();
     334
     335        // Core tests no longer check against open Trac tickets, but others using WP_UnitTestCase may do so.
     336        if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
     337            return;
     338        }
     339
    215340        if ( WP_TESTS_FORCE_KNOWN_BUGS )
    216341            return;
     
    237362        if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) )
    238363            return;
    239         if ( ! TracTickets::isTracTicketClosed( 'http://core.trac.wordpress.org', $ticket_id ) )
     364        if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) )
    240365            $this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) );
    241366    }
     
    247372        if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'UT' . $ticket_id, self::$forced_tickets ) )
    248373            return;
    249         if ( ! TracTickets::isTracTicketClosed( 'http://unit-tests.trac.wordpress.org', $ticket_id ) )
     374        if ( ! TracTickets::isTracTicketClosed( 'https://unit-tests.trac.wordpress.org', $ticket_id ) )
    250375            $this->markTestSkipped( sprintf( 'Unit Tests Ticket #%d is not fixed', $ticket_id ) );
    251376    }
     
    257382        if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) )
    258383            return;
    259         if ( ! TracTickets::isTracTicketClosed( 'http://plugins.trac.wordpress.org', $ticket_id ) )
     384        if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) )
    260385            $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) );
    261386    }
     
    291416        return tempnam( $tmp_dir, 'wpunit' );
    292417    }
     418
     419    function unlink( $file ) {
     420        $exists = is_file( $file );
     421        if ( $exists && ! in_array( $file, self::$ignore_files ) ) {
     422            //error_log( $file );
     423            unlink( $file );
     424        } elseif ( ! $exists ) {
     425            $this->fail( "Trying to delete a file that doesn't exist: $file" );
     426        }
     427    }
     428
     429    function rmdir( $path ) {
     430        $files = $this->files_in_dir( $path );
     431        foreach ( $files as $file ) {
     432            if ( ! in_array( $file, self::$ignore_files ) ) {
     433                $this->unlink( $file );
     434            }
     435        }
     436    }
     437
     438    function remove_added_uploads() {
     439        // Remove all uploads.
     440        $uploads = wp_upload_dir();
     441        $this->rmdir( $uploads['basedir'] );
     442    }
     443
     444    function files_in_dir( $dir ) {
     445        $files = array();
     446
     447        $iterator = new RecursiveDirectoryIterator( $dir );
     448        $objects = new RecursiveIteratorIterator( $iterator );
     449        foreach ( $objects as $name => $object ) {
     450            if ( is_file( $name ) ) {
     451                $files[] = $name;
     452            }
     453        }
     454
     455        return $files;
     456    }
     457
     458    function scan_user_uploads() {
     459        static $files = array();
     460        if ( ! empty( $files ) ) {
     461            return $files;
     462        }
     463
     464        $uploads = wp_upload_dir();
     465        $files = $this->files_in_dir( $uploads['basedir'] );
     466        return $files;
     467    }
    293468}
Note: See TracChangeset for help on using the changeset viewer.