Make WordPress Core


Ignore:
Timestamp:
02/21/2020 01:05:39 PM (7 years ago)
Author:
SergeyBiryukov
Message:

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

Among other fixes, this backports [26871], [26909-26910], [26940], [27086], [27168], [28799], [28873], [28943], [28961], [28964], [28966-28967], [29120], [29251], [29503], [29860], [29869], [29954], [30001], [30160], [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.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.8

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

    r42067 r47338  
    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        }
     
    206325        protected function checkRequirements() {
    207326                parent::checkRequirements();
     327
     328                // Core tests no longer check against open Trac tickets, but others using WP_UnitTestCase may do so.
     329                if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
     330                        return;
     331                }
     332
    208333                if ( WP_TESTS_FORCE_KNOWN_BUGS )
    209334                        return;
     
    230355                if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) )
    231356                        return;
    232                 if ( ! TracTickets::isTracTicketClosed( 'http://core.trac.wordpress.org', $ticket_id ) )
     357                if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) )
    233358                        $this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) );
    234359        }
     
    240365                if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'UT' . $ticket_id, self::$forced_tickets ) )
    241366                        return;
    242                 if ( ! TracTickets::isTracTicketClosed( 'http://unit-tests.trac.wordpress.org', $ticket_id ) )
     367                if ( ! TracTickets::isTracTicketClosed( 'https://unit-tests.trac.wordpress.org', $ticket_id ) )
    243368                        $this->markTestSkipped( sprintf( 'Unit Tests Ticket #%d is not fixed', $ticket_id ) );
    244369        }
     
    250375                if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) )
    251376                        return;
    252                 if ( ! TracTickets::isTracTicketClosed( 'http://plugins.trac.wordpress.org', $ticket_id ) )
     377                if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) )
    253378                        $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) );
    254379        }
     
    328453                $this->assertTrue( $passed, $message );
    329454        }
     455
     456        function unlink( $file ) {
     457                $exists = is_file( $file );
     458                if ( $exists && ! in_array( $file, self::$ignore_files ) ) {
     459                        //error_log( $file );
     460                        unlink( $file );
     461                } elseif ( ! $exists ) {
     462                        $this->fail( "Trying to delete a file that doesn't exist: $file" );
     463                }
     464        }
     465
     466        function rmdir( $path ) {
     467                $files = $this->files_in_dir( $path );
     468                foreach ( $files as $file ) {
     469                        if ( ! in_array( $file, self::$ignore_files ) ) {
     470                                $this->unlink( $file );
     471                        }
     472                }
     473        }
     474
     475        function remove_added_uploads() {
     476                // Remove all uploads.
     477                $uploads = wp_upload_dir();
     478                $this->rmdir( $uploads['basedir'] );
     479        }
     480
     481        function files_in_dir( $dir ) {
     482                $files = array();
     483
     484                $iterator = new RecursiveDirectoryIterator( $dir );
     485                $objects = new RecursiveIteratorIterator( $iterator );
     486                foreach ( $objects as $name => $object ) {
     487                        if ( is_file( $name ) ) {
     488                                $files[] = $name;
     489                        }
     490                }
     491
     492                return $files;
     493        }
     494
     495        function scan_user_uploads() {
     496                static $files = array();
     497                if ( ! empty( $files ) ) {
     498                        return $files;
     499                }
     500
     501                $uploads = wp_upload_dir();
     502                $files = $this->files_in_dir( $uploads['basedir'] );
     503                return $files;
     504        }
    330505}
Note: See TracChangeset for help on using the changeset viewer.