Skip to content

Commit 844cd16

Browse files
committed
Update unit test cases
1 parent d03b4a8 commit 844cd16

File tree

2 files changed

+69
-45
lines changed

2 files changed

+69
-45
lines changed

src/Admin/SiteHealth.php

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private function get_persistent_object_cache_learn_more_action() {
247247
public function persistent_object_cache() {
248248
$is_using_object_cache = wp_using_ext_object_cache();
249249
$page_cache_detail = $this->get_page_cache_detail( true );
250-
$has_page_caching = 'good' === $page_cache_detail['status'];
250+
$has_page_caching = ( is_array( $page_cache_detail ) && 'good' === $page_cache_detail['status'] );
251251

252252
$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>';
253253

@@ -337,64 +337,65 @@ static function ( $available_service ) {
337337
*/
338338
public function page_cache() {
339339
$page_cache_detail = $this->get_page_cache_detail();
340-
$page_cache_status = $page_cache_detail['status'];
341340

342341
$badge_color = 'red';
343-
$status = is_wp_error( $page_cache_status ) ? 'critical' : $page_cache_status;
342+
$status = is_wp_error( $page_cache_detail ) ? 'critical' : $page_cache_detail['status'];
344343
$label = __( 'Page caching is not detected and response time is slow.', 'amp' );
345344

346345
$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>';
347346

348347
/* translators: List of page cache headers. */
349348
$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>' );
350349

351-
if ( is_wp_error( $page_cache_status ) ) {
350+
if ( is_wp_error( $page_cache_detail ) ) {
352351
$error_info = sprintf(
353352
/* translators: 1 is error message, 2 is error code */
354353
__( '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' ),
355-
$page_cache_status->get_error_message(),
356-
$page_cache_status->get_error_code()
354+
$page_cache_detail->get_error_message(),
355+
$page_cache_detail->get_error_code()
357356
);
358357

359358
$description = "<p>$error_info</p>" . $description;
360-
} elseif ( 'recommended' === $page_cache_status ) {
359+
} elseif ( 'recommended' === $status ) {
361360
$badge_color = 'orange';
362361
$label = __( 'Page caching is not detected, but your response time is OK', 'amp' );
363-
} elseif ( 'good' === $page_cache_status ) {
362+
} elseif ( 'good' === $status ) {
364363
$badge_color = 'green';
365364
$label = __( 'Page caching is detected', 'amp' );
366365
}
367366

368-
$page_cache_test_summary = [];
369-
370-
if ( $page_cache_detail['advanced_cache_present'] ) {
371-
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . __( 'Page caching plugin is available.', 'amp' );
372-
} else {
373-
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span>' . __( 'Page caching plugin is not available.', 'amp' );
374-
}
367+
if ( ! is_wp_error( $page_cache_detail ) ) {
368+
$page_cache_test_summary = [];
375369

376-
if ( empty( $page_cache_detail['response_time'] ) ) {
377-
$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' );
378-
} else {
379-
380-
if ( $page_cache_detail['response_time'] < 600 ) {
381-
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . __( 'Site response time is less than 600 microseconds.', 'amp' );
370+
if ( $page_cache_detail['advanced_cache_present'] ) {
371+
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . __( 'Page caching plugin is available.', 'amp' );
382372
} else {
383-
$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' );
373+
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span>' . __( 'Page caching plugin is not available.', 'amp' );
384374
}
385375

386-
if ( empty( $page_cache_detail['headers'] ) ) {
387-
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span>' . __( 'We could not find any page cache headers in a response.', 'amp' );
376+
if ( empty( $page_cache_detail['response_time'] ) ) {
377+
$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' );
388378
} else {
389-
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . sprintf(
390-
/* translators: List of detected page cache headers. */
391-
__( 'These are headers we found in a response. %s', 'amp' ),
392-
'<code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>'
393-
);
379+
380+
if ( $page_cache_detail['response_time'] < 600 ) {
381+
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . __( 'Site response time is less than 600 microseconds.', 'amp' );
382+
} else {
383+
$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' );
384+
}
385+
386+
if ( empty( $page_cache_detail['headers'] ) ) {
387+
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning text-warning"></span>' . __( 'We could not find any page cache headers in a response.', 'amp' );
388+
} else {
389+
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt text-success"></span>' . sprintf(
390+
/* translators: List of detected page cache headers. */
391+
__( 'These are headers we found in a response. %s', 'amp' ),
392+
'<code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>'
393+
);
394+
}
394395
}
395-
}
396396

397-
$description .= '<p><ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul></p>';
397+
$description .= '<p><ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul></p>';
398+
}
398399

399400
return [
400401
'badge' => [

tests/php/src/Admin/SiteHealthTest.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,20 @@ public function test_page_cache( $responses, $expected_status, $good_basic_auth
860860
'good' => 'green',
861861
];
862862

863+
$labels = [
864+
'critical' => 'Page caching is not detected and response time is slow.',
865+
'recommended' => 'Page caching is not detected, but your response time is OK',
866+
'good' => 'Page caching is detected',
867+
];
868+
863869
$expected_props = [
864870
'badge' => [
865871
'label' => 'AMP',
866872
'color' => $badge_color[ $expected_status ],
867873
],
868874
'test' => 'amp_page_cache',
869875
'status' => $expected_status,
870-
'label' => 'good' === $expected_status ? 'Page caching is detected' : 'Page caching is not detected',
876+
'label' => $labels[ $expected_status ],
871877
];
872878

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

942948
set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, 'no', DAY_IN_SECONDS );
943-
$this->assertEquals( 'critical', $this->instance->get_page_cache_detail( true ) );
949+
$this->assertEquals(
950+
[
951+
'status' => 'critical',
952+
'advanced_cache_present' => null,
953+
'headers' => [],
954+
'response_time' => 0,
955+
],
956+
$this->instance->get_page_cache_detail( true )
957+
);
944958

945959
set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, 'yes', DAY_IN_SECONDS );
946-
$this->assertEquals( 'good', $this->instance->get_page_cache_detail( true ) );
960+
$this->assertEquals(
961+
[
962+
'status' => 'good',
963+
'advanced_cache_present' => null,
964+
'headers' => [],
965+
'response_time' => 0,
966+
],
967+
$this->instance->get_page_cache_detail( true )
968+
);
947969

948970
delete_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY );
949971
}
@@ -975,9 +997,11 @@ public function test_get_page_cache_detail() {
975997
];
976998
set_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY, $page_cache_status, DAY_IN_SECONDS );
977999

978-
$this->assertEquals( 'recommended', $this->instance->get_page_cache_detail( true ) );
1000+
$output = $this->instance->get_page_cache_detail( true );
1001+
$this->assertEquals( 'recommended', $output['status'] );
9791002

980-
$this->assertEquals( 'good', $this->instance->get_page_cache_detail() );
1003+
$output = $this->instance->get_page_cache_detail();
1004+
$this->assertEquals( 'good', $output['status'] );
9811005

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

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

992-
$this->assertEquals( 'good', $this->instance->get_page_cache_detail( true ) );
1016+
$output = $this->instance->get_page_cache_detail( true );
1017+
$this->assertEquals( 'good', $output['status'] );
9931018

9941019
delete_transient( SiteHealth::HAS_PAGE_CACHING_TRANSIENT_KEY );
9951020
}
@@ -1035,19 +1060,17 @@ public function test_get_page_cache_detail_with_error() {
10351060
);
10361061

10371062
// Test 3: Test for non-cached result again now that no error is returned.
1038-
$this->assertEquals(
1039-
'good',
1040-
$this->instance->get_page_cache_detail( false )
1041-
);
1063+
$output = $this->instance->get_page_cache_detail( false );
1064+
$this->assertEquals( 'good', $output['status'] );
1065+
$this->assertContains( 'cache-control', $output['headers'] );
10421066

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

10461070
// Test 4: Test for cached result again now that no error is returned.
1047-
$this->assertEquals(
1048-
'good',
1049-
$this->instance->get_page_cache_detail( true )
1050-
);
1071+
$output = $this->instance->get_page_cache_detail( true );
1072+
$this->assertEquals( 'good', $output['status'] );
1073+
$this->assertContains( 'cache-control', $output['headers'] );
10511074
}
10521075

10531076
/**

0 commit comments

Comments
 (0)