Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions tests/class-js-concat-test-case.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

abstract class JS_Concat_Test_Case extends WP_UnitTestCase {
protected $tmp_files = [];
protected $did_items = null;

public function set_up(): void {
parent::set_up();
require_once dirname( __DIR__ ) . '/concat-js.php';

add_action( 'js_concat_did_items', [ $this, 'capture_did_items' ], 10, 1 );
}

public function tear_down(): void {
remove_action( 'js_concat_did_items', [ $this, 'capture_did_items' ], 10 );

foreach ( $this->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;
}
}

73 changes: 31 additions & 42 deletions tests/test_css_concat_eligibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,6 @@ public function test_single_internal_style_is_not_served_via_static_combo_endpoi
$this->assertStringContainsString( '?m=', $html );
}

/**
* Stylesheets wrapped in IE conditional comments (e.g., <!--[if lt IE 9]>)
* cannot be concatenated.
*
* Conditional comments are HTML-level constructs that wrap the entire <link> tag.
* You can't concatenate a conditional stylesheet with a non-conditional one - the
* conditional wrapper would incorrectly apply to both.
*/
public function test_conditional_styles_are_not_concatenated(): void {
$styles = $this->new_concat_styles();

$a = $this->make_content_css( 'po-cond-a.css' );
$b = $this->make_content_css( 'po-cond-b.css' );

$styles->add( 'a', $a, [], null, 'all' );
$styles->add( 'b', $b, [], null, 'all' );

// Mark b as conditional (IE, etc.) - should not be concatenated.
$styles->registered['b']->extra['conditional'] = 'lt IE 9';

$styles->enqueue( 'a' );
$styles->enqueue( 'b' );

$html = $this->render( $styles );
$groups = $this->extract_handle_groups( $html );

foreach ( $groups as $g ) {
if ( in_array( 'b', $g, true ) ) {
$this->assertCount( 1, $g, 'Conditional stylesheet should never be concatenated with others.' );
}
}
}

/**
* When the page is in RTL (right-to-left) mode and a stylesheet has RTL-specific
* handling, it cannot be concatenated.
Expand Down Expand Up @@ -119,12 +86,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 <link> 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' );
Expand All @@ -149,6 +126,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 ) {
Expand All @@ -164,14 +143,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();
Expand All @@ -193,8 +180,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 );
}
}
}
Loading
Loading