Skip to content

Commit 0ad3084

Browse files
committed
Fix tests when run as part of enture test suite
1 parent a8fe022 commit 0ad3084

File tree

1 file changed

+15
-49
lines changed

1 file changed

+15
-49
lines changed

tests/phpunit/tests/template.php

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
7878
*/
7979
protected $original_theme_features;
8080

81-
/**
82-
* @var WP_Block_Type_Registry
83-
*/
84-
protected $original_block_type_registry;
85-
8681
/**
8782
* @var array
8883
*/
@@ -127,44 +122,28 @@ public function set_up() {
127122
remove_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
128123
remove_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' );
129124

130-
global $wp_scripts, $wp_styles, $_wp_theme_features;
125+
global $wp_scripts, $wp_styles;
131126
$this->original_wp_scripts = $wp_scripts;
132127
$this->original_wp_styles = $wp_styles;
133128
$wp_scripts = null;
134129
$wp_styles = null;
135130
wp_scripts();
136131
wp_styles();
137132

138-
$this->original_theme_features = $_wp_theme_features;
139-
$_wp_theme_features = array();
140133
foreach ( self::RESTORED_CONFIG_OPTIONS as $option ) {
141134
$this->original_ini_config[ $option ] = ini_get( $option );
142135
}
143-
144-
$this->original_block_type_registry = WP_Block_Type_Registry::get_instance();
145136
}
146137

147138
public function tear_down() {
148-
global $wp_scripts, $wp_styles, $_wp_theme_features;
139+
global $wp_scripts, $wp_styles;
149140
$wp_scripts = $this->original_wp_scripts;
150141
$wp_styles = $this->original_wp_styles;
151142

152-
$_wp_theme_features = $this->original_theme_features;
153-
foreach ( $this->original_ini_config as $option => $value ) {
154-
ini_set( $option, $value );
155-
}
156-
157143
unregister_post_type( 'cpt' );
158144
unregister_taxonomy( 'taxo' );
159145
$this->set_permalink_structure( '' );
160146

161-
$reflection_class = new ReflectionClass( WP_Block_Type_Registry::class );
162-
$instance_property = $reflection_class->getProperty( 'instance' );
163-
if ( PHP_VERSION_ID < 80100 ) {
164-
$instance_property->setAccessible( true );
165-
}
166-
$instance_property->setValue( null, $this->original_block_type_registry );
167-
168147
parent::tear_down();
169148
}
170149

@@ -1491,21 +1470,16 @@ public function test_wp_load_classic_theme_block_styles_on_demand( string $theme
14911470
/**
14921471
* Data provider.
14931472
*
1494-
* @return array<string, array{set_up: Closure|null, theme_supports: string[], expected_styles: array{ HEAD: string[], BODY: string[] }}>
1473+
* @return array<string, array{set_up: Closure|null, expected_styles: array{ HEAD: string[], BODY: string[] }}>
14951474
*/
14961475
public function data_wp_hoist_late_printed_styles(): array {
1497-
$theme_supports = array(
1498-
'wp-block-styles',
1499-
);
1500-
15011476
$common_expected_head_styles = array(
15021477
'wp-img-auto-sizes-contain-inline-css',
15031478
'early-css',
15041479
'early-inline-css',
15051480
'wp-emoji-styles-inline-css',
15061481
'wp-block-library-css',
15071482
'wp-block-separator-css',
1508-
'wp-block-separator-theme-css',
15091483
'global-styles-inline-css',
15101484
'core-block-supports-inline-css',
15111485
'classic-theme-styles-css',
@@ -1520,7 +1494,6 @@ public function data_wp_hoist_late_printed_styles(): array {
15201494
// TODO: Add test case for embed template.
15211495
'standard_classic_theme_config' => array(
15221496
'set_up' => null,
1523-
'theme_supports' => $theme_supports,
15241497
'expected_styles' => array(
15251498
'HEAD' => $common_expected_head_styles,
15261499
'BODY' => array(),
@@ -1535,7 +1508,6 @@ static function () {
15351508
}
15361509
);
15371510
},
1538-
'theme_supports' => $theme_supports,
15391511
'expected_styles' => array(
15401512
'HEAD' => ( function ( $expected_styles ) {
15411513
// Insert 'wp-block-library-inline-css' right after 'wp-block-library-css'.
@@ -1551,15 +1523,13 @@ static function () {
15511523
'set_up' => static function () {
15521524
add_filter( 'should_load_separate_core_block_assets', '__return_false' );
15531525
},
1554-
'theme_supports' => $theme_supports,
15551526
'expected_styles' => array(
15561527
'HEAD' => array(
15571528
'wp-img-auto-sizes-contain-inline-css',
15581529
'early-css',
15591530
'early-inline-css',
15601531
'wp-emoji-styles-inline-css',
15611532
'wp-block-library-css',
1562-
'wp-block-library-theme-css',
15631533
'classic-theme-styles-css',
15641534
'global-styles-inline-css',
15651535
'normal-css',
@@ -1575,7 +1545,6 @@ static function () {
15751545
),
15761546
'wp_block_styles_not_supported' => array(
15771547
'set_up' => null,
1578-
'theme_supports' => array(),
15791548
'expected_styles' => array(
15801549
'HEAD' => array_values(
15811550
array_diff(
@@ -1592,7 +1561,6 @@ static function () {
15921561
'set_up' => static function () {
15931562
remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
15941563
},
1595-
'theme_supports' => $theme_supports,
15961564
'expected_styles' => array(
15971565
'HEAD' => $common_expected_head_styles,
15981566
'BODY' => array(),
@@ -1602,7 +1570,6 @@ static function () {
16021570
'set_up' => static function () {
16031571
remove_action( 'wp_footer', 'wp_print_footer_scripts', 20 );
16041572
},
1605-
'theme_supports' => $theme_supports,
16061573
'expected_styles' => array(
16071574
'HEAD' => $common_expected_head_styles,
16081575
'BODY' => array(),
@@ -1613,7 +1580,6 @@ static function () {
16131580
remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
16141581
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
16151582
},
1616-
'theme_supports' => $theme_supports,
16171583
'expected_styles' => array(
16181584
'HEAD' => $common_expected_head_styles,
16191585
'BODY' => array(),
@@ -1630,7 +1596,6 @@ function (): void {
16301596
);
16311597
add_filter( 'should_load_separate_core_block_assets', '__return_false' );
16321598
},
1633-
'theme_supports' => array(),
16341599
'expected_styles' => array(
16351600
'HEAD' => array(
16361601
'wp-img-auto-sizes-contain-inline-css',
@@ -1662,15 +1627,11 @@ function (): void {
16621627
*
16631628
* @dataProvider data_wp_hoist_late_printed_styles
16641629
*/
1665-
public function test_wp_hoist_late_printed_styles( ?Closure $set_up, array $theme_supports, array $expected_styles ): void {
1630+
public function test_wp_hoist_late_printed_styles( ?Closure $set_up, array $expected_styles ): void {
16661631
switch_theme( 'default' );
16671632
global $wp_styles;
16681633
$wp_styles = null;
16691634

1670-
foreach ( $theme_supports as $theme_support ) {
1671-
add_theme_support( $theme_support );
1672-
}
1673-
16741635
// Disable the styles_inline_size_limit in order to prevent changes from invalidating the snapshots.
16751636
add_filter(
16761637
'styles_inline_size_limit',
@@ -1692,12 +1653,7 @@ static function () {
16921653

16931654
wp_load_classic_theme_block_styles_on_demand();
16941655

1695-
$block_registry = WP_Block_Type_Registry::get_instance();
1696-
foreach ( array_keys( $block_registry->get_all_registered() ) as $block_name ) {
1697-
$block_registry->unregister( $block_name );
1698-
}
16991656
register_core_block_style_handles();
1700-
register_core_block_types_from_metadata(); // See register_block_type_from_metadata().
17011657

17021658
$this->assertFalse( wp_is_block_theme(), 'Test is not relevant to block themes (only classic themes).' );
17031659

@@ -1763,9 +1719,19 @@ static function () {
17631719
}
17641720
}
17651721

1722+
/*
1723+
* Since new styles could appear at any time and since certain styles leak in from the global scope not being
1724+
* properly reset somewhere else in the test suite, we only check that the expected styles are at least present
1725+
* and in the same order.
1726+
*/
1727+
$found_subset_styles = array();
1728+
foreach ( array( 'HEAD', 'BODY' ) as $group ) {
1729+
$found_subset_styles[ $group ] = array_values( array_intersect( $found_styles[ $group ], $expected_styles[ $group ] ) );
1730+
}
1731+
17661732
$this->assertSame(
17671733
$expected_styles,
1768-
$found_styles,
1734+
$found_subset_styles,
17691735
'Expected the same styles. Snapshot: ' . self::get_array_snapshot_export( $found_styles )
17701736
);
17711737
}

0 commit comments

Comments
 (0)