WordPress.org

Make WordPress Core

Changeset 29251


Ignore:
Timestamp:
07/19/2014 11:58:07 PM (6 years ago)
Author:
wonderboymusic
Message:

Backup filter globals ( $merged_filters, $wp_actions, $wp_current_filter, $wp_filter ) statically when running unit tests, restore on tearDown(). This ensures that all tests initially use the same filters/actions.

Props mnelson4, wonderboymusic.
Fixes #28535.

File:
1 edited

Legend:

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

    r29120 r29251  
    1212    protected $caught_doing_it_wrong = array();
    1313
     14    protected static $hooks_saved = array();
    1415    protected static $ignore_files;
    1516
     
    2425        if ( ! self::$ignore_files ) {
    2526            self::$ignore_files = $this->scan_user_uploads();
     27        }
     28
     29        if ( ! self::$hooks_saved ) {
     30            $this->_backup_hooks();
    2631        }
    2732
     
    5358        remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
    5459        remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
     60        $this->_restore_hooks();
    5561    }
    5662
     
    6167    }
    6268
     69    /**
     70     * Saves the action and filter-related globals so they can be restored later
     71     *
     72     * Stores $merged_filters, $wp_actions, $wp_current_filter, and $wp_filter
     73     * on a class variable so they can be restored on tearDown() using _restore_hooks()
     74     * @global array $merged_filters
     75     * @global array $wp_actions
     76     * @global array $wp_current_filter
     77     * @global array $wp_filter
     78     * @return void
     79     */
     80    protected function _backup_hooks() {
     81        $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
     82        foreach ( $globals as $key ) {
     83            self::$hooks_saved[ $key ] = $GLOBALS[ $key ];
     84        }
     85    }
     86
     87    /**
     88     * Restores the hook-related globals to their state at setUp()
     89     *
     90     * so that future tests aren't affected by hooks set during this last test
     91     *
     92     * @global array $merged_filters
     93     * @global array $wp_actions
     94     * @global array $wp_current_filter
     95     * @global array $wp_filter
     96     * @return void
     97     */
     98    protected function _restore_hooks(){
     99        $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
     100        foreach ( $globals as $key ) {
     101            $GLOBALS[ $key ] = self::$hooks_saved[ $key ];
     102        }
     103    }
     104   
    63105    function flush_cache() {
    64106        global $wp_object_cache;
Note: See TracChangeset for help on using the changeset viewer.