Tests: Reset post-related globals after each test.

Globals like `$pages` were leaking between tests, resulting in various
bits of weirdness.

Globals will kill WordPress. Globals are killing WordPress.

See #38196.

git-svn-id: https://develop.svn.wordpress.org/trunk@38678 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-09-30 03:15:36 +00:00
parent 907b5fc527
commit c4c80d3dd4
2 changed files with 31 additions and 2 deletions

View File

@ -142,7 +142,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
* After a test method runs, reset any state in WordPress the test method might have changed.
*/
function tearDown() {
global $wpdb, $wp_query, $wp, $post;
global $wpdb, $wp_query, $wp;
$wpdb->query( 'ROLLBACK' );
if ( is_multisite() ) {
while ( ms_is_switched() ) {
@ -151,7 +151,13 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
$wp_query = new WP_Query();
$wp = new WP();
$post = null;
// Reset globals related to the post loop and `setup_postdata()`.
$post_globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
foreach ( $post_globals as $global ) {
$GLOBALS[ $global ] = null;
}
remove_theme_support( 'html5' );
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );

View File

@ -212,4 +212,27 @@ class Tests_TestHelpers extends WP_UnitTestCase {
public function test_die_handler_should_handle_wp_error() {
wp_die( new WP_Error( 'test', 'test' ) );
}
/**
* This test is just a setup for the one that follows.
*
* @ticket 38196
*/
public function test_setup_postdata_globals_should_be_reset_on_teardown__setup() {
$post = self::factory()->post->create_and_get();
$GLOBALS['wp_query'] = new WP_Query();
$GLOBALS['wp_query']->setup_postdata( $post );
$this->assertNotEmpty( $post );
}
/**
* @ticket 38196
*/
public function test_setup_postdata_globals_should_be_reset_on_teardown() {
$globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
foreach ( $globals as $global ) {
$this->assertTrue( ! isset( $GLOBALS[ $global ] ), $global );
}
}
}