From 26a581a5290bb01569fb08e7d1b1bf6f6b84357e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 19 Aug 2026 22:42:01 +0400 Subject: [PATCH 1/3] Tests: Improve theme test isolation. --- .../phpunit/data/themedir1/block-theme/theme.json | 5 +++++ tests/phpunit/tests/template.php | 5 +++++ tests/phpunit/tests/theme/support.php | 6 ++++++ .../tests/theme/wpAddGlobalStylesForBlocks.php | 14 +++++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/data/themedir1/block-theme/theme.json b/tests/phpunit/data/themedir1/block-theme/theme.json index 2c29884bf4dcb..4dca1dfb0ac2d 100644 --- a/tests/phpunit/data/themedir1/block-theme/theme.json +++ b/tests/phpunit/data/themedir1/block-theme/theme.json @@ -96,6 +96,11 @@ } } } + }, + "my/unregistered-third-party-block": { + "color": { + "background": "hotpink" + } } }, "elements": { diff --git a/tests/phpunit/tests/template.php b/tests/phpunit/tests/template.php index a79702554dc64..00d64ae4d982e 100644 --- a/tests/phpunit/tests/template.php +++ b/tests/phpunit/tests/template.php @@ -151,6 +151,11 @@ public function tear_down() { $registry->unregister( 'third-party/test' ); } + if ( isset( $GLOBALS['_wp_tests_development_mode'] ) ) { + remove_all_filters( 'theme_file_path' ); + wp_theme_has_theme_json(); + } + unset( $GLOBALS['_wp_tests_development_mode'] ); parent::tear_down(); } diff --git a/tests/phpunit/tests/theme/support.php b/tests/phpunit/tests/theme/support.php index aed9d94d3c1fd..d7262273e8536 100644 --- a/tests/phpunit/tests/theme/support.php +++ b/tests/phpunit/tests/theme/support.php @@ -41,6 +41,7 @@ public function test_post_thumbnails() { $this->assertFalse( current_theme_supports( 'post-thumbnails' ) ); add_theme_support( 'post-thumbnails' ); $this->assertTrue( current_theme_supports( 'post-thumbnails' ) ); + remove_theme_support( 'post-thumbnails' ); } public function test_post_thumbnails_flat_array_of_post_types() { @@ -57,6 +58,8 @@ public function test_post_thumbnails_flat_array_of_post_types() { * @ticket 22080 */ public function test_post_thumbnails_mixed_args() { + remove_theme_support( 'post-thumbnails' ); + add_theme_support( 'post-thumbnails', array( 'post', 'page' ) ); add_theme_support( 'post-thumbnails', array( 'page' ) ); $this->assertTrue( current_theme_supports( 'post-thumbnails', 'post' ) ); @@ -162,6 +165,8 @@ public function supports_foobar( $yesno, $args, $feature ) { * @ticket 11611 */ public function test_plugin_hook() { + remove_theme_support( 'foobar' ); + $this->assertFalse( current_theme_supports( 'foobar' ) ); add_theme_support( 'foobar' ); $this->assertTrue( current_theme_supports( 'foobar' ) ); @@ -185,6 +190,7 @@ public function test_plugin_hook_with_no_args() { add_filter( 'current_theme_supports-foobar', '__return_false' ); $this->assertFalse( current_theme_supports( 'foobar' ) ); + remove_theme_support( 'foobar' ); } /** diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index b3a836dee704f..a7f8301971dd4 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -18,6 +18,13 @@ class Tests_Theme_WpAddGlobalStylesForBlocks extends WP_Theme_UnitTestCase { */ private $test_blocks = array(); + /** + * Original stylesheet. + * + * @var string + */ + private $original_stylesheet; + /** * Administrator ID. * @@ -37,6 +44,7 @@ public static function set_up_before_class() { public function set_up() { parent::set_up(); + $this->original_stylesheet = get_stylesheet(); remove_action( 'wp_print_styles', 'print_emoji_styles' ); } @@ -49,6 +57,10 @@ public function tear_down() { $this->test_blocks = array(); } + if ( get_stylesheet() !== $this->original_stylesheet ) { + switch_theme( $this->original_stylesheet ); + } + parent::tear_down(); } @@ -63,7 +75,7 @@ public function test_third_party_blocks_inline_styles_not_register_to_global_sty wp_add_global_styles_for_blocks(); $this->assertNotContains( - ':root :where(.wp-block-my-third-party-block){background-color: hotpink;}', + ':root :where(.wp-block-my-unregistered-third-party-block){background-color: hotpink;}', $this->get_global_styles() ); } From a922788480fb7c0407e7c6908304a9ee505cbc62 Mon Sep 17 00:00:00 2001 From: Lance Willett Date: Thu, 3 Sep 2026 12:44:50 -0700 Subject: [PATCH 2/3] Tests: Reset theme support during test teardown --- tests/phpunit/tests/theme/support.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/theme/support.php b/tests/phpunit/tests/theme/support.php index d7262273e8536..42477426bd9c5 100644 --- a/tests/phpunit/tests/theme/support.php +++ b/tests/phpunit/tests/theme/support.php @@ -5,6 +5,13 @@ */ class Tests_Theme_Support extends WP_UnitTestCase { + public function tear_down() { + remove_theme_support( 'post-thumbnails' ); + remove_theme_support( 'foobar' ); + + parent::tear_down(); + } + public function test_the_basics() { add_theme_support( 'automatic-feed-links' ); $this->assertTrue( current_theme_supports( 'automatic-feed-links' ) ); @@ -41,7 +48,6 @@ public function test_post_thumbnails() { $this->assertFalse( current_theme_supports( 'post-thumbnails' ) ); add_theme_support( 'post-thumbnails' ); $this->assertTrue( current_theme_supports( 'post-thumbnails' ) ); - remove_theme_support( 'post-thumbnails' ); } public function test_post_thumbnails_flat_array_of_post_types() { @@ -58,8 +64,6 @@ public function test_post_thumbnails_flat_array_of_post_types() { * @ticket 22080 */ public function test_post_thumbnails_mixed_args() { - remove_theme_support( 'post-thumbnails' ); - add_theme_support( 'post-thumbnails', array( 'post', 'page' ) ); add_theme_support( 'post-thumbnails', array( 'page' ) ); $this->assertTrue( current_theme_supports( 'post-thumbnails', 'post' ) ); @@ -165,8 +169,6 @@ public function supports_foobar( $yesno, $args, $feature ) { * @ticket 11611 */ public function test_plugin_hook() { - remove_theme_support( 'foobar' ); - $this->assertFalse( current_theme_supports( 'foobar' ) ); add_theme_support( 'foobar' ); $this->assertTrue( current_theme_supports( 'foobar' ) ); @@ -190,7 +192,6 @@ public function test_plugin_hook_with_no_args() { add_filter( 'current_theme_supports-foobar', '__return_false' ); $this->assertFalse( current_theme_supports( 'foobar' ) ); - remove_theme_support( 'foobar' ); } /** From 235d65ffa9300bb98924e41c87c27cacc02416d5 Mon Sep 17 00:00:00 2001 From: Lance Willett Date: Tue, 15 Sep 2026 09:37:15 -0700 Subject: [PATCH 3/3] Tests: Restore theme state around individual tests. --- tests/phpunit/tests/theme.php | 19 ++++++++++++--- tests/phpunit/tests/theme/support.php | 35 ++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index 47d18bb40c71e..b7beb20c0dd67 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -33,14 +33,22 @@ class Tests_Theme extends WP_UnitTestCase { */ private $orig_theme_dir; + /** + * Theme features registered before the test ran. + * + * @var array + */ + private $orig_theme_features; + public function set_up() { global $wp_theme_directories; parent::set_up(); // Sets up the `wp-content/themes/` directory to ensure consistency when running tests. - $this->orig_theme_dir = $wp_theme_directories; - $wp_theme_directories = array( WP_CONTENT_DIR . '/themes', realpath( DIR_TESTDATA . '/themedir1' ) ); + $this->orig_theme_dir = $wp_theme_directories; + $wp_theme_directories = array( WP_CONTENT_DIR . '/themes', realpath( DIR_TESTDATA . '/themedir1' ) ); + $this->orig_theme_features = $GLOBALS['_wp_theme_features']; add_filter( 'extra_theme_headers', array( $this, 'theme_data_extra_headers' ) ); wp_clean_themes_cache(); @@ -56,7 +64,12 @@ public function tear_down() { wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); - parent::tear_down(); + try { + parent::tear_down(); + } finally { + // Restore after the parent removes HTML5 theme support. + $GLOBALS['_wp_theme_features'] = $this->orig_theme_features; + } } public function test_wp_get_themes_default() { diff --git a/tests/phpunit/tests/theme/support.php b/tests/phpunit/tests/theme/support.php index 42477426bd9c5..e2810ab5cb8ee 100644 --- a/tests/phpunit/tests/theme/support.php +++ b/tests/phpunit/tests/theme/support.php @@ -5,11 +5,38 @@ */ class Tests_Theme_Support extends WP_UnitTestCase { - public function tear_down() { - remove_theme_support( 'post-thumbnails' ); - remove_theme_support( 'foobar' ); + /** + * Theme features registered before the test ran. + * + * @var array + */ + private $orig_theme_features; + + /** + * Nav menu locations registered before the test ran. + * + * @var array + */ + private $orig_registered_nav_menus; + + public function set_up() { + parent::set_up(); + + $this->orig_theme_features = $GLOBALS['_wp_theme_features']; + $this->orig_registered_nav_menus = $GLOBALS['_wp_registered_nav_menus'] ?? array(); - parent::tear_down(); + // Start without support from the bootstrapped theme. + $GLOBALS['_wp_theme_features'] = array(); + } + + public function tear_down() { + try { + parent::tear_down(); + } finally { + // Restore after the parent removes HTML5 theme support. + $GLOBALS['_wp_theme_features'] = $this->orig_theme_features; + $GLOBALS['_wp_registered_nav_menus'] = $this->orig_registered_nav_menus; + } } public function test_the_basics() {