diff --git a/core/modules/system/lib/Drupal/system/Tests/Session/UserImpersonatingUserTest.php b/core/modules/system/lib/Drupal/system/Tests/Session/UserImpersonatingUserTest.php index 1aa319e..757f5bb 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Session/UserImpersonatingUserTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Session/UserImpersonatingUserTest.php @@ -5,7 +5,7 @@ * Contains Drupal\user\Tests\UserImpersonatingUserTest. */ -namespace Drupal\user\Tests; +namespace Drupal\system\Tests\Session; use Drupal\Core\Session\AnonymousUserSession; use Drupal\Core\Session\UserSession; @@ -29,10 +29,10 @@ function setUp() { } function testUserImpersonateUser() { - $user = \Drupal::currentUser(); + $user = $this->container->get('current_user'); $original_user = clone $user; - // If not currently logged in, use user_user_impersonate_user() to switch to + // If not currently logged in, use AccountProxy::impersonateAccount() to switch to // user 1. If logged in, switch to the anonymous user instead. if ($user->isAnonymous()) { $user->impersonateAccount(new UserSession(array('uid' => 1))); @@ -43,7 +43,10 @@ function testUserImpersonateUser() { // Verify that the active user has changed, and that session saving is // disabled. $this->assertEqual($user->id(), ($original_user->id() == 0 ? 1 : 0), t('User switched')); - $this->assertFalse(\Drupal::service('session_manager')->isEnabled(), t('Session saving is disabled.')); + $this->assertFalse($this->container->get('session_manager')->isEnabled(), t('Session saving is disabled.')); + + // Enable session saving for the purpose of this test. + $this->container->get('session_manager')->enable(); // Perform a second (nested) impersonation. $user->impersonateAccount(new UserSession(array('uid' => 2))); @@ -56,7 +59,7 @@ function testUserImpersonateUser() { // Since we are still impersonating the user from the first attempt, // session handling still needs to be disabled. $this->assertEqual($user->id(), ($original_user->id() == 0 ? 1 : 0), t('User switched.')); - $this->assertFalse(\Drupal::service('session_manager')->isEnabled(), t('Session saving is disabled.')); + $this->assertFalse($this->container->get('session_manager')->isEnabled(), t('Session saving is disabled.')); // Revert to the original user which was active before the first // impersonation attempt. @@ -65,11 +68,25 @@ function testUserImpersonateUser() { // Assert that the original user is the active user again, and that session // saving has been re-enabled. $this->assertEqual($user->id(), $original_user->id(), t('Original user successfully restored.')); + $this->assertTrue($this->container->get('session_manager')->isEnabled(), t('Session saving is enabled.')); - // Simpletest uses user_impersonate_user() too, revert the impersonation by - // Simpletest to enable session saving again. This is safe because calling - // user_revert_user() too often simply results in returning the active user. - $user->revertAccount(); - $this->assertTrue(\Drupal::service('session_manager')->isEnabled(), t('Session saving is enabled.')); + // Verify that AccountProxy::revertAccount and AccountProxy::revertAll() + // will throw exceptions if there is none left in the stack. + try { + $ex1 = new \RuntimeException(); + $user->revertAccount(); + } + catch (\RuntimeException $e) { + $ex1 = $e; + } + $this->assertEqual($ex1->getMessage(), t('No more account impersonations to revert.'), t('Revert account throws exception if called without previous impersonation.')); + try { + $ex2 = new \RuntimeException(); + $user->revertAll(); + } + catch (\RuntimeException $e) { + $ex2 = $e; + } + $this->assertEqual($ex2->getMessage(), t('No more account impersonations to revert.'), t('Revert all throws exception if called without previous impersonation.')); } }