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
This commit is contained in:
Sergey Biryukov 2023-01-29 16:24:02 +00:00
parent 533b9b10de
commit 3b4132d6c0

View File

@ -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;
}
}
}
/**