Skip to content

Commit

Permalink
Improve test_rest_api_availability
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Feb 6, 2025
1 parent 0edd448 commit 0307bb5
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions plugins/optimization-detective/tests/test-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ 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
*
* @dataProvider data_provider_test_rest_api_availability
*
Expand All @@ -172,9 +173,23 @@ public function test_rest_api_availability( $mocked_response, string $expected_o
$this->filter_rest_api_response( $mocked_response );

$result = od_test_rest_api_availability();
if ( 'nginx_forbidden' === $this->dataName() ) {
$notice = get_echo( 'od_render_rest_api_health_check_admin_notice_in_plugin_row', array( 'optimization-detective/load.php' ) );
$this->assertStringContainsString( '</iframe>', $notice );
$notice = get_echo( 'od_render_rest_api_health_check_admin_notice_in_plugin_row', array( 'optimization-detective/load.php' ) );
if ( $expected_unavailable ) {
$this->assertStringContainsString( '<code>', $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( '</iframe>', $notice );
$this->assertStringNotContainsString( '</pre>', $notice );
} else {
$this->assertStringContainsString( '</pre>', $notice );
$this->assertStringNotContainsString( '</iframe>', $notice );
}
} else {
$this->assertStringNotContainsString( '</iframe>', $notice );
$this->assertStringNotContainsString( '</pre>', $notice );
}
} else {
$this->assertSame( '', $notice );
}
$this->assertArrayHasKey( 'label', $result );
$this->assertArrayHasKey( 'status', $result );
Expand Down Expand Up @@ -417,7 +432,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.
Expand All @@ -431,6 +446,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',
),
);
}
}

0 comments on commit 0307bb5

Please sign in to comment.