Skip to content

Commit 99b1b42

Browse files
committed
Improve tests
1 parent 16d3f54 commit 99b1b42

File tree

1 file changed

+88
-56
lines changed

1 file changed

+88
-56
lines changed

tests/phpunit/tests/template.php

Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ public function test_wp_load_classic_theme_block_styles_on_demand( string $theme
14751475
/**
14761476
* Data provider.
14771477
*
1478-
* @return array<string, array{set_up: Closure|null, theme_supports: string[], expected: string[]}>
1478+
* @return array<string, array{set_up: Closure|null, theme_supports: string[], expected_head: string[], expected_footer: string[]}>
14791479
*/
14801480
public function data_wp_hoist_late_printed_styles(): array {
14811481
$theme_supports = array(
@@ -1489,93 +1489,121 @@ public function data_wp_hoist_late_printed_styles(): array {
14891489
'wp-emoji-styles-inline-css',
14901490
'wp-block-library-inline-css',
14911491
'wp-block-separator-inline-css',
1492+
'wp-block-separator-theme-inline-css',
14921493
'global-styles-inline-css',
14931494
'core-block-supports-inline-css',
14941495
'classic-theme-styles-inline-css',
14951496
'normal-css',
14961497
'normal-inline-css',
14971498
'wp-custom-css',
1498-
1499-
// 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.
15001499
'late-css',
15011500
'late-inline-css',
15021501
);
15031502

15041503
return array(
15051504
// TODO: Add test case for embed template.
1506-
'standard_classic_theme_config' => array(
1507-
'set_up' => null,
1508-
'theme_supports' => $theme_supports,
1509-
'expected' => $expected_head_styles,
1505+
'standard_classic_theme_config' => array(
1506+
'set_up' => null,
1507+
'theme_supports' => $theme_supports,
1508+
'expected_head' => $expected_head_styles,
1509+
'expected_footer' => array(),
15101510
),
1511-
'wp_block_styles_not_supported' => array(
1512-
'set_up' => null,
1513-
'theme_supports' => array(),
1514-
// The following excludes 'wp-block-separator-inline-css' from $expected_head_styles.
1515-
'expected' => array(
1511+
'classic_theme_opt_out_separate_block_styles' => array(
1512+
'set_up' => static function () {
1513+
add_filter( 'should_load_separate_core_block_assets', '__return_false' );
1514+
},
1515+
'theme_supports' => $theme_supports,
1516+
'expected_head' => array(
15161517
'wp-img-auto-sizes-contain-inline-css',
15171518
'early-css',
15181519
'early-inline-css',
15191520
'wp-emoji-styles-inline-css',
15201521
'wp-block-library-css',
1521-
'wp-block-library-inline-css',
1522-
'core-block-supports-inline-css',
1522+
'wp-block-library-theme-inline-css',
15231523
'classic-theme-styles-inline-css',
15241524
'global-styles-inline-css',
15251525
'normal-css',
15261526
'normal-inline-css',
15271527
'wp-custom-css',
1528+
),
1529+
'expected_footer' => array(
15281530
'late-css',
15291531
'late-inline-css',
15301532
),
15311533
),
1532-
'disabled_printing_late_styles' => array(
1533-
'set_up' => static function () {
1534+
'wp_block_styles_not_supported' => array(
1535+
'set_up' => null,
1536+
'theme_supports' => array(),
1537+
'expected_head' => array_values(
1538+
array_diff(
1539+
$expected_head_styles,
1540+
array(
1541+
'wp-block-separator-theme-inline-css',
1542+
)
1543+
)
1544+
),
1545+
'expected_footer' => array(),
1546+
),
1547+
'disabled_printing_late_styles' => array(
1548+
'set_up' => static function () {
15341549
add_filter( 'print_late_styles', '__return_false', 1000 );
15351550
},
1536-
'theme_supports' => $theme_supports,
1537-
'expected' => $expected_head_styles,
1551+
'theme_supports' => $theme_supports,
1552+
'expected_head' => $expected_head_styles,
1553+
'expected_footer' => array(),
15381554
),
1539-
'_wp_footer_scripts_removed' => array(
1540-
'set_up' => static function () {
1555+
'_wp_footer_scripts_removed' => array(
1556+
'set_up' => static function () {
15411557
remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
15421558
},
1543-
'theme_supports' => $theme_supports,
1544-
'expected' => $expected_head_styles,
1559+
'theme_supports' => $theme_supports,
1560+
'expected_head' => $expected_head_styles,
1561+
'expected_footer' => array(),
15451562
),
1546-
'wp_print_footer_scripts_removed' => array(
1547-
'set_up' => static function () {
1563+
'wp_print_footer_scripts_removed' => array(
1564+
'set_up' => static function () {
15481565
remove_action( 'wp_footer', 'wp_print_footer_scripts', 20 );
15491566
},
1550-
'theme_supports' => $theme_supports,
1551-
'expected' => $expected_head_styles,
1567+
'theme_supports' => $theme_supports,
1568+
'expected_head' => $expected_head_styles,
1569+
'expected_footer' => array(),
15521570
),
1553-
'both_actions_removed' => array(
1554-
'set_up' => static function () {
1571+
'both_actions_removed' => array(
1572+
'set_up' => static function () {
15551573
remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
15561574
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
15571575
},
1558-
'theme_supports' => $theme_supports,
1559-
'expected' => $expected_head_styles,
1576+
'theme_supports' => $theme_supports,
1577+
'expected_head' => $expected_head_styles,
1578+
'expected_footer' => array(),
15601579
),
1561-
'block_library_removed' => array(
1562-
'set_up' => static function () {
1563-
wp_deregister_style( 'wp-block-library' );
1580+
'disable_block_library' => array(
1581+
'set_up' => static function () {
1582+
add_action(
1583+
'enqueue_block_assets',
1584+
function (): void {
1585+
wp_deregister_style( 'wp-block-library' );
1586+
wp_register_style( 'wp-block-library', '' );
1587+
}
1588+
);
1589+
add_filter( 'should_load_separate_core_block_assets', '__return_false' );
15641590
},
1565-
'theme_supports' => array(),
1566-
'expected' => array_values(
1567-
array_diff(
1568-
$expected_head_styles,
1569-
array(
1570-
'wp-block-separator-inline-css',
1571-
'wp-block-library-css',
1572-
'wp-block-library-inline-css',
1573-
'core-block-supports-inline-css',
1574-
'classic-theme-styles-inline-css',
1575-
'global-styles-inline-css',
1576-
)
1577-
)
1591+
'theme_supports' => array(),
1592+
'expected_head' => array(
1593+
'wp-img-auto-sizes-contain-inline-css',
1594+
'early-css',
1595+
'early-inline-css',
1596+
'wp-emoji-styles-inline-css',
1597+
'classic-theme-styles-inline-css',
1598+
'global-styles-inline-css',
1599+
'normal-css',
1600+
'normal-inline-css',
1601+
'wp-custom-css',
1602+
'core-block-supports-inline-css',
1603+
'late-css',
1604+
'late-inline-css',
15781605
),
1606+
'expected_footer' => array(),
15791607
),
15801608
);
15811609
}
@@ -1589,7 +1617,7 @@ public function data_wp_hoist_late_printed_styles(): array {
15891617
*
15901618
* @dataProvider data_wp_hoist_late_printed_styles
15911619
*/
1592-
public function test_wp_hoist_late_printed_styles( ?Closure $set_up, array $theme_supports, array $expected ): void {
1620+
public function test_wp_hoist_late_printed_styles( ?Closure $set_up, array $theme_supports, array $expected_head, array $expected_footer ): void {
15931621
switch_theme( 'default' );
15941622
global $wp_styles;
15951623
$wp_styles = null;
@@ -1616,7 +1644,7 @@ static function () {
16161644
$block_registry->unregister( $block_name );
16171645
}
16181646
register_core_block_style_handles();
1619-
register_core_block_types_from_metadata();
1647+
register_core_block_types_from_metadata(); // See register_block_type_from_metadata().
16201648

16211649
$this->assertFalse( wp_is_block_theme(), 'Test is only relevant to block themes.' );
16221650

@@ -1633,7 +1661,10 @@ static function () {
16331661
}
16341662
);
16351663

1636-
wp_hoist_late_printed_styles();
1664+
// Call wp_hoist_late_printed_styles() if wp_load_classic_theme_block_styles_on_demand() queued it up.
1665+
if ( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) {
1666+
wp_hoist_late_printed_styles();
1667+
}
16371668

16381669
// Ensure late styles are printed.
16391670
$this->assertTrue( apply_filters( 'print_late_styles', true ), 'Expected late style printing to be forced.' );
@@ -1681,15 +1712,16 @@ static function () {
16811712
}
16821713
}
16831714

1684-
foreach ( $expected as $style_id ) {
1685-
$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'] ) );
1686-
}
16871715
$this->assertSame(
1688-
$expected,
1689-
array_values( array_intersect( $found_styles['HEAD'], $expected ) ),
1690-
'Expected styles to be printed in the same order. Snapshot: ' . self::get_array_snapshot_export( $found_styles['HEAD'] )
1716+
$expected_head,
1717+
$found_styles['HEAD'],
1718+
'Expected the same styles in the HEAD. Snapshot: ' . self::get_array_snapshot_export( $found_styles['HEAD'] )
1719+
);
1720+
$this->assertSame(
1721+
$expected_head,
1722+
$found_styles['HEAD'],
1723+
'Expected the same styles in the BODY. Snapshot: ' . self::get_array_snapshot_export( $found_styles['BODY'] )
16911724
);
1692-
$this->assertCount( 0, $found_styles['BODY'], 'Expected no styles to be present in the footer.' );
16931725
}
16941726

16951727
public function assertTemplateHierarchy( $url, array $expected, $message = '' ) {

0 commit comments

Comments
 (0)