Файл: symfony-2.7/src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php
Строк: 391
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyComponentSecurityHttpTestsFirewall;
use SymfonyComponentSecurityHttpFirewallSwitchUserListener;
class SwitchUserListenerTest extends PHPUnit_Framework_TestCase
{
private $tokenStorage;
private $userProvider;
private $userChecker;
private $accessDecisionManager;
private $request;
private $event;
protected function setUp()
{
$this->tokenStorage = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$this->userProvider = $this->getMock('SymfonyComponentSecurityCoreUserUserProviderInterface');
$this->userChecker = $this->getMock('SymfonyComponentSecurityCoreUserUserCheckerInterface');
$this->accessDecisionManager = $this->getMock('SymfonyComponentSecurityCoreAuthorizationAccessDecisionManagerInterface');
$this->request = $this->getMock('SymfonyComponentHttpFoundationRequest');
$this->request->query = $this->getMock('SymfonyComponentHttpFoundationParameterBag');
$this->request->server = $this->getMock('SymfonyComponentHttpFoundationServerBag');
$this->event = $this->getEvent($this->request);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage $providerKey must not be empty
*/
public function testProviderKeyIsRequired()
{
new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, '', $this->accessDecisionManager);
}
public function testEventIsIgnoredIfUsernameIsNotPassedWithTheRequest()
{
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue(null));
$this->event->expects($this->never())->method('setResponse');
$this->tokenStorage->expects($this->never())->method('setToken');
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event);
}
/**
* @expectedException SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException
*/
public function testExitUserThrowsAuthenticationExceptionIfOriginalTokenCannotBeFound()
{
$token = $this->getToken(array($this->getMock('SymfonyComponentSecurityCoreRoleRoleInterface')));
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token));
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('_exit'));
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event);
}
public function testExitUserUpdatesToken()
{
$originalToken = $this->getToken();
$role = $this->getMockBuilder('SymfonyComponentSecurityCoreRoleSwitchUserRole')
->disableOriginalConstructor()
->getMock();
$role->expects($this->any())->method('getSource')->will($this->returnValue($originalToken));
$this->tokenStorage->expects($this->any())
->method('getToken')
->will($this->returnValue($this->getToken(array($role))));
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('_exit'));
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
$this->request->query->expects($this->once())->method('remove', '_switch_user');
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array()));
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', '');
$this->tokenStorage->expects($this->once())
->method('setToken')->with($originalToken);
$this->event->expects($this->once())
->method('setResponse')->with($this->isInstanceOf('SymfonyComponentHttpFoundationRedirectResponse'));
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event);
}
/**
* @expectedException SymfonyComponentSecurityCoreExceptionAccessDeniedException
*/
public function testSwitchUserIsDisallowed()
{
$token = $this->getToken(array($this->getMock('SymfonyComponentSecurityCoreRoleRoleInterface')));
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token));
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba'));
$this->accessDecisionManager->expects($this->once())
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
->will($this->returnValue(false));
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event);
}
public function testSwitchUser()
{
$token = $this->getToken(array($this->getMock('SymfonyComponentSecurityCoreRoleRoleInterface')));
$user = $this->getMock('SymfonyComponentSecurityCoreUserUserInterface');
$user->expects($this->any())->method('getRoles')->will($this->returnValue(array()));
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token));
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba'));
$this->request->query->expects($this->once())->method('remove', '_switch_user');
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array()));
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', '');
$this->accessDecisionManager->expects($this->once())
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
->will($this->returnValue(true));
$this->userProvider->expects($this->once())
->method('loadUserByUsername')->with('kuba')
->will($this->returnValue($user));
$this->userChecker->expects($this->once())
->method('checkPostAuth')->with($user);
$this->tokenStorage->expects($this->once())
->method('setToken')->with($this->isInstanceOf('SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken'));
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event);
}
public function testSwitchUserKeepsOtherQueryStringParameters()
{
$token = $this->getToken(array($this->getMock('SymfonyComponentSecurityCoreRoleRoleInterface')));
$user = $this->getMock('SymfonyComponentSecurityCoreUserUserInterface');
$user->expects($this->any())->method('getRoles')->will($this->returnValue(array()));
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token));
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba'));
$this->request->query->expects($this->once())->method('remove', '_switch_user');
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array('page' => 3, 'section' => 2)));
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', 'page=3§ion=2');
$this->accessDecisionManager->expects($this->once())
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
->will($this->returnValue(true));
$this->userProvider->expects($this->once())
->method('loadUserByUsername')->with('kuba')
->will($this->returnValue($user));
$this->userChecker->expects($this->once())
->method('checkPostAuth')->with($user);
$this->tokenStorage->expects($this->once())
->method('setToken')->with($this->isInstanceOf('SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken'));
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event);
}
private function getEvent($request)
{
$event = $this->getMockBuilder('SymfonyComponentHttpKernelEventGetResponseEvent')
->disableOriginalConstructor()
->getMock();
$event->expects($this->any())
->method('getRequest')
->will($this->returnValue($request));
return $event;
}
private function getToken(array $roles = array())
{
$token = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$token->expects($this->any())
->method('getRoles')
->will($this->returnValue($roles));
return $token;
}
}