From 36e455d018c3505efe23e59985b2d40d076a090c Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 19 Jul 2014 23:58:07 +0000 Subject: [PATCH] Backup filter globals ( `$merged_filters, $wp_actions, $wp_current_filter, $wp_filter` ) statically when running unit tests, restore on `tearDown()`. This ensures that all tests initially use the same filters/actions. Props mnelson4, wonderboymusic. Fixes #28535. git-svn-id: https://develop.svn.wordpress.org/trunk@29251 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/testcase.php | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 1f6a29f91d..ba66439798 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -11,6 +11,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { protected $expected_doing_it_wrong = array(); protected $caught_doing_it_wrong = array(); + protected static $hooks_saved = array(); protected static $ignore_files; /** @@ -25,6 +26,10 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { self::$ignore_files = $this->scan_user_uploads(); } + if ( ! self::$hooks_saved ) { + $this->_backup_hooks(); + } + global $wpdb; $wpdb->suppress_errors = false; $wpdb->show_errors = true; @@ -52,6 +57,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); + $this->_restore_hooks(); } function clean_up_global_scope() { @@ -60,6 +66,42 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $this->flush_cache(); } + /** + * Saves the action and filter-related globals so they can be restored later + * + * Stores $merged_filters, $wp_actions, $wp_current_filter, and $wp_filter + * on a class variable so they can be restored on tearDown() using _restore_hooks() + * @global array $merged_filters + * @global array $wp_actions + * @global array $wp_current_filter + * @global array $wp_filter + * @return void + */ + protected function _backup_hooks() { + $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' ); + foreach ( $globals as $key ) { + self::$hooks_saved[ $key ] = $GLOBALS[ $key ]; + } + } + + /** + * Restores the hook-related globals to their state at setUp() + * + * so that future tests aren't affected by hooks set during this last test + * + * @global array $merged_filters + * @global array $wp_actions + * @global array $wp_current_filter + * @global array $wp_filter + * @return void + */ + protected function _restore_hooks(){ + $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' ); + foreach ( $globals as $key ) { + $GLOBALS[ $key ] = self::$hooks_saved[ $key ]; + } + } + function flush_cache() { global $wp_object_cache; $wp_object_cache->group_ops = array();