From 68ddef1190e3f0340a14598fae03dc623af7b9a7 Mon Sep 17 00:00:00 2001 From: Shail Mehta Date: Mon, 19 Oct 2026 12:33:56 +0530 Subject: [PATCH 1/2] Tests: Add unit tests for `wp_is_maintenance_mode()` --- .../tests/load/wpIsMaintenanceMode.php | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 tests/phpunit/tests/load/wpIsMaintenanceMode.php diff --git a/tests/phpunit/tests/load/wpIsMaintenanceMode.php b/tests/phpunit/tests/load/wpIsMaintenanceMode.php new file mode 100644 index 0000000000000..5df051929835d --- /dev/null +++ b/tests/phpunit/tests/load/wpIsMaintenanceMode.php @@ -0,0 +1,169 @@ +original_upgrading = $GLOBALS['upgrading'] ?? null; + $this->original_installing = wp_installing(); + + foreach ( array( 'wp_scrape_key', 'wp_scrape_nonce' ) as $key ) { + $this->original_scrape_request[ $key ] = array_key_exists( $key, $_REQUEST ) ? $_REQUEST[ $key ] : null; + } + + $this->delete_maintenance_file(); + } + + public function tear_down() { + $this->delete_maintenance_file(); + + if ( null !== $this->original_upgrading ) { + $GLOBALS['upgrading'] = $this->original_upgrading; + } else { + unset( $GLOBALS['upgrading'] ); + } + + wp_installing( $this->original_installing ); + + foreach ( $this->original_scrape_request as $key => $value ) { + if ( null !== $value ) { + $_REQUEST[ $key ] = $value; + } else { + unset( $_REQUEST[ $key ] ); + } + } + + parent::tear_down(); + } + + /** + * Writes a `.maintenance` file with the given upgrading timestamp. + * + * @param int $upgrading Unix timestamp. + */ + private function write_maintenance_file( $upgrading ) { + file_put_contents( ABSPATH . '.maintenance', 'assertFalse( wp_is_maintenance_mode() ); + } + + /** + * @ticket 65819 + */ + public function test_returns_true_when_maintenance_file_is_fresh() { + $this->write_maintenance_file( time() ); + + $this->assertTrue( wp_is_maintenance_mode() ); + } + + /** + * @ticket 65819 + */ + public function test_returns_false_when_maintenance_file_is_expired() { + $this->write_maintenance_file( time() - ( 10 * MINUTE_IN_SECONDS ) ); + + $this->assertFalse( wp_is_maintenance_mode() ); + } + + /** + * @ticket 65819 + */ + public function test_returns_false_while_installing() { + $this->write_maintenance_file( time() ); + wp_installing( true ); + + $this->assertFalse( wp_is_maintenance_mode() ); + } + + /** + * @ticket 65819 + */ + public function test_returns_false_when_scraping_fatal_errors() { + $upgrading = time(); + + $this->write_maintenance_file( $upgrading ); + + $_REQUEST['wp_scrape_key'] = md5( $upgrading ); + $_REQUEST['wp_scrape_nonce'] = (string) $upgrading; + + $this->assertFalse( wp_is_maintenance_mode() ); + } + + /** + * @ticket 65819 + */ + public function test_returns_true_when_scrape_credentials_do_not_match() { + $upgrading = time(); + + $this->write_maintenance_file( $upgrading ); + + $_REQUEST['wp_scrape_key'] = 'invalid'; + $_REQUEST['wp_scrape_nonce'] = '0'; + + $this->assertTrue( wp_is_maintenance_mode() ); + } + + /** + * @ticket 65819 + */ + public function test_filter_can_disable_maintenance_mode() { + $upgrading = time(); + + $this->write_maintenance_file( $upgrading ); + + $filter = new MockAction(); + add_filter( 'enable_maintenance_mode', array( $filter, 'filter' ), 10, 2 ); + add_filter( 'enable_maintenance_mode', '__return_false', 20 ); + + $this->assertFalse( wp_is_maintenance_mode() ); + $this->assertSame( + array( array( true, $upgrading ) ), + $filter->get_args() + ); + } +} From ca993d2e3cb2eef1e52b984883be1ffd403226b3 Mon Sep 17 00:00:00 2001 From: Shail Mehta Date: Mon, 19 Oct 2026 17:51:29 +0530 Subject: [PATCH 2/2] Apply Feedback changes --- tests/phpunit/tests/load/wpIsMaintenanceMode.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/phpunit/tests/load/wpIsMaintenanceMode.php b/tests/phpunit/tests/load/wpIsMaintenanceMode.php index 5df051929835d..88b9516bbcc6b 100644 --- a/tests/phpunit/tests/load/wpIsMaintenanceMode.php +++ b/tests/phpunit/tests/load/wpIsMaintenanceMode.php @@ -68,6 +68,14 @@ public function tear_down() { /** * Writes a `.maintenance` file with the given upgrading timestamp. * + * Note: wp_is_maintenance_mode() hard-codes the path to ABSPATH . '.maintenance', + * so this writes a real file there rather than into a temporary directory. + * tear_down() removes it. If a test run stops partway through (for example, + * a fatal error or an interrupted process), the file can be left behind, and + * the next WordPress bootstrap will stop in maintenance mode until the + * timestamp expires (up to 10 minutes). If that happens, delete the + * `.maintenance` file from the WordPress root manually. + * * @param int $upgrading Unix timestamp. */ private function write_maintenance_file( $upgrading ) {