From 5b722eae2f336d7651d0fca312dcbe607a76f1e0 Mon Sep 17 00:00:00 2001 From: Matthew Reishus Date: Fri, 30 Jan 2026 18:46:57 +0000 Subject: [PATCH 1/6] update tests --- tests/test_css_concat_eligibility.php | 40 ++++++++++++++++++----- tests/test_css_concat_order.php | 47 +++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/tests/test_css_concat_eligibility.php b/tests/test_css_concat_eligibility.php index 1e73914..eeaa9bf 100644 --- a/tests/test_css_concat_eligibility.php +++ b/tests/test_css_concat_eligibility.php @@ -119,12 +119,22 @@ public function test_rtl_marked_styles_not_concatenated_when_text_direction_is_r } /** - * Admins can configure specific handles to be excluded from concatenation via the - * page_optimize-css-exclude option. - * - * Some stylesheets break when concatenated (e.g., they use @import or have - * relative URLs that break). This gives admins an escape hatch. - */ + * Admins can configure specific handles to be excluded from concatenation via the + * page_optimize-css-exclude option. + * + * Some stylesheets break when concatenated (e.g., they use @import or have + * relative URLs that break). This gives admins an escape hatch. + * + * Excluded handles must: + * 1. Not be concatenated with other handles (rendered in their own tag) + * 2. Maintain their original enqueue order (not be pushed to the end) + * + * Enqueue order: a (excluded) -> b + * Expected output order: [a], [b] (a first, in its own tag) + * Bug: [b], [a] (excluded handle pushed to end) + * + * @group css-order-bug + */ public function test_exclusion_list_prevents_concatenation_of_handle(): void { $old = get_option( 'page_optimize-css-exclude', null ); update_option( 'page_optimize-css-exclude', 'a' ); @@ -149,6 +159,8 @@ public function test_exclusion_list_prevents_concatenation_of_handle(): void { $this->assertCount( 1, $g, 'Excluded handle should not be concatenated with others.' ); } } + $handles = $this->flatten_groups( $groups ); + $this->assertSame( [ 'a', 'b' ], $handles ); } finally { // Restore option so this test doesn't affect other tests. if ( null === $old ) { @@ -164,14 +176,22 @@ public function test_exclusion_list_prevents_concatenation_of_handle(): void { * * This is the code-based equivalent of the exclusion list-plugins or themes can * hook in and prevent concatenation of specific stylesheets based on custom logic. + * + * Enqueue order: a (filtered to be excluded) -> b + * Expected output order: [a], [b] (a first, in its own tag) + * Bug: [b], [a] (excluded handle pushed to end) + * + * @group css-order-bug */ public function test_css_do_concat_filter_can_disable_concatenation(): void { - add_filter( 'css_do_concat', function( $do_concat, $handle ) { + $filter_callback = function( $do_concat, $handle ) { if ( 'a' === $handle ) { return false; } return $do_concat; - }, 10, 2 ); + }; + + add_filter( 'css_do_concat', $filter_callback, 10, 2 ); try { $styles = $this->new_concat_styles(); @@ -193,8 +213,10 @@ public function test_css_do_concat_filter_can_disable_concatenation(): void { $this->assertCount( 1, $g, 'css_do_concat filter should be able to prevent concatenation of a handle.' ); } } + $handles = $this->flatten_groups( $groups ); + $this->assertSame( [ 'a', 'b' ], $handles, 'Filtered handle must not be reordered behind concatenated output.' ); } finally { - remove_all_filters( 'css_do_concat' ); + remove_filter( 'css_do_concat', $filter_callback, 10 ); } } } diff --git a/tests/test_css_concat_order.php b/tests/test_css_concat_order.php index a61b31f..b33e8af 100644 --- a/tests/test_css_concat_order.php +++ b/tests/test_css_concat_order.php @@ -2,9 +2,6 @@ require_once __DIR__ . '/class-css-concat-test-case.php'; -/** - * @group page-optimize - */ class Test_CSS_Concat_Order extends CSS_Concat_Test_Case { /** * When an external/CDN stylesheet appears between two local stylesheets, the concatenation must be split. @@ -35,6 +32,40 @@ public function test_nonconcat_item_breaks_concat_run_and_preserves_order(): voi $this->assertSame( [ [ 'a' ], [ 'b' ], [ 'c' ] ], $groups, 'Expected external stylesheet to break concat and keep order.' ); } + /** + * When an external/CDN stylesheet is enqueued first, it must remain first in the output. + * + * Enqueue order: a (external CDN) -> b (local) -> c (local) + * Expected output: [a], [b, c] or [a], [b], [c] (a must be first) + * + * @group css-order-bug + */ + public function test_external_stylesheet_first_preserves_order(): void { + $styles = $this->new_concat_styles(); + + $b = $this->make_content_css( 'po-b.css' ); + $c = $this->make_content_css( 'po-c.css' ); + + $styles->add( 'a', 'https://cdn.example.invalid/a.css', [], null, 'all' ); // External URL - can't be concatted + $styles->add( 'b', $b, [], null, 'all' ); + $styles->add( 'c', $c, [], null, 'all' ); + + $styles->enqueue( 'a' ); + $styles->enqueue( 'b' ); + $styles->enqueue( 'c' ); + + $html = $this->render( $styles ); + $groups = $this->extract_handle_groups( $html ); + $handles = $this->flatten_groups( $groups ); + + // External 'a' must be first, b and c must follow in order. + $this->assertSame( [ 'a', 'b', 'c' ], $handles, 'External stylesheet enqueued first must remain first in output.' ); + + // 'a' must be in its own group (not concatenated). + $this->assertNotEmpty( $groups ); + $this->assertSame( [ 'a' ], $groups[0], 'External stylesheet should not be concatenated.' ); + } + /** * When stylesheets have different media attributes, concatenation must respect the original order. * @@ -97,5 +128,15 @@ public function test_inline_style_should_break_concat_boundary(): void { // "a" and "b" must not be in the same concatenated . $this->assertSame( [ [ 'a' ], [ 'b' ] ], $groups, 'Inline CSS should force a boundary (do not concat across inline styles).' ); + + // Actually check that the inline style appears before "b" stylesheet. + // Otherwise, we could split A and B into groups, but then do A,B,Inline, which is wrong. + $this->assertNotFalse( strpos( $html, '.po-inline-a' ), 'Expected inline CSS to be printed.' ); + + $pos_inline = strpos( $html, '.po-inline-a' ); + $pos_b = strpos( $html, 'po-ib.css' ); + + $this->assertNotFalse( $pos_b, 'Expected b stylesheet URL in output.' ); + $this->assertLessThan( $pos_b, $pos_inline, 'Inline CSS for a must appear before b stylesheet.' ); } } From 157aa2b851224d14e350cf9696d7ca793d6af811 Mon Sep 17 00:00:00 2001 From: Matthew Reishus Date: Fri, 30 Jan 2026 18:51:38 +0000 Subject: [PATCH 2/6] Add tests covering resume concatenation after boundary, media run with actual concatenation --- tests/test_css_concat_order.php | 91 +++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tests/test_css_concat_order.php b/tests/test_css_concat_order.php index b33e8af..dc7bccd 100644 --- a/tests/test_css_concat_order.php +++ b/tests/test_css_concat_order.php @@ -139,4 +139,95 @@ public function test_inline_style_should_break_concat_boundary(): void { $this->assertNotFalse( $pos_b, 'Expected b stylesheet URL in output.' ); $this->assertLessThan( $pos_b, $pos_inline, 'Inline CSS for a must appear before b stylesheet.' ); } + + /** + * After a non-concatenatable item breaks a run, subsequent local stylesheets + * should still be concatenated together. + * + * This catches over-correction where fixing ordering bugs leads to giving up + * on concatenation entirely after any boundary. + * + * Enqueue order: a (local) -> ext (external) -> b (local) -> c (local) + * Expected: [a], [ext], [b, c] (order preserved, b+c still concatenated) + * + * @group css-order-bug + */ + public function test_concatenation_resumes_after_nonconcat_boundary(): void { + $styles = $this->new_concat_styles(); + + $a = $this->make_content_css( 'po-resume-a.css' ); + $b = $this->make_content_css( 'po-resume-b.css' ); + $c = $this->make_content_css( 'po-resume-c.css' ); + + $styles->add( 'a', $a, [], null, 'all' ); + $styles->add( 'ext', 'https://cdn.example.invalid/external.css', [], null, 'all' ); + $styles->add( 'b', $b, [], null, 'all' ); + $styles->add( 'c', $c, [], null, 'all' ); + + $styles->enqueue( 'a' ); + $styles->enqueue( 'ext' ); + $styles->enqueue( 'b' ); + $styles->enqueue( 'c' ); + + $html = $this->render( $styles ); + $groups = $this->extract_handle_groups( $html ); + + $handles = $this->flatten_groups( $groups ); + $this->assertSame( [ 'a', 'ext', 'b', 'c' ], $handles, 'Order must be preserved.' ); + + // 'a' should be alone (before external boundary). + $this->assertSame( [ 'a' ], $groups[0], 'First local stylesheet should be in its own group before external.' ); + + // 'ext' should be alone (external, not concatenatable). + $this->assertSame( [ 'ext' ], $groups[1], 'External stylesheet should be in its own group.' ); + + // 'b' and 'c' should be concatenated together (resume after boundary). + $this->assertSame( [ 'b', 'c' ], $groups[2], 'Concatenation should resume after external boundary.' ); + } + + /** + * Stylesheets with the same media attribute should still be concatenated + * within their run, even when different media types cause boundaries. + * + * This ensures the fix for media interleaving doesn't accidentally prevent + * concatenation within same-media runs. + * + * Enqueue order: a (all) -> b (screen) -> c (screen) -> d (all) + * Expected: [a], [b, c], [d] (order preserved, b+c concatenated) + * + * @group css-order-bug + */ + public function test_same_media_stylesheets_concatenate_within_run(): void { + $styles = $this->new_concat_styles(); + + $a = $this->make_content_css( 'po-media-run-a.css' ); + $b = $this->make_content_css( 'po-media-run-b.css' ); + $c = $this->make_content_css( 'po-media-run-c.css' ); + $d = $this->make_content_css( 'po-media-run-d.css' ); + + $styles->add( 'a', $a, [], null, 'all' ); + $styles->add( 'b', $b, [], null, 'screen' ); + $styles->add( 'c', $c, [], null, 'screen' ); + $styles->add( 'd', $d, [], null, 'all' ); + + $styles->enqueue( 'a' ); + $styles->enqueue( 'b' ); + $styles->enqueue( 'c' ); + $styles->enqueue( 'd' ); + + $html = $this->render( $styles ); + $groups = $this->extract_handle_groups( $html ); + + $handles = $this->flatten_groups( $groups ); + $this->assertSame( [ 'a', 'b', 'c', 'd' ], $handles, 'Order must be preserved across media boundaries.' ); + + // 'a' alone (media=all, before screen run). + $this->assertSame( [ 'a' ], $groups[0], 'First all-media stylesheet should be alone before screen run.' ); + + // 'b' and 'c' concatenated (both media=screen). + $this->assertSame( [ 'b', 'c' ], $groups[1], 'Same-media stylesheets should be concatenated within their run.' ); + + // 'd' alone (media=all, after screen run). + $this->assertSame( [ 'd' ], $groups[2], 'Final all-media stylesheet should be alone after screen run.' ); + } } From c81bc1cacf7e94866745626692e7e409aea1c898 Mon Sep 17 00:00:00 2001 From: Matthew Reishus Date: Fri, 30 Jan 2026 19:06:59 +0000 Subject: [PATCH 3/6] update comment --- tests/test_css_concat_order.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_css_concat_order.php b/tests/test_css_concat_order.php index dc7bccd..f2cdcb3 100644 --- a/tests/test_css_concat_order.php +++ b/tests/test_css_concat_order.php @@ -104,6 +104,7 @@ public function test_media_interleaving_must_not_reorder_handles(): void { * * Enqueue: a (with inline style after it) -> b * Expected output: [a], [b] (two separate tags, not combined) + * Expected output: Inline should appear before b stylesheet. * * @group css-order-bug * From 23a39b1f5c5aa671b82db2d35a2e4b0c80b35c84 Mon Sep 17 00:00:00 2001 From: Matthew Reishus Date: Fri, 30 Jan 2026 19:10:38 +0000 Subject: [PATCH 4/6] Add JS tests --- tests/class-js-concat-test-case.php | 110 +++++++++++++ tests/test_js_concat_eligibility.php | 233 +++++++++++++++++++++++++++ tests/test_js_concat_order.php | 121 ++++++++++++++ 3 files changed, 464 insertions(+) create mode 100644 tests/class-js-concat-test-case.php create mode 100644 tests/test_js_concat_eligibility.php create mode 100644 tests/test_js_concat_order.php diff --git a/tests/class-js-concat-test-case.php b/tests/class-js-concat-test-case.php new file mode 100644 index 0000000..76a2b81 --- /dev/null +++ b/tests/class-js-concat-test-case.php @@ -0,0 +1,110 @@ +tmp_files as $file ) { + @unlink( $file ); + } + $this->tmp_files = []; + + parent::tear_down(); + } + + public function capture_did_items( $items ): void { + $this->did_items = $items; + } + + protected function make_content_js( string $filename, string $contents = 'console.log("test");' ): string { + $fs = trailingslashit( WP_CONTENT_DIR ) . $filename; + $result = file_put_contents( $fs, $contents ); + $this->assertNotFalse( $result, 'Failed to write temporary JS fixture.' ); + $this->tmp_files[] = $fs; + + return content_url( $filename ); + } + + protected function new_concat_scripts(): Page_Optimize_JS_Concat { + $core = new WP_Scripts(); + $core->base_url = untrailingslashit( site_url() ); + + $concat = new Page_Optimize_JS_Concat( $core ); + $concat->allow_gzip_compression = false; + + return $concat; + } + + protected function render( WP_Scripts $scripts, $group = false ): string { + $this->did_items = null; + ob_start(); + $scripts->do_items( false, $group ); + return (string) ob_get_clean(); + } + + protected function render_head( WP_Scripts $scripts ): string { + $this->did_items = null; + ob_start(); + $scripts->do_items( false, 0 ); // head pass + return (string) ob_get_clean(); + } + + protected function render_footer( WP_Scripts $scripts ): string { + $this->did_items = null; + ob_start(); + + // This matches core behavior: footer pass processes only what head collected. + if ( method_exists( $scripts, 'do_footer_items' ) ) { + $scripts->do_footer_items(); + } else { + $scripts->do_items( $scripts->in_footer, 1 ); + } + + return (string) ob_get_clean(); + } + + protected function render_head_groups( WP_Scripts $scripts ): array { + $h = $this->render_head( $scripts ); + return $this->extract_handle_groups_from_did_items(); + } + + protected function render_footer_groups( WP_Scripts $scripts ): array { + $this->render_footer( $scripts ); + return $this->extract_handle_groups_from_did_items(); + } + + protected function extract_handle_groups_from_did_items(): array { + $this->assertNotNull( $this->did_items, 'Expected js_concat_did_items to fire and capture items.' ); + + $groups = []; + foreach ( $this->did_items as $item ) { + if ( ( $item['type'] ?? null ) === 'concat' ) { + $groups[] = $item['handles'] ?? []; + } elseif ( ( $item['type'] ?? null ) === 'do_item' ) { + $groups[] = [ $item['handle'] ]; + } + } + return $groups; + } + + protected function flatten_groups( array $groups ): array { + $out = []; + foreach ( $groups as $g ) { + foreach ( $g as $h ) { + $out[] = $h; + } + } + return $out; + } +} + diff --git a/tests/test_js_concat_eligibility.php b/tests/test_js_concat_eligibility.php new file mode 100644 index 0000000..10f1b45 --- /dev/null +++ b/tests/test_js_concat_eligibility.php @@ -0,0 +1,233 @@ + tag. + * + * Also verifies the concatenated URL uses the /_static/?? combo endpoint + * (the server-side mechanism that serves multiple files in one request). + */ + public function test_concats_two_internal_scripts_into_one_group(): void { + $scripts = $this->new_concat_scripts(); + + $a = $this->make_content_js( 'po-a.js' ); + $b = $this->make_content_js( 'po-b.js' ); + + $scripts->add( 'a', $a, [], null, false ); + $scripts->add( 'b', $b, [], null, false ); + + $scripts->enqueue( 'a' ); + $scripts->enqueue( 'b' ); + + $html = $this->render( $scripts ); + $groups = $this->extract_handle_groups_from_did_items(); + + $this->assertSame( [ [ 'a', 'b' ] ], $groups ); + $this->assertStringContainsString( '/_static/??', $html ); + } + + /** + * When an external/CDN script appears between two local scripts, the + * concatenation must be split to preserve execution order. + * + * Enqueue order: a (local) -> b (external CDN) -> c (local) + * Expected output: [a], [b], [c] (three separate