Skip to content

Commit

Permalink
Improve strings in site health test
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jan 27, 2022
1 parent 726e112 commit 0d97691
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
60 changes: 42 additions & 18 deletions src/Admin/SiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,16 @@ static function ( $available_service ) {
public function page_cache() {
$page_cache_detail = $this->get_page_cache_detail();

$badge_color = 'red';
$status = is_wp_error( $page_cache_detail ) ? 'critical' : $page_cache_detail['status'];
$label = __( 'Page caching is not detected and response time is slow.', 'amp' );

$description = '<p>' . esc_html__( 'The AMP plugin performs at its best when page caching is enabled. This is because the additional optimizations performed require additional server processing time, and page caching ensures that responses are served quickly.', 'amp' ) . '</p>';

/* translators: List of page cache headers. */
$description .= '<p>' . sprintf( __( 'Page caching is detected by looking for an active page caching plugin, making three requests to the homepage and looking for HTTP response headers like: %s.', 'amp' ), '<code>' . implode( '</code>, <code>', array_keys( self::get_page_cache_headers() ) ) . '</code>' );
$description .= '<p>' . sprintf( __( 'Page caching is detected by looking for an active page caching plugin as well as making three requests to the homepage and looking for one or more of the following HTTP client caching response headers: %s.', 'amp' ), '<code>' . implode( '</code>, <code>', array_keys( self::get_page_cache_headers() ) ) . '</code>' );

if ( is_wp_error( $page_cache_detail ) ) {
$badge_color = 'red';
$status = 'critical';
$label = __( 'Unable to detect the presence of page caching', 'amp' );

$error_info = sprintf(
/* translators: 1 is error message, 2 is error code */
__( 'Unable to detect page caching due to possible loopback request problem. Please verify that the loopback request test is passing. Error: %1$s (Code: %2$s)', 'amp' ),
Expand All @@ -356,41 +356,65 @@ public function page_cache() {
);

$description = "<p>$error_info</p>" . $description;
} elseif ( 'recommended' === $status ) {
} elseif ( 'recommended' === $page_cache_detail['status'] ) {
$badge_color = 'orange';
$status = $page_cache_detail['status'];
$label = __( 'Page caching is not detected, but your response time is OK', 'amp' );
} elseif ( 'good' === $status ) {
} elseif ( 'good' === $page_cache_detail['status'] ) {
$badge_color = 'green';
$status = $page_cache_detail['status'];
$label = __( 'Page caching is detected', 'amp' );
} else {
$badge_color = 'red';
$status = $page_cache_detail['status'];
$label = __( 'Page caching is not detected and response time is slow', 'amp' );
}

if ( ! is_wp_error( $page_cache_detail ) ) {
$page_cache_test_summary = [];

// @todo This should only be shown if no client page caching headers are found.
if ( $page_cache_detail['advanced_cache_present'] ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . __( 'Page caching plugin is available.', 'amp' );
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span> ' . __( 'A page caching plugin was detected.', 'amp' );
} else {
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span>' . __( 'Page caching plugin is not available.', 'amp' );
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span> ' . __( 'A page caching plugin was not detected.', 'amp' );
}

if ( empty( $page_cache_detail['response_time'] ) ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss text-error"></span>' . __( 'We couldn\'t able to find a response time. Please make sure loopback requests are allowed.', 'amp' );
$page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss text-error"></span> ' . __( 'Server response time could not be determined. Verify that loopback requests are working.', 'amp' );
} else {

// @todo The 600 millisecond threshold should be filterable.
if ( $page_cache_detail['response_time'] < 600 ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . __( 'Site response time is less than 600 microseconds.', 'amp' );
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span> ' . sprintf(
/* translators: %d is the response time in milliseconds */
__( 'Median server response time was %d milliseconds. This is less than the 600 millisecond threshold.', 'amp' ),
$page_cache_detail['response_time']
);
} else {
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span>' . __( 'Site response time is more than 600 microseconds, which is not ideal.', 'amp' );
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span> ' . sprintf(
/* translators: %d is the response time in milliseconds */
__( 'Median server response time was %d milliseconds. It should be less than 600 milliseconds.', 'amp' ),
$page_cache_detail['response_time'],
$page_cache_detail['response_time']
);
}

if ( empty( $page_cache_detail['headers'] ) ) {
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span>' . __( 'We could not find any page cache headers in a response.', 'amp' );
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span> ' . __( 'No client caching response headers were detected in response.', 'amp' );
} else {
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . sprintf(
/* translators: List of detected page cache headers. */
__( 'These are headers we found in a response. %s', 'amp' ),
'<code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>'
);
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span> ' .
sprintf(
/* translators: List of detected page cache headers. */
_n(
'There was %d client caching response header detected:',
'There were %d client caching response headers detected:',
count( $page_cache_detail['headers'] ),
'amp'
),
count( $page_cache_detail['headers'] )
) .
'<code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>';
}
}

Expand Down
32 changes: 24 additions & 8 deletions tests/php/src/Admin/SiteHealthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,28 +697,36 @@ public function test_add_extensions() {
* @return array[]
*/
public function get_page_cache_data() {
$recommended_label = 'Page caching is not detected, but your response time is OK';
$good_label = 'Page caching is detected';
$critical_label = 'Page caching is not detected and response time is slow';
$error_label = 'Unable to detect the presence of page caching';

return [
'basic-auth-fail' => [
'responses' => [
'unauthorized',
],
'expected_status' => 'critical',
'expected_label' => $error_label,
'good_basic_auth' => false,
],
'no-cache-control' => [
'responses' => array_fill( 0, 3, [] ),
'expected_status' => 'critical',
'expected_label' => $critical_label,
'good_basic_auth' => null,
'delay_the_response' => true,
],
'no-cache' => [
'responses' => array_fill( 0, 3, [ 'cache-control' => 'no-cache' ] ),
'expected_status' => 'recommended',
'expected_label' => $recommended_label,
],
'no-cache-with-delayed-response' => [
'responses' => array_fill( 0, 3, [ 'cache-control' => 'no-cache' ] ),
'expected_status' => 'critical',
'expected_label' => $critical_label,
'good_basic_auth' => null,
'delay_the_response' => true,
],
Expand All @@ -729,6 +737,7 @@ public function get_page_cache_data() {
[ 'age' => '1345' ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
],
'cache-control-max-age' => [
'responses' => array_fill(
Expand All @@ -737,6 +746,7 @@ public function get_page_cache_data() {
[ 'cache-control' => 'public; max-age=600' ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
],
'cache-control-max-age-after-2-requests' => [
'responses' => [
Expand All @@ -745,6 +755,7 @@ public function get_page_cache_data() {
[ 'cache-control' => 'public; max-age=600' ],
],
'expected_status' => 'good',
'expected_label' => $good_label,
],
'cache-control-with-future-expires' => [
'responses' => array_fill(
Expand All @@ -753,6 +764,7 @@ public function get_page_cache_data() {
[ 'expires' => gmdate( 'r', time() + MINUTE_IN_SECONDS * 10 ) ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
],
'cache-control-with-past-expires' => [
'responses' => array_fill(
Expand All @@ -761,6 +773,7 @@ public function get_page_cache_data() {
[ 'expires' => gmdate( 'r', time() - MINUTE_IN_SECONDS * 10 ) ]
),
'expected_status' => 'critical',
'expected_label' => $critical_label,
'good_basic_auth' => null,
'delay_the_response' => true,
],
Expand All @@ -771,6 +784,7 @@ public function get_page_cache_data() {
[ 'cache-control' => 'public; max-age=600' ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
'good_basic_auth' => true,
],
'cf-cache-status' => [
Expand All @@ -780,6 +794,7 @@ public function get_page_cache_data() {
[ 'cf-cache-status' => 'HIT: 1' ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
],
'cf-cache-status-without-header-and-delay' => [
'responses' => array_fill(
Expand All @@ -788,6 +803,7 @@ public function get_page_cache_data() {
[ 'cf-cache-status' => 'MISS' ]
),
'expected_status' => 'recommended',
'expected_label' => $recommended_label,
'good_basic_auth' => null,
'delay_the_response' => false,
],
Expand All @@ -798,6 +814,7 @@ public function get_page_cache_data() {
[ 'cf-cache-status' => 'MISS' ]
),
'expected_status' => 'critical',
'expected_label' => $critical_label,
'good_basic_auth' => null,
'delay_the_response' => true,
],
Expand All @@ -808,6 +825,7 @@ public function get_page_cache_data() {
[ 'x-cache-enabled' => 'true' ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
],
'x-cache-enabled-with-delay' => [
'responses' => array_fill(
Expand All @@ -816,6 +834,7 @@ public function get_page_cache_data() {
[ 'x-cache-enabled' => 'false' ]
),
'expected_status' => 'critical',
'expected_label' => $critical_label,
'good_basic_auth' => null,
'delay_the_response' => true,
],
Expand All @@ -826,6 +845,7 @@ public function get_page_cache_data() {
[ 'x-cache-disabled' => 'off' ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
],
'cf-apo-via' => [
'responses' => array_fill(
Expand All @@ -834,6 +854,7 @@ public function get_page_cache_data() {
[ 'cf-apo-via' => 'tcache' ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
],
'cf-edge-cache' => [
'responses' => array_fill(
Expand All @@ -842,6 +863,7 @@ public function get_page_cache_data() {
[ 'cf-edge-cache' => 'cache' ]
),
'expected_status' => 'good',
'expected_label' => $good_label,
],
];
}
Expand All @@ -852,28 +874,22 @@ public function get_page_cache_data() {
* @covers ::get_page_cache_headers()
* @covers ::check_for_page_caching()
*/
public function test_page_cache( $responses, $expected_status, $good_basic_auth = null, $delay_the_response = false ) {
public function test_page_cache( $responses, $expected_status, $expected_label, $good_basic_auth = null, $delay_the_response = false ) {

$badge_color = [
'critical' => 'red',
'recommended' => 'orange',
'good' => 'green',
];

$labels = [
'critical' => 'Page caching is not detected and response time is slow.',
'recommended' => 'Page caching is not detected, but your response time is OK',
'good' => 'Page caching is detected',
];

$expected_props = [
'badge' => [
'label' => 'AMP',
'color' => $badge_color[ $expected_status ],
],
'test' => 'amp_page_cache',
'status' => $expected_status,
'label' => $labels[ $expected_status ],
'label' => $expected_label,
];

if ( null !== $good_basic_auth ) {
Expand Down

0 comments on commit 0d97691

Please sign in to comment.