Skip to content

Commit 830c406

Browse files
committed
fix all unit tests
1 parent 2b1b2b9 commit 830c406

File tree

6 files changed

+89
-20
lines changed

6 files changed

+89
-20
lines changed

inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,17 @@ private function remove_internal_styles_from_html( string $clean_html, string $h
378378
* @return array Filtered head items.
379379
*/
380380
public function add_used_css_to_html( array $items ): array {
381-
if ( empty( $this->used_css_content ) ) {
381+
$used_css = $this->get_used_css_content();
382+
if ( empty( $used_css ) ) {
382383
return $items;
383384
}
384385

385386
$items[] = $this->style_tag(
386-
$this->get_used_css_markup( $this->used_css_content ),
387+
$this->get_used_css_markup( $used_css ),
387388
[
388389
'id' => 'wpr-usedcss',
389390
]
390-
);
391+
);
391392
return $items;
392393
}
393394

@@ -408,7 +409,7 @@ public function insert_preload_fonts( $items ) {
408409
'as' => 'font',
409410
1 => 'crossorigin',
410411
]
411-
);
412+
);
412413
}
413414

414415
return $items;
@@ -697,4 +698,13 @@ static function ( $item ) {
697698
public function has_one_completed_row_at_least() {
698699
return $this->used_css_query->get_completed_count() > 0;
699700
}
701+
702+
/**
703+
* Get generated used CSS, getter method for used_css_content property.
704+
*
705+
* @return string
706+
*/
707+
public function get_used_css_content() {
708+
return $this->used_css_content;
709+
}
700710
}

inc/Engine/Preload/Fonts.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function __construct( Options_Data $options, CDN $cdn ) {
8080
*/
8181
public static function get_subscribed_events() {
8282
return [
83-
'rocket_head_items' => [ 'preload_fonts', 30 ],
84-
'rocket_buffer' => [ 'insert_meta_comment', 20 ],
83+
'rocket_head_items' => [ 'insert_preloaded_fonts_into_head', 30 ],
84+
'rocket_buffer' => [ 'preload_fonts', 20 ],
8585
];
8686
}
8787

@@ -93,7 +93,7 @@ public static function get_subscribed_events() {
9393
*
9494
* @since 3.6
9595
*/
96-
public function insert_meta_comment( $html ): string {
96+
public function preload_fonts( $html ): string {
9797
if ( ! $this->is_allowed() ) {
9898
return $html;
9999
}
@@ -130,7 +130,7 @@ public function insert_meta_comment( $html ): string {
130130
* @param array $items Head elements.
131131
* @return array
132132
*/
133-
public function preload_fonts( $items ): array {
133+
public function insert_preloaded_fonts_into_head( $items ): array {
134134
if ( empty( $this->fonts ) ) {
135135
return $items;
136136
}
@@ -142,7 +142,7 @@ public function preload_fonts( $items ): array {
142142
'as' => 'font',
143143
1 => 'crossorigin',
144144
]
145-
);
145+
);
146146
}
147147
return $items;
148148
}
@@ -192,4 +192,13 @@ private function is_allowed(): bool {
192192
*/
193193
return ! apply_filters( 'rocket_disable_preload_fonts', false );
194194
}
195+
196+
/**
197+
* Get fonts to preload, getter method for fonts property.
198+
*
199+
* @return array
200+
*/
201+
public function get_fonts(): array {
202+
return $this->fonts;
203+
}
195204
}

tests/Fixtures/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/treeshake.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@
8888
],
8989
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/original.php'),
9090
],
91-
'expected' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered.php'),
91+
'expected' => [
92+
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered.php'),
93+
'used_css' => 'h1{color:red;}',
94+
],
9295
],
9396
'expectedFilteredHTMlWhenNoPreconnectGoogleAPI' => [
9497
'config' => [
@@ -112,7 +115,10 @@
112115
],
113116
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/google_fonts.php'),
114117
],
115-
'expected' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered.php'),
118+
'expected' => [
119+
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered.php'),
120+
'used_css' => 'h1{color:red;}',
121+
],
116122
],
117123
'expectedFilteredHTMlWhenNoEmptyUsedCSSExcludeAttr' => [
118124
'config' => [
@@ -142,7 +148,10 @@
142148
],
143149
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/original_exclude_attr.php'),
144150
],
145-
'expected' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered_exclude_attr.php'),
151+
'expected' => [
152+
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered_exclude_attr.php'),
153+
'used_css' => 'h1{color:red;}',
154+
],
146155
],
147156
'expectedFilteredHTMlWhenNoEmptyUsedCSSExcludeContent' => [
148157
'config' => [
@@ -172,7 +181,10 @@
172181
],
173182
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/original_exclude_content.php'),
174183
],
175-
'expected' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered_exclude_content.php'),
184+
'expected' => [
185+
'html' => file_get_contents(WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/HTML/filtered_exclude_content.php'),
186+
'used_css' => 'h1{color:red;}',
187+
],
176188
],
177189
'expectSameHtmlWhenNoTitleTag' => [
178190
'config' => [

tests/Fixtures/inc/Engine/Preload/Fonts/preloadFonts.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,18 @@
126126
'cdn' => false,
127127
'cdn_cnames' => [],
128128
],
129-
'expected' => $valid_preload_fonts_options,
129+
'expected' => [
130+
'html' => $valid_preload_fonts_options,
131+
'fonts' => [
132+
'http://example.org/wp-content/file.otf',
133+
'http://example.org/wp-content/file.ttf',
134+
'http://example.org/wp-content/file.svg',
135+
'http://example.org/wp-content/file.woff?v=4.4.0',
136+
'http://example.org/wp-content/file.woff2',
137+
'http://example.org/wp-content/themes/paperback/inc/fontawesome/fonts/fontawesome-webfont.woff2?v=4.4.0',
138+
'http://example.org/wp-content/themes/paperback/inc/fontawesome/fonts/fontawesome-webfont.woff2#123',
139+
],
140+
],
130141
],
131142
'validPreloadFontsOptionsWithCDN' => [
132143
'buffer' => $html,
@@ -146,6 +157,15 @@
146157
'https://123456.rocketcdn.me',
147158
],
148159
],
149-
'expected' => $valid_preload_fonts_options_wit_cdn,
160+
'expected' => [
161+
'html' => $valid_preload_fonts_options_wit_cdn,
162+
'fonts' => [
163+
'https://123456.rocketcdn.me/wp-content/file.otf',
164+
'https://123456.rocketcdn.me/wp-content/file.ttf',
165+
'https://123456.rocketcdn.me/wp-content/file.svg',
166+
'https://123456.rocketcdn.me/wp-content/file.woff',
167+
'https://123456.rocketcdn.me/wp-content/file.woff2',
168+
],
169+
],
150170
],
151171
];

tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/treeshake.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,16 @@ public function testShouldReturnAsExpected( $config, $expected ) {
107107
->once()
108108
->andReturn( $dynamic_lists );
109109

110-
$this->assertEquals($this->format_the_html($expected), $this->format_the_html($this->usedCss->treeshake($config['html'])));
110+
$optimized_html = $this->usedCss->treeshake($config['html']);
111+
if ( is_string( $expected ) ) {
112+
$this->assertEquals(
113+
$this->format_the_html($expected),
114+
$this->format_the_html( $optimized_html )
115+
);
116+
} else if ( is_array( $expected ) ) {
117+
$this->assertSame( $expected['used_css'], $this->usedCss->get_used_css_content() );
118+
}
119+
111120
}
112121

113122
protected function configureIsMobile($config) {

tests/Unit/inc/Engine/Preload/Fonts/preloadFonts.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,19 @@ public function testShouldAddPreloadTagsWhenValidFonts( $buffer, $bypass, $filte
6363
} );
6464
}
6565

66-
$this->assertSame(
67-
$expected,
68-
$this->fonts->preload_fonts( $buffer )
69-
);
66+
$optimized_buffer = $this->fonts->preload_fonts( $buffer );
67+
if ( is_string( $expected ) ) {
68+
$this->assertSame(
69+
$expected,
70+
$optimized_buffer,
71+
);
72+
} else if ( is_array( $expected ) ) {
73+
$this->assertSame(
74+
$expected['fonts'],
75+
$this->fonts->get_fonts(),
76+
);
77+
}
78+
7079
}
7180

7281
public function providerTestData() {

0 commit comments

Comments
 (0)