summaryrefslogtreecommitdiffstats
path: root/Tests/Http
diff options
context:
space:
mode:
Diffstat (limited to 'Tests/Http')
-rw-r--r--Tests/Http/EntryPoint/BasicAuthenticationEntryPointTest.php52
-rw-r--r--Tests/Http/EntryPoint/DigestAuthenticationEntryPointTest.php66
-rw-r--r--Tests/Http/EntryPoint/FormAuthenticationEntryPointTest.php75
-rw-r--r--Tests/Http/EntryPoint/RetryAuthenticationEntryPointTest.php71
-rw-r--r--Tests/Http/Firewall/AccessListenerTest.php223
-rw-r--r--Tests/Http/Firewall/AnonymousAuthenticationListenerTest.php62
-rw-r--r--Tests/Http/Firewall/BasicAuthenticationListenerTest.php198
-rw-r--r--Tests/Http/Firewall/ChannelListenerTest.php196
-rw-r--r--Tests/Http/Firewall/ContextListenerTest.php128
-rw-r--r--Tests/Http/Firewall/LogoutListenerTest.php256
-rw-r--r--Tests/Http/Firewall/RememberMeListenerTest.php199
-rw-r--r--Tests/Http/FirewallMapTest.php128
-rw-r--r--Tests/Http/FirewallTest.php123
-rw-r--r--Tests/Http/HttpUtilsTest.php151
-rw-r--r--Tests/Http/Logout/CookieClearingLogoutHandlerTest.php56
-rw-r--r--Tests/Http/Logout/SessionLogoutHandlerTest.php47
-rw-r--r--Tests/Http/RememberMe/AbstractRememberMeServicesTest.php268
-rw-r--r--Tests/Http/RememberMe/PersistentTokenBasedRememberMeServicesTest.php335
-rw-r--r--Tests/Http/RememberMe/TokenBasedRememberMeServicesTest.php280
19 files changed, 2914 insertions, 0 deletions
diff --git a/Tests/Http/EntryPoint/BasicAuthenticationEntryPointTest.php b/Tests/Http/EntryPoint/BasicAuthenticationEntryPointTest.php
new file mode 100644
index 0000000..b442309
--- /dev/null
+++ b/Tests/Http/EntryPoint/BasicAuthenticationEntryPointTest.php
@@ -0,0 +1,52 @@
+<?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 Symfony\Component\Security\Tests\Http\EntryPoint;
+
+use Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint;
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+
+class BasicAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ public function testStart()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+
+ $authException = new AuthenticationException('The exception message');
+
+ $entryPoint = new BasicAuthenticationEntryPoint('TheRealmName');
+ $response = $entryPoint->start($request, $authException);
+
+ $this->assertEquals('Basic realm="TheRealmName"', $response->headers->get('WWW-Authenticate'));
+ $this->assertEquals(401, $response->getStatusCode());
+ $this->assertAttributeEquals('The exception message', 'statusText', $response);
+ }
+
+ public function testStartWithoutAuthException()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+
+ $entryPoint = new BasicAuthenticationEntryPoint('TheRealmName');
+
+ $response = $entryPoint->start($request);
+
+ $this->assertEquals('Basic realm="TheRealmName"', $response->headers->get('WWW-Authenticate'));
+ $this->assertEquals(401, $response->getStatusCode());
+ $this->assertAttributeEquals('Unauthorized', 'statusText', $response);
+ }
+}
diff --git a/Tests/Http/EntryPoint/DigestAuthenticationEntryPointTest.php b/Tests/Http/EntryPoint/DigestAuthenticationEntryPointTest.php
new file mode 100644
index 0000000..ae0e3cc
--- /dev/null
+++ b/Tests/Http/EntryPoint/DigestAuthenticationEntryPointTest.php
@@ -0,0 +1,66 @@
+<?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 Symfony\Component\Security\Tests\Http\EntryPoint;
+
+use Symfony\Component\Security\Http\EntryPoint\DigestAuthenticationEntryPoint;
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use Symfony\Component\Security\Core\Exception\NonceExpiredException;
+
+class DigestAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ public function testStart()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+
+ $authenticationException = new AuthenticationException('TheAuthenticationExceptionMessage');
+
+ $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheKey');
+ $response = $entryPoint->start($request, $authenticationException);
+
+ $this->assertEquals(401, $response->getStatusCode());
+ $this->assertAttributeEquals('TheAuthenticationExceptionMessage', 'statusText', $response);
+ $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}"$/', $response->headers->get('WWW-Authenticate'));
+ }
+
+ public function testStartWithNoException()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+
+ $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheKey');
+ $response = $entryPoint->start($request);
+
+ $this->assertEquals(401, $response->getStatusCode());
+ $this->assertAttributeEquals('Unauthorized', 'statusText', $response);
+ $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}"$/', $response->headers->get('WWW-Authenticate'));
+ }
+
+ public function testStartWithNonceExpiredException()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+
+ $nonceExpiredException = new NonceExpiredException('TheNonceExpiredExceptionMessage');
+
+ $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheKey');
+ $response = $entryPoint->start($request, $nonceExpiredException);
+
+ $this->assertEquals(401, $response->getStatusCode());
+ $this->assertAttributeEquals('TheNonceExpiredExceptionMessage', 'statusText', $response);
+ $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}", stale="true"$/', $response->headers->get('WWW-Authenticate'));
+ }
+}
diff --git a/Tests/Http/EntryPoint/FormAuthenticationEntryPointTest.php b/Tests/Http/EntryPoint/FormAuthenticationEntryPointTest.php
new file mode 100644
index 0000000..2e72340
--- /dev/null
+++ b/Tests/Http/EntryPoint/FormAuthenticationEntryPointTest.php
@@ -0,0 +1,75 @@
+<?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 Symfony\Component\Security\Tests\Http\EntryPoint;
+
+use Symfony\Component\Security\Http\EntryPoint\FormAuthenticationEntryPoint;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+
+class FormAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
+ $this->markTestSkipped('The "HttpKernel" component is not available');
+ }
+ }
+
+ public function testStart()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+ $response = $this->getMock('Symfony\Component\HttpFoundation\Response');
+
+ $httpKernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
+ $httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils');
+ $httpUtils
+ ->expects($this->once())
+ ->method('createRedirectResponse')
+ ->with($this->equalTo($request), $this->equalTo('/the/login/path'))
+ ->will($this->returnValue($response))
+ ;
+
+ $entryPoint = new FormAuthenticationEntryPoint($httpKernel, $httpUtils, '/the/login/path', false);
+
+ $this->assertEquals($response, $entryPoint->start($request));
+ }
+
+ public function testStartWithUseForward()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+ $subRequest = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+ $response = $this->getMock('Symfony\Component\HttpFoundation\Response');
+
+ $httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils');
+ $httpUtils
+ ->expects($this->once())
+ ->method('createRequest')
+ ->with($this->equalTo($request), $this->equalTo('/the/login/path'))
+ ->will($this->returnValue($subRequest))
+ ;
+
+ $httpKernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
+ $httpKernel
+ ->expects($this->once())
+ ->method('handle')
+ ->with($this->equalTo($request), $this->equalTo(HttpKernelInterface::SUB_REQUEST))
+ ->will($this->returnValue($response))
+ ;
+
+ $entryPoint = new FormAuthenticationEntryPoint($httpKernel, $httpUtils, '/the/login/path', true);
+
+ $this->assertEquals($response, $entryPoint->start($request));
+ }
+}
diff --git a/Tests/Http/EntryPoint/RetryAuthenticationEntryPointTest.php b/Tests/Http/EntryPoint/RetryAuthenticationEntryPointTest.php
new file mode 100644
index 0000000..91de1ca
--- /dev/null
+++ b/Tests/Http/EntryPoint/RetryAuthenticationEntryPointTest.php
@@ -0,0 +1,71 @@
+<?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 Symfony\Component\Security\Tests\Http\EntryPoint;
+
+use Symfony\Component\Security\Http\EntryPoint\RetryAuthenticationEntryPoint;
+use Symfony\Component\HttpFoundation\Request;
+
+class RetryAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ /**
+ * @dataProvider dataForStart
+ */
+ public function testStart($httpPort, $httpsPort, $request, $expectedUrl)
+ {
+ $entryPoint = new RetryAuthenticationEntryPoint($httpPort, $httpsPort);
+ $response = $entryPoint->start($request);
+
+ $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
+ $this->assertEquals($expectedUrl, $response->headers->get('Location'));
+ }
+
+ public function dataForStart()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ return array(array());
+ }
+
+ return array(
+ array(
+ 80,
+ 443,
+ Request::create('http://localhost/foo/bar?baz=bat'),
+ 'https://localhost/foo/bar?baz=bat'
+ ),
+ array(
+ 80,
+ 443,
+ Request::create('https://localhost/foo/bar?baz=bat'),
+ 'http://localhost/foo/bar?baz=bat'
+ ),
+ array(
+ 80,
+ 123,
+ Request::create('http://localhost/foo/bar?baz=bat'),
+ 'https://localhost:123/foo/bar?baz=bat'
+ ),
+ array(
+ 8080,
+ 443,
+ Request::create('https://localhost/foo/bar?baz=bat'),
+ 'http://localhost:8080/foo/bar?baz=bat'
+ )
+ );
+ }
+}
diff --git a/Tests/Http/Firewall/AccessListenerTest.php b/Tests/Http/Firewall/AccessListenerTest.php
new file mode 100644
index 0000000..e3ffbfc
--- /dev/null
+++ b/Tests/Http/Firewall/AccessListenerTest.php
@@ -0,0 +1,223 @@
+<?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 Symfony\Component\Security\Tests\Http\Firewall;
+
+use Symfony\Component\Security\Http\Firewall\AccessListener;
+
+class AccessListenerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
+ $this->markTestSkipped('The "HttpKernel" component is not available');
+ }
+ }
+
+ /**
+ * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
+ */
+ public function testHandleWhenTheAccessDecisionManagerDecidesToRefuseAccess()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+
+ $accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMapInterface');
+ $accessMap
+ ->expects($this->any())
+ ->method('getPatterns')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(array(array('foo' => 'bar'), null)))
+ ;
+
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $token
+ ->expects($this->any())
+ ->method('isAuthenticated')
+ ->will($this->returnValue(true))
+ ;
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue($token))
+ ;
+
+ $accessDecisionManager = $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface');
+ $accessDecisionManager
+ ->expects($this->once())
+ ->method('decide')
+ ->with($this->equalTo($token), $this->equalTo(array('foo' => 'bar')), $this->equalTo($request))
+ ->will($this->returnValue(false))
+ ;
+
+ $listener = new AccessListener(
+ $context,
+ $accessDecisionManager,
+ $accessMap,
+ $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+
+ $listener->handle($event);
+ }
+
+ public function testHandleWhenTheTokenIsNotAuthenticated()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+
+ $accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMapInterface');
+ $accessMap
+ ->expects($this->any())
+ ->method('getPatterns')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(array(array('foo' => 'bar'), null)))
+ ;
+
+ $notAuthenticatedToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $notAuthenticatedToken
+ ->expects($this->any())
+ ->method('isAuthenticated')
+ ->will($this->returnValue(false))
+ ;
+
+ $authenticatedToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $authenticatedToken
+ ->expects($this->any())
+ ->method('isAuthenticated')
+ ->will($this->returnValue(true))
+ ;
+
+ $authManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
+ $authManager
+ ->expects($this->once())
+ ->method('authenticate')
+ ->with($this->equalTo($notAuthenticatedToken))
+ ->will($this->returnValue($authenticatedToken))
+ ;
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue($notAuthenticatedToken))
+ ;
+ $context
+ ->expects($this->once())
+ ->method('setToken')
+ ->with($this->equalTo($authenticatedToken))
+ ;
+
+ $accessDecisionManager = $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface');
+ $accessDecisionManager
+ ->expects($this->once())
+ ->method('decide')
+ ->with($this->equalTo($authenticatedToken), $this->equalTo(array('foo' => 'bar')), $this->equalTo($request))
+ ->will($this->returnValue(true))
+ ;
+
+ $listener = new AccessListener(
+ $context,
+ $accessDecisionManager,
+ $accessMap,
+ $authManager
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+
+ $listener->handle($event);
+ }
+
+ public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+
+ $accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMapInterface');
+ $accessMap
+ ->expects($this->any())
+ ->method('getPatterns')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(array(null, null)))
+ ;
+
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $token
+ ->expects($this->never())
+ ->method('isAuthenticated')
+ ;
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue($token))
+ ;
+
+ $listener = new AccessListener(
+ $context,
+ $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface'),
+ $accessMap,
+ $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+
+ $listener->handle($event);
+ }
+
+ /**
+ * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
+ */
+ public function testHandleWhenTheSecurityContextHasNoToken()
+ {
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue(null))
+ ;
+
+ $listener = new AccessListener(
+ $context,
+ $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface'),
+ $this->getMock('Symfony\Component\Security\Http\AccessMapInterface'),
+ $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+
+ $listener->handle($event);
+ }
+}
diff --git a/Tests/Http/Firewall/AnonymousAuthenticationListenerTest.php b/Tests/Http/Firewall/AnonymousAuthenticationListenerTest.php
new file mode 100644
index 0000000..9e7ea90
--- /dev/null
+++ b/Tests/Http/Firewall/AnonymousAuthenticationListenerTest.php
@@ -0,0 +1,62 @@
+<?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 Symfony\Component\Security\Tests\Http\Firewall;
+
+use Symfony\Component\Security\Http\Firewall\AnonymousAuthenticationListener;
+
+class AnonymousAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+ }
+
+ public function testHandleWithContextHavingAToken()
+ {
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')))
+ ;
+ $context
+ ->expects($this->never())
+ ->method('setToken')
+ ;
+
+ $listener = new AnonymousAuthenticationListener($context, 'TheKey');
+ $listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
+ }
+
+ public function testHandleWithContextHavingNoToken()
+ {
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue(null))
+ ;
+ $context
+ ->expects($this->once())
+ ->method('setToken')
+ ->with(self::logicalAnd(
+ $this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken'),
+ $this->attributeEqualTo('key', 'TheKey')
+ ))
+ ;
+
+ $listener = new AnonymousAuthenticationListener($context, 'TheKey');
+ $listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
+ }
+}
diff --git a/Tests/Http/Firewall/BasicAuthenticationListenerTest.php b/Tests/Http/Firewall/BasicAuthenticationListenerTest.php
new file mode 100644
index 0000000..667869f
--- /dev/null
+++ b/Tests/Http/Firewall/BasicAuthenticationListenerTest.php
@@ -0,0 +1,198 @@
+<?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 Symfony\Component\Security\Tests\Http\Firewall;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Http\Firewall\BasicAuthenticationListener;
+use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
+
+class BasicAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
+ $this->markTestSkipped('The "HttpKernel" component is not available');
+ }
+ }
+
+ public function testHandleWithValidUsernameAndPasswordServerParameters()
+ {
+ $request = new Request(array(), array(), array(), array(), array(), array(
+ 'PHP_AUTH_USER' => 'TheUsername',
+ 'PHP_AUTH_PW' => 'ThePassword'
+ ));
+
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue(null))
+ ;
+ $context
+ ->expects($this->once())
+ ->method('setToken')
+ ->with($this->equalTo($token))
+ ;
+
+ $authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
+ $authenticationManager
+ ->expects($this->once())
+ ->method('authenticate')
+ ->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken'))
+ ->will($this->returnValue($token))
+ ;
+
+ $listener = new BasicAuthenticationListener(
+ $context,
+ $authenticationManager,
+ 'TheProviderKey',
+ $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+
+ $listener->handle($event);
+ }
+
+ public function testHandleWhenAuthenticationFails()
+ {
+ $request = new Request(array(), array(), array(), array(), array(), array(
+ 'PHP_AUTH_USER' => 'TheUsername',
+ 'PHP_AUTH_PW' => 'ThePassword'
+ ));
+
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue(null))
+ ;
+ $context
+ ->expects($this->once())
+ ->method('setToken')
+ ->with($this->equalTo(null))
+ ;
+
+ $response = new Response();
+
+ $authenticationEntryPoint = $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface');
+ $authenticationEntryPoint
+ ->expects($this->any())
+ ->method('start')
+ ->with($this->equalTo($request), $this->isInstanceOf('Symfony\Component\Security\Core\Exception\AuthenticationException'))
+ ->will($this->returnValue($response))
+ ;
+
+ $listener = new BasicAuthenticationListener(
+ $context,
+ new AuthenticationProviderManager(array($this->getMock('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface'))),
+ 'TheProviderKey',
+ $authenticationEntryPoint
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+ $event
+ ->expects($this->once())
+ ->method('setResponse')
+ ->with($this->equalTo($response))
+ ;
+
+ $listener->handle($event);
+ }
+
+ public function testHandleWithNoUsernameServerParameter()
+ {
+ $request = new Request();
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->never())
+ ->method('getToken')
+ ;
+
+ $listener = new BasicAuthenticationListener(
+ $context,
+ $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'),
+ 'TheProviderKey',
+ $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+
+ $listener->handle($event);
+ }
+
+ public function testHandleWithASimilarAuthenticatedToken()
+ {
+ $request = new Request(array(), array(), array(), array(), array(), array('PHP_AUTH_USER' => 'TheUsername'));
+
+ $token = new UsernamePasswordToken('TheUsername', 'ThePassword', 'TheProviderKey', array('ROLE_FOO'));
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+ $context
+ ->expects($this->any())
+ ->method('getToken')
+ ->will($this->returnValue($token))
+ ;
+
+ $authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
+ $authenticationManager
+ ->expects($this->never())
+ ->method('authenticate')
+ ;
+
+ $listener = new BasicAuthenticationListener(
+ $context,
+ $authenticationManager,
+ 'TheProviderKey',
+ $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+
+ $listener->handle($event);
+ }
+}
diff --git a/Tests/Http/Firewall/ChannelListenerTest.php b/Tests/Http/Firewall/ChannelListenerTest.php
new file mode 100644
index 0000000..17bf0a0
--- /dev/null
+++ b/Tests/Http/Firewall/ChannelListenerTest.php
@@ -0,0 +1,196 @@
+<?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 Symfony\Component\Security\Tests\Http\Firewall;
+
+use Symfony\Component\Security\Http\Firewall\ChannelListener;
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
+use Symfony\Component\HttpFoundation\Response;
+
+class ChannelListenerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
+ $this->markTestSkipped('The "HttpKernel" component is not available');
+ }
+ }
+
+ public function testHandleWithNotSecuredRequestAndHttpChannel()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+ $request
+ ->expects($this->any())
+ ->method('isSecure')
+ ->will($this->returnValue(false))
+ ;
+
+ $accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMapInterface');
+ $accessMap
+ ->expects($this->any())
+ ->method('getPatterns')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(array(array(), 'http')))
+ ;
+
+ $entryPoint = $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface');
+ $entryPoint
+ ->expects($this->never())
+ ->method('start')
+ ;
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+ $event
+ ->expects($this->never())
+ ->method('setResponse')
+ ;
+
+ $listener = new ChannelListener($accessMap, $entryPoint);
+ $listener->handle($event);
+ }
+
+ public function testHandleWithSecuredRequestAndHttpsChannel()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+ $request
+ ->expects($this->any())
+ ->method('isSecure')
+ ->will($this->returnValue(true))
+ ;
+
+ $accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMapInterface');
+ $accessMap
+ ->expects($this->any())
+ ->method('getPatterns')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(array(array(), 'https')))
+ ;
+
+ $entryPoint = $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface');
+ $entryPoint
+ ->expects($this->never())
+ ->method('start')
+ ;
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+ $event
+ ->expects($this->never())
+ ->method('setResponse')
+ ;
+
+ $listener = new ChannelListener($accessMap, $entryPoint);
+ $listener->handle($event);
+ }
+
+ public function testHandleWithNotSecuredRequestAndHttpsChannel()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+ $request
+ ->expects($this->any())
+ ->method('isSecure')
+ ->will($this->returnValue(false))
+ ;
+
+ $response = new Response();
+
+ $accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMapInterface');
+ $accessMap
+ ->expects($this->any())
+ ->method('getPatterns')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(array(array(), 'https')))
+ ;
+
+ $entryPoint = $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface');
+ $entryPoint
+ ->expects($this->once())
+ ->method('start')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue($response))
+ ;
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+ $event
+ ->expects($this->once())
+ ->method('setResponse')
+ ->with($this->equalTo($response))
+ ;
+
+ $listener = new ChannelListener($accessMap, $entryPoint);
+ $listener->handle($event);
+ }
+
+ public function testHandleWithSecuredRequestAndHttpChannel()
+ {
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+ $request
+ ->expects($this->any())
+ ->method('isSecure')
+ ->will($this->returnValue(true))
+ ;
+
+ $response = new Response();
+
+ $accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMapInterface');
+ $accessMap
+ ->expects($this->any())
+ ->method('getPatterns')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(array(array(), 'http')))
+ ;
+
+ $entryPoint = $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface');
+ $entryPoint
+ ->expects($this->once())
+ ->method('start')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue($response))
+ ;
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+ $event
+ ->expects($this->once())
+ ->method('setResponse')
+ ->with($this->equalTo($response))
+ ;
+
+ $listener = new ChannelListener($accessMap, $entryPoint);
+ $listener->handle($event);
+ }
+}
diff --git a/Tests/Http/Firewall/ContextListenerTest.php b/Tests/Http/Firewall/ContextListenerTest.php
new file mode 100644
index 0000000..646ed23
--- /dev/null
+++ b/Tests/Http/Firewall/ContextListenerTest.php
@@ -0,0 +1,128 @@
+<?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 Symfony\Test\Component\Security\Http\Firewall;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
+use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
+use Symfony\Component\Security\Core\SecurityContext;
+use Symfony\Component\Security\Http\Firewall\ContextListener;
+
+class ContextListenerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
+ $this->markTestSkipped('The "HttpKernel" component is not available');
+ }
+
+ $this->securityContext = new SecurityContext(
+ $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'),
+ $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')
+ );
+ }
+
+ protected function tearDown()
+ {
+ unset($this->securityContext);
+ }
+
+ public function testOnKernelResponseWillAddSession()
+ {
+ $session = $this->runSessionOnKernelResponse(
+ new UsernamePasswordToken('test1', 'pass1', 'phpunit'),
+ null
+ );
+
+ $token = unserialize($session->get('_security_session'));
+ $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $token);
+ $this->assertEquals('test1', $token->getUsername());
+ }
+
+ public function testOnKernelResponseWillReplaceSession()
+ {
+ $session = $this->runSessionOnKernelResponse(
+ new UsernamePasswordToken('test1', 'pass1', 'phpunit'),
+ 'C:10:"serialized"'
+ );
+
+ $token = unserialize($session->get('_security_session'));
+ $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $token);
+ $this->assertEquals('test1', $token->getUsername());
+ }
+
+ public function testOnKernelResponseWillRemoveSession()
+ {
+ $session = $this->runSessionOnKernelResponse(
+ null,
+ 'C:10:"serialized"'
+ );
+
+ $this->assertFalse($session->has('_security_session'));
+ }
+
+ protected function runSessionOnKernelResponse($newToken, $original = null)
+ {
+ $session = new Session(new MockArraySessionStorage());
+
+ if ($original !== null) {
+ $session->set('_security_session', $original);
+ }
+
+ $this->securityContext->setToken($newToken);
+
+ $request = new Request();
+ $request->setSession($session);
+
+ $event = new FilterResponseEvent(
+ $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
+ $request,
+ HttpKernelInterface::MASTER_REQUEST,
+ new Response()
+ );
+
+ $listener = new ContextListener($this->securityContext, array(), 'session');
+ $listener->onKernelResponse($event);
+
+ return $session;
+ }
+
+ public function testOnKernelResponseWithoutSession()
+ {
+ $this->securityContext->setToken(new UsernamePasswordToken('test1', 'pass1', 'phpunit'));
+ $request = new Request();
+
+ $event = new FilterResponseEvent(
+ $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
+ $request,
+ HttpKernelInterface::MASTER_REQUEST,
+ new Response()
+ );
+
+ $listener = new ContextListener($this->securityContext, array(), 'session');
+ $listener->onKernelResponse($event);
+
+ $this->assertFalse($request->hasSession());
+ }
+}
diff --git a/Tests/Http/Firewall/LogoutListenerTest.php b/Tests/Http/Firewall/LogoutListenerTest.php
new file mode 100644
index 0000000..6ffeed9
--- /dev/null
+++ b/Tests/Http/Firewall/LogoutListenerTest.php
@@ -0,0 +1,256 @@
+<?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 Symfony\Component\Security\Tests\Http\Firewall;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Http\Firewall\LogoutListener;
+
+class LogoutListenerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\Form\Form')) {
+ $this->markTestSkipped('The "Form" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
+ $this->markTestSkipped('The "HttpKernel" component is not available');
+ }
+ }
+
+ public function testHandleUnmatchedPath()
+ {
+ list($listener, $context, $httpUtils, $options) = $this->getListener();
+
+ list($event, $request) = $this->getGetResponseEvent();
+
+ $event->expects($this->never())
+ ->method('setResponse');
+
+ $httpUtils->expects($this->once())
+ ->method('checkRequestPath')
+ ->with($request, $options['logout_path'])
+ ->will($this->returnValue(false));
+
+ $listener->handle($event);
+ }
+
+ public function testHandleMatchedPathWithSuccessHandlerAndCsrfValidation()
+ {
+ $successHandler = $this->getSuccessHandler();
+ $csrfProvider = $this->getCsrfProvider();
+
+ list($listener, $context, $httpUtils, $options) = $this->getListener($successHandler, $csrfProvider);
+
+ list($event, $request) = $this->getGetResponseEvent();
+
+ $request->query->set('_csrf_token', $csrfToken = 'token');
+
+ $httpUtils->expects($this->once())
+ ->method('checkRequestPath')
+ ->with($request, $options['logout_path'])
+ ->will($this->returnValue(true));
+
+ $csrfProvider->expects($this->once())
+ ->method('isCsrfTokenValid')
+ ->with('logout', $csrfToken)
+ ->will($this->returnValue(true));
+
+ $successHandler->expects($this->once())
+ ->method('onLogoutSuccess')
+ ->with($request)
+ ->will($this->returnValue($response = new Response()));
+
+ $context->expects($this->once())
+ ->method('getToken')
+ ->will($this->returnValue($token = $this->getToken()));
+
+ $handler = $this->getHandler();
+ $handler->expects($this->once())
+ ->method('logout')
+ ->with($request, $response, $token);
+
+ $context->expects($this->once())
+ ->method('setToken')
+ ->with(null);
+
+ $event->expects($this->once())
+ ->method('setResponse')
+ ->with($response);
+
+ $listener->addHandler($handler);
+
+ $listener->handle($event);
+ }
+
+ public function testHandleMatchedPathWithoutSuccessHandlerAndCsrfValidation()
+ {
+ list($listener, $context, $httpUtils, $options) = $this->getListener();
+
+ list($event, $request) = $this->getGetResponseEvent();
+
+ $httpUtils->expects($this->once())
+ ->method('checkRequestPath')
+ ->with($request, $options['logout_path'])
+ ->will($this->returnValue(true));
+
+ $httpUtils->expects($this->once())
+ ->method('createRedirectResponse')
+ ->with($request, $options['target_url'])
+ ->will($this->returnValue($response = new Response()));
+
+ $context->expects($this->once())
+ ->method('getToken')
+ ->will($this->returnValue($token = $this->getToken()));
+
+ $handler = $this->getHandler();
+ $handler->expects($this->once())
+ ->method('logout')
+ ->with($request, $response, $token);
+
+ $context->expects($this->once())
+ ->method('setToken')
+ ->with(null);
+
+ $event->expects($this->once())
+ ->method('setResponse')
+ ->with($response);
+
+ $listener->addHandler($handler);
+
+ $listener->handle($event);
+ }
+
+ /**
+ * @expectedException RuntimeException
+ */
+ public function testSuccessHandlerReturnsNonResponse()
+ {
+ $successHandler = $this->getSuccessHandler();
+
+ list($listener, $context, $httpUtils, $options) = $this->getListener($successHandler);
+
+ list($event, $request) = $this->getGetResponseEvent();
+
+ $httpUtils->expects($this->once())
+ ->method('checkRequestPath')
+ ->with($request, $options['logout_path'])
+ ->will($this->returnValue(true));
+
+ $successHandler->expects($this->once())
+ ->method('onLogoutSuccess')
+ ->with($request)
+ ->will($this->returnValue(null));
+
+ $listener->handle($event);
+ }
+
+ /**
+ * @expectedException Symfony\Component\Security\Core\Exception\LogoutException
+ */
+ public function testCsrfValidationFails()
+ {
+ $csrfProvider = $this->getCsrfProvider();
+
+ list($listener, $context, $httpUtils, $options) = $this->getListener(null, $csrfProvider);
+
+ list($event, $request) = $this->getGetResponseEvent();
+
+ $request->query->set('_csrf_token', $csrfToken = 'token');
+
+ $httpUtils->expects($this->once())
+ ->method('checkRequestPath')
+ ->with($request, $options['logout_path'])
+ ->will($this->returnValue(true));
+
+ $csrfProvider->expects($this->once())
+ ->method('isCsrfTokenValid')
+ ->with('logout', $csrfToken)
+ ->will($this->returnValue(false));
+
+ $listener->handle($event);
+ }
+
+ private function getCsrfProvider()
+ {
+ return $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
+ }
+
+ private function getContext()
+ {
+ return $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContext')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ private function getGetResponseEvent()
+ {
+ $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $event->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request = new Request()));
+
+ return array($event, $request);
+ }
+
+ private function getHandler()
+ {
+ return $this->getMock('Symfony\Component\Security\Http\Logout\LogoutHandlerInterface');
+ }
+
+ private function getHttpUtils()
+ {
+ return $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ private function getListener($successHandler = null, $csrfProvider = null)
+ {
+ $listener = new LogoutListener(
+ $context = $this->getContext(),
+ $httpUtils = $this->getHttpUtils(),
+ $options = array(
+ 'csrf_parameter' => '_csrf_token',
+ 'intention' => 'logout',
+ 'logout_path' => '/logout',
+ 'target_url' => '/',
+ ),
+ $successHandler,
+ $csrfProvider
+ );
+
+ return array($listener, $context, $httpUtils, $options);
+ }
+
+ private function getSuccessHandler()
+ {
+ return $this->getMock('Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface');
+ }
+
+ private function getToken()
+ {
+ return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ }
+}
diff --git a/Tests/Http/Firewall/RememberMeListenerTest.php b/Tests/Http/Firewall/RememberMeListenerTest.php
new file mode 100644
index 0000000..5f31273
--- /dev/null
+++ b/Tests/Http/Firewall/RememberMeListenerTest.php
@@ -0,0 +1,199 @@
+<?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 Symfony\Component\Security\Tests\Http\Firewall;
+
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use Symfony\Component\Security\Http\Firewall\RememberMeListener;
+use Symfony\Component\HttpFoundation\Request;
+
+class RememberMeListenerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
+ $this->markTestSkipped('The "HttpKernel" component is not available');
+ }
+ }
+
+ public function testOnCoreSecurityDoesNotTryToPopulateNonEmptySecurityContext()
+ {
+ list($listener, $context, $service,,) = $this->getListener();
+
+ $context
+ ->expects($this->once())
+ ->method('getToken')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')))
+ ;
+
+ $context
+ ->expects($this->never())
+ ->method('setToken')
+ ;
+
+ $this->assertNull($listener->handle($this->getGetResponseEvent()));
+ }
+
+ public function testOnCoreSecurityDoesNothingWhenNoCookieIsSet()
+ {
+ 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))
+ ;
+
+ $event = $this->getGetResponseEvent();
+ $event
+ ->expects($this->once())
+ ->method('getRequest')
+ ->will($this->returnValue(new Request()))
+ ;
+
+ $this->assertNull($listener->handle($event));
+ }
+
+ public function testOnCoreSecurityIgnoresAuthenticationExceptionThrownByAuthenticationManagerImplementation()
+ {
+ list($listener, $context, $service, $manager,) = $this->getListener();
+
+ $context
+ ->expects($this->once())
+ ->method('getToken')
+ ->will($this->returnValue(null))
+ ;
+
+ $service
+ ->expects($this->once())
+ ->method('autoLogin')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')))
+ ;
+
+ $service
+ ->expects($this->once())
+ ->method('loginFail')
+ ;
+
+ $exception = new AuthenticationException('Authentication failed.');
+ $manager
+ ->expects($this->once())
+ ->method('authenticate')
+ ->will($this->throwException($exception))
+ ;
+
+ $event = $this->getGetResponseEvent();
+ $event
+ ->expects($this->once())
+ ->method('getRequest')
+ ->will($this->returnValue(new Request()))
+ ;
+
+ $listener->handle($event);
+ }
+
+ public function testOnCoreSecurity()
+ {
+ list($listener, $context, $service, $manager,) = $this->getListener();
+
+ $context
+ ->expects($this->once())
+ ->method('getToken')
+ ->will($this->returnValue(null))
+ ;
+
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $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))
+ ;
+
+ $event = $this->getGetResponseEvent();
+ $event
+ ->expects($this->once())
+ ->method('getRequest')
+ ->will($this->returnValue(new Request()))
+ ;
+
+ $listener->handle($event);
+ }
+
+ protected function getGetResponseEvent()
+ {
+ return $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ }
+
+ protected function getFilterResponseEvent()
+ {
+ return $this->getMock('Symfony\Component\HttpKernel\Event\FilterResponseEvent', array(), array(), '', false);
+ }
+
+ 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()
+ {
+ return $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
+ }
+
+ protected function getService()
+ {
+ return $this->getMock('Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface');
+ }
+
+ protected function getContext()
+ {
+ return $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContext')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+}
diff --git a/Tests/Http/FirewallMapTest.php b/Tests/Http/FirewallMapTest.php
new file mode 100644
index 0000000..c7f13e1
--- /dev/null
+++ b/Tests/Http/FirewallMapTest.php
@@ -0,0 +1,128 @@
+<?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 Symfony\Component\Security\Tests\Http;
+
+use Symfony\Component\Security\Http\FirewallMap;
+use Symfony\Component\HttpFoundation\Request;
+
+class FirewallMapTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ public function testGetListeners()
+ {
+ $map = new FirewallMap();
+
+ $request = new Request();
+
+ $notMatchingMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
+ $notMatchingMatcher
+ ->expects($this->once())
+ ->method('matches')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(false))
+ ;
+
+ $map->add($notMatchingMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
+
+ $matchingMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
+ $matchingMatcher
+ ->expects($this->once())
+ ->method('matches')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(true))
+ ;
+ $theListener = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
+ $theException = $this->getMock('Symfony\Component\Security\Http\Firewall\ExceptionListener', array(), array(), '', false);
+
+ $map->add($matchingMatcher, array($theListener), $theException);
+
+ $tooLateMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
+ $tooLateMatcher
+ ->expects($this->never())
+ ->method('matches')
+ ;
+
+ $map->add($tooLateMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
+
+ list($listeners, $exception) = $map->getListeners($request);
+
+ $this->assertEquals(array($theListener), $listeners);
+ $this->assertEquals($theException, $exception);
+ }
+
+ public function testGetListenersWithAnEntryHavingNoRequestMatcher()
+ {
+ $map = new FirewallMap();
+
+ $request = new Request();
+
+ $notMatchingMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
+ $notMatchingMatcher
+ ->expects($this->once())
+ ->method('matches')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(false))
+ ;
+
+ $map->add($notMatchingMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
+
+ $theListener = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
+ $theException = $this->getMock('Symfony\Component\Security\Http\Firewall\ExceptionListener', array(), array(), '', false);
+
+ $map->add(null, array($theListener), $theException);
+
+ $tooLateMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
+ $tooLateMatcher
+ ->expects($this->never())
+ ->method('matches')
+ ;
+
+ $map->add($tooLateMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
+
+ list($listeners, $exception) = $map->getListeners($request);
+
+ $this->assertEquals(array($theListener), $listeners);
+ $this->assertEquals($theException, $exception);
+ }
+
+ public function testGetListenersWithNoMatchingEntry()
+ {
+ $map = new FirewallMap();
+
+ $request = new Request();
+
+ $notMatchingMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
+ $notMatchingMatcher
+ ->expects($this->once())
+ ->method('matches')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(false))
+ ;
+
+ $map->add($notMatchingMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
+
+ list($listeners, $exception) = $map->getListeners($request);
+
+ $this->assertEquals(array(), $listeners);
+ $this->assertNull($exception);
+ }
+}
diff --git a/Tests/Http/FirewallTest.php b/Tests/Http/FirewallTest.php
new file mode 100644
index 0000000..0c1d82c
--- /dev/null
+++ b/Tests/Http/FirewallTest.php
@@ -0,0 +1,123 @@
+<?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 Symfony\Component\Security\Tests\Http;
+
+use Symfony\Component\Security\Http\Firewall;
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+
+class FirewallTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
+ $this->markTestSkipped('The "EventDispatcher" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
+ $this->markTestSkipped('The "HttpKernel" component is not available');
+ }
+ }
+
+ public function testOnKernelRequestRegistersExceptionListener()
+ {
+ $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+
+ $listener = $this->getMock('Symfony\Component\Security\Http\Firewall\ExceptionListener', array(), array(), '', false);
+ $listener
+ ->expects($this->once())
+ ->method('register')
+ ->with($this->equalTo($dispatcher))
+ ;
+
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
+
+ $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
+ $map
+ ->expects($this->once())
+ ->method('getListeners')
+ ->with($this->equalTo($request))
+ ->will($this->returnValue(array(array(), $listener)))
+ ;
+
+ $event = new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST);
+
+ $firewall = new Firewall($map, $dispatcher);
+ $firewall->onKernelRequest($event);
+ }
+
+ public function testOnKernelRequestStopsWhenThereIsAResponse()
+ {
+ $response = $this->getMock('Symfony\Component\HttpFoundation\Response');
+
+ $first = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
+ $first
+ ->expects($this->once())
+ ->method('handle')
+ ;
+
+ $second = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
+ $second
+ ->expects($this->never())
+ ->method('handle')
+ ;
+
+ $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
+ $map
+ ->expects($this->once())
+ ->method('getListeners')
+ ->will($this->returnValue(array(array($first, $second), null)))
+ ;
+
+ $event = $this->getMock(
+ 'Symfony\Component\HttpKernel\Event\GetResponseEvent',
+ array('hasResponse'),
+ array(
+ $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
+ $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false),
+ HttpKernelInterface::MASTER_REQUEST
+ )
+ );
+ $event
+ ->expects($this->once())
+ ->method('hasResponse')
+ ->will($this->returnValue(true))
+ ;
+
+ $firewall = new Firewall($map, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
+ $firewall->onKernelRequest($event);
+ }
+
+ public function testOnKernelRequestWithSubRequest()
+ {
+ $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
+ $map
+ ->expects($this->never())
+ ->method('getListeners')
+ ;
+
+ $event = new GetResponseEvent(
+ $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
+ $this->getMock('Symfony\Component\HttpFoundation\Request'),
+ HttpKernelInterface::SUB_REQUEST
+ );
+
+ $firewall = new Firewall($map, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
+ $firewall->onKernelRequest($event);
+
+ $this->assertFalse($event->hasResponse());
+ }
+}
diff --git a/Tests/Http/HttpUtilsTest.php b/Tests/Http/HttpUtilsTest.php
new file mode 100644
index 0000000..ff6c241
--- /dev/null
+++ b/Tests/Http/HttpUtilsTest.php
@@ -0,0 +1,151 @@
+<?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 Symfony\Component\Security\Tests\Http;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Security\Http\HttpUtils;
+use Symfony\Component\Routing\Exception\ResourceNotFoundException;
+
+class HttpUtilsTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+
+ if (!class_exists('Symfony\Component\Routing\Router')) {
+ $this->markTestSkipped('The "Routing" component is not available');
+ }
+ }
+
+ public function testCreateRedirectResponse()
+ {
+ $utils = new HttpUtils($this->getRouter());
+
+ // absolute path
+ $response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
+ $this->assertTrue($response->isRedirect('http://localhost/foobar'));
+ $this->assertEquals(302, $response->getStatusCode());
+
+ // absolute URL
+ $response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
+ $this->assertTrue($response->isRedirect('http://symfony.com/'));
+
+ // route name
+ $utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
+ $router
+ ->expects($this->any())
+ ->method('generate')
+ ->with('foobar', array(), true)
+ ->will($this->returnValue('http://localhost/foo/bar'))
+ ;
+ $router
+ ->expects($this->any())
+ ->method('getContext')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
+ ;
+ $response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
+ $this->assertTrue($response->isRedirect('http://localhost/foo/bar'));
+ }
+
+ public function testCreateRequest()
+ {
+ $utils = new HttpUtils($this->getRouter());
+
+ // absolute path
+ $request = $this->getRequest();
+ $request->server->set('Foo', 'bar');
+ $subRequest = $utils->createRequest($request, '/foobar');
+
+ $this->assertEquals('GET', $subRequest->getMethod());
+ $this->assertEquals('/foobar', $subRequest->getPathInfo());
+ $this->assertEquals('bar', $subRequest->server->get('Foo'));
+
+ // route name
+ $utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
+ $router
+ ->expects($this->once())
+ ->method('generate')
+ ->will($this->returnValue('/foo/bar'))
+ ;
+ $router
+ ->expects($this->any())
+ ->method('getContext')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
+ ;
+ $subRequest = $utils->createRequest($this->getRequest(), 'foobar');
+ $this->assertEquals('/foo/bar', $subRequest->getPathInfo());
+
+ // absolute URL
+ $subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
+ $this->assertEquals('/', $subRequest->getPathInfo());
+ }
+
+ public function testCheckRequestPath()
+ {
+ $utils = new HttpUtils($this->getRouter());
+
+ $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
+ $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
+
+ $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
+ $router
+ ->expects($this->any())
+ ->method('match')
+ ->will($this->throwException(new ResourceNotFoundException()))
+ ;
+ $utils = new HttpUtils($router);
+ $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
+
+ $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
+ $router
+ ->expects($this->any())
+ ->method('match')
+ ->will($this->returnValue(array('_route' => 'foobar')))
+ ;
+ $utils = new HttpUtils($router);
+ $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testCheckRequestPathWithRouterLoadingException()
+ {
+ $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
+ $router
+ ->expects($this->any())
+ ->method('match')
+ ->will($this->throwException(new \RuntimeException()))
+ ;
+ $utils = new HttpUtils($router);
+ $utils->checkRequestPath($this->getRequest(), 'foobar');
+ }
+
+ private function getRouter()
+ {
+ $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
+ $router
+ ->expects($this->any())
+ ->method('generate')
+ ->will($this->returnValue('/foo/bar'))
+ ;
+
+ return $router;
+ }
+
+ private function getRequest($path = '/')
+ {
+ return Request::create($path, 'get');
+ }
+}
diff --git a/Tests/Http/Logout/CookieClearingLogoutHandlerTest.php b/Tests/Http/Logout/CookieClearingLogoutHandlerTest.php
new file mode 100644
index 0000000..b32a813
--- /dev/null
+++ b/Tests/Http/Logout/CookieClearingLogoutHandlerTest.php
@@ -0,0 +1,56 @@
+<?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 Symfony\Component\Security\Tests\Http\Logout;
+
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\ResponseHeaderBag;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Security\Http\Logout\CookieClearingLogoutHandler;
+
+class CookieClearingLogoutHandlerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ public function testLogout()
+ {
+ $request = new Request();
+ $response = new Response();
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+
+ $handler = new CookieClearingLogoutHandler(array('foo' => array('path' => '/foo', 'domain' => 'foo.foo'), 'foo2' => array('path' => null, 'domain' => null)));
+
+ $cookies = $response->headers->getCookies();
+ $this->assertCount(0, $cookies);
+
+ $handler->logout($request, $response, $token);
+
+ $cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
+ $this->assertCount(2, $cookies);
+
+ $cookie = $cookies['foo.foo']['/foo']['foo'];
+ $this->assertEquals('foo', $cookie->getName());
+ $this->assertEquals('/foo', $cookie->getPath());
+ $this->assertEquals('foo.foo', $cookie->getDomain());
+ $this->assertTrue($cookie->isCleared());
+
+ $cookie = $cookies['']['/']['foo2'];
+ $this->assertStringStartsWith('foo2', $cookie->getName());
+ $this->assertEquals('/', $cookie->getPath());
+ $this->assertNull($cookie->getDomain());
+ $this->assertTrue($cookie->isCleared());
+ }
+}
diff --git a/Tests/Http/Logout/SessionLogoutHandlerTest.php b/Tests/Http/Logout/SessionLogoutHandlerTest.php
new file mode 100644
index 0000000..8e2dd28
--- /dev/null
+++ b/Tests/Http/Logout/SessionLogoutHandlerTest.php
@@ -0,0 +1,47 @@
+<?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 Symfony\Component\Security\Tests\Http\Logout;
+
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Http\Logout\SessionLogoutHandler;
+
+class SessionLogoutHandlerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ public function testLogout()
+ {
+ $handler = new SessionLogoutHandler();
+
+ $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
+ $response = new Response();
+ $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array(), array(), '', false);
+
+ $request
+ ->expects($this->once())
+ ->method('getSession')
+ ->will($this->returnValue($session))
+ ;
+
+ $session
+ ->expects($this->once())
+ ->method('invalidate')
+ ;
+
+ $handler->logout($request, $response, $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
+ }
+}
diff --git a/Tests/Http/RememberMe/AbstractRememberMeServicesTest.php b/Tests/Http/RememberMe/AbstractRememberMeServicesTest.php
new file mode 100644
index 0000000..fc8dffb
--- /dev/null
+++ b/Tests/Http/RememberMe/AbstractRememberMeServicesTest.php
@@ -0,0 +1,268 @@
+<?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 Symfony\Component\Security\Tests\Http\RememberMe;
+
+use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+
+class AbstractRememberMeServicesTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ public function testGetRememberMeParameter()
+ {
+ $service = $this->getService(null, array('remember_me_parameter' => 'foo'));
+
+ $this->assertEquals('foo', $service->getRememberMeParameter());
+ }
+
+ public function testGetKey()
+ {
+ $service = $this->getService();
+ $this->assertEquals('fookey', $service->getKey());
+ }
+
+ public function testAutoLoginReturnsNullWhenNoCookie()
+ {
+ $service = $this->getService(null, array('name' => 'foo'));
+
+ $this->assertNull($service->autoLogin(new Request()));
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testAutoLoginThrowsExceptionWhenImplementationDoesNotReturnUserInterface()
+ {
+ $service = $this->getService(null, array('name' => 'foo'));
+ $request = new Request;
+ $request->cookies->set('foo', 'foo');
+
+ $service
+ ->expects($this->once())
+ ->method('processAutoLoginCookie')
+ ->will($this->returnValue(null))
+ ;
+
+ $service->autoLogin($request);
+ }
+
+ public function testAutoLogin()
+ {
+ $service = $this->getService(null, array('name' => 'foo'));
+ $request = new Request();
+ $request->cookies->set('foo', 'foo');
+
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->once())
+ ->method('getRoles')
+ ->will($this->returnValue(array()))
+ ;
+
+ $service
+ ->expects($this->once())
+ ->method('processAutoLoginCookie')
+ ->will($this->returnValue($user))
+ ;
+
+ $returnedToken = $service->autoLogin($request);
+
+ $this->assertSame($user, $returnedToken->getUser());
+ $this->assertSame('fookey', $returnedToken->getKey());
+ $this->assertSame('fookey', $returnedToken->getProviderKey());
+ }
+
+ public function testLogout()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
+ $request = new Request();
+ $response = new Response();
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+
+ $service->logout($request, $response, $token);
+
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testLoginFail()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
+ $request = new Request();
+
+ $service->loginFail($request);
+
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testLoginSuccessIsNotProcessedWhenTokenDoesNotContainUserInterfaceImplementation()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
+ $request = new Request;
+ $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('foo'))
+ ;
+
+ $service
+ ->expects($this->never())
+ ->method('onLoginSuccess')
+ ;
+
+ $this->assertFalse($request->request->has('foo'));
+
+ $service->loginSuccess($request, $response, $token);
+ }
+
+ public function testLoginSuccessIsNotProcessedWhenRememberMeIsNotRequested()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
+ $request = new Request;
+ $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->never())
+ ->method('onLoginSuccess')
+ ->will($this->returnValue(null))
+ ;
+
+ $this->assertFalse($request->request->has('foo'));
+
+ $service->loginSuccess($request, $response, $token);
+ }
+
+ public function testLoginSuccessWhenRememberMeAlwaysIsTrue()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
+ $request = new Request;
+ $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(null))
+ ;
+
+ $service->loginSuccess($request, $response, $token);
+ }
+
+ /**
+ * @dataProvider getPositiveRememberMeParameterValues
+ */
+ public function testLoginSuccessWhenRememberMeParameterWithPathIsPositive($value)
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo[bar]'));
+
+ $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);
+ }
+
+ /**
+ * @dataProvider getPositiveRememberMeParameterValues
+ */
+ public function testLoginSuccessWhenRememberMeParameterIsPositive($value)
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
+
+ $request = new Request;
+ $request->request->set('foo', $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);
+ }
+
+ 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();
+ }
+
+ return $this->getMockForAbstractClass('Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices', array(
+ array($userProvider), 'fookey', 'fookey', $options, $logger
+ ));
+ }
+
+ protected function getProvider()
+ {
+ $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
+ $provider
+ ->expects($this->any())
+ ->method('supportsClass')
+ ->will($this->returnValue(true))
+ ;
+
+ return $provider;
+ }
+}
diff --git a/Tests/Http/RememberMe/PersistentTokenBasedRememberMeServicesTest.php b/Tests/Http/RememberMe/PersistentTokenBasedRememberMeServicesTest.php
new file mode 100644
index 0000000..3b3691d
--- /dev/null
+++ b/Tests/Http/RememberMe/PersistentTokenBasedRememberMeServicesTest.php
@@ -0,0 +1,335 @@
+<?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 Symfony\Component\Security\Tests\Http\RememberMe;
+
+use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
+
+use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
+use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
+use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\ResponseHeaderBag;
+use Symfony\Component\Security\Http\RememberMe\PersistentTokenBasedRememberMeServices;
+use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
+use Symfony\Component\Security\Core\Exception\CookieTheftException;
+
+class PersistentTokenBasedRememberMeServicesTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ public function testAutoLoginReturnsNullWhenNoCookie()
+ {
+ $service = $this->getService(null, array('name' => 'foo'));
+
+ $this->assertNull($service->autoLogin(new Request()));
+ }
+
+ public function testAutoLoginThrowsExceptionOnInvalidCookie()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
+ $request = new Request;
+ $request->request->set('foo', 'true');
+ $request->cookies->set('foo', 'foo');
+
+ $this->assertNull($service->autoLogin($request));
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testAutoLoginThrowsExceptionOnNonExistentToken()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
+ $request = new Request;
+ $request->request->set('foo', 'true');
+ $request->cookies->set('foo', $this->encodeCookie(array(
+ $series = 'fooseries',
+ $tokenValue = 'foovalue',
+ )));
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $tokenProvider
+ ->expects($this->once())
+ ->method('loadTokenBySeries')
+ ->will($this->throwException(new TokenNotFoundException('Token not found.')))
+ ;
+ $service->setTokenProvider($tokenProvider);
+
+ $this->assertNull($service->autoLogin($request));
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testAutoLoginReturnsNullOnNonExistentUser()
+ {
+ $userProvider = $this->getProvider();
+ $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600, 'secure' => false, 'httponly' => false));
+ $request = new Request;
+ $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $tokenProvider
+ ->expects($this->once())
+ ->method('loadTokenBySeries')
+ ->will($this->returnValue(new PersistentToken('fooclass', 'fooname', 'fooseries', 'foovalue', new \DateTime())))
+ ;
+ $service->setTokenProvider($tokenProvider);
+
+ $userProvider
+ ->expects($this->once())
+ ->method('loadUserByUsername')
+ ->will($this->throwException(new UsernameNotFoundException('user not found')))
+ ;
+
+ $this->assertNull($service->autoLogin($request));
+ $this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
+ }
+
+ public function testAutoLoginThrowsExceptionOnStolenCookieAndRemovesItFromThePersistentBackend()
+ {
+ $userProvider = $this->getProvider();
+ $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true));
+ $request = new Request;
+ $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $service->setTokenProvider($tokenProvider);
+
+ $tokenProvider
+ ->expects($this->once())
+ ->method('loadTokenBySeries')
+ ->will($this->returnValue(new PersistentToken('fooclass', 'foouser', 'fooseries', 'anotherFooValue', new \DateTime())))
+ ;
+
+ $tokenProvider
+ ->expects($this->once())
+ ->method('deleteTokenBySeries')
+ ->with($this->equalTo('fooseries'))
+ ->will($this->returnValue(null))
+ ;
+
+ try {
+ $service->autoLogin($request);
+ $this->fail('Expected CookieTheftException was not thrown.');
+ } catch (CookieTheftException $theft) { }
+
+ $this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
+ }
+
+ public function testAutoLoginDoesNotAcceptAnExpiredCookie()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
+ $request = new Request;
+ $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $tokenProvider
+ ->expects($this->once())
+ ->method('loadTokenBySeries')
+ ->with($this->equalTo('fooseries'))
+ ->will($this->returnValue(new PersistentToken('fooclass', 'username', 'fooseries', 'foovalue', new \DateTime('yesterday'))))
+ ;
+ $service->setTokenProvider($tokenProvider);
+
+ $this->assertNull($service->autoLogin($request));
+ $this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
+ }
+
+ public function testAutoLogin()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->once())
+ ->method('getRoles')
+ ->will($this->returnValue(array('ROLE_FOO')))
+ ;
+
+ $userProvider = $this->getProvider();
+ $userProvider
+ ->expects($this->once())
+ ->method('loadUserByUsername')
+ ->with($this->equalTo('foouser'))
+ ->will($this->returnValue($user))
+ ;
+
+ $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'secure' => false, 'httponly' => false, 'always_remember_me' => true, 'lifetime' => 3600));
+ $request = new Request;
+ $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $tokenProvider
+ ->expects($this->once())
+ ->method('loadTokenBySeries')
+ ->with($this->equalTo('fooseries'))
+ ->will($this->returnValue(new PersistentToken('fooclass', 'foouser', 'fooseries', 'foovalue', new \DateTime())))
+ ;
+ $service->setTokenProvider($tokenProvider);
+
+ $returnedToken = $service->autoLogin($request);
+
+ $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', $returnedToken);
+ $this->assertSame($user, $returnedToken->getUser());
+ $this->assertEquals('fookey', $returnedToken->getKey());
+ $this->assertTrue($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
+ }
+
+ public function testLogout()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => '/foo', 'domain' => 'foodomain.foo'));
+ $request = new Request();
+ $request->cookies->set('foo', $this->encodeCookie(array('fooseries', 'foovalue')));
+ $response = new Response();
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $tokenProvider
+ ->expects($this->once())
+ ->method('deleteTokenBySeries')
+ ->with($this->equalTo('fooseries'))
+ ->will($this->returnValue(null))
+ ;
+ $service->setTokenProvider($tokenProvider);
+
+ $service->logout($request, $response, $token);
+
+ $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
+ $this->assertTrue($cookie->isCleared());
+ $this->assertEquals('/foo', $cookie->getPath());
+ $this->assertEquals('foodomain.foo', $cookie->getDomain());
+ }
+
+ public function testLogoutSimplyIgnoresNonSetRequestCookie()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
+ $request = new Request;
+ $response = new Response;
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $tokenProvider
+ ->expects($this->never())
+ ->method('deleteTokenBySeries')
+ ;
+ $service->setTokenProvider($tokenProvider);
+
+ $service->logout($request, $response, $token);
+
+ $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
+ $this->assertTrue($cookie->isCleared());
+ $this->assertEquals('/', $cookie->getPath());
+ $this->assertNull($cookie->getDomain());
+ }
+
+ public function testLogoutSimplyIgnoresInvalidCookie()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
+ $request = new Request;
+ $request->cookies->set('foo', 'somefoovalue');
+ $response = new Response;
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $tokenProvider
+ ->expects($this->never())
+ ->method('deleteTokenBySeries')
+ ;
+ $service->setTokenProvider($tokenProvider);
+
+ $service->logout($request, $response, $token);
+
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testLoginFail()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
+ $request = new Request();
+
+ $this->assertFalse($request->attributes->has(RememberMeServicesInterface::COOKIE_ATTR_NAME));
+ $service->loginFail($request);
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testLoginSuccessSetsCookieWhenLoggedInWithNonRememberMeTokenInterfaceImplementation()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600, 'always_remember_me' => true));
+ $request = new Request;
+ $response = new Response;
+
+ $account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $account
+ ->expects($this->once())
+ ->method('getUsername')
+ ->will($this->returnValue('foo'))
+ ;
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $token
+ ->expects($this->any())
+ ->method('getUser')
+ ->will($this->returnValue($account))
+ ;
+
+ $tokenProvider = $this->getMock('Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface');
+ $tokenProvider
+ ->expects($this->once())
+ ->method('createNewToken')
+ ;
+ $service->setTokenProvider($tokenProvider);
+
+ $cookies = $response->headers->getCookies();
+ $this->assertCount(0, $cookies);
+
+ $service->loginSuccess($request, $response, $token);
+
+ $cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
+ $cookie = $cookies['myfoodomain.foo']['/foo/path']['foo'];
+ $this->assertFalse($cookie->isCleared());
+ $this->assertTrue($cookie->isSecure());
+ $this->assertTrue($cookie->isHttpOnly());
+ $this->assertTrue($cookie->getExpiresTime() > time() + 3590 && $cookie->getExpiresTime() < time() + 3610);
+ $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
+ $this->assertEquals('/foo/path', $cookie->getPath());
+ }
+
+ protected function encodeCookie(array $parts)
+ {
+ $service = $this->getService();
+ $r = new \ReflectionMethod($service, 'encodeCookie');
+ $r->setAccessible(true);
+
+ return $r->invoke($service, $parts);
+ }
+
+ protected function getService($userProvider = null, $options = array(), $logger = null)
+ {
+ if (null === $userProvider) {
+ $userProvider = $this->getProvider();
+ }
+
+ return new PersistentTokenBasedRememberMeServices(array($userProvider), 'fookey', 'fookey', $options, $logger);
+ }
+
+ protected function getProvider()
+ {
+ $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
+ $provider
+ ->expects($this->any())
+ ->method('supportsClass')
+ ->will($this->returnValue(true))
+ ;
+
+ return $provider;
+ }
+}
diff --git a/Tests/Http/RememberMe/TokenBasedRememberMeServicesTest.php b/Tests/Http/RememberMe/TokenBasedRememberMeServicesTest.php
new file mode 100644
index 0000000..407db02
--- /dev/null
+++ b/Tests/Http/RememberMe/TokenBasedRememberMeServicesTest.php
@@ -0,0 +1,280 @@
+<?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 Symfony\Component\Security\Tests\Http\RememberMe;
+
+use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
+
+use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
+use Symfony\Component\Security\Core\Authentication\Token\Token;
+use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\ResponseHeaderBag;
+use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
+
+class TokenBasedRememberMeServicesTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ public function testAutoLoginReturnsNullWhenNoCookie()
+ {
+ $service = $this->getService(null, array('name' => 'foo'));
+
+ $this->assertNull($service->autoLogin(new Request()));
+ }
+
+ public function testAutoLoginThrowsExceptionOnInvalidCookie()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
+ $request = new Request;
+ $request->request->set('foo', 'true');
+ $request->cookies->set('foo', 'foo');
+
+ $this->assertNull($service->autoLogin($request));
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testAutoLoginThrowsExceptionOnNonExistentUser()
+ {
+ $userProvider = $this->getProvider();
+ $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
+ $request = new Request;
+ $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time()+3600, 'foopass'));
+
+ $userProvider
+ ->expects($this->once())
+ ->method('loadUserByUsername')
+ ->will($this->throwException(new UsernameNotFoundException('user not found')))
+ ;
+
+ $this->assertNull($service->autoLogin($request));
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testAutoLoginDoesNotAcceptCookieWithInvalidHash()
+ {
+ $userProvider = $this->getProvider();
+ $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
+ $request = new Request;
+ $request->cookies->set('foo', base64_encode('class:'.base64_encode('foouser').':123456789:fooHash'));
+
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->once())
+ ->method('getPassword')
+ ->will($this->returnValue('foopass'))
+ ;
+
+ $userProvider
+ ->expects($this->once())
+ ->method('loadUserByUsername')
+ ->with($this->equalTo('foouser'))
+ ->will($this->returnValue($user))
+ ;
+
+ $this->assertNull($service->autoLogin($request));
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testAutoLoginDoesNotAcceptAnExpiredCookie()
+ {
+ $userProvider = $this->getProvider();
+ $service = $this->getService($userProvider, array('name' => 'foo', 'path' => null, 'domain' => null, 'always_remember_me' => true, 'lifetime' => 3600));
+ $request = new Request;
+ $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time() - 1, 'foopass'));
+
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->once())
+ ->method('getPassword')
+ ->will($this->returnValue('foopass'))
+ ;
+
+ $userProvider
+ ->expects($this->once())
+ ->method('loadUserByUsername')
+ ->with($this->equalTo('foouser'))
+ ->will($this->returnValue($user))
+ ;
+
+ $this->assertNull($service->autoLogin($request));
+ $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
+ }
+
+ public function testAutoLogin()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->once())
+ ->method('getRoles')
+ ->will($this->returnValue(array('ROLE_FOO')))
+ ;
+ $user
+ ->expects($this->once())
+ ->method('getPassword')
+ ->will($this->returnValue('foopass'))
+ ;
+
+ $userProvider = $this->getProvider();
+ $userProvider
+ ->expects($this->once())
+ ->method('loadUserByUsername')
+ ->with($this->equalTo('foouser'))
+ ->will($this->returnValue($user))
+ ;
+
+ $service = $this->getService($userProvider, array('name' => 'foo', 'always_remember_me' => true, 'lifetime' => 3600));
+ $request = new Request;
+ $request->cookies->set('foo', $this->getCookie('fooclass', 'foouser', time()+3600, 'foopass'));
+
+ $returnedToken = $service->autoLogin($request);
+
+ $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', $returnedToken);
+ $this->assertSame($user, $returnedToken->getUser());
+ $this->assertEquals('fookey', $returnedToken->getKey());
+ }
+
+ public function testLogout()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
+ $request = new Request();
+ $response = new Response();
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+
+ $service->logout($request, $response, $token);
+
+ $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
+ $this->assertTrue($cookie->isCleared());
+ $this->assertEquals('/', $cookie->getPath());
+ $this->assertNull($cookie->getDomain());
+ }
+
+ public function testLoginFail()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'path' => '/foo', 'domain' => 'foodomain.foo'));
+ $request = new Request();
+ $response = new Response();
+
+ $service->loginFail($request, $response);
+
+ $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
+ $this->assertTrue($cookie->isCleared());
+ $this->assertEquals('/foo', $cookie->getPath());
+ $this->assertEquals('foodomain.foo', $cookie->getDomain());
+ }
+
+ public function testLoginSuccessIgnoresTokensWhichDoNotContainAnUserInterfaceImplementation()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
+ $request = new Request;
+ $response = new Response;
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $token
+ ->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue('foo'))
+ ;
+
+ $cookies = $response->headers->getCookies();
+ $this->assertCount(0, $cookies);
+
+ $service->loginSuccess($request, $response, $token);
+
+ $cookies = $response->headers->getCookies();
+ $this->assertCount(0, $cookies);
+ }
+
+ public function testLoginSuccess()
+ {
+ $service = $this->getService(null, array('name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600, 'always_remember_me' => true));
+ $request = new Request;
+ $response = new Response;
+
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->once())
+ ->method('getPassword')
+ ->will($this->returnValue('foopass'))
+ ;
+ $user
+ ->expects($this->once())
+ ->method('getUsername')
+ ->will($this->returnValue('foouser'))
+ ;
+ $token
+ ->expects($this->atLeastOnce())
+ ->method('getUser')
+ ->will($this->returnValue($user))
+ ;
+
+ $cookies = $response->headers->getCookies();
+ $this->assertCount(0, $cookies);
+
+ $service->loginSuccess($request, $response, $token);
+
+ $cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
+ $cookie = $cookies['myfoodomain.foo']['/foo/path']['foo'];
+ $this->assertFalse($cookie->isCleared());
+ $this->assertTrue($cookie->isSecure());
+ $this->assertTrue($cookie->isHttpOnly());
+ $this->assertTrue($cookie->getExpiresTime() > time() + 3590 && $cookie->getExpiresTime() < time() + 3610);
+ $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
+ $this->assertEquals('/foo/path', $cookie->getPath());
+ }
+
+ protected function getCookie($class, $username, $expires, $password)
+ {
+ $service = $this->getService();
+ $r = new \ReflectionMethod($service, 'generateCookieValue');
+ $r->setAccessible(true);
+
+ return $r->invoke($service, $class, $username, $expires, $password);
+ }
+
+ protected function encodeCookie(array $parts)
+ {
+ $service = $this->getService();
+ $r = new \ReflectionMethod($service, 'encodeCookie');
+ $r->setAccessible(true);
+
+ return $r->invoke($service, $parts);
+ }
+
+ protected function getService($userProvider = null, $options = array(), $logger = null)
+ {
+ if (null === $userProvider) {
+ $userProvider = $this->getProvider();
+ }
+
+ $service = new TokenBasedRememberMeServices(array($userProvider), 'fookey', 'fookey', $options, $logger);
+
+ return $service;
+ }
+
+ protected function getProvider()
+ {
+ $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
+ $provider
+ ->expects($this->any())
+ ->method('supportsClass')
+ ->will($this->returnValue(true))
+ ;
+
+ return $provider;
+ }
+}