diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 1c7fe55e6d652..69fa2f3088e19 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -160,6 +160,22 @@ public function tearDown() { // Reset $wp_sitemap global so that sitemap-related dynamic $wp->public_query_vars are added when the next test runs. $GLOBALS['wp_sitemaps'] = null; + /* + * 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 instantiating an instance + * of WP_Screen, making 2 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; + } + $this->unregister_all_meta_keys(); remove_theme_support( 'html5' ); remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); diff --git a/tests/phpunit/tests/admin/includesPlugin.php b/tests/phpunit/tests/admin/includesPlugin.php index cbbc181b460f1..e1154254b321e 100644 --- a/tests/phpunit/tests/admin/includesPlugin.php +++ b/tests/phpunit/tests/admin/includesPlugin.php @@ -97,8 +97,6 @@ function test_submenu_position( $position, $expected_position ) { // Clean up the temporary user. wp_delete_user( $admin_user ); - // Reset current screen. - set_current_screen( 'front' ); // Verify the menu was inserted at the expected position. $this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] ); @@ -207,8 +205,6 @@ function test_submenu_helpers_position( $position, $expected_position ) { // Clean up the temporary user. wp_delete_user( $admin_user ); - // Reset current screen. - set_current_screen( 'front' ); foreach ( $actual_positions as $test => $actual_position ) { // Verify the menu was inserted at the expected position. @@ -292,8 +288,6 @@ function test_position_when_parent_slug_child_slug_are_the_same() { // Clean up the temporary user. wp_set_current_user( $current_user ); wp_delete_user( $admin_user ); - // Reset current screen. - set_current_screen( 'front' ); // Verify the menu was inserted at the expected position. $this->assertSame( 'main_slug', $submenu['main_slug'][0][2] ); @@ -325,8 +319,6 @@ function test_passing_string_as_position_fires_doing_it_wrong() { // Clean up the temporary user. wp_set_current_user( $current_user ); wp_delete_user( $admin_user ); - // Reset current screen. - set_current_screen( 'front' ); // Verify the menu was inserted at the expected position. $this->assertSame( 'submenu_page_1', $submenu['main_slug'][1][2] ); diff --git a/tests/phpunit/tests/admin/includesScreen.php b/tests/phpunit/tests/admin/includesScreen.php index 09432dedca96e..1eab0827bca82 100644 --- a/tests/phpunit/tests/admin/includesScreen.php +++ b/tests/phpunit/tests/admin/includesScreen.php @@ -155,15 +155,8 @@ class Tests_Admin_includesScreen extends WP_UnitTestCase { ), ); - function setUp() { - parent::setUp(); - set_current_screen( 'front' ); - } - function tearDown() { unset( $GLOBALS['wp_taxonomies']['old-or-new'] ); - unset( $GLOBALS['screen'] ); - unset( $GLOBALS['current_screen'] ); parent::tearDown(); } @@ -311,6 +304,7 @@ function test_help_tabs() { 'callback' => false, ); + set_current_screen( 'edit.php' ); $screen = get_current_screen(); $screen->add_help_tab( $tab_args ); $this->assertSame( @@ -372,6 +366,7 @@ function test_help_tabs_priority() { // Don't include a priority. ); + set_current_screen( 'edit.php' ); $screen = get_current_screen(); // Add help tabs. @@ -440,6 +435,7 @@ function test_options() { 'option' => $option, ); + set_current_screen( 'edit.php' ); $screen = get_current_screen(); $screen->add_option( $option, $option_args ); @@ -456,8 +452,6 @@ function test_options() { } function test_in_admin() { - $screen = get_current_screen(); - set_current_screen( 'edit.php' ); $this->assertTrue( get_current_screen()->in_admin() ); $this->assertTrue( get_current_screen()->in_admin( 'site' ) ); @@ -481,8 +475,6 @@ function test_in_admin() { $this->assertFalse( get_current_screen()->in_admin( 'site' ) ); $this->assertFalse( get_current_screen()->in_admin( 'network' ) ); $this->assertFalse( get_current_screen()->in_admin( 'user' ) ); - - $GLOBALS['current_screen'] = $screen; } /** diff --git a/tests/phpunit/tests/admin/includesTemplate.php b/tests/phpunit/tests/admin/includesTemplate.php index 6cb6cda3d4244..48c5accf772e6 100644 --- a/tests/phpunit/tests/admin/includesTemplate.php +++ b/tests/phpunit/tests/admin/includesTemplate.php @@ -234,8 +234,6 @@ public function test_wp_add_dashboard_widget() { // This doesn't actually get removed due to the invalid priority. remove_meta_box( 'dashboard2', 'dashboard', 'normal' ); - - set_current_screen( 'front' ); } } diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 2004e30ae3f52..31d6a4667be5d 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -400,8 +400,6 @@ public function test_admin_bar_has_no_archives_link_if_no_static_front_page() { $wp_admin_bar = $this->get_standard_admin_bar(); $node = $wp_admin_bar->get_node( 'archive' ); - set_current_screen( 'front' ); - $this->assertNull( $node ); } @@ -415,8 +413,6 @@ public function test_admin_bar_contains_view_archive_link_if_static_front_page() $wp_admin_bar = $this->get_standard_admin_bar(); $node = $wp_admin_bar->get_node( 'archive' ); - set_current_screen( 'front' ); - $this->assertNotNull( $node ); } @@ -429,8 +425,6 @@ public function test_admin_bar_has_no_archives_link_for_pages() { $wp_admin_bar = $this->get_standard_admin_bar(); $node = $wp_admin_bar->get_node( 'archive' ); - set_current_screen( 'front' ); - $this->assertNull( $node ); } @@ -508,7 +502,6 @@ public function test_admin_bar_has_no_archives_link_for_non_public_cpt() { $wp_admin_bar = $this->get_standard_admin_bar(); $node = $wp_admin_bar->get_node( 'archive' ); - set_current_screen( 'front' ); unregister_post_type( 'foo-non-public' ); $this->assertNull( $node ); @@ -532,7 +525,6 @@ public function test_admin_bar_has_no_archives_link_for_cpt_without_archive() { $wp_admin_bar = $this->get_standard_admin_bar(); $node = $wp_admin_bar->get_node( 'archive' ); - set_current_screen( 'front' ); unregister_post_type( 'foo-non-public' ); $this->assertNull( $node ); @@ -556,7 +548,6 @@ public function test_admin_bar_has_no_archives_link_for_cpt_not_shown_in_admin_b $wp_admin_bar = $this->get_standard_admin_bar(); $node = $wp_admin_bar->get_node( 'archive' ); - set_current_screen( 'front' ); unregister_post_type( 'foo-non-public' ); $this->assertNull( $node ); @@ -679,7 +670,6 @@ public function test_customize_link() { ); $wp_customize->start_previewing_theme(); - set_current_screen( 'front' ); $wp_admin_bar = $this->get_standard_admin_bar(); $node = $wp_admin_bar->get_node( 'customize' ); $this->assertNotEmpty( $node ); diff --git a/tests/phpunit/tests/dependencies/jquery.php b/tests/phpunit/tests/dependencies/jquery.php index 082d0e6bd00c8..f1ee89e853c03 100644 --- a/tests/phpunit/tests/dependencies/jquery.php +++ b/tests/phpunit/tests/dependencies/jquery.php @@ -82,8 +82,6 @@ function test_dont_allow_deregister_core_scripts_in_admin() { wp_deregister_script( $library ); $this->assertTrue( wp_script_is( $library, 'registered' ) ); } - - set_current_screen( 'front' ); } /** diff --git a/tests/phpunit/tests/l10n/getUserLocale.php b/tests/phpunit/tests/l10n/getUserLocale.php index f1d6f3828aa8f..eae697febc543 100644 --- a/tests/phpunit/tests/l10n/getUserLocale.php +++ b/tests/phpunit/tests/l10n/getUserLocale.php @@ -20,12 +20,6 @@ public function setUp() { wp_set_current_user( $this->user_id ); } - public function tearDown() { - set_current_screen( 'front' ); - - parent::tearDown(); - } - public function test_user_locale_property() { set_current_screen( 'dashboard' ); $this->assertSame( 'de_DE', get_user_locale() ); diff --git a/tests/phpunit/tests/l10n/loadTextdomain.php b/tests/phpunit/tests/l10n/loadTextdomain.php index ec54a0e6cd917..ef62106d0a6de 100644 --- a/tests/phpunit/tests/l10n/loadTextdomain.php +++ b/tests/phpunit/tests/l10n/loadTextdomain.php @@ -195,8 +195,6 @@ public function test_load_muplugin_textdomain_user_locale() { load_muplugin_textdomain( 'wp-tests-domain' ); - set_current_screen( 'front' ); - $this->assertSame( get_user_locale(), $this->locale ); } @@ -215,8 +213,6 @@ public function test_load_plugin_textdomain_user_locale() { load_plugin_textdomain( 'wp-tests-domain' ); - set_current_screen( 'front' ); - $this->assertSame( get_user_locale(), $this->locale ); } @@ -235,8 +231,6 @@ public function test_load_theme_textdomain_user_locale() { load_theme_textdomain( 'wp-tests-domain' ); - set_current_screen( 'front' ); - $this->assertSame( get_user_locale(), $this->locale ); } } diff --git a/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php b/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php index dd380c3d0c11e..3619e8110c414 100644 --- a/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php +++ b/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php @@ -211,8 +211,6 @@ public function test_plugin_translation_with_user_locale() { $expected = i18n_plugin_test(); - set_current_screen( 'front' ); - $this->assertSame( 'Das ist ein Dummy Plugin', $expected ); } @@ -228,7 +226,6 @@ public function test_theme_translation_with_user_locale() { $expected = i18n_theme_test(); - set_current_screen( 'front' ); switch_theme( WP_DEFAULT_THEME ); $this->assertSame( 'Das ist ein Dummy Theme', $expected ); diff --git a/tests/phpunit/tests/l10n/wpLocaleSwitcher.php b/tests/phpunit/tests/l10n/wpLocaleSwitcher.php index 029bb2bc826c3..726863eeee784 100644 --- a/tests/phpunit/tests/l10n/wpLocaleSwitcher.php +++ b/tests/phpunit/tests/l10n/wpLocaleSwitcher.php @@ -284,8 +284,6 @@ public function test_switch_to_site_locale_if_user_locale_is_set() { $wp_locale_switcher = $locale_switcher; - set_current_screen( 'front' ); - $this->assertFalse( $locale_switched_user_locale ); $this->assertTrue( $locale_switched_site_locale ); $this->assertSame( $site_locale, $site_locale_after_switch ); @@ -335,8 +333,6 @@ public function test_switch_to_different_site_locale_if_user_locale_is_set() { $wp_locale_switcher = $locale_switcher; - set_current_screen( 'front' ); - remove_filter( 'locale', array( $this, 'filter_locale' ) ); $this->assertFalse( $locale_switched_user_locale ); @@ -382,8 +378,6 @@ public function test_multiple_switches_to_site_locale_and_user_locale() { $wp_locale_switcher = $locale_switcher; - set_current_screen( 'front' ); - $this->assertSame( 'en_US', get_locale() ); $this->assertSame( 'This is a dummy plugin', $expected ); } diff --git a/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php b/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php index ed9c2cf25ab02..232b729c231d2 100644 --- a/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php +++ b/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php @@ -5,32 +5,6 @@ */ class Tests_Menu_WpAjaxMenuQuickSeach extends WP_UnitTestCase { - /** - * Current screen. - * - * @var mixed - */ - protected $current_screen; - - /** - * Set up. Workaround set_current_screen( null ) not working due to $hook_suffix not being set. - */ - function setUp() { - parent::setUp(); - - global $current_screen; - $this->current_screen = $current_screen; - } - - /** - * Tear down. Workaround set_current_screen( null ) not working due to $hook_suffix not being set. - */ - function tearDown() { - global $current_screen; - $current_screen = $this->current_screen; - parent::tearDown(); - } - /** * Test search returns results for pages. * diff --git a/tests/phpunit/tests/post/attachments.php b/tests/phpunit/tests/post/attachments.php index 0a03d77ed6c0b..7b03d4b7f2ccf 100644 --- a/tests/phpunit/tests/post/attachments.php +++ b/tests/phpunit/tests/post/attachments.php @@ -410,9 +410,6 @@ public function test_wp_get_attachment_url_should_not_force_https_when_administe $url = wp_get_attachment_url( $attachment_id ); - // Cleanup. - set_current_screen( 'front' ); - $this->assertSame( set_url_scheme( $url, 'http' ), $url ); } @@ -438,7 +435,6 @@ public function test_wp_get_attachment_url_should_force_https_when_administering $url = wp_get_attachment_url( $attachment_id ); // Cleanup. - set_current_screen( 'front' ); remove_filter( 'upload_dir', '_upload_dir_https' ); $this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) ); diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php index c4f36bee8ec16..2fb22f50c72af 100644 --- a/tests/phpunit/tests/query/conditionals.php +++ b/tests/phpunit/tests/query/conditionals.php @@ -17,8 +17,6 @@ class Tests_Query_Conditionals extends WP_UnitTestCase { function setUp() { parent::setUp(); - set_current_screen( 'front' ); - update_option( 'comments_per_page', 5 ); update_option( 'posts_per_page', 5 ); diff --git a/tests/phpunit/tests/query/isTerm.php b/tests/phpunit/tests/query/isTerm.php index 77d7a597d9ede..822d57147c140 100644 --- a/tests/phpunit/tests/query/isTerm.php +++ b/tests/phpunit/tests/query/isTerm.php @@ -25,8 +25,6 @@ class Tests_Query_IsTerm extends WP_UnitTestCase { function setUp() { parent::setUp(); - set_current_screen( 'front' ); - $GLOBALS['wp_the_query'] = new WP_Query(); $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; diff --git a/tests/phpunit/tests/query/postStatus.php b/tests/phpunit/tests/query/postStatus.php index 7d4a236b50fc7..ffa0aaf3fb037 100644 --- a/tests/phpunit/tests/query/postStatus.php +++ b/tests/phpunit/tests/query/postStatus.php @@ -211,7 +211,6 @@ public function test_protected_should_be_included_when_in_the_admin() { ); $this->assertContains( "post_status = 'foo", $q->request ); - set_current_screen( 'front' ); } public function test_private_statuses_should_be_included_when_current_user_can_read_private_posts() { diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index f6e057ba8d89e..538d0f9d42c78 100644 --- a/tests/phpunit/tests/rest-api.php +++ b/tests/phpunit/tests/rest-api.php @@ -793,8 +793,6 @@ public function test_rest_url_scheme() { // Reset. update_option( 'siteurl', $_siteurl ); - set_current_screen( 'front' ); - } /** diff --git a/tests/phpunit/tests/url.php b/tests/phpunit/tests/url.php index 67240a73b40bc..9ecb7d90ac766 100644 --- a/tests/phpunit/tests/url.php +++ b/tests/phpunit/tests/url.php @@ -199,8 +199,6 @@ function data_home_urls() { } function test_home_url_from_admin() { - $screen = get_current_screen(); - // Pretend to be in the site admin. set_current_screen( 'dashboard' ); $home = get_option( 'home' ); @@ -243,13 +241,9 @@ function test_home_url_from_admin() { $this->assertSame( $home, home_url() ); update_option( 'home', set_url_scheme( $home, 'http' ) ); - - $GLOBALS['current_screen'] = $screen; } function test_network_home_url_from_admin() { - $screen = get_current_screen(); - // Pretend to be in the site admin. set_current_screen( 'dashboard' ); $home = network_home_url(); @@ -268,8 +262,6 @@ function test_network_home_url_from_admin() { $this->assertSame( $home, network_home_url() ); $_SERVER['HTTPS'] = 'on'; $this->assertSame( $home_https, network_home_url() ); - - $GLOBALS['current_screen'] = $screen; } function test_set_url_scheme() { diff --git a/tests/phpunit/tests/user/wpSendUserRequest.php b/tests/phpunit/tests/user/wpSendUserRequest.php index 0b4d19af80040..d3ab88793947c 100644 --- a/tests/phpunit/tests/user/wpSendUserRequest.php +++ b/tests/phpunit/tests/user/wpSendUserRequest.php @@ -79,9 +79,6 @@ public function tearDown() { reset_phpmailer_instance(); unset( $GLOBALS['locale'] ); - unset( $GLOBALS['current_screen'] ); - unset( $GLOBALS['taxnow'] ); - unset( $GLOBALS['typenow'] ); restore_previous_locale(); parent::tearDown();