Skip to content

Commit

Permalink
Update unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaval-parekh committed Jan 27, 2022
1 parent d03b4a8 commit 844cd16
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 45 deletions.
63 changes: 32 additions & 31 deletions src/Admin/SiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private function get_persistent_object_cache_learn_more_action() {
public function persistent_object_cache() {
$is_using_object_cache = wp_using_ext_object_cache();
$page_cache_detail = $this->get_page_cache_detail( true );
$has_page_caching = 'good' === $page_cache_detail['status'];
$has_page_caching = ( is_array( $page_cache_detail ) && 'good' === $page_cache_detail['status'] );

$description = '<p>' . esc_html__( 'The AMP plugin performs at its best when persistent object cache is enabled. Persistent object caching is used to more effectively store image dimensions and parsed CSS using a caching backend rather than using the options table in the database.', 'amp' ) . '</p>';

Expand Down Expand Up @@ -337,64 +337,65 @@ static function ( $available_service ) {
*/
public function page_cache() {
$page_cache_detail = $this->get_page_cache_detail();
$page_cache_status = $page_cache_detail['status'];

$badge_color = 'red';
$status = is_wp_error( $page_cache_status ) ? 'critical' : $page_cache_status;
$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>' );

if ( is_wp_error( $page_cache_status ) ) {
if ( is_wp_error( $page_cache_detail ) ) {
$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' ),
$page_cache_status->get_error_message(),
$page_cache_status->get_error_code()
$page_cache_detail->get_error_message(),
$page_cache_detail->get_error_code()
);

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

$page_cache_test_summary = [];

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' );
} else {
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span>' . __( 'Page caching plugin is not available.', 'amp' );
}
if ( ! is_wp_error( $page_cache_detail ) ) {
$page_cache_test_summary = [];

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' );
} else {

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' );
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' );
} 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>' . __( 'Page caching plugin is not available.', 'amp' );
}

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' );
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' );
} 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>'
);

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' );
} 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' );
}

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' );
} 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>'
);
}
}
}

$description .= '<p><ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul></p>';
$description .= '<p><ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul></p>';
}

return [
'badge' => [
Expand Down
51 changes: 37 additions & 14 deletions tests/php/src/Admin/SiteHealthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,14 +860,20 @@ public function test_page_cache( $responses, $expected_status, $good_basic_auth
'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' => 'good' === $expected_status ? 'Page caching is detected' : 'Page caching is not detected',
'label' => $labels[ $expected_status ],
];

if ( null !== $good_basic_auth ) {
Expand Down Expand Up @@ -940,10 +946,26 @@ function ( $r, $parsed_args ) use ( &$responses, &$is_unauthorized, $good_basic_
public function test_get_page_cache_detail_with_legacy_cache_result() {

set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, 'no', DAY_IN_SECONDS );
$this->assertEquals( 'critical', $this->instance->get_page_cache_detail( true ) );
$this->assertEquals(
[
'status' => 'critical',
'advanced_cache_present' => null,
'headers' => [],
'response_time' => 0,
],
$this->instance->get_page_cache_detail( true )
);

set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, 'yes', DAY_IN_SECONDS );
$this->assertEquals( 'good', $this->instance->get_page_cache_detail( true ) );
$this->assertEquals(
[
'status' => 'good',
'advanced_cache_present' => null,
'headers' => [],
'response_time' => 0,
],
$this->instance->get_page_cache_detail( true )
);

delete_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY );
}
Expand Down Expand Up @@ -975,9 +997,11 @@ public function test_get_page_cache_detail() {
];
set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, $page_cache_status, DAY_IN_SECONDS );

$this->assertEquals( 'recommended', $this->instance->get_page_cache_detail( true ) );
$output = $this->instance->get_page_cache_detail( true );
$this->assertEquals( 'recommended', $output['status'] );

$this->assertEquals( 'good', $this->instance->get_page_cache_detail() );
$output = $this->instance->get_page_cache_detail();
$this->assertEquals( 'good', $output['status'] );

remove_filter( 'pre_http_request', $callback, 20 );

Expand All @@ -989,7 +1013,8 @@ public function test_get_page_cache_detail() {
];
set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, $page_cache_status, DAY_IN_SECONDS );

$this->assertEquals( 'good', $this->instance->get_page_cache_detail( true ) );
$output = $this->instance->get_page_cache_detail( true );
$this->assertEquals( 'good', $output['status'] );

delete_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY );
}
Expand Down Expand Up @@ -1035,19 +1060,17 @@ public function test_get_page_cache_detail_with_error() {
);

// Test 3: Test for non-cached result again now that no error is returned.
$this->assertEquals(
'good',
$this->instance->get_page_cache_detail( false )
);
$output = $this->instance->get_page_cache_detail( false );
$this->assertEquals( 'good', $output['status'] );
$this->assertContains( 'cache-control', $output['headers'] );

remove_filter( 'pre_http_request', $return_cached_response, 20 );
add_filter( 'pre_http_request', $return_error, 20 );

// Test 4: Test for cached result again now that no error is returned.
$this->assertEquals(
'good',
$this->instance->get_page_cache_detail( true )
);
$output = $this->instance->get_page_cache_detail( true );
$this->assertEquals( 'good', $output['status'] );
$this->assertContains( 'cache-control', $output['headers'] );
}

/**
Expand Down

0 comments on commit 844cd16

Please sign in to comment.