Make WordPress Core

Ticket #47223: 47223.8.diff

File 47223.8.diff, 7.4 KB (added by peterwilsoncc, 6 years ago)
  • src/wp-admin/includes/class-wp-site-health.php

    diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php
    index d8c097c93d..a42732d277 100644
    a b class WP_Site_Health { 
    1111        private $mysql_min_version_check;
    1212        private $mysql_rec_version_check;
    1313
    14         public  $is_mariadb                          = false;
     14        public $is_mariadb                           = false;
    1515        private $mysql_server_version                = '';
    1616        private $health_check_mysql_required_version = '5.5';
    1717        private $health_check_mysql_rec_version      = '';
    1818
    1919        public $schedules;
    2020        public $crons;
    21         public $last_missed_cron = null;
     21        public $last_missed_cron     = null;
     22        public $last_late_cron       = null;
     23        private $timeout_missed_cron = null;
     24        private $timeout_late_cron   = null;
    2225
    2326        /**
    2427         * WP_Site_Health constructor.
    class WP_Site_Health { 
    2831        public function __construct() {
    2932                $this->prepare_sql_data();
    3033
     34                $this->timeout_late_cron   = 0;
     35                $this->timeout_missed_cron = - 5 * MINUTE_IN_SECONDS;
     36
     37                if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
     38                        $this->timeout_late_cron   = - 15 * MINUTE_IN_SECONDS;
     39                        $this->timeout_missed_cron = - 1 * HOUR_IN_SECONDS;
     40                }
     41
    3142                add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
    3243
    3344                add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    class WP_Site_Health { 
    14161427                                        $this->has_missed_cron()->get_error_message()
    14171428                                )
    14181429                        );
    1419                 } else {
    1420                         if ( $this->has_missed_cron() ) {
    1421                                 $result['status'] = 'recommended';
     1430                } elseif ( $this->has_missed_cron() ) {
     1431                        $result['status'] = 'recommended';
    14221432
    1423                                 $result['label'] = __( 'A scheduled event has failed' );
     1433                        $result['label'] = __( 'A scheduled event has failed' );
    14241434
    1425                                 $result['description'] = sprintf(
    1426                                         '<p>%s</p>',
    1427                                         sprintf(
    1428                                                 /* translators: %s: The name of the failed cron event. */
    1429                                                 __( 'The scheduled event, %s, failed to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
    1430                                                 $this->last_missed_cron
    1431                                         )
    1432                                 );
    1433                         }
     1435                        $result['description'] = sprintf(
     1436                                '<p>%s</p>',
     1437                                sprintf(
     1438                                        /* translators: %s: The name of the failed cron event. */
     1439                                        __( 'The scheduled event, %s, failed to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
     1440                                        $this->last_missed_cron
     1441                                )
     1442                        );
     1443                } elseif ( $this->has_late_cron() ) {
     1444                        $result['status'] = 'recommended';
     1445
     1446                        $result['label'] = __( 'A scheduled event is late' );
     1447
     1448                        $result['description'] = sprintf(
     1449                                '<p>%s</p>',
     1450                                sprintf(
     1451                                        /* translators: %s: The name of the late cron event. */
     1452                                        __( 'The scheduled event, %s, is late to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
     1453                                        $this->last_late_cron
     1454                                )
     1455                        );
    14341456                }
    14351457
    14361458                return $result;
    class WP_Site_Health { 
    19261948                }
    19271949
    19281950                foreach ( $this->crons as $id => $cron ) {
    1929                         if ( ( $cron->time - time() ) < 0 ) {
     1951                        if ( ( $cron->time - time() ) < $this->timeout_missed_cron ) {
    19301952                                $this->last_missed_cron = $cron->hook;
    19311953                                return true;
    19321954                        }
    class WP_Site_Health { 
    19351957                return false;
    19361958        }
    19371959
     1960        /**
     1961         * Check if any scheduled tasks are late.
     1962         *
     1963         * Returns a boolean value of `true` if a scheduled task is late and ends processing. If the list of
     1964         * crons is an instance of WP_Error, return the instance instead of a boolean value.
     1965         *
     1966         * @return bool|WP_Error true if a cron is late, false if it wasn't. WP_Error if the cron is set to that.
     1967         */
     1968        public function has_late_cron() {
     1969                if ( is_wp_error( $this->crons ) ) {
     1970                        return $this->crons;
     1971                }
     1972
     1973                foreach ( $this->crons as $id => $cron ) {
     1974                        $cron_offset = $cron->time - time();
     1975                        if (
     1976                                        $cron_offset >= $this->timeout_missed_cron &&
     1977                                        $cron_offset < $this->timeout_late_cron
     1978                                ) {
     1979                                $this->last_late_cron = $cron->hook;
     1980                                return true;
     1981                        }
     1982                }
     1983
     1984                return false;
     1985        }
     1986
    19381987        /**
    19391988         * Run a loopback test on our site.
    19401989         *
  • new file tests/phpunit/tests/site-health.php

    diff --git a/tests/phpunit/tests/site-health.php b/tests/phpunit/tests/site-health.php
    new file mode 100644
    index 0000000000..6c22fe4ddf
    - +  
     1<?php
     2
     3/**
     4 * @group site-health
     5 */
     6class Tests_Site_Health extends WP_UnitTestCase {
     7        public static function wpSetUpBeforeClass() {
     8                // Include the `WP_Site_Health` file.
     9                include_once( ABSPATH . 'wp-admin/includes/class-wp-site-health.php' );
     10        }
     11
     12        /**
     13         * Ensure Site Health reports correctly cron job reports.
     14         *
     15         * @ticket 47223
     16         */
     17        function test_cron_health_checks_critical() {
     18                // Clear the cron array.
     19                _set_cron_array( array() );
     20                $wp_site_health = new WP_Site_Health();
     21                $cron_health    = $wp_site_health->get_test_scheduled_events();
     22
     23                $this->assertSame( 'critical', $cron_health['status'] );
     24                $this->assertSame( __( 'It was not possible to check your scheduled events' ), $cron_health['label'] );
     25                $this->assertWPError( $wp_site_health->has_late_cron() );
     26                $this->assertWPError( $wp_site_health->has_missed_cron() );
     27        }
     28
     29        /**
     30         * Ensure Site Health reports correctly cron job reports.
     31         *
     32         * @dataProvider data_cron_health_checks
     33         * @ticket 47223
     34         */
     35        function test_cron_health_checks( $times, $expected_status, $expected_label, $expected_late, $expected_missed ) {
     36                /*
     37                 * Clear the cron array.
     38                 *
     39                 * The core jobs may register as late/missed in the test suite as they
     40                 * are not run. Clearing the array ensures the site health tests are only
     41                 * reported based on the jobs set in the test.
     42                 */
     43                _set_cron_array( array() );
     44                $times = (array) $times;
     45                foreach ( $times as $job => $time ) {
     46                        $timestamp = strtotime( $time );
     47                        wp_schedule_event( $timestamp, 'daily', __FUNCTION__ . "_{$job}" );
     48                }
     49
     50                $wp_site_health = new WP_Site_Health();
     51                $cron_health    = $wp_site_health->get_test_scheduled_events();
     52
     53                $this->assertSame( $expected_status, $cron_health['status'] );
     54                $this->assertSame( $expected_label, $cron_health['label'] );
     55                $this->assertSame( $expected_late, $wp_site_health->has_late_cron() );
     56                $this->assertSame( $expected_missed, $wp_site_health->has_missed_cron() );
     57        }
     58
     59        /**
     60         * Data provider for Site Health cron reports.
     61         *
     62         * The test suite runs with `DISABLE_WP_CRON === true` so the
     63         * missed and late tests need to account for the extended periods
     64         * allowed for with this flag enabled.
     65         *
     66         * 1. string|array Times to schedule (run through strtotime())
     67         * 2. string       Expected status
     68         * 3. string       Expected label
     69         * 4. bool         Expected outcome has_late_cron()
     70         * 5. bool         Expected outcome has_missed_cron()
     71         */
     72        function data_cron_health_checks() {
     73                return array(
     74                        array(
     75                                '+5 minutes',
     76                                'good',
     77                                __( 'Scheduled events are running' ),
     78                                false,
     79                                false,
     80                        ),
     81                        array(
     82                                '-50 minutes',
     83                                'recommended',
     84                                __( 'A scheduled event is late' ),
     85                                true,
     86                                false,
     87                        ),
     88                        array(
     89                                '-500 minutes',
     90                                'recommended',
     91                                __( 'A scheduled event has failed' ),
     92                                false,
     93                                true,
     94                        ),
     95                        array(
     96                                array(
     97                                        '-50 minutes',
     98                                        '-500 minutes',
     99                                ),
     100                                'recommended',
     101                                __( 'A scheduled event has failed' ),
     102                                true,
     103                                true,
     104                        ),
     105                );
     106        }
     107}