|
6 | 6 | * @group audit-enqueued-assets
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -class Test_Audit_Enqueued_Assets_Helper extends WP_UnitTestCase { |
| 9 | +class Test_Audit_Enqueued_Assets_Helper extends WP_Ajax_UnitTestCase { |
10 | 10 |
|
11 | 11 | const WARNING_SCRIPTS_THRESHOLD = 31;
|
12 | 12 |
|
13 | 13 | const WARNING_STYLES_THRESHOLD = 11;
|
14 | 14 |
|
| 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 | + |
15 | 23 | /**
|
16 | 24 | * Tear down.
|
17 | 25 | */
|
@@ -269,6 +277,64 @@ public function test_perflab_aea_enqueued_blocking_assets_test_but_one_style_is_
|
269 | 277 | $this->assertStringContainsString( 'Not found', $test['description'] );
|
270 | 278 | }
|
271 | 279 |
|
| 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 | + |
272 | 338 | /**
|
273 | 339 | * Test perflab_aea_enqueued_blocking_scripts() with scripts less than WARNING_SCRIPTS_threshold.
|
274 | 340 | *
|
@@ -514,4 +580,41 @@ public function mock_data_perflab_aea_enqueued_css_assets_test_callback( int $nu
|
514 | 580 | }
|
515 | 581 | return Site_Health_Mock_Responses::return_aea_enqueued_css_assets_test_callback_more_than_threshold( $number_of_assets );
|
516 | 582 | }
|
| 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 | + } |
517 | 620 | }
|
0 commit comments