Make WordPress Core

Changeset 54047


Ignore:
Timestamp:
09/01/2022 03:26:29 PM (2 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-site-health.php

    r54044 r54047  
    352352     * Test if plugins are outdated, or unnecessary.
    353353     *
    354      * The tests checks if your plugins are up to date, and encourages you to remove any
     354     * The test checks if your plugins are up to date, and encourages you to remove any
    355355     * that are not in use.
    356356     *
     
    16681668
    16691669    /**
    1670      * Tests if a full page cache is available.
    1671      *
    1672      * @since 6.1.0
    1673      *
    1674      * @return array The test result.
    1675      */
    1676     public function get_test_page_cache() {
    1677         $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>';
    1678         $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>';
    1679         $description .= '<code>' . implode( '</code>, <code>', array_keys( $this->get_page_cache_headers() ) ) . '.</code>';
    1680 
    1681         $result = array(
    1682             'badge'       => array(
    1683                 'label' => __( 'Performance' ),
    1684                 'color' => 'blue',
    1685             ),
    1686             'description' => wp_kses_post( $description ),
    1687             'test'        => 'page_cache',
    1688             'status'      => 'good',
    1689             'label'       => '',
    1690             'actions'     => sprintf(
    1691                 '<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>',
    1692                 __( 'https://wordpress.org/support/article/optimization/#Caching' ),
    1693                 __( 'Learn more about page cache' ),
    1694                 /* translators: Accessibility text. */
    1695                 __( '(opens in a new tab)' )
    1696             ),
    1697         );
    1698 
    1699         $page_cache_detail = $this->get_page_cache_detail();
    1700 
    1701         if ( is_wp_error( $page_cache_detail ) ) {
    1702             $result['label']  = __( 'Unable to detect the presence of page cache' );
    1703             $result['status'] = 'recommended';
    1704             $error_info       = sprintf(
    1705             /* translators: 1: Error message, 2: Error code. */
    1706                 __( '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)' ),
    1707                 $page_cache_detail->get_error_message(),
    1708                 $page_cache_detail->get_error_code()
    1709             );
    1710             $result['description'] = wp_kses_post( "<p>$error_info</p>" ) . $result['description'];
    1711             return $result;
    1712         }
    1713 
    1714         $result['status'] = $page_cache_detail['status'];
    1715 
    1716         switch ( $page_cache_detail['status'] ) {
    1717             case 'recommended':
    1718                 $result['label'] = __( 'Page cache is not detected but the server response time is OK' );
    1719                 break;
    1720             case 'good':
    1721                 $result['label'] = __( 'Page cache is detected and the server response time is good' );
    1722                 break;
    1723             default:
    1724                 if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
    1725                     $result['label'] = __( 'Page cache is not detected and the server response time is slow' );
    1726                 } else {
    1727                     $result['label'] = __( 'Page cache is detected but the server response time is still slow' );
    1728                 }
    1729         }
    1730 
    1731         $page_cache_test_summary = array();
    1732 
    1733         if ( empty( $page_cache_detail['response_time'] ) ) {
    1734             $page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss"></span> ' . __( 'Server response time could not be determined. Verify that loopback requests are working.' );
    1735         } else {
    1736 
    1737             $threshold = $this->get_good_response_time_threshold();
    1738             if ( $page_cache_detail['response_time'] < $threshold ) {
    1739                 $page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . sprintf(
    1740                     /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
    1741                     __( 'Median server response time was %1$s milliseconds. This is less than the recommended %2$s milliseconds threshold.' ),
    1742                     number_format_i18n( $page_cache_detail['response_time'] ),
    1743                     number_format_i18n( $threshold )
    1744                 );
    1745             } else {
    1746                 $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . sprintf(
    1747                     /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
    1748                     __( 'Median server response time was %1$s milliseconds. It should be less than the recommended %2$s milliseconds threshold.' ),
    1749                     number_format_i18n( $page_cache_detail['response_time'] ),
    1750                     number_format_i18n( $threshold )
    1751                 );
    1752             }
    1753 
    1754             if ( empty( $page_cache_detail['headers'] ) ) {
    1755                 $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'No client caching response headers were detected.' );
    1756             } else {
    1757                 $headers_summary  = '<span class="dashicons dashicons-yes-alt"></span>';
    1758                 $headers_summary .= ' ' . sprintf(
    1759                     /* translators: %d: Number of caching headers. */
    1760                     _n(
    1761                         'There was %d client caching response header detected:',
    1762                         'There were %d client caching response headers detected:',
    1763                         count( $page_cache_detail['headers'] )
    1764                     ),
    1765                     count( $page_cache_detail['headers'] )
    1766                 );
    1767                 $headers_summary          .= ' <code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>.';
    1768                 $page_cache_test_summary[] = $headers_summary;
    1769             }
    1770         }
    1771 
    1772         if ( $page_cache_detail['advanced_cache_present'] ) {
    1773             $page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . __( 'A page cache plugin was detected.' );
    1774         } elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
    1775             // Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
    1776             $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'A page cache plugin was not detected.' );
    1777         }
    1778 
    1779         $result['description'] .= '<ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul>';
    1780         return $result;
    1781     }
    1782 
    1783     /**
    17841670     * Check if the HTTP API can handle SSL/TLS requests.
    17851671     *
     
    23792265
    23802266    /**
    2381      * Tests if sites uses persistent object cache.
    2382      *
    2383      * Checks if site uses persistent object cache or recommends to use it if not.
     2267     * Tests if a full page cache is available.
     2268     *
     2269     * @since 6.1.0
     2270     *
     2271     * @return array The test result.
     2272     */
     2273    public function get_test_page_cache() {
     2274        $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>';
     2275        $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>';
     2276        $description .= '<code>' . implode( '</code>, <code>', array_keys( $this->get_page_cache_headers() ) ) . '.</code>';
     2277
     2278        $result = array(
     2279            'badge'       => array(
     2280                'label' => __( 'Performance' ),
     2281                'color' => 'blue',
     2282            ),
     2283            'description' => wp_kses_post( $description ),
     2284            'test'        => 'page_cache',
     2285            'status'      => 'good',
     2286            'label'       => '',
     2287            'actions'     => sprintf(
     2288                '<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>',
     2289                __( 'https://wordpress.org/support/article/optimization/#Caching' ),
     2290                __( 'Learn more about page cache' ),
     2291                /* translators: Accessibility text. */
     2292                __( '(opens in a new tab)' )
     2293            ),
     2294        );
     2295
     2296        $page_cache_detail = $this->get_page_cache_detail();
     2297
     2298        if ( is_wp_error( $page_cache_detail ) ) {
     2299            $result['label']  = __( 'Unable to detect the presence of page cache' );
     2300            $result['status'] = 'recommended';
     2301            $error_info       = sprintf(
     2302            /* translators: 1: Error message, 2: Error code. */
     2303                __( '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)' ),
     2304                $page_cache_detail->get_error_message(),
     2305                $page_cache_detail->get_error_code()
     2306            );
     2307            $result['description'] = wp_kses_post( "<p>$error_info</p>" ) . $result['description'];
     2308            return $result;
     2309        }
     2310
     2311        $result['status'] = $page_cache_detail['status'];
     2312
     2313        switch ( $page_cache_detail['status'] ) {
     2314            case 'recommended':
     2315                $result['label'] = __( 'Page cache is not detected but the server response time is OK' );
     2316                break;
     2317            case 'good':
     2318                $result['label'] = __( 'Page cache is detected and the server response time is good' );
     2319                break;
     2320            default:
     2321                if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
     2322                    $result['label'] = __( 'Page cache is not detected and the server response time is slow' );
     2323                } else {
     2324                    $result['label'] = __( 'Page cache is detected but the server response time is still slow' );
     2325                }
     2326        }
     2327
     2328        $page_cache_test_summary = array();
     2329
     2330        if ( empty( $page_cache_detail['response_time'] ) ) {
     2331            $page_cache_test_summary[] = '<span class="dashicons dashicons-dismiss"></span> ' . __( 'Server response time could not be determined. Verify that loopback requests are working.' );
     2332        } else {
     2333
     2334            $threshold = $this->get_good_response_time_threshold();
     2335            if ( $page_cache_detail['response_time'] < $threshold ) {
     2336                $page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . sprintf(
     2337                    /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
     2338                    __( 'Median server response time was %1$s milliseconds. This is less than the recommended %2$s milliseconds threshold.' ),
     2339                    number_format_i18n( $page_cache_detail['response_time'] ),
     2340                    number_format_i18n( $threshold )
     2341                );
     2342            } else {
     2343                $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . sprintf(
     2344                    /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
     2345                    __( 'Median server response time was %1$s milliseconds. It should be less than the recommended %2$s milliseconds threshold.' ),
     2346                    number_format_i18n( $page_cache_detail['response_time'] ),
     2347                    number_format_i18n( $threshold )
     2348                );
     2349            }
     2350
     2351            if ( empty( $page_cache_detail['headers'] ) ) {
     2352                $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'No client caching response headers were detected.' );
     2353            } else {
     2354                $headers_summary  = '<span class="dashicons dashicons-yes-alt"></span>';
     2355                $headers_summary .= ' ' . sprintf(
     2356                    /* translators: %d: Number of caching headers. */
     2357                    _n(
     2358                        'There was %d client caching response header detected:',
     2359                        'There were %d client caching response headers detected:',
     2360                        count( $page_cache_detail['headers'] )
     2361                    ),
     2362                    count( $page_cache_detail['headers'] )
     2363                );
     2364                $headers_summary          .= ' <code>' . implode( '</code>, <code>', $page_cache_detail['headers'] ) . '</code>.';
     2365                $page_cache_test_summary[] = $headers_summary;
     2366            }
     2367        }
     2368
     2369        if ( $page_cache_detail['advanced_cache_present'] ) {
     2370            $page_cache_test_summary[] = '<span class="dashicons dashicons-yes-alt"></span> ' . __( 'A page cache plugin was detected.' );
     2371        } elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
     2372            // Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
     2373            $page_cache_test_summary[] = '<span class="dashicons dashicons-warning"></span> ' . __( 'A page cache plugin was not detected.' );
     2374        }
     2375
     2376        $result['description'] .= '<ul><li>' . implode( '</li><li>', $page_cache_test_summary ) . '</li></ul>';
     2377        return $result;
     2378    }
     2379
     2380    /**
     2381     * Tests if the site uses persistent object cache and recommends to use it if not.
    23842382     *
    23852383     * @since 6.1.0
  • trunk/tests/phpunit/tests/site-health.php

    r54045 r54047  
    33/**
    44 * @group site-health
     5 *
     6 * @coversDefaultClass WP_Site_Health
    57 */
    68class Tests_Site_Health extends WP_UnitTestCase {
     
    111113    /**
    112114     * @ticket 56041
    113      * @dataProvider data_page_cache_test
     115     * @dataProvider data_get_page_cache
    114116     * @covers ::get_test_page_cache()
    115117     * @covers ::get_page_cache_detail()
     
    203205
    204206    /**
    205      * @group ms-excluded
    206      * @ticket 56040
    207      */
    208     public function test_object_cache_default_thresholds() {
    209         $wp_site_health = new WP_Site_Health();
    210 
    211         $this->assertFalse(
    212             $wp_site_health->should_suggest_persistent_object_cache()
    213         );
    214     }
    215 
    216 
    217     /**
    218      * @group ms-required
    219      * @ticket 56040
    220      */
    221     public function test_object_cache_default_thresholds_on_multisite() {
    222         $wp_site_health = new WP_Site_Health();
    223         $this->assertTrue(
    224             $wp_site_health->should_suggest_persistent_object_cache()
    225         );
    226     }
    227 
    228     /**
    229      * @ticket 56040
    230      */
    231     public function test_object_cache_thresholds_check_can_be_bypassed() {
    232         $wp_site_health = new WP_Site_Health();
    233         add_filter( 'site_status_should_suggest_persistent_object_cache', '__return_true' );
    234 
    235         $this->assertTrue(
    236             $wp_site_health->should_suggest_persistent_object_cache()
    237         );
    238     }
    239 
    240     /**
    241      * @dataProvider thresholds
    242      * @ticket 56040
    243      */
    244     public function test_object_cache_thresholds( $threshold, $count ) {
    245         $wp_site_health = new WP_Site_Health();
    246         add_filter(
    247             'site_status_persistent_object_cache_thresholds',
    248             function ( $thresholds ) use ( $threshold, $count ) {
    249                 return array_merge( $thresholds, array( $threshold => $count ) );
    250             }
    251         );
    252 
    253         $this->assertTrue(
    254             $wp_site_health->should_suggest_persistent_object_cache()
    255         );
    256     }
    257 
    258     /**
    259      * Gets response data for get_test_page_cache().
     207     * Data provider for test_get_page_cache().
     208     *
     209     * Gets response data for WP_Site_Health::get_test_page_cache().
     210     *
    260211     * @ticket 56041
    261212     *
    262213     * @return array[]
    263214     */
    264     public function data_page_cache_test() {
     215    public function data_get_page_cache() {
    265216        $recommended_label = 'Page cache is not detected but the server response time is OK';
    266217        $good_label        = 'Page cache is detected and the server response time is good';
     
    409360
    410361    /**
    411      * Data provider.
    412      *
     362     * @group ms-excluded
    413363     * @ticket 56040
    414364     */
    415     public function thresholds() {
     365    public function test_object_cache_default_thresholds() {
     366        $wp_site_health = new WP_Site_Health();
     367
     368        $this->assertFalse(
     369            $wp_site_health->should_suggest_persistent_object_cache()
     370        );
     371    }
     372
     373
     374    /**
     375     * @group ms-required
     376     * @ticket 56040
     377     */
     378    public function test_object_cache_default_thresholds_on_multisite() {
     379        $wp_site_health = new WP_Site_Health();
     380        $this->assertTrue(
     381            $wp_site_health->should_suggest_persistent_object_cache()
     382        );
     383    }
     384
     385    /**
     386     * @ticket 56040
     387     */
     388    public function test_object_cache_thresholds_check_can_be_bypassed() {
     389        $wp_site_health = new WP_Site_Health();
     390        add_filter( 'site_status_should_suggest_persistent_object_cache', '__return_true' );
     391
     392        $this->assertTrue(
     393            $wp_site_health->should_suggest_persistent_object_cache()
     394        );
     395    }
     396
     397    /**
     398     * @dataProvider data_object_cache_thresholds
     399     * @ticket 56040
     400     */
     401    public function test_object_cache_thresholds( $threshold, $count ) {
     402        $wp_site_health = new WP_Site_Health();
     403        add_filter(
     404            'site_status_persistent_object_cache_thresholds',
     405            function ( $thresholds ) use ( $threshold, $count ) {
     406                return array_merge( $thresholds, array( $threshold => $count ) );
     407            }
     408        );
     409
     410        $this->assertTrue(
     411            $wp_site_health->should_suggest_persistent_object_cache()
     412        );
     413    }
     414
     415    /**
     416     * Data provider for test_object_cache_thresholds().
     417     *
     418     * @ticket 56040
     419     */
     420    public function data_object_cache_thresholds() {
    416421        return array(
    417422            array( 'comments_count', 0 ),
Note: See TracChangeset for help on using the changeset viewer.