Build/Test Tools: Reset main query object after each test.

This resets the main query variable, `$wp_the_query` during the `WP_UnitTestCase_Base::tear_down` method that runs after each PHPUnit test. This ensures any changes to the main query object is reset after each test to avoid cross contamination between tests, similar to how other globals are reset.

Props flixos90, spacedmonkey, joemcgill.
Fixes #58776.


git-svn-id: https://develop.svn.wordpress.org/trunk@56212 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe McGill
2023-07-11 13:31:03 +00:00
parent 8c1b5794e4
commit cb54fdb142
3 changed files with 305 additions and 305 deletions

598
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -142,15 +142,18 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
* After a test method runs, resets any state in WordPress the test method might have changed.
*/
public function tear_down() {
global $wpdb, $wp_query, $wp;
global $wpdb, $wp_the_query, $wp_query, $wp;
$wpdb->query( 'ROLLBACK' );
if ( is_multisite() ) {
while ( ms_is_switched() ) {
restore_current_blog();
}
}
$wp_query = new WP_Query();
$wp = new WP();
// Reset query, main query, and WP globals similar to wp-settings.php.
$wp_the_query = new WP_Query();
$wp_query = $wp_the_query;
$wp = new WP();
// Reset globals related to the post loop and `setup_postdata()`.
$post_globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );

View File

@@ -25,9 +25,6 @@ class Tests_Query_IsTerm extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
$GLOBALS['wp_the_query'] = new WP_Query();
$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
create_initial_taxonomies();