diff --git a/inc/Engine/HealthCheck/PageCache.php b/inc/Engine/HealthCheck/PageCache.php new file mode 100644 index 0000000000..93d68a8b26 --- /dev/null +++ b/inc/Engine/HealthCheck/PageCache.php @@ -0,0 +1,37 @@ + [ 'page_cache_useragent', 10, 2 ], + ]; + } + + /** + * Pass plugin header to skip test "mandatory cookie". + * + * @param string $user_agent WordPress user agent string. + * @param string $url The request URL. + * @return string + */ + public function page_cache_useragent( $user_agent, $url = null ) { + $uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ); + if ( + strpos( $uri, 'wp-site-health' ) !== false && + strpos( $uri, 'page-cache' ) !== false + ) { + $user_agent = 'WP Rocket'; + } + + return $user_agent; + } +} diff --git a/inc/Engine/HealthCheck/ServiceProvider.php b/inc/Engine/HealthCheck/ServiceProvider.php index 276daf64a4..aede286b80 100644 --- a/inc/Engine/HealthCheck/ServiceProvider.php +++ b/inc/Engine/HealthCheck/ServiceProvider.php @@ -14,6 +14,7 @@ class ServiceProvider extends AbstractServiceProvider { */ protected $provides = [ 'health_check', + 'health_check_page_cache', 'action_scheduler_check', ]; @@ -37,6 +38,8 @@ public function register(): void { $this->getContainer()->addShared( 'health_check', HealthCheck::class ) ->addArgument( $this->getContainer()->get( 'options' ) ) ->addTag( 'admin_subscriber' ); + $this->getContainer()->addShared( 'health_check_page_cache', PageCache::class ) + ->addTag( 'common_subscriber' ); $this->getContainer()->addShared( 'action_scheduler_check', ActionSchedulerCheck::class ) ->addTag( 'common_subscriber' ); } diff --git a/inc/Plugin.php b/inc/Plugin.php index 104258d8ce..f3de64cf8e 100644 --- a/inc/Plugin.php +++ b/inc/Plugin.php @@ -394,6 +394,7 @@ private function init_common_subscribers() { 'atf_cron_subscriber', 'saas_admin_subscriber', 'warmup_subscriber', + 'health_check_page_cache', ]; $host_type = HostResolver::get_host_service(); diff --git a/tests/Fixtures/inc/Engine/HealthCheck/PageCache/userAgent.php b/tests/Fixtures/inc/Engine/HealthCheck/PageCache/userAgent.php new file mode 100644 index 0000000000..272c2185fd --- /dev/null +++ b/tests/Fixtures/inc/Engine/HealthCheck/PageCache/userAgent.php @@ -0,0 +1,19 @@ + [ + 'request_uri' => null, + 'user_agent_default' => 'WordPress', + 'user_agent_expected' => 'WordPress', + ], + 'default' => [ + 'request_uri' => '/wp-json/wp-site-health/v1/tests/https-status?_locale=user', + 'user_agent_default' => 'WordPress', + 'user_agent_expected' => 'WordPress', + ], + 'plugin' => [ + 'request_uri' => '/wp-json/wp-site-health/v1/tests/page-cache?_locale=user', + 'user_agent_default' => 'WP Rocket', + 'user_agent_expected' => 'WP Rocket', + ], +]; diff --git a/tests/Unit/inc/Engine/HealthCheck/PageCache/userAgent.php b/tests/Unit/inc/Engine/HealthCheck/PageCache/userAgent.php new file mode 100644 index 0000000000..d53717e3d3 --- /dev/null +++ b/tests/Unit/inc/Engine/HealthCheck/PageCache/userAgent.php @@ -0,0 +1,47 @@ +health = new PageCache(); + + Functions\when( 'sanitize_text_field' )->alias( + function ( $value ) { + return $value; + } + ); + + Functions\when( 'wp_unslash' )->alias( + function ( $value ) { + return stripslashes( $value ); + } + ); + } + + /** + * @dataProvider configTestData + */ + public function testShouldReturnExpected( $request_uri, $user_agent_default, $user_agent_expected ) { + $_SERVER['REQUEST_URI'] = $request_uri; + + $user_agent = $this->health->page_cache_useragent( $user_agent_default ); + $this->assertSame( $user_agent_expected, $user_agent ); + } +}