|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for cache-control headers for bfcache compatibility site health check. |
| 4 | + * |
| 5 | + * @package performance-lab |
| 6 | + * @group bfcache-compatibility-headers |
| 7 | + */ |
| 8 | + |
| 9 | +class Test_BFCache_Compatibility_Headers extends WP_UnitTestCase { |
| 10 | + |
| 11 | + /** |
| 12 | + * Holds mocked response headers for different test scenarios. |
| 13 | + * |
| 14 | + * @var array<string, array<string, mixed>> |
| 15 | + */ |
| 16 | + protected $mocked_responses = array(); |
| 17 | + |
| 18 | + /** |
| 19 | + * Setup each test. |
| 20 | + */ |
| 21 | + public function setUp(): void { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + // Clear any filters or mocks. |
| 25 | + remove_all_filters( 'pre_http_request' ); |
| 26 | + |
| 27 | + // Add the filter to mock HTTP requests. |
| 28 | + add_filter( 'pre_http_request', array( $this, 'mock_http_requests' ), 10, 3 ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Test that the bfcache compatibility test is added to the site health tests. |
| 33 | + * |
| 34 | + * @covers ::perflab_bfcache_compatibility_headers_add_test |
| 35 | + */ |
| 36 | + public function test_perflab_bfcache_compatibility_headers_add_test(): void { |
| 37 | + $tests = array( |
| 38 | + 'direct' => array(), |
| 39 | + ); |
| 40 | + |
| 41 | + $tests = perflab_bfcache_compatibility_headers_add_test( $tests ); |
| 42 | + $this->assertArrayHasKey( 'perflab_bfcache_compatibility_headers', $tests['direct'] ); |
| 43 | + $this->assertEquals( 'Cache-Control headers may prevent fast back/forward navigation', $tests['direct']['perflab_bfcache_compatibility_headers']['label'] ); |
| 44 | + $this->assertEquals( 'perflab_bfcache_compatibility_headers_check', $tests['direct']['perflab_bfcache_compatibility_headers']['test'] ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test that the bfcache compatibility test is attached to the site status tests. |
| 49 | + * |
| 50 | + * @covers ::perflab_bfcache_compatibility_headers_add_test |
| 51 | + */ |
| 52 | + public function test_perflab_bfcache_compatibility_headers_add_test_is_attached(): void { |
| 53 | + $this->assertNotFalse( has_filter( 'site_status_tests', 'perflab_bfcache_compatibility_headers_add_test' ) ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test that different Cache-Control headers return the correct bfcache compatibility result. |
| 58 | + * |
| 59 | + * @dataProvider data_test_bfcache_compatibility |
| 60 | + * @covers ::perflab_bfcache_compatibility_headers_check |
| 61 | + * |
| 62 | + * @param array<int, mixed>|WP_Error $response The response headers. |
| 63 | + * @param string $expected_status The expected status. |
| 64 | + * @param string $expected_message The expected message. |
| 65 | + */ |
| 66 | + public function test_perflab_bfcache_compatibility_headers_check( $response, string $expected_status, string $expected_message ): void { |
| 67 | + $this->mocked_responses = array( home_url( '/' ) => $response ); |
| 68 | + |
| 69 | + $result = perflab_bfcache_compatibility_headers_check(); |
| 70 | + |
| 71 | + $this->assertEquals( $expected_status, $result['status'] ); |
| 72 | + $this->assertStringContainsString( $expected_message, $result['description'] ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Data provider for bfcache compatibility tests. |
| 77 | + * |
| 78 | + * @return array<string, array<int, mixed>> Test data. |
| 79 | + */ |
| 80 | + public function data_test_bfcache_compatibility(): array { |
| 81 | + return array( |
| 82 | + 'headers_not_set' => array( |
| 83 | + $this->build_response( 200, array( 'cache-control' => '' ) ), |
| 84 | + 'good', |
| 85 | + 'If the <code>Cache-Control</code> page response header includes directives like', |
| 86 | + ), |
| 87 | + 'no_store' => array( |
| 88 | + $this->build_response( 200, array( 'cache-control' => 'no-store' ) ), |
| 89 | + 'recommended', |
| 90 | + '<p>The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the following directive: <code>no-store</code>', |
| 91 | + ), |
| 92 | + 'no_cache' => array( |
| 93 | + $this->build_response( 200, array( 'cache-control' => 'no-cache' ) ), |
| 94 | + 'recommended', |
| 95 | + '<p>The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the following directive: <code>no-cache</code>', |
| 96 | + ), |
| 97 | + 'max_age_0' => array( |
| 98 | + $this->build_response( 200, array( 'cache-control' => 'max-age=0' ) ), |
| 99 | + 'recommended', |
| 100 | + '<p>The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the following directive: <code>max-age=0</code>', |
| 101 | + ), |
| 102 | + 'max_age_0_no_store' => array( |
| 103 | + $this->build_response( 200, array( 'cache-control' => 'max-age=0, no-store' ) ), |
| 104 | + 'recommended', |
| 105 | + '<p>The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the following directives: <code>no-store</code>, <code>max-age=0</code>', |
| 106 | + ), |
| 107 | + 'error' => array( |
| 108 | + new WP_Error( 'http_request_failed', 'HTTP request failed' ), |
| 109 | + 'recommended', |
| 110 | + 'The unauthenticated request to check the <code>Cache-Control</code> response header for the home page resulted in an error with code', |
| 111 | + ), |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Mock HTTP requests for assets to simulate different responses. |
| 117 | + * |
| 118 | + * @param bool $response A preemptive return value of an HTTP request. Default false. |
| 119 | + * @param array<string, mixed> $args Request arguments. |
| 120 | + * @param string $url The request URL. |
| 121 | + * @return array<string, mixed>|WP_Error Mocked response. |
| 122 | + */ |
| 123 | + public function mock_http_requests( bool $response, array $args, string $url ) { |
| 124 | + if ( isset( $this->mocked_responses[ $url ] ) ) { |
| 125 | + return $this->mocked_responses[ $url ]; |
| 126 | + } |
| 127 | + |
| 128 | + // If no specific mock set, default to a generic success with no caching. |
| 129 | + return $this->build_response( 200 ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Helper method to build a mock HTTP response. |
| 134 | + * |
| 135 | + * @param int $status_code HTTP status code. |
| 136 | + * @param array<string, string|int> $headers HTTP headers. |
| 137 | + * @return array{response: array{code: int, message: string}, headers: WpOrg\Requests\Utility\CaseInsensitiveDictionary} |
| 138 | + */ |
| 139 | + protected function build_response( int $status_code = 200, array $headers = array() ): array { |
| 140 | + return array( |
| 141 | + 'response' => array( |
| 142 | + 'code' => $status_code, |
| 143 | + 'message' => '', |
| 144 | + ), |
| 145 | + 'headers' => new WpOrg\Requests\Utility\CaseInsensitiveDictionary( $headers ), |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
0 commit comments