Skip to content

Commit

Permalink
fix all unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Feb 18, 2025
1 parent 2b1b2b9 commit 830c406
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 20 deletions.
18 changes: 14 additions & 4 deletions inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,17 @@ private function remove_internal_styles_from_html( string $clean_html, string $h
* @return array Filtered head items.
*/
public function add_used_css_to_html( array $items ): array {
if ( empty( $this->used_css_content ) ) {
$used_css = $this->get_used_css_content();
if ( empty( $used_css ) ) {
return $items;
}

$items[] = $this->style_tag(
$this->get_used_css_markup( $this->used_css_content ),
$this->get_used_css_markup( $used_css ),
[
'id' => 'wpr-usedcss',
]
);
);
return $items;
}

Expand All @@ -408,7 +409,7 @@ public function insert_preload_fonts( $items ) {
'as' => 'font',
1 => 'crossorigin',
]
);
);
}

return $items;
Expand Down Expand Up @@ -697,4 +698,13 @@ static function ( $item ) {
public function has_one_completed_row_at_least() {
return $this->used_css_query->get_completed_count() > 0;
}

/**
* Get generated used CSS, getter method for used_css_content property.
*
* @return string
*/
public function get_used_css_content() {
return $this->used_css_content;
}
}
19 changes: 14 additions & 5 deletions inc/Engine/Preload/Fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public function __construct( Options_Data $options, CDN $cdn ) {
*/
public static function get_subscribed_events() {
return [
'rocket_head_items' => [ 'preload_fonts', 30 ],
'rocket_buffer' => [ 'insert_meta_comment', 20 ],
'rocket_head_items' => [ 'insert_preloaded_fonts_into_head', 30 ],
'rocket_buffer' => [ 'preload_fonts', 20 ],
];
}

Expand All @@ -93,7 +93,7 @@ public static function get_subscribed_events() {
*
* @since 3.6
*/
public function insert_meta_comment( $html ): string {
public function preload_fonts( $html ): string {
if ( ! $this->is_allowed() ) {
return $html;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public function insert_meta_comment( $html ): string {
* @param array $items Head elements.
* @return array
*/
public function preload_fonts( $items ): array {
public function insert_preloaded_fonts_into_head( $items ): array {
if ( empty( $this->fonts ) ) {
return $items;
}
Expand All @@ -142,7 +142,7 @@ public function preload_fonts( $items ): array {
'as' => 'font',
1 => 'crossorigin',
]
);
);
}
return $items;
}
Expand Down Expand Up @@ -192,4 +192,13 @@ private function is_allowed(): bool {
*/
return ! apply_filters( 'rocket_disable_preload_fonts', false );
}

/**
* Get fonts to preload, getter method for fonts property.
*
* @return array
*/
public function get_fonts(): array {
return $this->fonts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@
],
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/original.php'),
],
'expected' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered.php'),
'expected' => [
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered.php'),
'used_css' => 'h1{color:red;}',
],
],
'expectedFilteredHTMlWhenNoPreconnectGoogleAPI' => [
'config' => [
Expand All @@ -112,7 +115,10 @@
],
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/google_fonts.php'),
],
'expected' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered.php'),
'expected' => [
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered.php'),
'used_css' => 'h1{color:red;}',
],
],
'expectedFilteredHTMlWhenNoEmptyUsedCSSExcludeAttr' => [
'config' => [
Expand Down Expand Up @@ -142,7 +148,10 @@
],
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/original_exclude_attr.php'),
],
'expected' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered_exclude_attr.php'),
'expected' => [
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered_exclude_attr.php'),
'used_css' => 'h1{color:red;}',
],
],
'expectedFilteredHTMlWhenNoEmptyUsedCSSExcludeContent' => [
'config' => [
Expand Down Expand Up @@ -172,7 +181,10 @@
],
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/original_exclude_content.php'),
],
'expected' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered_exclude_content.php'),
'expected' => [
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered_exclude_content.php'),
'used_css' => 'h1{color:red;}',
],
],
'expectSameHtmlWhenNoTitleTag' => [
'config' => [
Expand Down
24 changes: 22 additions & 2 deletions tests/Fixtures/inc/Engine/Preload/Fonts/preloadFonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,18 @@
'cdn' => false,
'cdn_cnames' => [],
],
'expected' => $valid_preload_fonts_options,
'expected' => [
'html' => $valid_preload_fonts_options,
'fonts' => [
'http://example.org/wp-content/file.otf',
'http://example.org/wp-content/file.ttf',
'http://example.org/wp-content/file.svg',
'http://example.org/wp-content/file.woff?v=4.4.0',
'http://example.org/wp-content/file.woff2',
'http://example.org/wp-content/themes/paperback/inc/fontawesome/fonts/fontawesome-webfont.woff2?v=4.4.0',
'http://example.org/wp-content/themes/paperback/inc/fontawesome/fonts/fontawesome-webfont.woff2#123',
],
],
],
'validPreloadFontsOptionsWithCDN' => [
'buffer' => $html,
Expand All @@ -146,6 +157,15 @@
'https://123456.rocketcdn.me',
],
],
'expected' => $valid_preload_fonts_options_wit_cdn,
'expected' => [
'html' => $valid_preload_fonts_options_wit_cdn,
'fonts' => [
'https://123456.rocketcdn.me/wp-content/file.otf',
'https://123456.rocketcdn.me/wp-content/file.ttf',
'https://123456.rocketcdn.me/wp-content/file.svg',
'https://123456.rocketcdn.me/wp-content/file.woff',
'https://123456.rocketcdn.me/wp-content/file.woff2',
],
],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ public function testShouldReturnAsExpected( $config, $expected ) {
->once()
->andReturn( $dynamic_lists );

$this->assertEquals($this->format_the_html($expected), $this->format_the_html($this->usedCss->treeshake($config['html'])));
$optimized_html = $this->usedCss->treeshake($config['html']);
if ( is_string( $expected ) ) {
$this->assertEquals(
$this->format_the_html($expected),
$this->format_the_html( $optimized_html )
);
} else if ( is_array( $expected ) ) {
$this->assertSame( $expected['used_css'], $this->usedCss->get_used_css_content() );
}

}

protected function configureIsMobile($config) {
Expand Down
17 changes: 13 additions & 4 deletions tests/Unit/inc/Engine/Preload/Fonts/preloadFonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ public function testShouldAddPreloadTagsWhenValidFonts( $buffer, $bypass, $filte
} );
}

$this->assertSame(
$expected,
$this->fonts->preload_fonts( $buffer )
);
$optimized_buffer = $this->fonts->preload_fonts( $buffer );
if ( is_string( $expected ) ) {
$this->assertSame(
$expected,
$optimized_buffer,
);
} else if ( is_array( $expected ) ) {
$this->assertSame(
$expected['fonts'],
$this->fonts->get_fonts(),
);
}

}

public function providerTestData() {
Expand Down

0 comments on commit 830c406

Please sign in to comment.