Tests: Move Site Health unit test class to phpunit/tests/admin/.

Includes:
* Renaming the test class per the [https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#naming-and-organization naming conventions].
* Creating a `WP_Site_Health` instance in the `set_up()` method, instead of leaving that to each individual test.

This brings some consistency with the tests for other admin classes, e.g. `WP_Community_Events`.

Follow-up to [45802], [51639].

See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@54075 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-09-05 22:25:54 +00:00
parent 98cf070991
commit af52988c22
2 changed files with 44 additions and 35 deletions

View File

@ -16,10 +16,10 @@
* @since 4.8.0
*/
class Tests_Admin_wpCommunityEvents extends WP_UnitTestCase {
/**
* An instance of the class to test.
*
* @access private
* @since 4.8.0
*
* @var WP_Community_Events

View File

@ -5,12 +5,33 @@
*
* @coversDefaultClass WP_Site_Health
*/
class Tests_Site_Health extends WP_UnitTestCase {
class Tests_Admin_wpSiteHealth extends WP_UnitTestCase {
/**
* An instance of the class to test.
*
* @since 6.1.0
*
* @var WP_Site_Health
*/
private $instance;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// Include the `WP_Site_Health` file.
require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
}
/**
* Performs setup tasks for every test.
*
* @since 6.1.0
*/
public function set_up() {
parent::set_up();
$this->instance = new WP_Site_Health();
}
/**
* @ticket 55791
* @covers ::prepare_sql_data()
@ -20,10 +41,9 @@ class Tests_Site_Health extends WP_UnitTestCase {
// This test is designed to only run on trunk.
$this->skipOnAutomatedBranches();
$wp_site_health = new WP_Site_Health();
$wp_site_health->get_test_sql_server();
$this->instance->get_test_sql_server();
$reflection = new ReflectionClass( $wp_site_health );
$reflection = new ReflectionClass( $this->instance );
$reflection_property = $reflection->getProperty( 'mysql_recommended_version' );
$reflection_property->setAccessible( true );
@ -31,7 +51,7 @@ class Tests_Site_Health extends WP_UnitTestCase {
preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]*)#s', $readme, $matches );
$this->assertSame( $matches[1], $reflection_property->getValue( $wp_site_health ) );
$this->assertSame( $matches[1], $reflection_property->getValue( $this->instance ) );
}
/**
@ -43,10 +63,9 @@ class Tests_Site_Health extends WP_UnitTestCase {
// This test is designed to only run on trunk.
$this->skipOnAutomatedBranches();
$wp_site_health = new WP_Site_Health();
$wp_site_health->get_test_sql_server();
$this->instance->get_test_sql_server();
$reflection = new ReflectionClass( $wp_site_health );
$reflection = new ReflectionClass( $this->instance );
$reflection_property = $reflection->getProperty( 'mariadb_recommended_version' );
$reflection_property->setAccessible( true );
@ -54,7 +73,7 @@ class Tests_Site_Health extends WP_UnitTestCase {
preg_match( '#Recommendations.*MariaDB</a> version <strong>([0-9.]*)#s', $readme, $matches );
$this->assertSame( $matches[1], $reflection_property->getValue( $wp_site_health ) );
$this->assertSame( $matches[1], $reflection_property->getValue( $this->instance ) );
}
/**
@ -63,17 +82,15 @@ class Tests_Site_Health extends WP_UnitTestCase {
* @ticket 47223
*/
public 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();
$cron_health = $this->instance->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() );
$this->assertWPError( $this->instance->has_late_cron() );
$this->assertWPError( $this->instance->has_missed_cron() );
}
/**
@ -83,8 +100,6 @@ class Tests_Site_Health extends WP_UnitTestCase {
* @ticket 47223
*/
public 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.
*
@ -93,18 +108,19 @@ class Tests_Site_Health extends WP_UnitTestCase {
* 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();
$cron_health = $this->instance->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() );
$this->assertSame( $expected_late, $this->instance->has_late_cron() );
$this->assertSame( $expected_missed, $this->instance->has_missed_cron() );
}
/**
@ -165,8 +181,6 @@ class Tests_Site_Health extends WP_UnitTestCase {
* @covers ::check_for_page_caching()
*/
public function test_get_page_cache( $responses, $expected_status, $expected_label, $good_basic_auth = null, $delay_the_response = false ) {
$wp_site_health = new WP_Site_Health();
$expected_props = array(
'badge' => array(
'label' => __( 'Performance' ),
@ -234,16 +248,17 @@ class Tests_Site_Health extends WP_UnitTestCase {
2
);
$actual = $wp_site_health->get_test_page_cache();
$actual = $this->instance->get_test_page_cache();
$this->assertArrayHasKey( 'description', $actual );
$this->assertArrayHasKey( 'actions', $actual );
if ( $is_unauthorized ) {
$this->assertStringContainsString( 'Unauthorized', $actual['description'] );
} else {
$this->assertStringNotContainsString( 'Unauthorized', $actual['description'] );
}
$this->assertEquals(
$this->assertSame(
$expected_props,
wp_array_slice_assoc( $actual, array_keys( $expected_props ) )
);
@ -409,8 +424,6 @@ class Tests_Site_Health extends WP_UnitTestCase {
* @ticket 56040
*/
public function test_object_cache_default_thresholds_non_multisite() {
$wp_site_health = new WP_Site_Health();
// Set thresholds so high they should never be exceeded.
add_filter(
'site_status_persistent_object_cache_thresholds',
@ -428,7 +441,7 @@ class Tests_Site_Health extends WP_UnitTestCase {
);
$this->assertFalse(
$wp_site_health->should_suggest_persistent_object_cache()
$this->instance->should_suggest_persistent_object_cache()
);
}
@ -438,9 +451,8 @@ class Tests_Site_Health extends WP_UnitTestCase {
* @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()
$this->instance->should_suggest_persistent_object_cache()
);
}
@ -448,16 +460,14 @@ class Tests_Site_Health extends WP_UnitTestCase {
* @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()
$this->instance->should_suggest_persistent_object_cache()
);
add_filter( 'site_status_should_suggest_persistent_object_cache', '__return_false', 11 );
$this->assertFalse(
$wp_site_health->should_suggest_persistent_object_cache()
$this->instance->should_suggest_persistent_object_cache()
);
}
@ -466,7 +476,6 @@ class Tests_Site_Health extends WP_UnitTestCase {
* @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 ) {
@ -475,7 +484,7 @@ class Tests_Site_Health extends WP_UnitTestCase {
);
$this->assertTrue(
$wp_site_health->should_suggest_persistent_object_cache()
$this->instance->should_suggest_persistent_object_cache()
);
}