Wordpress/tests/phpunit/tests/site-health.php
Sergey Biryukov e72fff9cef Code Modernization: Replace dirname( __FILE__ ) calls with __DIR__ magic constant.
This avoids the performance overhead of the function call every time `dirname( __FILE__ )` was used instead of `__DIR__`.

This commit also includes:

* Removing unnecessary parentheses from `include`/`require` statements. These are language constructs, not function calls.
* Replacing `include` statements for several files with `require_once`, for consistency:
 * `wp-admin/admin-header.php`
 * `wp-admin/admin-footer.php`
 * `wp-includes/version.php`

Props ayeshrajans, desrosj, valentinbora, jrf, joostdevalk, netweb.
Fixes #48082.

git-svn-id: https://develop.svn.wordpress.org/trunk@47198 602fd350-edb4-49c9-b593-d223f7449a82
2020-02-06 06:31:22 +00:00

111 lines
2.9 KiB
PHP

<?php
/**
* @group site-health
*/
class Tests_Site_Health extends WP_UnitTestCase {
public static function wpSetUpBeforeClass() {
// Include the `WP_Site_Health` file.
require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
}
/**
* Ensure Site Health reports correctly cron job reports.
*
* @ticket 47223
*/
function test_cron_health_checks_critical() {
$wp_site_health = new WP_Site_Health();
// Clear the cron array.
_set_cron_array( array() );
$cron_health = $wp_site_health->get_test_scheduled_events();
$this->assertSame( 'critical', $cron_health['status'] );
$this->assertSame( __( 'It was not possible to check your scheduled events' ), $cron_health['label'] );
$this->assertWPError( $wp_site_health->has_late_cron() );
$this->assertWPError( $wp_site_health->has_missed_cron() );
}
/**
* Ensure Site Health reports correctly cron job reports.
*
* @dataProvider data_cron_health_checks
* @ticket 47223
*/
function test_cron_health_checks( $times, $expected_status, $expected_label, $expected_late, $expected_missed ) {
$wp_site_health = new WP_Site_Health();
/*
* Clear the cron array.
*
* The core jobs may register as late/missed in the test suite as they
* are not run. Clearing the array ensures the site health tests are only
* reported based on the jobs set in the test.
*/
_set_cron_array( array() );
$times = (array) $times;
foreach ( $times as $job => $time ) {
$timestamp = strtotime( $time );
wp_schedule_event( $timestamp, 'daily', __FUNCTION__ . "_{$job}" );
}
$cron_health = $wp_site_health->get_test_scheduled_events();
$this->assertSame( $expected_status, $cron_health['status'] );
$this->assertSame( $expected_label, $cron_health['label'] );
$this->assertSame( $expected_late, $wp_site_health->has_late_cron() );
$this->assertSame( $expected_missed, $wp_site_health->has_missed_cron() );
}
/**
* Data provider for Site Health cron reports.
*
* The test suite runs with `DISABLE_WP_CRON === true` so the
* missed and late tests need to account for the extended periods
* allowed for with this flag enabled.
*
* 1. string|array Times to schedule (run through strtotime())
* 2. string Expected status
* 3. string Expected label
* 4. bool Expected outcome has_late_cron()
* 5. bool Expected outcome has_missed_cron()
*/
function data_cron_health_checks() {
return array(
array(
'+5 minutes',
'good',
__( 'Scheduled events are running' ),
false,
false,
),
array(
'-50 minutes',
'recommended',
__( 'A scheduled event is late' ),
true,
false,
),
array(
'-500 minutes',
'recommended',
__( 'A scheduled event has failed' ),
false,
true,
),
array(
array(
'-50 minutes',
'-500 minutes',
),
'recommended',
__( 'A scheduled event has failed' ),
true,
true,
),
);
}
}