diff --git a/core/lib/Drupal/Core/Session/AccountProxy.php b/core/lib/Drupal/Core/Session/AccountProxy.php index 3915840..9e0051a 100644 --- a/core/lib/Drupal/Core/Session/AccountProxy.php +++ b/core/lib/Drupal/Core/Session/AccountProxy.php @@ -72,6 +72,8 @@ class AccountProxy implements AccountProxyInterface { * The authentication manager. * @param \Symfony\Component\HttpFoundation\Request $request * The request object used for authenticating. + * @param \Drupal\Core\Session\SessionManager $session_manager + * The session manager. */ public function __construct(AuthenticationManagerInterface $authentication_manager, Request $request, SessionManager $session_manager) { $this->authenticationManager = $authentication_manager; @@ -131,6 +133,21 @@ public function revertAccount() { } /** + /** + * {@inheritdoc} + */ + public function revertAll() { + // Restore the previous account from the stack. + if (!empty($this->accountStack)) { + $this->account = $this->accountStack[0]; + $this->accountStack = array(); + } + // Restore original session saving status if all impersonations are reverted. + $this->sessionManager->enable(); + return $this->account; + } + + /** * {@inheritdoc} */ public function id() { diff --git a/core/lib/Drupal/Core/Session/AccountProxyInterface.php b/core/lib/Drupal/Core/Session/AccountProxyInterface.php index dc4dc4f..b90ee99 100644 --- a/core/lib/Drupal/Core/Session/AccountProxyInterface.php +++ b/core/lib/Drupal/Core/Session/AccountProxyInterface.php @@ -52,5 +52,16 @@ public function impersonateAccount(AccountInterface $account); * The last account that was impersonated. */ public function revertAccount(); + + /** + * Reverts all account impersonations to original account. + * + * This is useful for when there is a failure or an exception and there has + * been possibility of multiple impersonations. + * + * @return \Drupal\Core\Session\AccountInterface + * The original account that was impersonated. + */ + public function revertAll(); } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index 18df7d4..e761fbb 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -15,9 +15,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Database\ConnectionNotDefinedException; use Drupal\Core\Config\StorageInterface; -use Drupal\Core\DrupalKernel; use Drupal\Core\Language\Language; -use Drupal\Core\Session\AccountProxy; use Drupal\Core\Session\AnonymousUserSession; use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; @@ -994,7 +992,6 @@ protected function beforePrepareEnvironment() { * @see TestBase::beforePrepareEnvironment() */ private function prepareEnvironment() { - $user = \Drupal::currentUser(); // Allow (base) test classes to backup global state information. $this->beforePrepareEnvironment(); @@ -1027,9 +1024,6 @@ private function prepareEnvironment() { // simpletest directory if a test is executed within a test. $this->originalFileDirectory = Settings::get('file_public_path', conf_path() . '/files'); $this->originalProfile = drupal_get_profile(); - if (isset($user)) { - $user->impersonateAccount(clone $user); - } // Save and clean the shutdown callbacks array because it is static cached // and will be changed by the test run. Otherwise it will contain callbacks @@ -1088,9 +1082,10 @@ private function prepareEnvironment() { $request = Request::create('/'); $this->container->set('request', $request); - // Run all tests as a anonymous user by default, web tests will replace that + // Run all tests as an anonymous user by default, web tests will replace that // during the test set up. - $this->container->set('current_user', new AnonymousUserSession()); + $this->container->set('current_user', \Drupal::currentUser()); + $this->container->get('current_user')->impersonateAccount(new AnonymousUserSession()); \Drupal::setContainer($this->container); @@ -1228,7 +1223,7 @@ private function restoreEnvironment() { $callbacks = $this->originalShutdownCallbacks; // Restore original user session. - \Drupal::currentUser()->revertAccount(); + \Drupal::currentUser()->revertAll(); } /**