Skip to content

Commit 747284f

Browse files
committed
Improve test code coverage
1 parent b28e5ef commit 747284f

File tree

4 files changed

+79
-154
lines changed

4 files changed

+79
-154
lines changed

plugins/performance-lab/includes/site-health/audit-enqueued-assets/helper.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function perflab_aea_audit_blocking_assets(): array {
134134
* description: string,
135135
* actions: string,
136136
* test: string
137-
* }|array{omitted: true} Result.
137+
* } Result.
138138
*/
139139
function perflab_aea_enqueued_blocking_assets_test(): array {
140140
$result = array(
@@ -160,13 +160,6 @@ function perflab_aea_enqueued_blocking_assets_test(): array {
160160
$scripts_result = perflab_aea_enqueued_blocking_scripts( $audit_result['assets'] );
161161
$styles_result = perflab_aea_enqueued_blocking_styles( $audit_result['assets'] );
162162

163-
if ( null === $scripts_result && null === $styles_result ) {
164-
// The return value is validated in JavaScript at:
165-
// <https://github.com/WordPress/wordpress-develop/blob/d1e0a6241dcc34f4a5ed464a741116461a88d43b/src/js/_enqueues/admin/site-health.js#L65-L114>
166-
// If the value lacks the required keys of test, label, and description then it is omitted.
167-
return array( 'omitted' => true );
168-
}
169-
170163
$result['description'] .= perflab_aea_generate_blocking_assets_table( $audit_result['assets'] );
171164

172165
if ( isset( $scripts_result ) ) {

plugins/performance-lab/tests/data/class-audit-assets-mock-assets.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ public static function mock_requests( array $additional_responses = array() ): v
138138
remove_all_filters( 'pre_http_request' );
139139

140140
self::$mocked_responses[ home_url( '/' ) ] = array(
141-
'code' => 200,
142-
'body' => self::get_mocked_html(),
141+
'code' => 200,
142+
'body' => self::get_mocked_html(),
143+
'message' => 'OK',
143144
);
144145
self::add_mock_responses( $additional_responses );
145146

@@ -153,10 +154,7 @@ static function ( $preempt, $parsed_args, $url ) {
153154
}
154155

155156
return array(
156-
'response' => array(
157-
'code' => self::$mocked_responses[ $url ]['code'] ?? 200,
158-
'message' => self::$mocked_responses[ $url ]['message'] ?? 'OK',
159-
),
157+
'response' => self::$mocked_responses[ $url ],
160158
'body' => self::$mocked_responses[ $url ]['body'] ?? '',
161159
);
162160
}

plugins/performance-lab/tests/includes/site-health/audit-enqueued-assets/test-audit-enqueued-assets-helper.php

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,14 @@ public function test_perflab_aea_enqueued_blocking_assets_test_good_js_but_bad_c
140140
* @covers ::perflab_aea_blocking_assets_retrieval_failure
141141
*/
142142
public function test_perflab_aea_enqueued_blocking_assets_test_but_response_is_wp_error(): void {
143-
set_transient( 'aea_blocking_assets_response', new WP_Error( 'something_bad', 'Oh no!!!' ) );
143+
Audit_Assets_Mock_Assets::mock_requests(
144+
array(
145+
array(
146+
'url' => home_url( '/' ),
147+
'response' => new WP_Error( 'something_bad', 'Oh no!!!' ),
148+
),
149+
)
150+
);
144151

145152
$test = perflab_aea_enqueued_blocking_assets_test();
146153
$this->assertSameSets(
@@ -456,45 +463,41 @@ public function test_perflab_aea_get_total_size_bytes_enqueued_styles(): void {
456463
* @covers ::perflab_aea_get_asset_size
457464
*/
458465
public function test_perflab_aea_get_asset_size(): void {
459-
$this->mock_request(
460-
'https://example.com/script.js',
461-
new WP_Error(
462-
'http_error',
463-
'Mocked HTTP error for testing.'
464-
)
465-
);
466-
$this->assertWPError( perflab_aea_get_asset_size( 'https://example.com/script.js' ) );
467-
468-
$this->mock_request(
469-
'https://example.com/script.js',
466+
Audit_Assets_Mock_Assets::clear_mocked();
467+
Audit_Assets_Mock_Assets::mock_requests(
470468
array(
471-
'response' => array(
472-
'code' => 404,
473-
'message' => 'Not Found',
469+
array(
470+
'url' => 'https://example.com/script1.js',
471+
'response' => new WP_Error( 'http_error', 'Mocked HTTP error for testing.' ),
472+
),
473+
array(
474+
'url' => 'https://example.com/script2.js',
475+
'response' => array(
476+
'code' => 404,
477+
'message' => 'Not Found',
478+
),
479+
),
480+
array(
481+
'url' => 'https://example.com/script3.js',
482+
'response' => array(
483+
'code' => 200,
484+
'body' => '',
485+
),
486+
),
487+
array(
488+
'url' => 'https://example.com/script4.js',
489+
'response' => array(
490+
'code' => 200,
491+
'body' => str_repeat( 'A', 1000 ),
492+
),
474493
),
475-
'body' => 'Not Found',
476-
)
477-
);
478-
$this->assertWPError( perflab_aea_get_asset_size( 'https://example.com/script.js' ) );
479-
480-
$this->mock_request(
481-
'https://example.com/script.js',
482-
array(
483-
'response' => array( 'code' => 200 ),
484-
'body' => '',
485494
)
486495
);
487-
$this->assertEquals( 0, perflab_aea_get_asset_size( 'https://example.com/script.js' ) );
488496

489-
$this->mock_request(
490-
'https://example.com/script.js',
491-
array(
492-
'response' => array( 'code' => 200 ),
493-
'body' => str_repeat( 'a', 1000 ),
494-
)
495-
);
496-
$size = perflab_aea_get_asset_size( 'https://example.com/script.js' );
497-
$this->assertEquals( 1000, $size );
497+
$this->assertWPError( perflab_aea_get_asset_size( 'https://example.com/script1.js' ) );
498+
$this->assertWPError( perflab_aea_get_asset_size( 'https://example.com/script2.js' ) );
499+
$this->assertEquals( 0, perflab_aea_get_asset_size( 'https://example.com/script3.js' ) );
500+
$this->assertEquals( 1000, perflab_aea_get_asset_size( 'https://example.com/script4.js' ) );
498501
}
499502

500503
/**
@@ -547,34 +550,6 @@ public function test_perflab_aea_generate_blocking_assets_table_css(): void {
547550
$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'TABLE' ) ) );
548551
}
549552

550-
/**
551-
* Mocks HTTP requests for tests.
552-
*
553-
* @param string $resource_url The URL to mock.
554-
* @param array<string|mixed>|WP_Error $response The response to return for the mocked request.
555-
*/
556-
public function mock_request( string $resource_url, $response ): void {
557-
remove_all_filters( 'pre_http_request' );
558-
add_filter(
559-
'pre_http_request',
560-
static function ( $preempt, $parsed_args, $url ) use ( $resource_url, $response ) {
561-
if ( $url === $resource_url ) {
562-
if ( is_wp_error( $response ) ) {
563-
return $response;
564-
}
565-
566-
return array(
567-
'response' => $response['response'],
568-
'body' => $response['body'] ?? '',
569-
);
570-
}
571-
return $preempt;
572-
},
573-
10,
574-
3
575-
);
576-
}
577-
578553
/**
579554
* @param int $number_of_assets Number of assets mocked.
580555
* @return array<string, mixed>

plugins/performance-lab/tests/includes/site-health/audit-enqueued-assets/test-audit-enqueued-assets.php

Lines changed: 36 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88

99
class Test_Audit_Enqueued_Assets extends WP_UnitTestCase {
1010

11-
/**
12-
* Mocked responses for HTTP requests.
13-
*
14-
* @var array<string, mixed>
15-
*/
16-
private $mocked_responses = array();
17-
1811
/**
1912
* Tests perflab_aea_audit_enqueued_scripts() when blocking scripts are present.
2013
*
@@ -43,14 +36,16 @@ public function test_perflab_aea_audit_enqueued_scripts_blocking_scripts_are_pre
4336
}
4437

4538
/**
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
4842
*/
4943
public function test_perflab_aea_audit_enqueued_scripts(): void {
5044
/**
5145
* Prepare scenario for test.
5246
*/
5347
$this->current_user_can_view_site_health_checks_cap();
48+
Audit_Assets_Mock_Assets::clear_mocked();
5449

5550
wp_enqueue_script( 'script1', 'https://example1.com', array(), null );
5651
wp_enqueue_script( 'script2', '/wp-includes/example2.js', array(), null );
@@ -59,6 +54,8 @@ public function test_perflab_aea_audit_enqueued_scripts(): void {
5954
wp_enqueue_script( 'script-async', 'https://async-script.com', array(), null, true );
6055
wp_enqueue_script( 'script-defer', 'https://defer-script.com', array(), null, true );
6156
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 );
6259

6360
add_filter(
6461
'wp_script_attributes',
@@ -69,6 +66,8 @@ static function ( $attributes ) {
6966
$attributes['defer'] = true;
7067
} elseif ( 'type-noscript-js' === $attributes['id'] ) {
7168
$attributes['type'] = 'noscript';
69+
} elseif ( 'no-src-js' === $attributes['id'] ) {
70+
unset( $attributes['src'] );
7271
}
7372
return $attributes;
7473
}
@@ -77,15 +76,8 @@ static function ( $attributes ) {
7776
// Avoid deprecation warning due to related change in WordPress 6.4.
7877
remove_action( 'wp_print_styles', 'print_emoji_styles' );
7978

80-
$this->add_mock_responses(
79+
Audit_Assets_Mock_Assets::add_mock_responses(
8180
array(
82-
array(
83-
'url' => home_url( '/' ),
84-
'response' => array(
85-
'code' => 200,
86-
'body' => $this->get_mocked_html(),
87-
),
88-
),
8981
array(
9082
'url' => 'https://example1.com',
9183
'response' => array(
@@ -102,7 +94,7 @@ static function ( $attributes ) {
10294
),
10395
)
10496
);
105-
$this->mock_request();
97+
Audit_Assets_Mock_Assets::mock_requests();
10698
$result = perflab_aea_audit_blocking_assets();
10799
$this->assertIsArray( $result['assets'] );
108100
$assets = $result['assets'];
@@ -148,17 +140,27 @@ static function ( $item ) {
148140
}
149141
);
150142
$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 );
151151
}
152152

153153
/**
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
156157
*/
157158
public function test_perflab_aea_audit_enqueued_styles(): void {
158159
/**
159160
* Prepare scenario for test.
160161
*/
161162
$this->current_user_can_view_site_health_checks_cap();
163+
Audit_Assets_Mock_Assets::clear_mocked();
162164

163165
wp_enqueue_style( 'style1', 'https://example1.com', array(), null );
164166
wp_enqueue_style( 'style2', '/wp-includes/example2.css', array(), null );
@@ -183,15 +185,8 @@ static function ( $tag, $handle ) {
183185
// Avoid deprecation warning due to related change in WordPress 6.4.
184186
remove_action( 'wp_print_styles', 'print_emoji_styles' );
185187

186-
$this->add_mock_responses(
188+
Audit_Assets_Mock_Assets::add_mock_responses(
187189
array(
188-
array(
189-
'url' => home_url( '/' ),
190-
'response' => array(
191-
'code' => 200,
192-
'body' => $this->get_mocked_html(),
193-
),
194-
),
195190
array(
196191
'url' => 'https://example1.com',
197192
'response' => array(
@@ -209,7 +204,7 @@ static function ( $tag, $handle ) {
209204
)
210205
);
211206

212-
$this->mock_request();
207+
Audit_Assets_Mock_Assets::mock_requests();
213208
$result = perflab_aea_audit_blocking_assets();
214209
$this->assertIsArray( $result['assets'] );
215210
$assets = $result['assets'];
@@ -302,8 +297,9 @@ public function test_perflab_aea_invalidate_cache_transients(): void {
302297
*/
303298
public function test_perflab_aea_audit_blocking_assets_home_request_failure( array $mocked_response ): void {
304299
$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 ) );
307303

308304
// Avoid deprecation warning due to related change in WordPress 6.4.
309305
remove_action( 'wp_print_styles', 'print_emoji_styles' );
@@ -359,57 +355,20 @@ public function data_perflab_aea_audit_blocking_assets_home_request_failure(): a
359355
}
360356

361357
/**
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.
380359
*
381-
* @param array<string|mixed> $responses An array containing the URLs and the mocked responses.
360+
* @covers ::perflab_aea_add_enqueued_assets_test
382361
*/
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' ) );
387365
}
388366

389367
/**
390-
* Mocks HTTP requests for tests.
368+
* Adds view_site_health_checks capability to current user.
391369
*/
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' );
414373
}
415374
}

0 commit comments

Comments
 (0)