Make WordPress Core

Changeset 56348


Ignore:
Timestamp:
08/02/2023 05:58:18 PM (11 months ago)
Author:
hellofromTonya
Message:

Tests: Fix leakage in WP_List_Table tests.

Fixes WP_List_table tests leaking into other tests by:

  • Restores the original $hook_suffix global value.

Rather than modifying the global for all tests, it now restores the original value between tests. Why? To ensure each test starts at a known state.

  • Uses a new instance of WP_List_Table for each test.

A test may modify the $list_table object. If it does, it could impact tests yet to run. By instantiating a new instance in the set_up() test fixture, each test is isolated from the others.

Follow-up to [53868], [54215].

Props hellofromTonya, antonvlasenko.
See #58955, #58896.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/wpListTable.php

    r54215 r56348  
    1313     * @var WP_List_Table $list_table
    1414     */
    15     protected static $list_table;
     15    private $list_table;
     16
     17    /**
     18     * Original value of $GLOBALS['hook_suffix'].
     19     *
     20     * @var string
     21     */
     22    private static $original_hook_suffix;
    1623
    1724    public static function set_up_before_class() {
     25        parent::set_up_before_class();
     26
     27        static::$original_hook_suffix = $GLOBALS['hook_suffix'];
     28
     29        require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
     30    }
     31
     32    public function set_up() {
     33        parent::set_up();
    1834        global $hook_suffix;
    19 
    20         parent::set_up_before_class();
    21 
    22         require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
    23 
    2435        $hook_suffix      = '_wp_tests';
    25         self::$list_table = new WP_List_Table();
     36        $this->list_table = new WP_List_Table();
     37    }
     38
     39    public function clean_up_global_scope() {
     40        global $hook_suffix;
     41        $hook_suffix = static::$original_hook_suffix;
     42        parent::clean_up_global_scope();
    2643    }
    2744
     
    143160     */
    144161    public function test_get_views_links( $link_data, $expected ) {
    145         $get_views_links = new ReflectionMethod( self::$list_table, 'get_views_links' );
     162        $get_views_links = new ReflectionMethod( $this->list_table, 'get_views_links' );
    146163        $get_views_links->setAccessible( true );
    147164
    148         $actual = $get_views_links->invokeArgs( self::$list_table, array( $link_data ) );
     165        $actual = $get_views_links->invokeArgs( $this->list_table, array( $link_data ) );
    149166
    150167        $this->assertSameSetsWithIndex( $expected, $actual );
     
    258275     */
    259276    public function test_get_views_links_doing_it_wrong( $link_data ) {
    260         $get_views_links = new ReflectionMethod( self::$list_table, 'get_views_links' );
     277        $get_views_links = new ReflectionMethod( $this->list_table, 'get_views_links' );
    261278        $get_views_links->setAccessible( true );
    262         $get_views_links->invokeArgs( self::$list_table, array( $link_data ) );
     279        $get_views_links->invokeArgs( $this->list_table, array( $link_data ) );
    263280    }
    264281
Note: See TracChangeset for help on using the changeset viewer.