Make WordPress Core

Changeset 51419


Ignore:
Timestamp:
07/13/2021 04:13:01 PM (3 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.

Location:
trunk/tests/phpunit
Files:
18 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
  • trunk/tests/phpunit/tests/admin/includesPlugin.php

    r51397 r51419  
    9898        // Clean up the temporary user.
    9999        wp_delete_user( $admin_user );
    100         // Reset current screen.
    101         set_current_screen( 'front' );
    102100
    103101        // Verify the menu was inserted at the expected position.
     
    208206        // Clean up the temporary user.
    209207        wp_delete_user( $admin_user );
    210         // Reset current screen.
    211         set_current_screen( 'front' );
    212208
    213209        foreach ( $actual_positions as $test => $actual_position ) {
     
    293289        wp_set_current_user( $current_user );
    294290        wp_delete_user( $admin_user );
    295         // Reset current screen.
    296         set_current_screen( 'front' );
    297291
    298292        // Verify the menu was inserted at the expected position.
     
    326320        wp_set_current_user( $current_user );
    327321        wp_delete_user( $admin_user );
    328         // Reset current screen.
    329         set_current_screen( 'front' );
    330322
    331323        // Verify the menu was inserted at the expected position.
  • trunk/tests/phpunit/tests/admin/includesScreen.php

    r51367 r51419  
    156156    );
    157157
    158     function setUp() {
    159         parent::setUp();
    160         set_current_screen( 'front' );
    161     }
    162 
    163158    function tearDown() {
    164159        unset( $GLOBALS['wp_taxonomies']['old-or-new'] );
    165         unset( $GLOBALS['screen'] );
    166         unset( $GLOBALS['current_screen'] );
    167160        parent::tearDown();
    168161    }
     
    312305        );
    313306
     307        set_current_screen( 'edit.php' );
    314308        $screen = get_current_screen();
    315309        $screen->add_help_tab( $tab_args );
     
    373367        );
    374368
     369        set_current_screen( 'edit.php' );
    375370        $screen = get_current_screen();
    376371
     
    441436        );
    442437
     438        set_current_screen( 'edit.php' );
    443439        $screen = get_current_screen();
    444440
     
    457453
    458454    function test_in_admin() {
    459         $screen = get_current_screen();
    460 
    461455        set_current_screen( 'edit.php' );
    462456        $this->assertTrue( get_current_screen()->in_admin() );
     
    482476        $this->assertFalse( get_current_screen()->in_admin( 'network' ) );
    483477        $this->assertFalse( get_current_screen()->in_admin( 'user' ) );
    484 
    485         $GLOBALS['current_screen'] = $screen;
    486478    }
    487479
  • trunk/tests/phpunit/tests/admin/includesTemplate.php

    r49124 r51419  
    235235        // This doesn't actually get removed due to the invalid priority.
    236236        remove_meta_box( 'dashboard2', 'dashboard', 'normal' );
    237 
    238         set_current_screen( 'front' );
    239237    }
    240238
  • trunk/tests/phpunit/tests/adminbar.php

    r51397 r51419  
    401401        $node         = $wp_admin_bar->get_node( 'archive' );
    402402
    403         set_current_screen( 'front' );
    404 
    405403        $this->assertNull( $node );
    406404    }
     
    416414        $node         = $wp_admin_bar->get_node( 'archive' );
    417415
    418         set_current_screen( 'front' );
    419 
    420416        $this->assertNotNull( $node );
    421417    }
     
    429425        $wp_admin_bar = $this->get_standard_admin_bar();
    430426        $node         = $wp_admin_bar->get_node( 'archive' );
    431 
    432         set_current_screen( 'front' );
    433427
    434428        $this->assertNull( $node );
     
    509503        $node         = $wp_admin_bar->get_node( 'archive' );
    510504
    511         set_current_screen( 'front' );
    512505        unregister_post_type( 'foo-non-public' );
    513506
     
    533526        $node         = $wp_admin_bar->get_node( 'archive' );
    534527
    535         set_current_screen( 'front' );
    536528        unregister_post_type( 'foo-non-public' );
    537529
     
    557549        $node         = $wp_admin_bar->get_node( 'archive' );
    558550
    559         set_current_screen( 'front' );
    560551        unregister_post_type( 'foo-non-public' );
    561552
     
    680671        $wp_customize->start_previewing_theme();
    681672
    682         set_current_screen( 'front' );
    683673        $wp_admin_bar = $this->get_standard_admin_bar();
    684674        $node         = $wp_admin_bar->get_node( 'customize' );
  • trunk/tests/phpunit/tests/dependencies/jquery.php

    r51397 r51419  
    8383            $this->assertTrue( wp_script_is( $library, 'registered' ) );
    8484        }
    85 
    86         set_current_screen( 'front' );
    8785    }
    8886
  • trunk/tests/phpunit/tests/l10n/getUserLocale.php

    r51415 r51419  
    1919
    2020        wp_set_current_user( $this->user_id );
    21     }
    22 
    23     public function tearDown() {
    24         set_current_screen( 'front' );
    25 
    26         parent::tearDown();
    2721    }
    2822
  • trunk/tests/phpunit/tests/l10n/loadTextdomain.php

    r50463 r51419  
    196196        load_muplugin_textdomain( 'wp-tests-domain' );
    197197
    198         set_current_screen( 'front' );
    199 
    200198        $this->assertSame( get_user_locale(), $this->locale );
    201199    }
     
    216214        load_plugin_textdomain( 'wp-tests-domain' );
    217215
    218         set_current_screen( 'front' );
    219 
    220216        $this->assertSame( get_user_locale(), $this->locale );
    221217    }
     
    236232        load_theme_textdomain( 'wp-tests-domain' );
    237233
    238         set_current_screen( 'front' );
    239 
    240234        $this->assertSame( get_user_locale(), $this->locale );
    241235    }
  • trunk/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php

    r50926 r51419  
    212212        $expected = i18n_plugin_test();
    213213
    214         set_current_screen( 'front' );
    215 
    216214        $this->assertSame( 'Das ist ein Dummy Plugin', $expected );
    217215    }
     
    229227        $expected = i18n_theme_test();
    230228
    231         set_current_screen( 'front' );
    232229        switch_theme( WP_DEFAULT_THEME );
    233230
  • trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php

    r50456 r51419  
    285285        $wp_locale_switcher = $locale_switcher;
    286286
    287         set_current_screen( 'front' );
    288 
    289287        $this->assertFalse( $locale_switched_user_locale );
    290288        $this->assertTrue( $locale_switched_site_locale );
     
    336334        $wp_locale_switcher = $locale_switcher;
    337335
    338         set_current_screen( 'front' );
    339 
    340336        remove_filter( 'locale', array( $this, 'filter_locale' ) );
    341337
     
    383379        $wp_locale_switcher = $locale_switcher;
    384380
    385         set_current_screen( 'front' );
    386 
    387381        $this->assertSame( 'en_US', get_locale() );
    388382        $this->assertSame( 'This is a dummy plugin', $expected );
  • trunk/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php

    r50450 r51419  
    55 */
    66class Tests_Menu_WpAjaxMenuQuickSeach extends WP_UnitTestCase {
    7 
    8     /**
    9      * Current screen.
    10      *
    11      * @var mixed
    12      */
    13     protected $current_screen;
    14 
    15     /**
    16      * Set up. Workaround set_current_screen( null ) not working due to $hook_suffix not being set.
    17      */
    18     function setUp() {
    19         parent::setUp();
    20 
    21         global $current_screen;
    22         $this->current_screen = $current_screen;
    23     }
    24 
    25     /**
    26      * Tear down. Workaround set_current_screen( null ) not working due to $hook_suffix not being set.
    27      */
    28     function tearDown() {
    29         global $current_screen;
    30         $current_screen = $this->current_screen;
    31         parent::tearDown();
    32     }
    337
    348    /**
  • trunk/tests/phpunit/tests/post/attachments.php

    r51403 r51419  
    411411        $url = wp_get_attachment_url( $attachment_id );
    412412
    413         // Cleanup.
    414         set_current_screen( 'front' );
    415 
    416413        $this->assertSame( set_url_scheme( $url, 'http' ), $url );
    417414    }
     
    439436
    440437        // Cleanup.
    441         set_current_screen( 'front' );
    442438        remove_filter( 'upload_dir', '_upload_dir_https' );
    443439
  • trunk/tests/phpunit/tests/query/conditionals.php

    r48937 r51419  
    1717    function setUp() {
    1818        parent::setUp();
    19 
    20         set_current_screen( 'front' );
    2119
    2220        update_option( 'comments_per_page', 5 );
  • trunk/tests/phpunit/tests/query/isTerm.php

    r50463 r51419  
    2626        parent::setUp();
    2727
    28         set_current_screen( 'front' );
    29 
    3028        $GLOBALS['wp_the_query'] = new WP_Query();
    3129        $GLOBALS['wp_query']     = $GLOBALS['wp_the_query'];
  • trunk/tests/phpunit/tests/query/postStatus.php

    r51276 r51419  
    275275
    276276        $this->assertContains( "post_status = 'foo", $q->request );
    277         set_current_screen( 'front' );
    278277    }
    279278
  • trunk/tests/phpunit/tests/rest-api.php

    r51404 r51419  
    794794        // Reset.
    795795        update_option( 'siteurl', $_siteurl );
    796         set_current_screen( 'front' );
    797 
    798796    }
    799797
  • trunk/tests/phpunit/tests/url.php

    r51415 r51419  
    200200
    201201    function test_home_url_from_admin() {
    202         $screen = get_current_screen();
    203 
    204202        // Pretend to be in the site admin.
    205203        set_current_screen( 'dashboard' );
     
    244242
    245243        update_option( 'home', set_url_scheme( $home, 'http' ) );
    246 
    247         $GLOBALS['current_screen'] = $screen;
    248244    }
    249245
    250246    function test_network_home_url_from_admin() {
    251         $screen = get_current_screen();
    252 
    253247        // Pretend to be in the site admin.
    254248        set_current_screen( 'dashboard' );
     
    269263        $_SERVER['HTTPS'] = 'on';
    270264        $this->assertSame( $home_https, network_home_url() );
    271 
    272         $GLOBALS['current_screen'] = $screen;
    273265    }
    274266
  • trunk/tests/phpunit/tests/user/wpSendUserRequest.php

    r50463 r51419  
    8080
    8181        unset( $GLOBALS['locale'] );
    82         unset( $GLOBALS['current_screen'] );
    83         unset( $GLOBALS['taxnow'] );
    84         unset( $GLOBALS['typenow'] );
    8582
    8683        restore_previous_locale();
Note: See TracChangeset for help on using the changeset viewer.