Make WordPress Core


Ignore:
Timestamp:
07/11/2023 01:31:03 PM (11 months ago)
Author:
joemcgill
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r55940 r56212  
    143143     */
    144144    public function tear_down() {
    145         global $wpdb, $wp_query, $wp;
     145        global $wpdb, $wp_the_query, $wp_query, $wp;
    146146        $wpdb->query( 'ROLLBACK' );
    147147        if ( is_multisite() ) {
     
    150150            }
    151151        }
    152         $wp_query = new WP_Query();
    153         $wp       = new WP();
     152
     153        // Reset query, main query, and WP globals similar to wp-settings.php.
     154        $wp_the_query = new WP_Query();
     155        $wp_query     = $wp_the_query;
     156        $wp           = new WP();
    154157
    155158        // Reset globals related to the post loop and `setup_postdata()`.
Note: See TracChangeset for help on using the changeset viewer.