diff --git a/module/Auth/src/Auth/Controller/Plugin/UserSwitcher.php b/module/Auth/src/Auth/Controller/Plugin/UserSwitcher.php index 6ee1a075a..9180ca130 100644 --- a/module/Auth/src/Auth/Controller/Plugin/UserSwitcher.php +++ b/module/Auth/src/Auth/Controller/Plugin/UserSwitcher.php @@ -114,13 +114,6 @@ public function clear() $_SESSION = $oldSession; return $ref ? $ref : true; - $originalUser = $session->originalUser; - $this->exchangeAuthUser($originalUser); - /* @var \Zend\Session\Storage\StorageInterface $sessionStorage */ - $sessionStorage = $session->getManager()->getStorage(); - $sessionStorage->clear(self::SESSION_NAMESPACE); - - return true; } /** diff --git a/module/Auth/test/AuthTest/Controller/PasswordControllerFunctionalTest.php b/module/Auth/test/AuthTest/Controller/PasswordControllerFunctionalTest.php index 261f172cf..706012001 100644 --- a/module/Auth/test/AuthTest/Controller/PasswordControllerFunctionalTest.php +++ b/module/Auth/test/AuthTest/Controller/PasswordControllerFunctionalTest.php @@ -33,16 +33,23 @@ public function setUp() parent::setUp(); + $orgImageRepo = $this->getMockBuilder(OrganizationImage::class)->disableOriginalConstructor()->getMock(); $this->repositoriesMock = $this->getMockBuilder('Core\Repository\RepositoryService') ->disableOriginalConstructor() ->getMock(); + $this->repositoriesMock->expects($this->any())->method('get') + ->will($this->returnValueMap([ + [ 'Organizations/OrganizationImage', $orgImageRepo ] + ])); $manager = $this->getMockBuilder(Manager::class) ->disableOriginalConstructor() ->getMock() ; + $hybridAuth = $this->getMockBuilder(\Hybrid_Auth::class)->disableOriginalConstructor()->getMock(); $this->setMockToServiceLocator('repositories', $this->repositoriesMock); $this->setMockToServiceLocator('Organizations\ImageFileCache\Manager',$manager); + $this->setMockToServiceLocator('HybridAuth', $hybridAuth); } /** diff --git a/module/Auth/test/AuthTest/Controller/Plugin/UserSwitcherTest.php b/module/Auth/test/AuthTest/Controller/Plugin/UserSwitcherTest.php index 79a4e2b37..0a52f579b 100644 --- a/module/Auth/test/AuthTest/Controller/Plugin/UserSwitcherTest.php +++ b/module/Auth/test/AuthTest/Controller/Plugin/UserSwitcherTest.php @@ -17,6 +17,8 @@ use CoreTestUtils\TestCase\TestInheritanceTrait; use CoreTestUtils\TestCase\TestSetterGetterTrait; use Zend\Authentication\Storage\StorageInterface; +use Zend\Http\PhpEnvironment\Request; +use Zend\Mvc\Controller\AbstractActionController; use Zend\Mvc\Controller\Plugin\AbstractPlugin; use Zend\Session\Container; @@ -122,11 +124,11 @@ private function getComplexAuthMock() ->setMethods(['read', 'write']) ->getMockForAbstractClass(); - $storage->expects($this->atLeast(1))->method('read')->willReturn('originalUser'); - $storage->expects($this->atLeast(1))->method('write')->with('switchedUser'); + $storage->expects($this->any())->method('read')->willReturn('originalUser'); + $storage->expects($this->any())->method('write')->with('switchedUser'); - $auth->expects($this->atLeast(1))->method('getStorage')->willReturn($storage); - $auth->expects($this->atLeast(1))->method('clearIdentity'); + $auth->expects($this->any())->method('getStorage')->willReturn($storage); + $auth->expects($this->any())->method('clearIdentity'); $auth->expects($this->any())->method('getUser')->willReturn(new User()); @@ -144,14 +146,39 @@ public function testClearReturnsEarlyWhenNoSwitchedUserIsSet() $this->assertFalse($this->target->clear()); } - public function testClearRestoresOriginalUserAndClearsSession() + public function returnReferenceProvider() + { + return [ + [ null ], [ 'some/ref/uri' ], + ]; + } + + /** + * @dataProvider returnReferenceProvider + * + * @param $ref + */ + public function testClearRestoresOriginalUserAndClearsSession($ref) { $session = new Container(UserSwitcher::SESSION_NAMESPACE); $session->isSwitchedUser = true; $session->originalUser = 'switchedUser'; + $oldSession = [ + 'oldSession' => true, + 'must' => 'be same' + ]; + if (null !== $ref) { + $session->ref = $ref; + } + $session->session = serialize($oldSession); + + if (null === $ref) { + $this->assertTrue($this->target->clear()); + } else { + $this->assertEquals($ref, $this->target->clear()); + } + $this->assertEquals($oldSession, $_SESSION); - $this->assertTrue($this->target->clear()); - $this->assertArrayNotHasKey(UserSwitcher::SESSION_NAMESPACE, $_SESSION); } public function testSwitchUserReturnsEarlyWhenSwitchedUserIsSet() @@ -165,28 +192,45 @@ public function testSwitchUserReturnsEarlyWhenSwitchedUserIsSet() public function testSwitchUserExchangeOriginalUserAndStoresSession() { + $request = new Request(); + $request->setRequestUri('/some/ref'); + + $controller = $this->getMockBuilder(AbstractActionController::class)->disableOriginalConstructor()->getMock(); + $controller->expects($this->once())->method('getRequest')->willReturn($request); + + $this->target->setController($controller); + $oldSession = [ + 'this is' => 'the old session' + ]; + $_SESSION = $oldSession; $this->assertTrue($this->target->switchUser('switchedUser')); $this->assertEquals( - ['isSwitchedUser' => true, 'originalUser' => 'originalUser', 'params' => []], + [ + 'isSwitchedUser' => true, + 'originalUser' => 'originalUser', + 'params' => [], 'ref' => '/some/ref', + 'session' => serialize($oldSession) + ], $_SESSION[UserSwitcher::SESSION_NAMESPACE]->getArrayCopy() ); - $_SESSION = []; } public function testSwitchUserUsesUserIdFromProvidedUserInterface() { + $_SESSION = []; $user = $this->getMockBuilder(User::class)->disableOriginalConstructor() ->setMethods(['getId'])->getMock(); $user->expects($this->once())->method('getId')->willReturn('switchedUser'); - $this->assertTrue($this->target->switchUser($user)); - + $this->assertTrue($this->target->switchUser($user, ['ref' => 'ref'])); + $this->assertEquals($_SESSION[UserSwitcher::SESSION_NAMESPACE]['ref'], 'ref'); $_SESSION = []; } public function testSwitchUserSetsUserOnAclPluginIfProvided() { + $_SESSION = []; $acl = $this->getMockBuilder(Acl::class)->disableOriginalConstructor()->setMethods(['setUser'])->getMock(); $acl->expects($this->once())->method('setUser')->with($this->isInstanceOf(User::class)); @@ -194,10 +238,10 @@ public function testSwitchUserSetsUserOnAclPluginIfProvided() ->setMethods(['getId'])->getMock(); $user->expects($this->any())->method('getId')->willReturn('switchedUser'); - $this->target->switchUser($user); + $this->target->switchUser($user, ['ref' => 'ref']); $_SESSION = []; $this->target->setAclPlugin($acl); - $this->target->switchUser($user); + $this->target->switchUser($user, ['ref' => 'ref']); $_SESSION = []; } @@ -212,4 +256,4 @@ public function testIsSwitchedUser() $_SESSION = []; } -} \ No newline at end of file +}