Make WordPress Core

Changeset 55160


Ignore:
Timestamp:
01/29/2023 04:24:02 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Build/Test Tools: Back up and restore the $wp_filters global when running tests.

Introduced along with the did_filter() function, the $wp_filters global stores the number of times each filter has been applied during the current request.

This commit adds $wp_filters to the list of globals that are saved in WP_UnitTestCase_Base::_backup_hooks() and restored in ::_restore_hooks() so that hooks set by the current test do not accidentally affect future tests.

Additionally, this commit brings some consistency by backing up and restoring the hook-related globals in the same order they are defined in wp-includes/plugin.php.

Follow-up to [29251], [53803].

Props sanjucta, SergeyBiryukov.
Fixes #57236.

File:
1 edited

Legend:

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

    r55148 r55160  
    320320
    321321        /**
    322          * Saves the action and filter-related globals so they can be restored later.
    323          *
    324          * Stores $wp_actions, $wp_current_filter, and $wp_filter on a class variable
    325          * so they can be restored on tear_down() using _restore_hooks().
    326          *
     322         * Saves the hook-related globals so they can be restored later.
     323         *
     324         * Stores $wp_filter, $wp_actions, $wp_filters, and $wp_current_filter
     325         * on a class variable so they can be restored on tear_down() using _restore_hooks().
     326         *
     327         * @global array $wp_filter
    327328         * @global array $wp_actions
     329         * @global array $wp_filters
    328330         * @global array $wp_current_filter
    329          * @global array $wp_filter
    330331         */
    331332        protected function _backup_hooks() {
    332                 $globals = array( 'wp_actions', 'wp_current_filter' );
     333                self::$hooks_saved['wp_filter'] = array();
     334
     335                foreach ( $GLOBALS['wp_filter'] as $hook_name => $hook_object ) {
     336                        self::$hooks_saved['wp_filter'][ $hook_name ] = clone $hook_object;
     337                }
     338
     339                $globals = array( 'wp_actions', 'wp_filters', 'wp_current_filter' );
     340
    333341                foreach ( $globals as $key ) {
    334342                        self::$hooks_saved[ $key ] = $GLOBALS[ $key ];
    335343                }
    336                 self::$hooks_saved['wp_filter'] = array();
    337                 foreach ( $GLOBALS['wp_filter'] as $hook_name => $hook_object ) {
    338                         self::$hooks_saved['wp_filter'][ $hook_name ] = clone $hook_object;
    339                 }
    340344        }
    341345
     
    344348         * so that future tests aren't affected by hooks set during this last test.
    345349         *
     350         * @global array $wp_filter
    346351         * @global array $wp_actions
     352         * @global array $wp_filters
    347353         * @global array $wp_current_filter
    348          * @global array $wp_filter
    349354         */
    350355        protected function _restore_hooks() {
    351                 $globals = array( 'wp_actions', 'wp_current_filter' );
     356                if ( isset( self::$hooks_saved['wp_filter'] ) ) {
     357                        $GLOBALS['wp_filter'] = array();
     358
     359                        foreach ( self::$hooks_saved['wp_filter'] as $hook_name => $hook_object ) {
     360                                $GLOBALS['wp_filter'][ $hook_name ] = clone $hook_object;
     361                        }
     362                }
     363
     364                $globals = array( 'wp_actions', 'wp_filters', 'wp_current_filter' );
     365
    352366                foreach ( $globals as $key ) {
    353367                        if ( isset( self::$hooks_saved[ $key ] ) ) {
    354368                                $GLOBALS[ $key ] = self::$hooks_saved[ $key ];
    355                         }
    356                 }
    357                 if ( isset( self::$hooks_saved['wp_filter'] ) ) {
    358                         $GLOBALS['wp_filter'] = array();
    359                         foreach ( self::$hooks_saved['wp_filter'] as $hook_name => $hook_object ) {
    360                                 $GLOBALS['wp_filter'][ $hook_name ] = clone $hook_object;
    361369                        }
    362370                }
Note: See TracChangeset for help on using the changeset viewer.