From ae7f39bab844fed61acb020b4cc18a8ea6339047 Mon Sep 17 00:00:00 2001
From: Michael Nelson <michael@eventespresso.com>
Date: Fri, 19 Sep 2014 12:17:24 -0700
Subject: [PATCH] clone the current_user before the first unit test, and then
restore the current_user to be a clone of it after each
unit test
---
tests/phpunit/includes/testcase.php | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php
index 1e8ea11..5e58d2e 100644
a
|
b
|
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
13 | 13 | |
14 | 14 | protected static $hooks_saved = array(); |
15 | 15 | protected static $ignore_files; |
| 16 | /** |
| 17 | * The current user, as it was before the first unit test. |
| 18 | * After each unit test $current_user gets restored to this. |
| 19 | * @var WP_User |
| 20 | */ |
| 21 | protected static $_original_current_user; |
16 | 22 | |
17 | 23 | /** |
18 | 24 | * @var WP_UnitTest_Factory |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
20 | 26 | protected $factory; |
21 | 27 | |
22 | 28 | function setUp() { |
| 29 | global $wpdb, $current_user; |
23 | 30 | set_time_limit(0); |
24 | 31 | |
25 | 32 | if ( ! self::$ignore_files ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
30 | 37 | $this->_backup_hooks(); |
31 | 38 | } |
32 | 39 | |
33 | | global $wpdb; |
| 40 | if ( ! self::$_original_current_user ){ |
| 41 | self::$_original_current_user = clone $current_user; |
| 42 | } |
| 43 | |
34 | 44 | $wpdb->suppress_errors = false; |
35 | 45 | $wpdb->show_errors = true; |
36 | 46 | $wpdb->db_connect(); |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
43 | 53 | } |
44 | 54 | |
45 | 55 | function tearDown() { |
46 | | global $wpdb, $wp_query, $post; |
| 56 | global $wpdb, $wp_query, $post, $current_user; |
47 | 57 | $this->expectedDeprecated(); |
48 | 58 | $wpdb->query( 'ROLLBACK' ); |
49 | 59 | if ( is_multisite() ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
58 | 68 | remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); |
59 | 69 | remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); |
60 | 70 | $this->_restore_hooks(); |
| 71 | $current_user = clone self::$_original_current_user; |
61 | 72 | } |
62 | 73 | |
63 | 74 | function clean_up_global_scope() { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
103 | 114 | } |
104 | 115 | } |
105 | 116 | } |
106 | | |
| 117 | |
107 | 118 | function flush_cache() { |
108 | 119 | global $wp_object_cache; |
109 | 120 | $wp_object_cache->group_ops = array(); |