Repository navigation
Tests: Add unit tests for wp_is_maintenance_mode()
#14474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
shail-mehta
wants to merge
2
commits into
WordPress:trunk
from
shail-mehta:65819-add-wp-is-maintenance-mode-tests
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for wp_is_maintenance_mode(). | ||
| * | ||
| * @group load | ||
| * | ||
| * @covers ::wp_is_maintenance_mode | ||
| */ | ||
| class Tests_Load_WpIsMaintenanceMode extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Original $upgrading global. | ||
| * | ||
| * @var mixed | ||
| */ | ||
| private $original_upgrading; | ||
|
|
||
| /** | ||
| * Original installing status. | ||
| * | ||
| * @var bool | ||
| */ | ||
| private $original_installing; | ||
|
|
||
| /** | ||
| * Original scrape request values. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $original_scrape_request = array(); | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
|
|
||
| $this->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. | ||
| * | ||
| * 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 ) { | ||
| file_put_contents( ABSPATH . '.maintenance', '<?php $upgrading = ' . (int) $upgrading . ";\n" ); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes the `.maintenance` file if it exists. | ||
| */ | ||
| private function delete_maintenance_file() { | ||
| $file = ABSPATH . '.maintenance'; | ||
|
|
||
| if ( file_exists( $file ) ) { | ||
| unlink( $file ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 65819 | ||
| */ | ||
| public function test_returns_false_when_maintenance_file_is_missing() { | ||
| $this->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() | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional note: this writes a real
.maintenancefile intoABSPATH, becausewp_is_maintenance_mode()hard-codes that path.tear_down()removes it. If a run stops partway, though, the file stays, and the next bootstrap would stop in maintenance mode for up to 10 minutes. A short comment here would help the next person who sees that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in ca993d2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in ca993d2