Reset post types and taxonomies before each unit test.

Registering a post type or taxonomy during a unit test causes modifications to
global variables. If the test fails to clean up these globals - either by
neglecting to call _unregister_post_type()/_unregister_taxonomy() at all or by
failing before getting a chance to do so - tests that run later in the suite
can fail, leading to much gnashing of teeth. Wiping all taxonomies and
restoring to the defaults before each test ensures that we always start with a
clean slate.

Fixes #29827.

git-svn-id: https://develop.svn.wordpress.org/trunk@29860 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-10-09 00:57:26 +00:00
parent 91a3b376de
commit 5e7be10860
1 changed files with 30 additions and 0 deletions

View File

@ -37,6 +37,8 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
ini_set('display_errors', 1 ); ini_set('display_errors', 1 );
$this->factory = new WP_UnitTest_Factory; $this->factory = new WP_UnitTest_Factory;
$this->clean_up_global_scope(); $this->clean_up_global_scope();
$this->reset_post_types();
$this->reset_taxonomies();
$this->start_transaction(); $this->start_transaction();
$this->expectDeprecated(); $this->expectDeprecated();
add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
@ -66,6 +68,34 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$this->flush_cache(); $this->flush_cache();
} }
/**
* Unregister existing post types and register defaults.
*
* Run before each test in order to clean up the global scope, in case
* a test forgets to unregister a post type on its own, or fails before
* it has a chance to do so.
*/
protected function reset_post_types() {
foreach ( get_post_types() as $pt ) {
_unregister_post_type( $pt );
}
create_initial_post_types();
}
/**
* Unregister existing taxonomies and register defaults.
*
* Run before each test in order to clean up the global scope, in case
* a test forgets to unregister a taxonomy on its own, or fails before
* it has a chance to do so.
*/
protected function reset_taxonomies() {
foreach ( get_taxonomies() as $tax ) {
_unregister_taxonomy( $tax );
}
create_initial_taxonomies();
}
/** /**
* Saves the action and filter-related globals so they can be restored later. * Saves the action and filter-related globals so they can be restored later.
* *