Skip to content

Commit eeaea90

Browse files
committed
Add missing auth checks and tests for Ajax action
1 parent 7dc29cd commit eeaea90

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ function perflab_aea_enqueued_blocking_assets_test(): array {
200200
* @since n.e.x.t
201201
*/
202202
function perflab_aea_enqueued_ajax_blocking_assets_test(): void {
203+
check_ajax_referer( 'health-check-site-status' );
204+
205+
if ( ! current_user_can( 'view_site_health_checks' ) ) {
206+
wp_send_json_error();
207+
}
208+
203209
wp_send_json_success( perflab_aea_enqueued_blocking_assets_test() );
204210
}
205211

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

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66
* @group audit-enqueued-assets
77
*/
88

9-
class Test_Audit_Enqueued_Assets_Helper extends WP_UnitTestCase {
9+
class Test_Audit_Enqueued_Assets_Helper extends WP_Ajax_UnitTestCase {
1010

1111
const WARNING_SCRIPTS_THRESHOLD = 31;
1212

1313
const WARNING_STYLES_THRESHOLD = 11;
1414

15+
/**
16+
* Set up.
17+
*/
18+
public function set_up(): void {
19+
parent::set_up();
20+
remove_action( 'wp_print_styles', 'print_emoji_styles' );
21+
}
22+
1523
/**
1624
* Tear down.
1725
*/
@@ -269,6 +277,64 @@ public function test_perflab_aea_enqueued_blocking_assets_test_but_one_style_is_
269277
$this->assertStringContainsString( 'Not found', $test['description'] );
270278
}
271279

280+
/**
281+
* Tests perflab_aea_enqueued_ajax_blocking_assets_test
282+
*
283+
* @covers ::perflab_aea_enqueued_ajax_blocking_assets_test
284+
*/
285+
public function test_perflab_aea_enqueued_ajax_blocking_assets_test_unauthenticated_without_nonce(): void {
286+
$this->add_filter_to_mock_front_page_loopback_request();
287+
$this->expectException( WPAjaxDieStopException::class );
288+
$this->_handleAjax( 'health-check-enqueued-blocking-assets-test' );
289+
$response = json_decode( $this->_last_response, true );
290+
$this->assertArrayHasKey( 'success', $response );
291+
$this->assertFalse( $response['success'] );
292+
}
293+
294+
/**
295+
* Tests perflab_aea_enqueued_ajax_blocking_assets_test
296+
*
297+
* @covers ::perflab_aea_enqueued_ajax_blocking_assets_test
298+
*/
299+
public function test_perflab_aea_enqueued_ajax_blocking_assets_test_unauthenticated_with_nonce(): void {
300+
$this->add_filter_to_mock_front_page_loopback_request();
301+
$_GET['_wpnonce'] = wp_create_nonce( 'health-check-site-status' );
302+
$this->expectException( WPAjaxDieContinueException::class );
303+
$this->_handleAjax( 'health-check-enqueued-blocking-assets-test' );
304+
$response = json_decode( $this->_last_response, true );
305+
$this->assertFalse( $response['success'] );
306+
}
307+
308+
/**
309+
* Tests perflab_aea_enqueued_ajax_blocking_assets_test
310+
*
311+
* @covers ::perflab_aea_enqueued_ajax_blocking_assets_test
312+
*/
313+
public function test_perflab_aea_enqueued_ajax_blocking_assets_test_unauthorized(): void {
314+
$this->add_filter_to_mock_front_page_loopback_request();
315+
wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
316+
$_GET['_wpnonce'] = wp_create_nonce( 'health-check-site-status' );
317+
$this->expectException( WPAjaxDieContinueException::class );
318+
$this->_handleAjax( 'health-check-enqueued-blocking-assets-test' );
319+
$response = json_decode( $this->_last_response, true );
320+
$this->assertFalse( $response['success'] );
321+
}
322+
323+
/**
324+
* Tests perflab_aea_enqueued_ajax_blocking_assets_test
325+
*
326+
* @covers ::perflab_aea_enqueued_ajax_blocking_assets_test
327+
*/
328+
public function test_perflab_aea_enqueued_ajax_blocking_assets_test_authorized(): void {
329+
$this->add_filter_to_mock_front_page_loopback_request();
330+
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
331+
$_GET['_wpnonce'] = wp_create_nonce( 'health-check-site-status' );
332+
$this->expectException( WPAjaxDieContinueException::class );
333+
$this->_handleAjax( 'health-check-enqueued-blocking-assets-test' );
334+
$response = json_decode( $this->_last_response, true );
335+
$this->assertTrue( $response['success'] );
336+
}
337+
272338
/**
273339
* Test perflab_aea_enqueued_blocking_scripts() with scripts less than WARNING_SCRIPTS_threshold.
274340
*
@@ -514,4 +580,41 @@ public function mock_data_perflab_aea_enqueued_css_assets_test_callback( int $nu
514580
}
515581
return Site_Health_Mock_Responses::return_aea_enqueued_css_assets_test_callback_more_than_threshold( $number_of_assets );
516582
}
583+
584+
/**
585+
* Add filter to intercept loopback requests.
586+
*/
587+
public function add_filter_to_mock_front_page_loopback_request(): void {
588+
add_filter(
589+
'pre_http_request',
590+
static function ( $r, $args, $url ) {
591+
if ( home_url( '/' ) === remove_query_arg( 'cache_bust', $url ) ) {
592+
$r = array(
593+
'response' => array(
594+
'status' => 200,
595+
'message' => 'OK',
596+
),
597+
'body' => '<html></html>',
598+
'headers' => array(
599+
'Content-Type' => 'text/html',
600+
),
601+
);
602+
} else {
603+
$r = array(
604+
'response' => array(
605+
'status' => 503,
606+
'message' => "Oh no you didn't",
607+
),
608+
'body' => 'NO WAY',
609+
'headers' => array(
610+
'Content-Type' => 'text/plain',
611+
),
612+
);
613+
}
614+
return $r;
615+
},
616+
10,
617+
3
618+
);
619+
}
517620
}

0 commit comments

Comments
 (0)