8
8
9
9
class Test_Audit_Enqueued_Assets extends WP_UnitTestCase {
10
10
11
- /**
12
- * Mocked responses for HTTP requests.
13
- *
14
- * @var array<string, mixed>
15
- */
16
- private $ mocked_responses = array ();
17
-
18
11
/**
19
12
* Tests perflab_aea_audit_enqueued_scripts() when blocking scripts are present.
20
13
*
@@ -43,14 +36,16 @@ public function test_perflab_aea_audit_enqueued_scripts_blocking_scripts_are_pre
43
36
}
44
37
45
38
/**
46
- * Tests perflab_aea_audit_enqueued_scripts() with no transient.
47
- * Enqueued scripts ( not belonging to core /wp-includes/ ) will be saved in transient.
39
+ * Tests perflab_aea_audit_enqueued_scripts() with blocking scripts.
40
+ *
41
+ * @covers ::perflab_aea_audit_blocking_assets
48
42
*/
49
43
public function test_perflab_aea_audit_enqueued_scripts (): void {
50
44
/**
51
45
* Prepare scenario for test.
52
46
*/
53
47
$ this ->current_user_can_view_site_health_checks_cap ();
48
+ Audit_Assets_Mock_Assets::clear_mocked ();
54
49
55
50
wp_enqueue_script ( 'script1 ' , 'https://example1.com ' , array (), null );
56
51
wp_enqueue_script ( 'script2 ' , '/wp-includes/example2.js ' , array (), null );
@@ -59,6 +54,8 @@ public function test_perflab_aea_audit_enqueued_scripts(): void {
59
54
wp_enqueue_script ( 'script-async ' , 'https://async-script.com ' , array (), null , true );
60
55
wp_enqueue_script ( 'script-defer ' , 'https://defer-script.com ' , array (), null , true );
61
56
wp_enqueue_script ( 'type-noscript ' , 'https://non-javascript.com ' , array (), null );
57
+ wp_enqueue_script ( 'no-src ' , 'no-src ' , array (), null );
58
+ wp_enqueue_script_module ( 'module1 ' , 'https://module1.com ' , array (), null );
62
59
63
60
add_filter (
64
61
'wp_script_attributes ' ,
@@ -69,6 +66,8 @@ static function ( $attributes ) {
69
66
$ attributes ['defer ' ] = true ;
70
67
} elseif ( 'type-noscript-js ' === $ attributes ['id ' ] ) {
71
68
$ attributes ['type ' ] = 'noscript ' ;
69
+ } elseif ( 'no-src-js ' === $ attributes ['id ' ] ) {
70
+ unset( $ attributes ['src ' ] );
72
71
}
73
72
return $ attributes ;
74
73
}
@@ -77,15 +76,8 @@ static function ( $attributes ) {
77
76
// Avoid deprecation warning due to related change in WordPress 6.4.
78
77
remove_action ( 'wp_print_styles ' , 'print_emoji_styles ' );
79
78
80
- $ this -> add_mock_responses (
79
+ Audit_Assets_Mock_Assets:: add_mock_responses (
81
80
array (
82
- array (
83
- 'url ' => home_url ( '/ ' ),
84
- 'response ' => array (
85
- 'code ' => 200 ,
86
- 'body ' => $ this ->get_mocked_html (),
87
- ),
88
- ),
89
81
array (
90
82
'url ' => 'https://example1.com ' ,
91
83
'response ' => array (
@@ -102,7 +94,7 @@ static function ( $attributes ) {
102
94
),
103
95
)
104
96
);
105
- $ this -> mock_request ();
97
+ Audit_Assets_Mock_Assets:: mock_requests ();
106
98
$ result = perflab_aea_audit_blocking_assets ();
107
99
$ this ->assertIsArray ( $ result ['assets ' ] );
108
100
$ assets = $ result ['assets ' ];
@@ -148,17 +140,27 @@ static function ( $item ) {
148
140
}
149
141
);
150
142
$ this ->assertEmpty ( $ noscript_script );
143
+
144
+ $ module_script = array_filter (
145
+ $ assets ['scripts ' ],
146
+ static function ( $ item ) {
147
+ return 'https://module1.com ' === $ item ['src ' ];
148
+ }
149
+ );
150
+ $ this ->assertEmpty ( $ module_script );
151
151
}
152
152
153
153
/**
154
- * Tests perflab_aea_audit_enqueued_styles() with no transient.
155
- * Enqueued styles ( not belonging to core /wp-includes/ ) will be saved in transient.
154
+ * Tests perflab_aea_audit_enqueued_styles() with blocking styles.
155
+ *
156
+ * @covers ::perflab_aea_audit_blocking_assets
156
157
*/
157
158
public function test_perflab_aea_audit_enqueued_styles (): void {
158
159
/**
159
160
* Prepare scenario for test.
160
161
*/
161
162
$ this ->current_user_can_view_site_health_checks_cap ();
163
+ Audit_Assets_Mock_Assets::clear_mocked ();
162
164
163
165
wp_enqueue_style ( 'style1 ' , 'https://example1.com ' , array (), null );
164
166
wp_enqueue_style ( 'style2 ' , '/wp-includes/example2.css ' , array (), null );
@@ -183,15 +185,8 @@ static function ( $tag, $handle ) {
183
185
// Avoid deprecation warning due to related change in WordPress 6.4.
184
186
remove_action ( 'wp_print_styles ' , 'print_emoji_styles ' );
185
187
186
- $ this -> add_mock_responses (
188
+ Audit_Assets_Mock_Assets:: add_mock_responses (
187
189
array (
188
- array (
189
- 'url ' => home_url ( '/ ' ),
190
- 'response ' => array (
191
- 'code ' => 200 ,
192
- 'body ' => $ this ->get_mocked_html (),
193
- ),
194
- ),
195
190
array (
196
191
'url ' => 'https://example1.com ' ,
197
192
'response ' => array (
@@ -209,7 +204,7 @@ static function ( $tag, $handle ) {
209
204
)
210
205
);
211
206
212
- $ this -> mock_request ();
207
+ Audit_Assets_Mock_Assets:: mock_requests ();
213
208
$ result = perflab_aea_audit_blocking_assets ();
214
209
$ this ->assertIsArray ( $ result ['assets ' ] );
215
210
$ assets = $ result ['assets ' ];
@@ -302,8 +297,9 @@ public function test_perflab_aea_invalidate_cache_transients(): void {
302
297
*/
303
298
public function test_perflab_aea_audit_blocking_assets_home_request_failure ( array $ mocked_response ): void {
304
299
$ this ->current_user_can_view_site_health_checks_cap ();
305
- $ this ->add_mock_responses ( array ( $ mocked_response ) );
306
- $ this ->mock_request ();
300
+ Audit_Assets_Mock_Assets::clear_mocked ();
301
+ Audit_Assets_Mock_Assets::add_mock_responses ( array ( $ mocked_response ) );
302
+ Audit_Assets_Mock_Assets::mock_requests ( array ( $ mocked_response ) );
307
303
308
304
// Avoid deprecation warning due to related change in WordPress 6.4.
309
305
remove_action ( 'wp_print_styles ' , 'print_emoji_styles ' );
@@ -359,57 +355,20 @@ public function data_perflab_aea_audit_blocking_assets_home_request_failure(): a
359
355
}
360
356
361
357
/**
362
- * Adds view_site_health_checks capability to current user.
363
- */
364
- public function current_user_can_view_site_health_checks_cap (): void {
365
- $ current_user = wp_get_current_user ();
366
- $ current_user ->add_cap ( 'view_site_health_checks ' );
367
- }
368
-
369
- public function get_mocked_html (): string {
370
- $ mock_html = '<!DOCTYPE html><head> ' ;
371
- $ mock_html .= get_echo ( 'wp_head ' );
372
- $ mock_html .= '</head><body> ' ;
373
- $ mock_html .= get_echo ( 'wp_footer ' );
374
- $ mock_html .= '</body></html> ' ;
375
- return $ mock_html ;
376
- }
377
-
378
- /**
379
- * Adds a mocked response for a specific URL.
358
+ * Tests that hooks are set correctly.
380
359
*
381
- * @param array<string|mixed> $responses An array containing the URLs and the mocked responses.
360
+ * @covers ::perflab_aea_add_enqueued_assets_test
382
361
*/
383
- public function add_mock_responses ( array $ responses ): void {
384
- foreach ( $ responses as $ response ) {
385
- $ this ->mocked_responses [ $ response ['url ' ] ] = $ response ['response ' ];
386
- }
362
+ public function test_hooks_are_set (): void {
363
+ $ this ->assertSame ( 10 , has_filter ( 'site_status_tests ' , 'perflab_aea_add_enqueued_assets_test ' ) );
364
+ $ this ->assertSame ( 10 , has_action ( 'wp_ajax_health-check-enqueued-blocking-assets-test ' , 'perflab_aea_enqueued_ajax_blocking_assets_test ' ) );
387
365
}
388
366
389
367
/**
390
- * Mocks HTTP requests for tests .
368
+ * Adds view_site_health_checks capability to current user .
391
369
*/
392
- public function mock_request (): void {
393
- remove_all_filters ( 'pre_http_request ' );
394
- add_filter (
395
- 'pre_http_request ' ,
396
- function ( $ preempt , $ parsed_args , $ url ) {
397
- $ url = remove_query_arg ( 'cache_bust ' , $ url );
398
-
399
- if ( isset ( $ this ->mocked_responses [ $ url ] ) ) {
400
- if ( is_wp_error ( $ this ->mocked_responses [ $ url ] ) ) {
401
- return $ this ->mocked_responses [ $ url ];
402
- }
403
-
404
- return array (
405
- 'response ' => $ this ->mocked_responses [ $ url ],
406
- 'body ' => $ this ->mocked_responses [ $ url ]['body ' ] ?? '' ,
407
- );
408
- }
409
- return $ preempt ;
410
- },
411
- 10 ,
412
- 3
413
- );
370
+ public function current_user_can_view_site_health_checks_cap (): void {
371
+ $ current_user = wp_get_current_user ();
372
+ $ current_user ->add_cap ( 'view_site_health_checks ' );
414
373
}
415
374
}
0 commit comments