Skip to content

Commit c8ce572

Browse files
committed
Add setup and tear down for test cases
1 parent 116df23 commit c8ce572

1 file changed

Lines changed: 61 additions & 63 deletions

File tree

tests/phpunit/tests/admin/wpSiteHealth.php

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class Tests_Admin_wpSiteHealth extends WP_UnitTestCase {
1515
*/
1616
private WP_Site_Health $instance;
1717

18+
/**
19+
* Original error_log ini setting, restored after each test.
20+
*
21+
* @since 7.0.0
22+
* @var string|false
23+
*/
24+
private $original_error_log;
25+
1826
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
1927
// Include the `WP_Site_Health` file.
2028
require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
@@ -28,7 +36,18 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2836
public function set_up() {
2937
parent::set_up();
3038

31-
$this->instance = new WP_Site_Health();
39+
$this->instance = new WP_Site_Health();
40+
$this->original_error_log = ini_get( 'error_log' );
41+
}
42+
43+
/**
44+
* Tear down after each test.
45+
*
46+
* @since 7.0.0
47+
*/
48+
public function tear_down() {
49+
ini_set( 'error_log', $this->original_error_log );
50+
parent::tear_down();
3251
}
3352

3453
/**
@@ -741,27 +760,6 @@ private function setup_site_health_with_debug_properties( bool $wp_debug = false
741760
return $site_health;
742761
}
743762

744-
/**
745-
* Helper method to set error_log ini setting and restore it later.
746-
*
747-
* @param string $log_path Path to set for error_log.
748-
* @return string|false Original error_log value.
749-
*/
750-
private function set_error_log_path( string $log_path = '' ) {
751-
$original_error_log = ini_get( 'error_log' );
752-
ini_set( 'error_log', $log_path );
753-
return $original_error_log;
754-
}
755-
756-
/**
757-
* Helper method to restore error_log ini setting.
758-
*
759-
* @param string|false $original_value Original error_log value.
760-
*/
761-
private function restore_error_log_path( $original_value ): void {
762-
ini_set( 'error_log', $original_value );
763-
}
764-
765763
/**
766764
* Returns the expected result array when debug mode is disabled.
767765
*
@@ -873,12 +871,12 @@ public function test_is_in_debug_mode_disabled(): void {
873871
* @covers ::get_test_is_in_debug_mode()
874872
*/
875873
public function test_is_in_debug_mode_enabled_no_error_log(): void {
876-
$site_health = $this->setup_site_health_with_debug_properties( true, false, null );
877-
$original_error_log = $this->set_error_log_path( '' );
878-
$actual_result = $site_health->get_test_is_in_debug_mode();
879-
$expected_result = $this->get_debug_mode_disabled_result();
874+
$site_health = $this->setup_site_health_with_debug_properties( true, false, null );
880875

881-
$this->restore_error_log_path( $original_error_log );
876+
ini_set( 'error_log', '' );
877+
878+
$actual_result = $site_health->get_test_is_in_debug_mode();
879+
$expected_result = $this->get_debug_mode_disabled_result();
882880

883881
$this->assertSame( $expected_result['status'], $actual_result['status'], 'Status should be "good" when no error log is configured.' );
884882
$this->assertSame( $expected_result['label'], $actual_result['label'], 'Label should indicate no error log is configured.' );
@@ -894,13 +892,13 @@ public function test_is_in_debug_mode_enabled_no_error_log(): void {
894892
* @covers ::get_test_is_in_debug_mode()
895893
*/
896894
public function test_is_in_debug_mode_error_log_public(): void {
897-
$site_health = $this->setup_site_health_with_debug_properties( true, true, null );
898-
$public_log_path = ABSPATH . 'wp-content/debug.log';
899-
$original_error_log = $this->set_error_log_path( $public_log_path );
900-
$actual_result = $site_health->get_test_is_in_debug_mode();
901-
$expected_result = $this->get_debug_error_log_public_result( true );
895+
$site_health = $this->setup_site_health_with_debug_properties( true, true, null );
896+
$public_log_path = ABSPATH . 'wp-content/debug.log';
897+
898+
ini_set( 'error_log', $public_log_path );
902899

903-
$this->restore_error_log_path( $original_error_log );
900+
$actual_result = $site_health->get_test_is_in_debug_mode();
901+
$expected_result = $this->get_debug_error_log_public_result( true );
904902

905903
$this->assertSame( $expected_result['status'], $actual_result['status'], 'Status should be "critical" when error log is in a public location.' );
906904
$this->assertSame( $expected_result['label'], $actual_result['label'], 'Label should indicate error log is in a public location.' );
@@ -915,13 +913,13 @@ public function test_is_in_debug_mode_error_log_public(): void {
915913
* @covers ::get_test_is_in_debug_mode()
916914
*/
917915
public function test_is_in_debug_mode_error_log_public_without_wp_debug_log(): void {
918-
$site_health = $this->setup_site_health_with_debug_properties( true, false, null );
919-
$public_log_path = ABSPATH . 'wp-content/debug.log';
920-
$original_error_log = $this->set_error_log_path( $public_log_path );
921-
$actual_result = $site_health->get_test_is_in_debug_mode();
922-
$expected_result = $this->get_debug_error_log_public_result( false );
916+
$site_health = $this->setup_site_health_with_debug_properties( true, false, null );
917+
$public_log_path = ABSPATH . 'wp-content/debug.log';
918+
919+
ini_set( 'error_log', $public_log_path );
923920

924-
$this->restore_error_log_path( $original_error_log );
921+
$actual_result = $site_health->get_test_is_in_debug_mode();
922+
$expected_result = $this->get_debug_error_log_public_result( false );
925923

926924
$this->assertSame( $expected_result['status'], $actual_result['status'], 'Status should be "critical" when error log is in a public location.' );
927925
$this->assertSame( $expected_result['label'], $actual_result['label'], 'Label should indicate error log is in a public location.' );
@@ -936,13 +934,13 @@ public function test_is_in_debug_mode_error_log_public_without_wp_debug_log(): v
936934
* @covers ::get_test_is_in_debug_mode()
937935
*/
938936
public function test_is_in_debug_mode_error_log_private(): void {
939-
$site_health = $this->setup_site_health_with_debug_properties( true, true, null );
940-
$private_log_path = '/var/log/php-error.log';
941-
$original_error_log = $this->set_error_log_path( $private_log_path );
942-
$actual_result = $site_health->get_test_is_in_debug_mode();
943-
$expected_result = $this->get_debug_error_log_private_result( true );
937+
$site_health = $this->setup_site_health_with_debug_properties( true, true, null );
938+
$private_log_path = '/var/log/php-error.log';
939+
940+
ini_set( 'error_log', $private_log_path );
944941

945-
$this->restore_error_log_path( $original_error_log );
942+
$actual_result = $site_health->get_test_is_in_debug_mode();
943+
$expected_result = $this->get_debug_error_log_private_result( true );
946944

947945
$this->assertSame( $expected_result['status'], $actual_result['status'], 'Status should be "good" when error log is in a private location.' );
948946
$this->assertSame( $expected_result['label'], $actual_result['label'], 'Label should indicate error log is in a private location.' );
@@ -957,13 +955,13 @@ public function test_is_in_debug_mode_error_log_private(): void {
957955
* @covers ::get_test_is_in_debug_mode()
958956
*/
959957
public function test_is_in_debug_mode_error_log_private_without_wp_debug_log(): void {
960-
$site_health = $this->setup_site_health_with_debug_properties( true, false, null );
961-
$private_log_path = '/var/log/php-error.log';
962-
$original_error_log = $this->set_error_log_path( $private_log_path );
963-
$actual_result = $site_health->get_test_is_in_debug_mode();
964-
$expected_result = $this->get_debug_error_log_private_result( false );
958+
$site_health = $this->setup_site_health_with_debug_properties( true, false, null );
959+
$private_log_path = '/var/log/php-error.log';
965960

966-
$this->restore_error_log_path( $original_error_log );
961+
ini_set( 'error_log', $private_log_path );
962+
963+
$actual_result = $site_health->get_test_is_in_debug_mode();
964+
$expected_result = $this->get_debug_error_log_private_result( false );
967965

968966
$this->assertSame( $expected_result['status'], $actual_result['status'], 'Status should be "good" when error log is in a private location.' );
969967
$this->assertSame( $expected_result['label'], $actual_result['label'], 'Label should indicate error log is in a private location.' );
@@ -978,13 +976,13 @@ public function test_is_in_debug_mode_error_log_private_without_wp_debug_log():
978976
* @covers ::get_test_is_in_debug_mode()
979977
*/
980978
public function test_is_in_debug_mode_error_log_non_existent(): void {
981-
$site_health = $this->setup_site_health_with_debug_properties( true, true, null );
982-
$invalid_log_path = '/nonexistent/path/that/does/not/exist/debug.log';
983-
$original_error_log = $this->set_error_log_path( $invalid_log_path );
984-
$actual_result = $site_health->get_test_is_in_debug_mode();
985-
$expected_result = $this->get_debug_log_non_existent_path_result( true );
979+
$site_health = $this->setup_site_health_with_debug_properties( true, true, null );
980+
$invalid_log_path = '/nonexistent/path/that/does/not/exist/debug.log';
986981

987-
$this->restore_error_log_path( $original_error_log );
982+
ini_set( 'error_log', $invalid_log_path );
983+
984+
$actual_result = $site_health->get_test_is_in_debug_mode();
985+
$expected_result = $this->get_debug_log_non_existent_path_result( true );
988986

989987
$this->assertSame( $expected_result['status'], $actual_result['status'], 'Status should be "critical" when error log location cannot be determined.' );
990988
$this->assertSame( $expected_result['label'], $actual_result['label'], 'Label should indicate that error log location could not be determined.' );
@@ -999,13 +997,13 @@ public function test_is_in_debug_mode_error_log_non_existent(): void {
999997
* @covers ::get_test_is_in_debug_mode()
1000998
*/
1001999
public function test_is_in_debug_mode_error_log_non_existent_without_wp_debug_log(): void {
1002-
$site_health = $this->setup_site_health_with_debug_properties( true, false, null );
1003-
$invalid_log_path = '/nonexistent/path/that/does/not/exist/debug.log';
1004-
$original_error_log = $this->set_error_log_path( $invalid_log_path );
1005-
$actual_result = $site_health->get_test_is_in_debug_mode();
1006-
$expected_result = $this->get_debug_log_non_existent_path_result( false );
1000+
$site_health = $this->setup_site_health_with_debug_properties( true, false, null );
1001+
$invalid_log_path = '/nonexistent/path/that/does/not/exist/debug.log';
10071002

1008-
$this->restore_error_log_path( $original_error_log );
1003+
ini_set( 'error_log', $invalid_log_path );
1004+
1005+
$actual_result = $site_health->get_test_is_in_debug_mode();
1006+
$expected_result = $this->get_debug_log_non_existent_path_result( false );
10091007

10101008
$this->assertSame( $expected_result['status'], $actual_result['status'], 'Status should be "critical" when error log location cannot be determined.' );
10111009
$this->assertSame( $expected_result['label'], $actual_result['label'], 'Label should indicate that error log location could not be determined.' );

0 commit comments

Comments
 (0)