Tests: Reset $current_screen global between tests to avoid cross-test interdependencies.

This provides a consistent global starting state for tests that interact with admin screens.

Individual tests no longer need to invoke `set_current_screen( 'front' )` (or an alternative implementation) as a reset.

Follow-up to [29251], [29860], [31046], [36721], [38678], [48908], [50433].

Props hellofromTonya, johnbillion.
Fixes #53431.

git-svn-id: https://develop.svn.wordpress.org/trunk@51419 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
SergeyBiryukov
2021-07-13 16:13:01 +00:00
parent 16d538c0f8
commit 013b0c11f0
18 changed files with 24 additions and 103 deletions
+21 -1
View File
@@ -157,7 +157,27 @@ abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase {
$GLOBALS[ $global ] = null;
}
// Reset $wp_sitemap global so that sitemap-related dynamic $wp->public_query_vars are added when the next test runs.
/*
* Reset globals related to current screen to provide a consistent global starting state
* for tests that interact with admin screens. Replaces the need for individual tests
* to invoke `set_current_screen( 'front' )` (or an alternative implementation) as a reset.
*
* The globals are from `WP_Screen::set_current_screen()`.
*
* Why not invoke `set_current_screen( 'front' )`?
* Performance (faster test runs with less memory usage). How so? For each test,
* it saves creating an instance of WP_Screen, making two method calls,
* and firing of the `current_screen` action.
*/
$current_screen_globals = array( 'current_screen', 'taxnow', 'typenow' );
foreach ( $current_screen_globals as $global ) {
$GLOBALS[ $global ] = null;
}
/*
* Reset $wp_sitemap global so that sitemap-related dynamic $wp->public_query_vars
* are added when the next test runs.
*/
$GLOBALS['wp_sitemaps'] = null;
$this->unregister_all_meta_keys();