2011-01-25 19:28:26 +00:00
|
|
|
<?php
|
|
|
|
|
2011-05-31 09:57:06 +01:00
|
|
|
/*
|
2012-03-31 22:00:32 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-05-31 09:57:06 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
2012-03-31 22:00:32 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-05-31 09:57:06 +01:00
|
|
|
*/
|
|
|
|
|
2013-09-16 09:03:00 +01:00
|
|
|
namespace Symfony\Component\Security\Http\Tests\RememberMe;
|
2011-01-25 19:28:26 +00:00
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
|
2011-01-25 19:28:26 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
class AbstractRememberMeServicesTest extends \PHPUnit_Framework_TestCase
|
2011-01-25 19:28:26 +00:00
|
|
|
{
|
2011-02-26 20:44:40 +00:00
|
|
|
public function testGetRememberMeParameter()
|
|
|
|
{
|
|
|
|
$service = $this->getService(null, array('remember_me_parameter' => 'foo'));
|
|
|
|
|
|
|
|
$this->assertEquals('foo', $service->getRememberMeParameter());
|
|
|
|
}
|
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
public function testGetKey()
|
|
|
|
{
|
|
|
|
$service = $this->getService();
|
|
|
|
$this->assertEquals('fookey', $service->getKey());
|
|
|
|
}
|
|
|
|
|
2011-01-25 19:28:26 +00:00
|
|
|
public function testAutoLoginReturnsNullWhenNoCookie()
|
|
|
|
{
|
2012-11-18 21:41:04 +00:00
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
|
2011-01-25 19:28:26 +00:00
|
|
|
|
|
|
|
$this->assertNull($service->autoLogin(new Request()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \RuntimeException
|
|
|
|
*/
|
2011-03-10 20:27:42 +00:00
|
|
|
public function testAutoLoginThrowsExceptionWhenImplementationDoesNotReturnUserInterface()
|
2011-01-25 19:28:26 +00:00
|
|
|
{
|
2012-11-18 21:41:04 +00:00
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
|
2011-01-25 19:28:26 +00:00
|
|
|
$request = new Request;
|
|
|
|
$request->cookies->set('foo', 'foo');
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->once())
|
|
|
|
->method('processAutoLoginCookie')
|
|
|
|
->will($this->returnValue(null))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service->autoLogin($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAutoLogin()
|
|
|
|
{
|
2012-11-18 21:41:04 +00:00
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
|
2011-01-25 19:28:26 +00:00
|
|
|
$request = new Request();
|
|
|
|
$request->cookies->set('foo', 'foo');
|
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getRoles')
|
|
|
|
->will($this->returnValue(array()))
|
|
|
|
;
|
2011-01-25 19:28:26 +00:00
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->once())
|
|
|
|
->method('processAutoLoginCookie')
|
2011-03-10 20:27:42 +00:00
|
|
|
->will($this->returnValue($user))
|
2011-01-25 19:28:26 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
$returnedToken = $service->autoLogin($request);
|
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
$this->assertSame($user, $returnedToken->getUser());
|
|
|
|
$this->assertSame('fookey', $returnedToken->getKey());
|
|
|
|
$this->assertSame('fookey', $returnedToken->getProviderKey());
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogout()
|
|
|
|
{
|
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
|
|
|
|
$request = new Request();
|
|
|
|
$response = new Response();
|
2011-01-26 20:34:11 +00:00
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
2011-01-25 19:28:26 +00:00
|
|
|
|
|
|
|
$service->logout($request, $response, $token);
|
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
$this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginFail()
|
|
|
|
{
|
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
|
|
|
|
$request = new Request();
|
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
$service->loginFail($request);
|
2011-01-25 19:28:26 +00:00
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
$this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:17:46 +00:00
|
|
|
public function testLoginSuccessIsNotProcessedWhenTokenDoesNotContainUserInterfaceImplementation()
|
2011-01-25 19:28:26 +00:00
|
|
|
{
|
2012-11-18 21:41:04 +00:00
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true, 'path' => null, 'domain' => null));
|
2011-01-25 19:28:26 +00:00
|
|
|
$request = new Request;
|
|
|
|
$response = new Response;
|
2011-03-07 17:17:46 +00:00
|
|
|
$account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
|
2011-01-26 20:34:11 +00:00
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
2011-01-25 19:28:26 +00:00
|
|
|
$token
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue('foo'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->never())
|
|
|
|
->method('onLoginSuccess')
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertFalse($request->request->has('foo'));
|
|
|
|
|
|
|
|
$service->loginSuccess($request, $response, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginSuccessIsNotProcessedWhenRememberMeIsNotRequested()
|
|
|
|
{
|
2012-11-18 21:41:04 +00:00
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo', 'path' => null, 'domain' => null));
|
2011-01-25 19:28:26 +00:00
|
|
|
$request = new Request;
|
|
|
|
$response = new Response;
|
2011-03-07 17:17:46 +00:00
|
|
|
$account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
|
2011-01-26 20:34:11 +00:00
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
2011-01-25 19:28:26 +00:00
|
|
|
$token
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($account))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->never())
|
|
|
|
->method('onLoginSuccess')
|
|
|
|
->will($this->returnValue(null))
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertFalse($request->request->has('foo'));
|
|
|
|
|
|
|
|
$service->loginSuccess($request, $response, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginSuccessWhenRememberMeAlwaysIsTrue()
|
|
|
|
{
|
2012-11-18 21:41:04 +00:00
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true, 'path' => null, 'domain' => null));
|
2011-01-25 19:28:26 +00:00
|
|
|
$request = new Request;
|
|
|
|
$response = new Response;
|
2011-03-07 17:17:46 +00:00
|
|
|
$account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
|
2011-01-26 20:34:11 +00:00
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
2011-01-25 19:28:26 +00:00
|
|
|
$token
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($account))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->once())
|
|
|
|
->method('onLoginSuccess')
|
|
|
|
->will($this->returnValue(null))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service->loginSuccess($request, $response, $token);
|
|
|
|
}
|
|
|
|
|
2011-07-13 17:10:58 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getPositiveRememberMeParameterValues
|
|
|
|
*/
|
|
|
|
public function testLoginSuccessWhenRememberMeParameterWithPathIsPositive($value)
|
|
|
|
{
|
2012-11-18 21:41:04 +00:00
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo[bar]', 'path' => null, 'domain' => null));
|
2011-07-13 17:10:58 +01:00
|
|
|
|
|
|
|
$request = new Request;
|
|
|
|
$request->request->set('foo', array('bar' => $value));
|
|
|
|
$response = new Response;
|
|
|
|
$account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
|
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
|
|
|
$token
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($account))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->once())
|
|
|
|
->method('onLoginSuccess')
|
|
|
|
->will($this->returnValue(true))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service->loginSuccess($request, $response, $token);
|
|
|
|
}
|
|
|
|
|
2011-01-25 19:28:26 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getPositiveRememberMeParameterValues
|
|
|
|
*/
|
|
|
|
public function testLoginSuccessWhenRememberMeParameterIsPositive($value)
|
|
|
|
{
|
2012-11-18 21:41:04 +00:00
|
|
|
$service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo', 'path' => null, 'domain' => null));
|
2011-01-25 19:28:26 +00:00
|
|
|
|
|
|
|
$request = new Request;
|
|
|
|
$request->request->set('foo', $value);
|
|
|
|
$response = new Response;
|
2011-03-07 17:17:46 +00:00
|
|
|
$account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
|
2011-01-26 20:34:11 +00:00
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
2011-01-25 19:28:26 +00:00
|
|
|
$token
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($account))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->once())
|
|
|
|
->method('onLoginSuccess')
|
|
|
|
->will($this->returnValue(true))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service->loginSuccess($request, $response, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPositiveRememberMeParameterValues()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('true'),
|
|
|
|
array('1'),
|
|
|
|
array('on'),
|
|
|
|
array('yes'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getService($userProvider = null, $options = array(), $logger = null)
|
|
|
|
{
|
|
|
|
if (null === $userProvider) {
|
|
|
|
$userProvider = $this->getProvider();
|
|
|
|
}
|
|
|
|
|
2011-03-10 20:27:42 +00:00
|
|
|
return $this->getMockForAbstractClass('Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices', array(
|
2011-01-25 19:28:26 +00:00
|
|
|
array($userProvider), 'fookey', 'fookey', $options, $logger
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getProvider()
|
|
|
|
{
|
2011-01-26 20:34:11 +00:00
|
|
|
$provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
|
2011-01-25 19:28:26 +00:00
|
|
|
$provider
|
|
|
|
->expects($this->any())
|
|
|
|
->method('supportsClass')
|
|
|
|
->will($this->returnValue(true))
|
|
|
|
;
|
|
|
|
|
|
|
|
return $provider;
|
|
|
|
}
|
|
|
|
}
|