diff --git a/tests/phpunit/tests/admin/includesPlugin.php b/tests/phpunit/tests/admin/includesPlugin.php index 4d47fb7db0a81..21e3064ff6b55 100644 --- a/tests/phpunit/tests/admin/includesPlugin.php +++ b/tests/phpunit/tests/admin/includesPlugin.php @@ -12,6 +12,29 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase { */ public static $admin_id; + public function set_up() { + parent::set_up(); + $this->reset_menu_globals(); + } + + public function tear_down() { + $this->reset_menu_globals(); + parent::tear_down(); + } + + /** + * Resets the global menu registries modified by the menu API tests. + */ + private function reset_menu_globals() { + global $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages; + + $menu = array(); + $submenu = array(); + $admin_page_hooks = array(); + $_registered_pages = array(); + $_parent_pages = array(); + } + public static function wpSetUpBeforeClass( $factory ) { self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); self::_back_up_mu_plugins(); @@ -501,6 +524,7 @@ public function test_get_mu_plugins_should_ignore_files_without_php_extensions() // Clean up. unlink( WPMU_PLUGIN_DIR . '/foo.php' ); unlink( WPMU_PLUGIN_DIR . '/bar.txt' ); + rmdir( WPMU_PLUGIN_DIR ); $this->assertSame( array( 'foo.php' ), array_keys( $found ) ); } diff --git a/tests/phpunit/tests/admin/plugin-dependencies/base.php b/tests/phpunit/tests/admin/plugin-dependencies/base.php index 22c509807b205..badd4cfe63c92 100644 --- a/tests/phpunit/tests/admin/plugin-dependencies/base.php +++ b/tests/phpunit/tests/admin/plugin-dependencies/base.php @@ -60,15 +60,29 @@ public static function tear_down_after_class() { parent::tear_down_after_class(); } + /** + * Resets all static properties to a default value before each test. + */ + public function set_up() { + parent::set_up(); + $this->reset_static_properties(); + } + /** * Resets all static properties to a default value after each test. */ public function tear_down() { + $this->reset_static_properties(); + parent::tear_down(); + } + + /** + * Resets all static properties to their default values. + */ + private function reset_static_properties() { foreach ( self::$static_properties as $name => $default_value ) { $this->set_property_value( $name, $default_value ); } - - parent::tear_down(); } /** diff --git a/tests/phpunit/tests/admin/wpAutomaticUpdater.php b/tests/phpunit/tests/admin/wpAutomaticUpdater.php index ee52f44daf9c1..2aca85616cb5f 100644 --- a/tests/phpunit/tests/admin/wpAutomaticUpdater.php +++ b/tests/phpunit/tests/admin/wpAutomaticUpdater.php @@ -40,6 +40,31 @@ public function set_up() { add_filter( 'pre_wp_mail', '__return_false' ); } + /** + * Builds an open_basedir value that allows PHPUnit to load Composer dependencies. + * + * Composer dependencies may live outside a secondary worktree, and PHPUnit can + * load assertion-related classes after the restriction is set. + * + * @param string $abspath_grandparent Directory containing the repository. + * @return string The open_basedir value. + */ + private function get_open_basedir_for_tests( $abspath_grandparent ) { + $composer_vendor_dir = dirname( + ( new ReflectionClass( Composer\Autoload\ClassLoader::class ) )->getFileName(), + 2 + ); + + return implode( + PATH_SEPARATOR, + array( + sys_get_temp_dir(), + wp_normalize_path( $abspath_grandparent ), + wp_normalize_path( $composer_vendor_dir ), + ) + ); + } + /** * Tests that `WP_Automatic_Updater::send_plugin_theme_email()` appends * plugin URLs. @@ -612,8 +637,7 @@ public function test_is_allowed_dir_should_return_true_if_open_basedir_is_set_an $abspath_grandparent = trailingslashit( dirname( $abspath_parent ) ); $open_basedir_backup = ini_get( 'open_basedir' ); - // Allow access to the directory one level above the repository. - ini_set( 'open_basedir', sys_get_temp_dir() . PATH_SEPARATOR . wp_normalize_path( $abspath_grandparent ) ); + ini_set( 'open_basedir', $this->get_open_basedir_for_tests( $abspath_grandparent ) ); // Checking an allowed directory should succeed. $actual = self::$updater->is_allowed_dir( wp_normalize_path( ABSPATH ) ); @@ -647,8 +671,7 @@ public function test_is_allowed_dir_should_return_false_if_open_basedir_is_set_a $abspath_grandparent = trailingslashit( dirname( $abspath_parent ) ); $open_basedir_backup = ini_get( 'open_basedir' ); - // Allow access to the directory one level above the repository. - ini_set( 'open_basedir', sys_get_temp_dir() . PATH_SEPARATOR . wp_normalize_path( $abspath_grandparent ) ); + ini_set( 'open_basedir', $this->get_open_basedir_for_tests( $abspath_grandparent ) ); // Checking a directory not within the allowed path should trigger an `open_basedir` warning. $actual = self::$updater->is_allowed_dir( '/.git' ); diff --git a/tests/phpunit/tests/admin/wpListTable.php b/tests/phpunit/tests/admin/wpListTable.php index b3de66659b308..ccf003f1b9129 100644 --- a/tests/phpunit/tests/admin/wpListTable.php +++ b/tests/phpunit/tests/admin/wpListTable.php @@ -17,14 +17,22 @@ class Tests_Admin_WpListTable extends WP_UnitTestCase { /** * Original value of $GLOBALS['hook_suffix']. * - * @var string + * @var string|null */ private static $original_hook_suffix; + /** + * Whether $GLOBALS['hook_suffix'] existed before the test class ran. + * + * @var bool + */ + private static $hook_suffix_was_set; + public static function set_up_before_class() { parent::set_up_before_class(); - static::$original_hook_suffix = $GLOBALS['hook_suffix']; + static::$hook_suffix_was_set = array_key_exists( 'hook_suffix', $GLOBALS ); + static::$original_hook_suffix = $GLOBALS['hook_suffix'] ?? null; require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; } @@ -37,8 +45,12 @@ public function set_up() { } public function clean_up_global_scope() { - global $hook_suffix; - $hook_suffix = static::$original_hook_suffix; + if ( static::$hook_suffix_was_set ) { + $GLOBALS['hook_suffix'] = static::$original_hook_suffix; + } else { + unset( $GLOBALS['hook_suffix'] ); + } + parent::clean_up_global_scope(); } @@ -67,7 +79,10 @@ public function test_should_only_add_primary_column_when_needed( $list_class, $h */ $GLOBALS['hook_suffix'] = 'my-hook'; - $list_table = _get_list_table( $list_class ); + $list_table = _get_list_table( + $list_class, + array( 'screen' => 'wp-list-table-test-' . sanitize_key( $list_class ) ) + ); $column_headers = new ReflectionProperty( $list_table, '_column_headers' ); if ( PHP_VERSION_ID < 80100 ) { @@ -97,7 +112,7 @@ public function data_should_only_add_primary_column_when_needed() { */ $list_primary_columns = array( 'WP_Application_Passwords_List_Table' => 'name', - 'WP_Comments_List_Table' => 'author', + 'WP_Comments_List_Table' => 'comment', 'WP_Links_List_Table' => 'name', 'WP_Media_List_Table' => 'title', 'WP_MS_Sites_List_Table' => 'blogname',