2011-01-25 19:28:26 +00:00
|
|
|
<?php
|
|
|
|
|
2011-05-31 09:57:06 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2011-01-26 20:34:11 +00:00
|
|
|
namespace Symfony\Tests\Component\Security\Http\Firewall;
|
2011-01-25 19:28:26 +00:00
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
2011-03-07 14:42:52 +00:00
|
|
|
use Symfony\Component\HttpKernel\Events;
|
2011-01-25 19:28:26 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2011-01-26 20:34:11 +00:00
|
|
|
use Symfony\Component\Security\Core\Exception\CookieTheftException;
|
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
|
|
|
use Symfony\Component\Security\Http\Firewall\RememberMeListener;
|
2011-01-25 19:28:26 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
class RememberMeListenerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2011-03-07 14:42:52 +00:00
|
|
|
public function testOnCoreSecurityDoesNotTryToPopulateNonEmptySecurityContext()
|
2011-01-25 19:28:26 +00:00
|
|
|
{
|
|
|
|
list($listener, $context, $service,,) = $this->getListener();
|
|
|
|
|
|
|
|
$context
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
2011-01-26 20:34:11 +00:00
|
|
|
->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')))
|
2011-01-25 19:28:26 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
$context
|
|
|
|
->expects($this->never())
|
|
|
|
->method('setToken')
|
|
|
|
;
|
|
|
|
|
2011-03-17 12:24:23 +00:00
|
|
|
$this->assertNull($listener->handle($this->getGetResponseEvent()));
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-07 14:42:52 +00:00
|
|
|
public function testOnCoreSecurityDoesNothingWhenNoCookieIsSet()
|
2011-01-25 19:28:26 +00:00
|
|
|
{
|
|
|
|
list($listener, $context, $service,,) = $this->getListener();
|
|
|
|
|
|
|
|
$context
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->will($this->returnValue(null))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->once())
|
|
|
|
->method('autoLogin')
|
|
|
|
->will($this->returnValue(null))
|
|
|
|
;
|
|
|
|
|
2011-03-07 14:42:52 +00:00
|
|
|
$event = $this->getGetResponseEvent();
|
2011-01-25 19:28:26 +00:00
|
|
|
$event
|
|
|
|
->expects($this->once())
|
2011-03-07 14:42:52 +00:00
|
|
|
->method('getRequest')
|
2011-01-25 19:28:26 +00:00
|
|
|
->will($this->returnValue(new Request()))
|
|
|
|
;
|
|
|
|
|
2011-03-17 12:24:23 +00:00
|
|
|
$this->assertNull($listener->handle($event));
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-13 17:10:39 +00:00
|
|
|
public function testOnCoreSecurityIgnoresAuthenticationExceptionThrownByAuthenticationManagerImplementation()
|
2011-01-25 19:28:26 +00:00
|
|
|
{
|
|
|
|
list($listener, $context, $service, $manager,) = $this->getListener();
|
|
|
|
|
|
|
|
$context
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->will($this->returnValue(null))
|
|
|
|
;
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->once())
|
|
|
|
->method('autoLogin')
|
2011-01-26 20:34:11 +00:00
|
|
|
->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')))
|
2011-01-25 19:28:26 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
$service
|
|
|
|
->expects($this->once())
|
2011-03-10 20:27:42 +00:00
|
|
|
->method('loginFail')
|
2011-01-25 19:28:26 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
$exception = new AuthenticationException('Authentication failed.');
|
|
|
|
$manager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('authenticate')
|
|
|
|
->will($this->throwException($exception))
|
|
|
|
;
|
|
|
|
|
2011-03-07 14:42:52 +00:00
|
|
|
$event = $this->getGetResponseEvent();
|
2011-01-25 19:28:26 +00:00
|
|
|
$event
|
|
|
|
->expects($this->once())
|
2011-03-07 14:42:52 +00:00
|
|
|
->method('getRequest')
|
2011-01-25 19:28:26 +00:00
|
|
|
->will($this->returnValue(new Request()))
|
|
|
|
;
|
|
|
|
|
2011-03-17 12:24:23 +00:00
|
|
|
$listener->handle($event);
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-07 14:42:52 +00:00
|
|
|
public function testOnCoreSecurity()
|
2011-01-25 19:28:26 +00:00
|
|
|
{
|
|
|
|
list($listener, $context, $service, $manager,) = $this->getListener();
|
|
|
|
|
|
|
|
$context
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->will($this->returnValue(null))
|
|
|
|
;
|
|
|
|
|
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
|
|
|
|
->expects($this->once())
|
|
|
|
->method('autoLogin')
|
|
|
|
->will($this->returnValue($token))
|
|
|
|
;
|
|
|
|
|
|
|
|
$context
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setToken')
|
|
|
|
->with($this->equalTo($token))
|
|
|
|
;
|
|
|
|
|
|
|
|
$manager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('authenticate')
|
|
|
|
->will($this->returnValue($token))
|
|
|
|
;
|
|
|
|
|
2011-03-07 14:42:52 +00:00
|
|
|
$event = $this->getGetResponseEvent();
|
2011-01-25 19:28:26 +00:00
|
|
|
$event
|
|
|
|
->expects($this->once())
|
2011-03-07 14:42:52 +00:00
|
|
|
->method('getRequest')
|
2011-01-25 19:28:26 +00:00
|
|
|
->will($this->returnValue(new Request()))
|
|
|
|
;
|
|
|
|
|
2011-03-17 12:24:23 +00:00
|
|
|
$listener->handle($event);
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-07 14:42:52 +00:00
|
|
|
protected function getGetResponseEvent()
|
|
|
|
{
|
2011-03-13 18:16:56 +00:00
|
|
|
return $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
|
2011-03-07 14:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getFilterResponseEvent()
|
2011-01-25 19:28:26 +00:00
|
|
|
{
|
2011-03-13 18:16:56 +00:00
|
|
|
return $this->getMock('Symfony\Component\HttpKernel\Event\FilterResponseEvent', array(), array(), '', false);
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getListener()
|
|
|
|
{
|
|
|
|
$listener = new RememberMeListener(
|
|
|
|
$context = $this->getContext(),
|
|
|
|
$service = $this->getService(),
|
|
|
|
$manager = $this->getManager(),
|
|
|
|
$logger = $this->getLogger()
|
|
|
|
);
|
|
|
|
|
|
|
|
return array($listener, $context, $service, $manager, $logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLogger()
|
|
|
|
{
|
|
|
|
return $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getManager()
|
|
|
|
{
|
2011-01-26 20:34:11 +00:00
|
|
|
return $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getService()
|
|
|
|
{
|
2011-01-26 20:34:11 +00:00
|
|
|
return $this->getMock('Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface');
|
2011-01-25 19:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getContext()
|
|
|
|
{
|
2011-01-26 20:34:11 +00:00
|
|
|
return $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContext')
|
2011-01-25 19:28:26 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|