Make WordPress Core


Ignore:
Timestamp:
07/13/2021 04:13:01 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

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

    r51335 r51419  
    158158        }
    159159
    160         // Reset $wp_sitemap global so that sitemap-related dynamic $wp->public_query_vars are added when the next test runs.
     160        /*
     161         * Reset globals related to current screen to provide a consistent global starting state
     162         * for tests that interact with admin screens. Replaces the need for individual tests
     163         * to invoke `set_current_screen( 'front' )` (or an alternative implementation) as a reset.
     164         *
     165         * The globals are from `WP_Screen::set_current_screen()`.
     166         *
     167         * Why not invoke `set_current_screen( 'front' )`?
     168         * Performance (faster test runs with less memory usage). How so? For each test,
     169         * it saves creating an instance of WP_Screen, making two method calls,
     170         * and firing of the `current_screen` action.
     171         */
     172        $current_screen_globals = array( 'current_screen', 'taxnow', 'typenow' );
     173        foreach ( $current_screen_globals as $global ) {
     174            $GLOBALS[ $global ] = null;
     175        }
     176
     177        /*
     178         * Reset $wp_sitemap global so that sitemap-related dynamic $wp->public_query_vars
     179         * are added when the next test runs.
     180         */
    161181        $GLOBALS['wp_sitemaps'] = null;
    162182
Note: See TracChangeset for help on using the changeset viewer.