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/tests/phpunit/includes/testcase.php
+++ b/tests/phpunit/includes/testcase.php
@@ -13,6 +13,12 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
 
 	protected static $hooks_saved = array();
 	protected static $ignore_files;
+	/**
+	 * The current user, as it was before the first unit test.
+	 * After each unit test $current_user gets restored to this.
+	 * @var WP_User
+	 */
+	protected static $_original_current_user;
 
 	/**
 	 * @var WP_UnitTest_Factory
@@ -20,6 +26,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
 	protected $factory;
 
 	function setUp() {
+		global $wpdb, $current_user;
 		set_time_limit(0);
 
 		if ( ! self::$ignore_files ) {
@@ -30,7 +37,10 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
 			$this->_backup_hooks();
 		}
 
-		global $wpdb;
+		if ( ! self::$_original_current_user ){
+			self::$_original_current_user = clone $current_user;
+		}
+
 		$wpdb->suppress_errors = false;
 		$wpdb->show_errors = true;
 		$wpdb->db_connect();
@@ -43,7 +53,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
 	}
 
 	function tearDown() {
-		global $wpdb, $wp_query, $post;
+		global $wpdb, $wp_query, $post, $current_user;
 		$this->expectedDeprecated();
 		$wpdb->query( 'ROLLBACK' );
 		if ( is_multisite() ) {
@@ -58,6 +68,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
 		remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
 		remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
 		$this->_restore_hooks();
+		$current_user = clone self::$_original_current_user;
 	}
 
 	function clean_up_global_scope() {
@@ -103,7 +114,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
 			}
 		}
 	}
-	
+
 	function flush_cache() {
 		global $wp_object_cache;
 		$wp_object_cache->group_ops = array();
-- 
1.8.0.msysgit.0

