Skip to content

Commit 2b88415

Browse files
committed
add test for wp_use_placeholder_for_delayed_css()
1 parent 80d18e8 commit 2b88415

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/phpunit/tests/template.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,47 @@ public function test_wp_always_load_block_styles_on_demand_init_in_classic_theme
851851
$this->assertTrue( true == has_action( 'wp_template_enhancement_output_buffer_started', 'wp_use_placeholder_for_delayed_css' ), 'Expect wp_template_enhancement_output_buffer_started filter to be added for classic themes.' );
852852
}
853853

854+
/**
855+
* Tests that wp_use_placeholder_for_delayed_css adds a placeholder for delayed CSS, then removes it and adds all CSS to the head including late enqueued styles.
856+
*
857+
* @ticket 64099
858+
* @covers ::wp_use_placeholder_for_delayed_css
859+
*/
860+
public function test_wp_use_placeholder_for_delayed_css(): void {
861+
862+
// Enqueue a style
863+
wp_enqueue_style( 'test-style', 'http://example.com/style.css' );
864+
865+
wp_use_placeholder_for_delayed_css();
866+
867+
// Simulate wp_head
868+
ob_start();
869+
do_action( 'wp_head' );
870+
$head_output = ob_get_clean();
871+
872+
$this->assertMatchesRegularExpression( '/<!--late_styles:[a-f0-9-]{36}-->/', $head_output, 'Expect the placeholder to be present' );
873+
$this->assertStringContainsString( 'test-style', $head_output, 'Expect the enqueued stylesheet to be present' );
874+
875+
// Enqueue a late style (after wp_head)
876+
wp_enqueue_style( 'test-late-style', 'http://example.com/late-style.css' );
877+
878+
// Simulate footer scripts
879+
ob_start();
880+
do_action( 'wp_print_footer_scripts' );
881+
$footer_output = ob_get_clean();
882+
883+
// Create a complete HTML buffer
884+
$buffer = '<html><head>' . $head_output . '</head><body>Content' . $footer_output . '</body></html>';
885+
886+
// Apply the output buffer filter
887+
$filtered_buffer = apply_filters( 'wp_template_enhancement_output_buffer', $buffer );
888+
889+
preg_match( '/<head>(.+?)<\/head>/s', $filtered_buffer, $head_matches );
890+
$this->assertNotEmpty( $head_matches, 'Expect the late enqueued styles to be present in the header' );
891+
$this->assertStringContainsString( 'test-late-style', $head_matches[1], 'Expect the late enqueued styles to be present in the header' );
892+
$this->assertStringNotContainsString( '<!--late_styles:', $filtered_buffer, 'Expect the placeholder to be gone' );
893+
}
894+
854895
public function assertTemplateHierarchy( $url, array $expected, $message = '' ) {
855896
$this->go_to( $url );
856897
$hierarchy = $this->get_template_hierarchy();

0 commit comments

Comments
 (0)