Skip to content

Commit 2d537f7

Browse files
committed
Add failing test cases for test_wp_hoist_late_printed_styles
1 parent 2d94ec6 commit 2d537f7

File tree

1 file changed

+138
-19
lines changed

1 file changed

+138
-19
lines changed

tests/phpunit/tests/template.php

Lines changed: 138 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -991,38 +991,111 @@ public function test_wp_load_classic_theme_block_styles_on_demand( string $theme
991991
/**
992992
* Data provider.
993993
*
994-
* @return array<string, array{set_up: Closure|null}>
994+
* @return array<string, array{set_up: Closure|null, theme_supports: string[], expected: string[]}>
995995
*/
996996
public function data_wp_hoist_late_printed_styles(): array {
997+
$theme_supports = array(
998+
'wp-block-styles',
999+
);
1000+
1001+
$expected_head_styles = array(
1002+
'wp-img-auto-sizes-contain-inline-css',
1003+
'early-css',
1004+
'early-inline-css',
1005+
'wp-emoji-styles-inline-css',
1006+
'wp-block-library-css',
1007+
'wp-block-library-inline-css',
1008+
1009+
'wp-block-separator-inline-css',
1010+
'global-styles-inline-css',
1011+
'core-block-supports-inline-css',
1012+
'classic-theme-styles-inline-css',
1013+
'normal-css',
1014+
'normal-inline-css',
1015+
'wp-custom-css',
1016+
1017+
// TODO: This is unexpected. Just because something is enqueued late shouldn't necessitate that it be inserted right after wp-block-library and before a normal style enqueued at wp_enqueue_scripts.
1018+
'late-css',
1019+
'late-inline-css',
1020+
);
1021+
9971022
return array(
998-
'no_actions_removed' => array(
999-
'set_up' => null,
1023+
// TODO: Add test case for embed template.
1024+
'standard_classic_theme_config' => array(
1025+
'set_up' => null,
1026+
'theme_supports' => $theme_supports,
1027+
'expected' => $expected_head_styles,
1028+
),
1029+
'wp_block_styles_not_supported' => array(
1030+
'set_up' => null,
1031+
'theme_supports' => array(),
1032+
// The following excludes 'wp-block-separator-inline-css' from $expected_head_styles.
1033+
'expected' => array(
1034+
'wp-img-auto-sizes-contain-inline-css',
1035+
'early-css',
1036+
'early-inline-css',
1037+
'wp-emoji-styles-inline-css',
1038+
'wp-block-library-css',
1039+
'wp-block-library-inline-css',
1040+
'late-css',
1041+
'late-inline-css',
1042+
1043+
// TODO: The following three are enqueued in a different order compared to $expected_head_styles above. Why?
1044+
'core-block-supports-inline-css',
1045+
'classic-theme-styles-inline-css',
1046+
'global-styles-inline-css',
1047+
1048+
'normal-css',
1049+
'normal-inline-css',
1050+
),
10001051
),
10011052
'disabled_printing_late_styles' => array(
1002-
'set_up' => static function () {
1053+
'set_up' => static function () {
10031054
add_filter( 'print_late_styles', '__return_false', 1000 );
10041055
},
1056+
'theme_supports' => $theme_supports,
1057+
'expected' => $expected_head_styles,
10051058
),
10061059
'_wp_footer_scripts_removed' => array(
1007-
'set_up' => static function () {
1060+
'set_up' => static function () {
10081061
remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
10091062
},
1063+
'theme_supports' => $theme_supports,
1064+
'expected' => $expected_head_styles,
10101065
),
10111066
'wp_print_footer_scripts_removed' => array(
1012-
'set_up' => static function () {
1067+
'set_up' => static function () {
10131068
remove_action( 'wp_footer', 'wp_print_footer_scripts', 20 );
10141069
},
1070+
'theme_supports' => $theme_supports,
1071+
'expected' => $expected_head_styles,
10151072
),
10161073
'both_actions_removed' => array(
1017-
'set_up' => static function () {
1074+
'set_up' => static function () {
10181075
remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
10191076
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
10201077
},
1078+
'theme_supports' => $theme_supports,
1079+
'expected' => $expected_head_styles,
10211080
),
10221081
'block_library_removed' => array(
1023-
'set_up' => static function () {
1082+
'set_up' => static function () {
10241083
wp_deregister_style( 'wp-block-library' );
10251084
},
1085+
'theme_supports' => array(),
1086+
'expected' => array_values(
1087+
array_diff(
1088+
$expected_head_styles,
1089+
array(
1090+
'wp-block-separator-inline-css',
1091+
'wp-block-library-css',
1092+
'wp-block-library-inline-css',
1093+
'core-block-supports-inline-css',
1094+
'classic-theme-styles-inline-css',
1095+
'global-styles-inline-css',
1096+
)
1097+
)
1098+
),
10261099
),
10271100
);
10281101
}
@@ -1031,21 +1104,52 @@ public function data_wp_hoist_late_printed_styles(): array {
10311104
* Tests that wp_hoist_late_printed_styles() adds a placeholder for delayed CSS, then removes it and adds all CSS to the head including late enqueued styles.
10321105
*
10331106
* @ticket 64099
1107+
* @covers ::wp_load_classic_theme_block_styles_on_demand
10341108
* @covers ::wp_hoist_late_printed_styles
10351109
*
10361110
* @dataProvider data_wp_hoist_late_printed_styles
10371111
*/
1038-
public function test_wp_hoist_late_printed_styles( ?Closure $set_up ): void {
1112+
public function test_wp_hoist_late_printed_styles( ?Closure $set_up, array $theme_supports, array $expected ): void {
1113+
switch_theme( 'default' );
1114+
1115+
foreach ( $theme_supports as $theme_support ) {
1116+
add_theme_support( $theme_support );
1117+
}
1118+
1119+
add_filter(
1120+
'wp_get_custom_css',
1121+
static function () {
1122+
return '/* CUSTOM CSS from Customizer */';
1123+
}
1124+
);
1125+
10391126
if ( $set_up ) {
10401127
$set_up();
10411128
}
10421129

1043-
switch_theme( 'default' );
1130+
wp_load_classic_theme_block_styles_on_demand();
1131+
1132+
$block_registry = WP_Block_Type_Registry::get_instance();
1133+
foreach ( array_keys( $block_registry->get_all_registered() ) as $block_name ) {
1134+
$block_registry->unregister( $block_name );
1135+
}
1136+
register_core_block_types_from_metadata();
10441137

1045-
// Enqueue a style.
1138+
$this->assertFalse( wp_is_block_theme(), 'Test is only relevant to block themes.' );
1139+
1140+
// Enqueue a style early, before wp_enqueue_scripts.
10461141
wp_enqueue_style( 'early', 'https://example.com/style.css' );
10471142
wp_add_inline_style( 'early', '/* EARLY */' );
10481143

1144+
// Enqueue a style at the normal spot.
1145+
add_action(
1146+
'wp_enqueue_scripts',
1147+
static function () {
1148+
wp_enqueue_style( 'normal', 'https://example.com/normal.css' );
1149+
wp_add_inline_style( 'normal', '/* NORMAL */' );
1150+
}
1151+
);
1152+
10491153
wp_hoist_late_printed_styles();
10501154

10511155
// Ensure late styles are printed.
@@ -1094,19 +1198,13 @@ public function test_wp_hoist_late_printed_styles( ?Closure $set_up ): void {
10941198
}
10951199
}
10961200

1097-
$expected = array(
1098-
'early-css',
1099-
'early-inline-css',
1100-
'late-css',
1101-
'late-inline-css',
1102-
);
11031201
foreach ( $expected as $style_id ) {
1104-
$this->assertContains( $style_id, $found_styles['HEAD'], 'Expected stylesheet with ID to be in the HEAD.' );
1202+
$this->assertContains( $style_id, $found_styles['HEAD'], 'Expected stylesheet with ID to be in the HEAD among this snapshot: ' . self::get_array_snapshot_export( $found_styles['HEAD'] ) );
11051203
}
11061204
$this->assertSame(
11071205
$expected,
11081206
array_values( array_intersect( $found_styles['HEAD'], $expected ) ),
1109-
'Expected styles to be printed in the same order.'
1207+
'Expected styles to be printed in the same order. Snapshot: ' . self::get_array_snapshot_export( $found_styles['HEAD'] )
11101208
);
11111209
$this->assertCount( 0, $found_styles['BODY'], 'Expected no styles to be present in the footer.' );
11121210
}
@@ -1118,6 +1216,27 @@ public function assertTemplateHierarchy( $url, array $expected, $message = '' )
11181216
$this->assertSame( $expected, $hierarchy, $message );
11191217
}
11201218

1219+
/**
1220+
* Export PHP array as string formatted for pasting into unit test case.
1221+
*
1222+
* @param array $snapshot Snapshot.
1223+
* @return string Snapshot export.
1224+
*/
1225+
protected static function get_array_snapshot_export( array $snapshot ): string {
1226+
$export = var_export( $snapshot, true );
1227+
$export = preg_replace( '/\barray \($/m', 'array(', $export );
1228+
if ( isset( $snapshot[0] ) ) {
1229+
$export = preg_replace( '/^(\s+)\d+\s+=>\s+/m', '$1', $export );
1230+
}
1231+
return preg_replace_callback(
1232+
'/(^ +)/m',
1233+
static function ( $matches ) {
1234+
return str_repeat( "\t", strlen( $matches[0] ) / 2 );
1235+
},
1236+
$export
1237+
);
1238+
}
1239+
11211240
protected static function get_query_template_conditions() {
11221241
return array(
11231242
'embed' => 'is_embed',

0 commit comments

Comments
 (0)