diff --git a/plugins/optimization-detective/site-health.php b/plugins/optimization-detective/site-health.php index f70ed72553..26a4405b59 100644 --- a/plugins/optimization-detective/site-health.php +++ b/plugins/optimization-detective/site-health.php @@ -127,6 +127,10 @@ function od_compose_site_health_result( $response ): array { $message = wp_remote_retrieve_response_message( $response ); $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); + $header = wp_remote_retrieve_header( $response, 'content-type' ); + if ( is_array( $header ) ) { + $header = array_pop( $header ); + } $is_expected = ( 400 === $code && @@ -156,7 +160,18 @@ function od_compose_site_health_result( $response ): array { $result['description'] .= '
' . esc_html( $data['message'] ) . ''; } - $result['description'] .= '
' . esc_html( $body ) . '
' . esc_html( $body ) . ''; + } + $result['description'] .= '
You don\'t have permission to access this resource.
', + ), + 'expected_option' => '1', + 'expected_status' => 'recommended', + 'expected_unavailable' => true, + ), 'error' => array( 'mocked_response' => new WP_Error( 'bad', 'Something terrible has happened' ), 'expected_option' => '1', @@ -142,6 +163,8 @@ public function data_provider_test_rest_api_availability(): array { * @covers ::od_compose_site_health_result * @covers ::od_get_rest_api_health_check_response * @covers ::od_is_rest_api_unavailable + * @covers ::od_render_rest_api_health_check_admin_notice_in_plugin_row + * @covers ::od_maybe_render_rest_api_health_check_admin_notice * * @dataProvider data_provider_test_rest_api_availability * @@ -151,6 +174,24 @@ public function test_rest_api_availability( $mocked_response, string $expected_o $this->filter_rest_api_response( $mocked_response ); $result = od_test_rest_api_availability(); + $notice = get_echo( 'od_render_rest_api_health_check_admin_notice_in_plugin_row', array( 'optimization-detective/load.php' ) ); + if ( $expected_unavailable ) { + $this->assertStringContainsString( '', $notice );
+ if ( is_array( $mocked_response ) ) {
+ if ( isset( $mocked_response['headers']['content-type'] ) && str_contains( join( '', (array) $mocked_response['headers']['content-type'] ), 'html' ) ) {
+ $this->assertStringContainsString( '', $notice );
+ $this->assertStringNotContainsString( '', $notice );
+ } else {
+ $this->assertStringContainsString( '', $notice );
+ $this->assertStringNotContainsString( '', $notice );
+ }
+ } else {
+ $this->assertStringNotContainsString( '', $notice );
+ $this->assertStringNotContainsString( '', $notice );
+ }
+ } else {
+ $this->assertSame( '', $notice );
+ }
$this->assertArrayHasKey( 'label', $result );
$this->assertArrayHasKey( 'status', $result );
$this->assertArrayHasKey( 'badge', $result );
@@ -392,7 +433,7 @@ static function ( $pre, array $args, string $url ) use ( $mocked_response, $obse
}
/**
- * Build a mock response.
+ * Build a mock JSON response.
*
* @param int $status_code HTTP status code.
* @param string $message HTTP status message.
@@ -406,6 +447,9 @@ protected function build_mock_response( int $status_code, string $message, array
'message' => $message,
),
'body' => wp_json_encode( $body ),
+ 'headers' => array(
+ 'content-type' => 'application/json',
+ ),
);
}
}