Make WordPress Core

Changeset 38678


Ignore:
Timestamp:
09/30/2016 03:15:36 AM (8 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk/tests/phpunit
Files:
2 edited

Legend:

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

    r38571 r38678  
    143143     */
    144144    function tearDown() {
    145         global $wpdb, $wp_query, $wp, $post;
     145        global $wpdb, $wp_query, $wp;
    146146        $wpdb->query( 'ROLLBACK' );
    147147        if ( is_multisite() ) {
     
    152152        $wp_query = new WP_Query();
    153153        $wp = new WP();
    154         $post = null;
     154
     155        // Reset globals related to the post loop and `setup_postdata()`.
     156        $post_globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
     157        foreach ( $post_globals as $global ) {
     158            $GLOBALS[ $global ] = null;
     159        }
     160
    155161        remove_theme_support( 'html5' );
    156162        remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
  • trunk/tests/phpunit/tests/includes/helpers.php

    r37071 r38678  
    213213        wp_die( new WP_Error( 'test', 'test' ) );
    214214    }
     215
     216    /**
     217     * This test is just a setup for the one that follows.
     218     *
     219     * @ticket 38196
     220     */
     221    public function test_setup_postdata_globals_should_be_reset_on_teardown__setup() {
     222        $post = self::factory()->post->create_and_get();
     223        $GLOBALS['wp_query'] = new WP_Query();
     224        $GLOBALS['wp_query']->setup_postdata( $post );
     225        $this->assertNotEmpty( $post );
     226    }
     227
     228    /**
     229     * @ticket 38196
     230     */
     231    public function test_setup_postdata_globals_should_be_reset_on_teardown() {
     232        $globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
     233
     234        foreach ( $globals as $global ) {
     235            $this->assertTrue( ! isset( $GLOBALS[ $global ] ), $global );
     236        }
     237    }
    215238}
Note: See TracChangeset for help on using the changeset viewer.