From 5321bc4957e6ae10521b7a17ee7b646bb7f4552e Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 4 Nov 2013 22:46:44 +0000 Subject: [PATCH] `WP_UnitTestCase::go_to()` tried its best to clean up global space, but ultimately fell short. Because it was blowing away `WP` every time it was called, it was dropping all the query vars that were registered for custom taxonomies and custom post types (ouch). Introduces `_cleanup_query_vars()`. This is a prerequisite for the unit tests on #20767. All unit tests pass with this change. See #20767. Fixes #25818. git-svn-id: https://develop.svn.wordpress.org/trunk@26006 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 2 +- tests/phpunit/includes/testcase.php | 9 +-------- tests/phpunit/includes/utils.php | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index fabf3f7983..2fe76fc8b7 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -605,7 +605,7 @@ class WP { $this->query_posts(); $this->handle_404(); $this->register_globals(); - + /** * Fires once the WordPress environment has been set up. * diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 2b082abffe..2dbe04de71 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -186,14 +186,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $GLOBALS['wp_the_query'] = new WP_Query(); $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; $GLOBALS['wp'] = new WP(); - - // clean out globals to stop them polluting wp and wp_query - foreach ($GLOBALS['wp']->public_query_vars as $v) { - unset($GLOBALS[$v]); - } - foreach ($GLOBALS['wp']->private_query_vars as $v) { - unset($GLOBALS[$v]); - } + _cleanup_query_vars(); $GLOBALS['wp']->main($parts['query']); } diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index b57cfcc522..dc2fde4d1d 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -363,3 +363,22 @@ function _unregister_post_type( $cpt_name ) { function _unregister_taxonomy( $taxonomy_name ) { unset( $GLOBALS['wp_taxonomies'][$taxonomy_name] ); } + +function _cleanup_query_vars() { + // clean out globals to stop them polluting wp and wp_query + foreach ( $GLOBALS['wp']->public_query_vars as $v ) + unset( $GLOBALS[$v] ); + + foreach ( $GLOBALS['wp']->private_query_vars as $v ) + unset( $GLOBALS[$v] ); + + foreach ( get_taxonomies( array() , 'objects' ) as $t ) { + if ( ! empty( $t->query_var ) ) + $GLOBALS['wp']->add_query_var( $t->query_var ); + } + + foreach ( get_post_types( array() , 'objects' ) as $t ) { + if ( ! empty( $t->query_var ) ) + $GLOBALS['wp']->add_query_var( $t->query_var ); + } +} \ No newline at end of file