mirror of
git://develop.git.wordpress.org/
synced 2025-02-07 08:04:27 +01:00
Site Health: Some documentation and test improvements for WP_Site_Health
:
* Add a `@coversDefaultClass` annotation to address `@covers ::get_test_page_cache" is invalid` notices. * Rename data providers to start with the `data_` prefix and match the test method names, for consistency. * Move data providers next to the test methods they are used in. * Move `::get_test_page_cache()` closer to `::get_test_persistent_object_cache()`, for a bit more predictable placement. * Fix a typo in `::get_test_persistent_object_cache()` description. Follow-up to [53955], [54043], [54044], [54045]. See #56041. git-svn-id: https://develop.svn.wordpress.org/trunk@54047 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
08057c8cc5
commit
3909611f9a
@ -351,7 +351,7 @@ class WP_Site_Health {
|
||||
/**
|
||||
* Test if plugins are outdated, or unnecessary.
|
||||
*
|
||||
* The tests checks if your plugins are up to date, and encourages you to remove any
|
||||
* The test checks if your plugins are up to date, and encourages you to remove any
|
||||
* that are not in use.
|
||||
*
|
||||
* @since 5.2.0
|
||||
@ -1666,120 +1666,6 @@ class WP_Site_Health {
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a full page cache is available.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @return array The test result.
|
||||
*/
|
||||
public function get_test_page_cache() {
|
||||
$description = '<p>' . __( 'Page cache enhances the speed and performance of your site by saving and serving static pages instead of calling for a page every time a user visits.' ) . '</p>';
|
||||
$description .= '<p>' . __( 'Page cache is detected by looking for an active page cache plugin as well as making three requests to the homepage and looking for one or more of the following HTTP client caching response headers:' ) . '</p>';
|
||||
$description .= '<code>' . implode( '</code>, <code>', array_keys( $this->get_page_cache_headers() ) ) . '.</code>';
|
||||
|
||||
$result = array(
|
||||
'badge' => array(
|
||||
'label' => __( 'Performance' ),
|
||||
'color' => 'blue',
|
||||
),
|
||||
'description' => wp_kses_post( $description ),
|
||||
'test' => 'page_cache',
|
||||
'status' => 'good',
|
||||
'label' => '',
|
||||
'actions' => sprintf(
|
||||
'<p><a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
|
||||
__( 'https://wordpress.org/support/article/optimization/#Caching' ),
|
||||
__( 'Learn more about page cache' ),
|
||||
/* translators: Accessibility text. */
|
||||
__( '(opens in a new tab)' )
|
||||
),
|
||||
);
|
||||
|
||||
$page_cache_detail = $this->get_page_cache_detail();
|
||||
|
||||
if ( is_wp_error( $page_cache_detail ) ) {
|
||||
$result['label'] = __( 'Unable to detect the presence of page cache' );
|
||||
$result['status'] = 'recommended';
|
||||
$error_info = sprintf(
|
||||
/* translators: 1: Error message, 2: Error code. */
|
||||
__( 'Unable to detect page cache due to possible loopback request problem. Please verify that the loopback request test is passing. Error: %1$s (Code: %2$s)' ),
|
||||
$page_cache_detail->get_error_message(),
|
||||
$page_cache_detail->get_error_code()
|
||||
);
|
||||
$result['description'] = wp_kses_post( "<p>$error_info</p>" ) . $result['description'];
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result['status'] = $page_cache_detail['status'];
|
||||
|
||||
switch ( $page_cache_detail['status'] ) {
|
||||
case 'recommended':
|
||||
$result['label'] = __( 'Page cache is not detected but the server response time is OK' );
|
||||
break;
|
||||
case 'good':
|
||||
$result['label'] = __( 'Page cache is detected and the server response time is good' );
|
||||
break;
|
||||
default:
|
||||
if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
|
||||
$result['label'] = __( 'Page cache is not detected and the server response time is slow' );
|
||||
} else {
|
||||
$result['label'] = __( 'Page cache is detected but the server response time is still slow' );
|
||||
}
|
||||
}
|
||||
|
||||
$page_cache_test_summary = array();
|
||||
|
||||
if ( empty( $page_cache_detail['response_time'] ) ) {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss"></span> ' . __( 'Server response time could not be determined. Verify that loopback requests are working.' );
|
||||
} else {
|
||||
|
||||
$threshold = $this->get_good_response_time_threshold();
|
||||
if ( $page_cache_detail['response_time'] < $threshold ) {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . sprintf(
|
||||
/* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
|
||||
__( 'Median server response time was %1$s milliseconds. This is less than the recommended %2$s milliseconds threshold.' ),
|
||||
number_format_i18n( $page_cache_detail['response_time'] ),
|
||||
number_format_i18n( $threshold )
|
||||
);
|
||||
} else {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . sprintf(
|
||||
/* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
|
||||
__( 'Median server response time was %1$s milliseconds. It should be less than the recommended %2$s milliseconds threshold.' ),
|
||||
number_format_i18n( $page_cache_detail['response_time'] ),
|
||||
number_format_i18n( $threshold )
|
||||
);
|
||||
}
|
||||
|
||||
if ( empty( $page_cache_detail['headers'] ) ) {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'No client caching response headers were detected.' );
|
||||
} else {
|
||||
$headers_summary = '<span class="dashicons dashicons-yes-alt"></span>';
|
||||
$headers_summary .= ' ' . sprintf(
|
||||
/* translators: %d: Number of caching headers. */
|
||||
_n(
|
||||
'There was %d client caching response header detected:',
|
||||
'There were %d client caching response headers detected:',
|
||||
count( $page_cache_detail['headers'] )
|
||||
),
|
||||
count( $page_cache_detail['headers'] )
|
||||
);
|
||||
$headers_summary .= ' <code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>.';
|
||||
$page_cache_test_summary[] = $headers_summary;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $page_cache_detail['advanced_cache_present'] ) {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . __( 'A page cache plugin was detected.' );
|
||||
} elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
|
||||
// Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'A page cache plugin was not detected.' );
|
||||
}
|
||||
|
||||
$result['description'] .= '<ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul>';
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the HTTP API can handle SSL/TLS requests.
|
||||
*
|
||||
@ -2378,9 +2264,121 @@ class WP_Site_Health {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if sites uses persistent object cache.
|
||||
* Tests if a full page cache is available.
|
||||
*
|
||||
* Checks if site uses persistent object cache or recommends to use it if not.
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @return array The test result.
|
||||
*/
|
||||
public function get_test_page_cache() {
|
||||
$description = '<p>' . __( 'Page cache enhances the speed and performance of your site by saving and serving static pages instead of calling for a page every time a user visits.' ) . '</p>';
|
||||
$description .= '<p>' . __( 'Page cache is detected by looking for an active page cache plugin as well as making three requests to the homepage and looking for one or more of the following HTTP client caching response headers:' ) . '</p>';
|
||||
$description .= '<code>' . implode( '</code>, <code>', array_keys( $this->get_page_cache_headers() ) ) . '.</code>';
|
||||
|
||||
$result = array(
|
||||
'badge' => array(
|
||||
'label' => __( 'Performance' ),
|
||||
'color' => 'blue',
|
||||
),
|
||||
'description' => wp_kses_post( $description ),
|
||||
'test' => 'page_cache',
|
||||
'status' => 'good',
|
||||
'label' => '',
|
||||
'actions' => sprintf(
|
||||
'<p><a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
|
||||
__( 'https://wordpress.org/support/article/optimization/#Caching' ),
|
||||
__( 'Learn more about page cache' ),
|
||||
/* translators: Accessibility text. */
|
||||
__( '(opens in a new tab)' )
|
||||
),
|
||||
);
|
||||
|
||||
$page_cache_detail = $this->get_page_cache_detail();
|
||||
|
||||
if ( is_wp_error( $page_cache_detail ) ) {
|
||||
$result['label'] = __( 'Unable to detect the presence of page cache' );
|
||||
$result['status'] = 'recommended';
|
||||
$error_info = sprintf(
|
||||
/* translators: 1: Error message, 2: Error code. */
|
||||
__( 'Unable to detect page cache due to possible loopback request problem. Please verify that the loopback request test is passing. Error: %1$s (Code: %2$s)' ),
|
||||
$page_cache_detail->get_error_message(),
|
||||
$page_cache_detail->get_error_code()
|
||||
);
|
||||
$result['description'] = wp_kses_post( "<p>$error_info</p>" ) . $result['description'];
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result['status'] = $page_cache_detail['status'];
|
||||
|
||||
switch ( $page_cache_detail['status'] ) {
|
||||
case 'recommended':
|
||||
$result['label'] = __( 'Page cache is not detected but the server response time is OK' );
|
||||
break;
|
||||
case 'good':
|
||||
$result['label'] = __( 'Page cache is detected and the server response time is good' );
|
||||
break;
|
||||
default:
|
||||
if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
|
||||
$result['label'] = __( 'Page cache is not detected and the server response time is slow' );
|
||||
} else {
|
||||
$result['label'] = __( 'Page cache is detected but the server response time is still slow' );
|
||||
}
|
||||
}
|
||||
|
||||
$page_cache_test_summary = array();
|
||||
|
||||
if ( empty( $page_cache_detail['response_time'] ) ) {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss"></span> ' . __( 'Server response time could not be determined. Verify that loopback requests are working.' );
|
||||
} else {
|
||||
|
||||
$threshold = $this->get_good_response_time_threshold();
|
||||
if ( $page_cache_detail['response_time'] < $threshold ) {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . sprintf(
|
||||
/* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
|
||||
__( 'Median server response time was %1$s milliseconds. This is less than the recommended %2$s milliseconds threshold.' ),
|
||||
number_format_i18n( $page_cache_detail['response_time'] ),
|
||||
number_format_i18n( $threshold )
|
||||
);
|
||||
} else {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . sprintf(
|
||||
/* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
|
||||
__( 'Median server response time was %1$s milliseconds. It should be less than the recommended %2$s milliseconds threshold.' ),
|
||||
number_format_i18n( $page_cache_detail['response_time'] ),
|
||||
number_format_i18n( $threshold )
|
||||
);
|
||||
}
|
||||
|
||||
if ( empty( $page_cache_detail['headers'] ) ) {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'No client caching response headers were detected.' );
|
||||
} else {
|
||||
$headers_summary = '<span class="dashicons dashicons-yes-alt"></span>';
|
||||
$headers_summary .= ' ' . sprintf(
|
||||
/* translators: %d: Number of caching headers. */
|
||||
_n(
|
||||
'There was %d client caching response header detected:',
|
||||
'There were %d client caching response headers detected:',
|
||||
count( $page_cache_detail['headers'] )
|
||||
),
|
||||
count( $page_cache_detail['headers'] )
|
||||
);
|
||||
$headers_summary .= ' <code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>.';
|
||||
$page_cache_test_summary[] = $headers_summary;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $page_cache_detail['advanced_cache_present'] ) {
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . __( 'A page cache plugin was detected.' );
|
||||
} elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
|
||||
// Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
|
||||
$page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'A page cache plugin was not detected.' );
|
||||
}
|
||||
|
||||
$result['description'] .= '<ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul>';
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the site uses persistent object cache and recommends to use it if not.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
/**
|
||||
* @group site-health
|
||||
*
|
||||
* @coversDefaultClass WP_Site_Health
|
||||
*/
|
||||
class Tests_Site_Health extends WP_UnitTestCase {
|
||||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
||||
@ -110,7 +112,7 @@ class Tests_Site_Health extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 56041
|
||||
* @dataProvider data_page_cache_test
|
||||
* @dataProvider data_get_page_cache
|
||||
* @covers ::get_test_page_cache()
|
||||
* @covers ::get_page_cache_detail()
|
||||
* @covers ::get_page_cache_headers()
|
||||
@ -202,66 +204,15 @@ class Tests_Site_Health extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* @group ms-excluded
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function test_object_cache_default_thresholds() {
|
||||
$wp_site_health = new WP_Site_Health();
|
||||
|
||||
$this->assertFalse(
|
||||
$wp_site_health->should_suggest_persistent_object_cache()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @group ms-required
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function test_object_cache_default_thresholds_on_multisite() {
|
||||
$wp_site_health = new WP_Site_Health();
|
||||
$this->assertTrue(
|
||||
$wp_site_health->should_suggest_persistent_object_cache()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function test_object_cache_thresholds_check_can_be_bypassed() {
|
||||
$wp_site_health = new WP_Site_Health();
|
||||
add_filter( 'site_status_should_suggest_persistent_object_cache', '__return_true' );
|
||||
|
||||
$this->assertTrue(
|
||||
$wp_site_health->should_suggest_persistent_object_cache()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider thresholds
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function test_object_cache_thresholds( $threshold, $count ) {
|
||||
$wp_site_health = new WP_Site_Health();
|
||||
add_filter(
|
||||
'site_status_persistent_object_cache_thresholds',
|
||||
function ( $thresholds ) use ( $threshold, $count ) {
|
||||
return array_merge( $thresholds, array( $threshold => $count ) );
|
||||
}
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$wp_site_health->should_suggest_persistent_object_cache()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets response data for get_test_page_cache().
|
||||
* Data provider for test_get_page_cache().
|
||||
*
|
||||
* Gets response data for WP_Site_Health::get_test_page_cache().
|
||||
*
|
||||
* @ticket 56041
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data_page_cache_test() {
|
||||
public function data_get_page_cache() {
|
||||
$recommended_label = 'Page cache is not detected but the server response time is OK';
|
||||
$good_label = 'Page cache is detected and the server response time is good';
|
||||
$critical_label = 'Page cache is not detected and the server response time is slow';
|
||||
@ -408,11 +359,65 @@ class Tests_Site_Health extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
* @group ms-excluded
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function test_object_cache_default_thresholds() {
|
||||
$wp_site_health = new WP_Site_Health();
|
||||
|
||||
$this->assertFalse(
|
||||
$wp_site_health->should_suggest_persistent_object_cache()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @group ms-required
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function test_object_cache_default_thresholds_on_multisite() {
|
||||
$wp_site_health = new WP_Site_Health();
|
||||
$this->assertTrue(
|
||||
$wp_site_health->should_suggest_persistent_object_cache()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function test_object_cache_thresholds_check_can_be_bypassed() {
|
||||
$wp_site_health = new WP_Site_Health();
|
||||
add_filter( 'site_status_should_suggest_persistent_object_cache', '__return_true' );
|
||||
|
||||
$this->assertTrue(
|
||||
$wp_site_health->should_suggest_persistent_object_cache()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_object_cache_thresholds
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function test_object_cache_thresholds( $threshold, $count ) {
|
||||
$wp_site_health = new WP_Site_Health();
|
||||
add_filter(
|
||||
'site_status_persistent_object_cache_thresholds',
|
||||
function ( $thresholds ) use ( $threshold, $count ) {
|
||||
return array_merge( $thresholds, array( $threshold => $count ) );
|
||||
}
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$wp_site_health->should_suggest_persistent_object_cache()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_object_cache_thresholds().
|
||||
*
|
||||
* @ticket 56040
|
||||
*/
|
||||
public function thresholds() {
|
||||
public function data_object_cache_thresholds() {
|
||||
return array(
|
||||
array( 'comments_count', 0 ),
|
||||
array( 'posts_count', 0 ),
|
||||
|
Loading…
x
Reference in New Issue
Block a user