From 3b4132d6c096a8a17fa52b9e00a201985a243406 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 29 Jan 2023 16:24:02 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@55160 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 44 ++++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index fa048cdfa7..be96f06e31 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -319,47 +319,55 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { } /** - * Saves the action and filter-related globals so they can be restored later. + * Saves the hook-related globals so they can be restored later. * - * Stores $wp_actions, $wp_current_filter, and $wp_filter on a class variable - * so they can be restored on tear_down() using _restore_hooks(). + * Stores $wp_filter, $wp_actions, $wp_filters, and $wp_current_filter + * on a class variable so they can be restored on tear_down() using _restore_hooks(). * - * @global array $wp_actions - * @global array $wp_current_filter * @global array $wp_filter + * @global array $wp_actions + * @global array $wp_filters + * @global array $wp_current_filter */ protected function _backup_hooks() { - $globals = array( 'wp_actions', 'wp_current_filter' ); - foreach ( $globals as $key ) { - self::$hooks_saved[ $key ] = $GLOBALS[ $key ]; - } self::$hooks_saved['wp_filter'] = array(); + foreach ( $GLOBALS['wp_filter'] as $hook_name => $hook_object ) { self::$hooks_saved['wp_filter'][ $hook_name ] = clone $hook_object; } + + $globals = array( 'wp_actions', 'wp_filters', 'wp_current_filter' ); + + foreach ( $globals as $key ) { + self::$hooks_saved[ $key ] = $GLOBALS[ $key ]; + } } /** * Restores the hook-related globals to their state at set_up() * so that future tests aren't affected by hooks set during this last test. * - * @global array $wp_actions - * @global array $wp_current_filter * @global array $wp_filter + * @global array $wp_actions + * @global array $wp_filters + * @global array $wp_current_filter */ protected function _restore_hooks() { - $globals = array( 'wp_actions', 'wp_current_filter' ); + if ( isset( self::$hooks_saved['wp_filter'] ) ) { + $GLOBALS['wp_filter'] = array(); + + foreach ( self::$hooks_saved['wp_filter'] as $hook_name => $hook_object ) { + $GLOBALS['wp_filter'][ $hook_name ] = clone $hook_object; + } + } + + $globals = array( 'wp_actions', 'wp_filters', 'wp_current_filter' ); + foreach ( $globals as $key ) { if ( isset( self::$hooks_saved[ $key ] ) ) { $GLOBALS[ $key ] = self::$hooks_saved[ $key ]; } } - if ( isset( self::$hooks_saved['wp_filter'] ) ) { - $GLOBALS['wp_filter'] = array(); - foreach ( self::$hooks_saved['wp_filter'] as $hook_name => $hook_object ) { - $GLOBALS['wp_filter'][ $hook_name ] = clone $hook_object; - } - } } /**