From 88570fd5b301f78d4e9280aa6b643a1937888d52 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Wed, 2 Aug 2023 17:58:18 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@56348 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/admin/wpListTable.php | 35 +++++++++++++++++------ 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/tests/admin/wpListTable.php b/tests/phpunit/tests/admin/wpListTable.php index c28113d99f..b9ada05365 100644 --- a/tests/phpunit/tests/admin/wpListTable.php +++ b/tests/phpunit/tests/admin/wpListTable.php @@ -12,17 +12,34 @@ class Tests_Admin_WpListTable extends WP_UnitTestCase { * * @var WP_List_Table $list_table */ - protected static $list_table; + private $list_table; + + /** + * Original value of $GLOBALS['hook_suffix']. + * + * @var string + */ + private static $original_hook_suffix; public static function set_up_before_class() { - global $hook_suffix; - parent::set_up_before_class(); - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; + static::$original_hook_suffix = $GLOBALS['hook_suffix']; + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; + } + + public function set_up() { + parent::set_up(); + global $hook_suffix; $hook_suffix = '_wp_tests'; - self::$list_table = new WP_List_Table(); + $this->list_table = new WP_List_Table(); + } + + public function clean_up_global_scope() { + global $hook_suffix; + $hook_suffix = static::$original_hook_suffix; + parent::clean_up_global_scope(); } /** @@ -142,10 +159,10 @@ class Tests_Admin_WpListTable extends WP_UnitTestCase { * @param array $expected */ public function test_get_views_links( $link_data, $expected ) { - $get_views_links = new ReflectionMethod( self::$list_table, 'get_views_links' ); + $get_views_links = new ReflectionMethod( $this->list_table, 'get_views_links' ); $get_views_links->setAccessible( true ); - $actual = $get_views_links->invokeArgs( self::$list_table, array( $link_data ) ); + $actual = $get_views_links->invokeArgs( $this->list_table, array( $link_data ) ); $this->assertSameSetsWithIndex( $expected, $actual ); } @@ -257,9 +274,9 @@ class Tests_Admin_WpListTable extends WP_UnitTestCase { * } */ public function test_get_views_links_doing_it_wrong( $link_data ) { - $get_views_links = new ReflectionMethod( self::$list_table, 'get_views_links' ); + $get_views_links = new ReflectionMethod( $this->list_table, 'get_views_links' ); $get_views_links->setAccessible( true ); - $get_views_links->invokeArgs( self::$list_table, array( $link_data ) ); + $get_views_links->invokeArgs( $this->list_table, array( $link_data ) ); } /**