@@ -120,12 +120,33 @@ public function data_provider_test_rest_api_availability(): array {
120120 'code ' => 403 ,
121121 'message ' => 'Forbidden ' ,
122122 ),
123+ 'headers ' => array (
124+ 'content-type ' => 'text/html ' ,
125+ ),
123126 'body ' => "<html> \n<head><title>403 Forbidden</title></head> \n<body> \n<center><h1>403 Forbidden</h1></center> \n<hr><center>nginx</center> \n</body> \n</html> " ,
124127 ),
125128 'expected_option ' => '1 ' ,
126129 'expected_status ' => 'recommended ' ,
127130 'expected_unavailable ' => true ,
128131 ),
132+ 'other_forbidden ' => array (
133+ 'mocked_response ' => array (
134+ 'response ' => array (
135+ 'code ' => 403 ,
136+ 'message ' => 'Forbidden ' ,
137+ ),
138+ 'headers ' => array (
139+ 'content-type ' => array (
140+ 'text/html; charset=utf-8 ' ,
141+ 'application/xhtml+xml ' ,
142+ ),
143+ ),
144+ 'body ' => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don \'t have permission to access this resource.</p></body></html> ' ,
145+ ),
146+ 'expected_option ' => '1 ' ,
147+ 'expected_status ' => 'recommended ' ,
148+ 'expected_unavailable ' => true ,
149+ ),
129150 'error ' => array (
130151 'mocked_response ' => new WP_Error ( 'bad ' , 'Something terrible has happened ' ),
131152 'expected_option ' => '1 ' ,
@@ -142,6 +163,8 @@ public function data_provider_test_rest_api_availability(): array {
142163 * @covers ::od_compose_site_health_result
143164 * @covers ::od_get_rest_api_health_check_response
144165 * @covers ::od_is_rest_api_unavailable
166+ * @covers ::od_render_rest_api_health_check_admin_notice_in_plugin_row
167+ * @covers ::od_maybe_render_rest_api_health_check_admin_notice
145168 *
146169 * @dataProvider data_provider_test_rest_api_availability
147170 *
@@ -151,6 +174,24 @@ public function test_rest_api_availability( $mocked_response, string $expected_o
151174 $ this ->filter_rest_api_response ( $ mocked_response );
152175
153176 $ result = od_test_rest_api_availability ();
177+ $ notice = get_echo ( 'od_render_rest_api_health_check_admin_notice_in_plugin_row ' , array ( 'optimization-detective/load.php ' ) );
178+ if ( $ expected_unavailable ) {
179+ $ this ->assertStringContainsString ( '<code> ' , $ notice );
180+ if ( is_array ( $ mocked_response ) ) {
181+ if ( isset ( $ mocked_response ['headers ' ]['content-type ' ] ) && str_contains ( join ( '' , (array ) $ mocked_response ['headers ' ]['content-type ' ] ), 'html ' ) ) {
182+ $ this ->assertStringContainsString ( '</iframe> ' , $ notice );
183+ $ this ->assertStringNotContainsString ( '</pre> ' , $ notice );
184+ } else {
185+ $ this ->assertStringContainsString ( '</pre> ' , $ notice );
186+ $ this ->assertStringNotContainsString ( '</iframe> ' , $ notice );
187+ }
188+ } else {
189+ $ this ->assertStringNotContainsString ( '</iframe> ' , $ notice );
190+ $ this ->assertStringNotContainsString ( '</pre> ' , $ notice );
191+ }
192+ } else {
193+ $ this ->assertSame ( '' , $ notice );
194+ }
154195 $ this ->assertArrayHasKey ( 'label ' , $ result );
155196 $ this ->assertArrayHasKey ( 'status ' , $ result );
156197 $ this ->assertArrayHasKey ( 'badge ' , $ result );
@@ -392,7 +433,7 @@ static function ( $pre, array $args, string $url ) use ( $mocked_response, $obse
392433 }
393434
394435 /**
395- * Build a mock response.
436+ * Build a mock JSON response.
396437 *
397438 * @param int $status_code HTTP status code.
398439 * @param string $message HTTP status message.
@@ -406,6 +447,9 @@ protected function build_mock_response( int $status_code, string $message, array
406447 'message ' => $ message ,
407448 ),
408449 'body ' => wp_json_encode ( $ body ),
450+ 'headers ' => array (
451+ 'content-type ' => 'application/json ' ,
452+ ),
409453 );
410454 }
411455}
0 commit comments