From c4c80d3dd43bf049362ee4d365fe19a97f5e695f Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 30 Sep 2016 03:15:36 +0000 Subject: [PATCH] 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 --- tests/phpunit/includes/testcase.php | 10 ++++++++-- tests/phpunit/tests/includes/helpers.php | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 1c4d758539..344f9eb331 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -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' ) ); diff --git a/tests/phpunit/tests/includes/helpers.php b/tests/phpunit/tests/includes/helpers.php index b035d31672..84ce8ce5b8 100644 --- a/tests/phpunit/tests/includes/helpers.php +++ b/tests/phpunit/tests/includes/helpers.php @@ -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 ); + } + } }