summaryrefslogtreecommitdiffstats
path: root/Core/Tests/Authentication
diff options
context:
space:
mode:
authorBernhard Schussek <bschussek@gmail.com>2013-09-16 10:03:00 +0200
committerFabien Potencier <fabien.potencier@gmail.com>2013-09-18 09:16:41 +0200
commit5a6aaab2c35213f5ca7e57f061fbb2675e2ece35 (patch)
tree461816fef8160401dc113d3fef190fb437d01cc7 /Core/Tests/Authentication
parent513a354be10f0ed87933adcb788e48660f8e6ed4 (diff)
downloadsymfony-security-5a6aaab2c35213f5ca7e57f061fbb2675e2ece35.zip
symfony-security-5a6aaab2c35213f5ca7e57f061fbb2675e2ece35.tar.gz
symfony-security-5a6aaab2c35213f5ca7e57f061fbb2675e2ece35.tar.bz2
[Security] Split the component into 3 sub-components Core, ACL, HTTP
Diffstat (limited to 'Core/Tests/Authentication')
-rw-r--r--Core/Tests/Authentication/AuthenticationProviderManagerTest.php138
-rw-r--r--Core/Tests/Authentication/AuthenticationTrustResolverTest.php72
-rw-r--r--Core/Tests/Authentication/Provider/AnonymousAuthenticationProviderTest.php66
-rw-r--r--Core/Tests/Authentication/Provider/DaoAuthenticationProviderTest.php300
-rw-r--r--Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php133
-rw-r--r--Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php111
-rw-r--r--Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php206
-rw-r--r--Core/Tests/Authentication/RememberMe/InMemoryTokenProviderTest.php63
-rw-r--r--Core/Tests/Authentication/RememberMe/PersistentTokenTest.php29
-rw-r--r--Core/Tests/Authentication/Token/AbstractTokenTest.php244
-rw-r--r--Core/Tests/Authentication/Token/AnonymousTokenTest.php45
-rw-r--r--Core/Tests/Authentication/Token/PreAuthenticatedTokenTest.php48
-rw-r--r--Core/Tests/Authentication/Token/RememerMeTokenTest.php83
-rw-r--r--Core/Tests/Authentication/Token/UsernamePasswordTokenTest.php58
14 files changed, 1596 insertions, 0 deletions
diff --git a/Core/Tests/Authentication/AuthenticationProviderManagerTest.php b/Core/Tests/Authentication/AuthenticationProviderManagerTest.php
new file mode 100644
index 0000000..f3aaa85
--- /dev/null
+++ b/Core/Tests/Authentication/AuthenticationProviderManagerTest.php
@@ -0,0 +1,138 @@
+<?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\Core\Tests\Authentication;
+
+use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
+use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use Symfony\Component\Security\Core\Exception\AccountStatusException;
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
+
+class AuthenticationProviderManagerTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testAuthenticateWithoutProviders()
+ {
+ new AuthenticationProviderManager(array());
+ }
+
+ public function testAuthenticateWhenNoProviderSupportsToken()
+ {
+ $manager = new AuthenticationProviderManager(array(
+ $this->getAuthenticationProvider(false),
+ ));
+
+ try {
+ $manager->authenticate($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
+ $this->fail();
+ } catch (ProviderNotFoundException $e) {
+ $this->assertSame($token, $e->getToken());
+ }
+ }
+
+ public function testAuthenticateWhenProviderReturnsAccountStatusException()
+ {
+ $manager = new AuthenticationProviderManager(array(
+ $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AccountStatusException'),
+ ));
+
+ try {
+ $manager->authenticate($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
+ $this->fail();
+ } catch (AccountStatusException $e) {
+ $this->assertSame($token, $e->getToken());
+ }
+ }
+
+ public function testAuthenticateWhenProviderReturnsAuthenticationException()
+ {
+ $manager = new AuthenticationProviderManager(array(
+ $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AuthenticationException'),
+ ));
+
+ try {
+ $manager->authenticate($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
+ $this->fail();
+ } catch (AuthenticationException $e) {
+ $this->assertSame($token, $e->getToken());
+ }
+ }
+
+ public function testAuthenticateWhenOneReturnsAuthenticationExceptionButNotAll()
+ {
+ $manager = new AuthenticationProviderManager(array(
+ $this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AuthenticationException'),
+ $this->getAuthenticationProvider(true, $expected = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')),
+ ));
+
+ $token = $manager->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
+ $this->assertSame($expected, $token);
+ }
+
+ public function testAuthenticateReturnsTokenOfTheFirstMatchingProvider()
+ {
+ $second = $this->getMock('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface');
+ $second
+ ->expects($this->never())
+ ->method('supports')
+ ;
+ $manager = new AuthenticationProviderManager(array(
+ $this->getAuthenticationProvider(true, $expected = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')),
+ $second,
+ ));
+
+ $token = $manager->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
+ $this->assertSame($expected, $token);
+ }
+
+ public function testEraseCredentialFlag()
+ {
+ $manager = new AuthenticationProviderManager(array(
+ $this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar', 'key')),
+ ));
+
+ $token = $manager->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
+ $this->assertEquals('', $token->getCredentials());
+
+ $manager = new AuthenticationProviderManager(array(
+ $this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar', 'key')),
+ ), false);
+
+ $token = $manager->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
+ $this->assertEquals('bar', $token->getCredentials());
+ }
+
+ protected function getAuthenticationProvider($supports, $token = null, $exception = null)
+ {
+ $provider = $this->getMock('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface');
+ $provider->expects($this->once())
+ ->method('supports')
+ ->will($this->returnValue($supports))
+ ;
+
+ if (null !== $token) {
+ $provider->expects($this->once())
+ ->method('authenticate')
+ ->will($this->returnValue($token))
+ ;
+ } elseif (null !== $exception) {
+ $provider->expects($this->once())
+ ->method('authenticate')
+ ->will($this->throwException($this->getMock($exception, null, array(), '', false)))
+ ;
+ }
+
+ return $provider;
+ }
+}
diff --git a/Core/Tests/Authentication/AuthenticationTrustResolverTest.php b/Core/Tests/Authentication/AuthenticationTrustResolverTest.php
new file mode 100644
index 0000000..07ce08b
--- /dev/null
+++ b/Core/Tests/Authentication/AuthenticationTrustResolverTest.php
@@ -0,0 +1,72 @@
+<?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\Core\Tests\Authentication;
+
+use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
+use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
+use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
+
+class AuthenticationTrustResolverTest extends \PHPUnit_Framework_TestCase
+{
+ public function testIsAnonymous()
+ {
+ $resolver = $this->getResolver();
+
+ $this->assertFalse($resolver->isAnonymous(null));
+ $this->assertFalse($resolver->isAnonymous($this->getToken()));
+ $this->assertFalse($resolver->isAnonymous($this->getRememberMeToken()));
+ $this->assertTrue($resolver->isAnonymous($this->getAnonymousToken()));
+ }
+
+ public function testIsRememberMe()
+ {
+ $resolver = $this->getResolver();
+
+ $this->assertFalse($resolver->isRememberMe(null));
+ $this->assertFalse($resolver->isRememberMe($this->getToken()));
+ $this->assertFalse($resolver->isRememberMe($this->getAnonymousToken()));
+ $this->assertTrue($resolver->isRememberMe($this->getRememberMeToken()));
+ }
+
+ public function testisFullFledged()
+ {
+ $resolver = $this->getResolver();
+
+ $this->assertFalse($resolver->isFullFledged(null));
+ $this->assertFalse($resolver->isFullFledged($this->getAnonymousToken()));
+ $this->assertFalse($resolver->isFullFledged($this->getRememberMeToken()));
+ $this->assertTrue($resolver->isFullFledged($this->getToken()));
+ }
+
+ protected function getToken()
+ {
+ return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ }
+
+ protected function getAnonymousToken()
+ {
+ return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken', null, array('', ''));
+ }
+
+ protected function getRememberMeToken()
+ {
+ return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', array('setPersistent'), array(), '', false);
+ }
+
+ protected function getResolver()
+ {
+ return new AuthenticationTrustResolver(
+ 'Symfony\\Component\\Security\\Core\\Authentication\\Token\\AnonymousToken',
+ 'Symfony\\Component\\Security\\Core\\Authentication\\Token\\RememberMeToken'
+ );
+ }
+}
diff --git a/Core/Tests/Authentication/Provider/AnonymousAuthenticationProviderTest.php b/Core/Tests/Authentication/Provider/AnonymousAuthenticationProviderTest.php
new file mode 100644
index 0000000..5a189b0
--- /dev/null
+++ b/Core/Tests/Authentication/Provider/AnonymousAuthenticationProviderTest.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\Core\Tests\Authentication\Provider;
+
+use Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider;
+
+class AnonymousAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testSupports()
+ {
+ $provider = $this->getProvider('foo');
+
+ $this->assertTrue($provider->supports($this->getSupportedToken('foo')));
+ $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
+ }
+
+ public function testAuthenticateWhenTokenIsNotSupported()
+ {
+ $provider = $this->getProvider('foo');
+
+ $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ */
+ public function testAuthenticateWhenKeyIsNotValid()
+ {
+ $provider = $this->getProvider('foo');
+
+ $this->assertNull($provider->authenticate($this->getSupportedToken('bar')));
+ }
+
+ public function testAuthenticate()
+ {
+ $provider = $this->getProvider('foo');
+ $token = $this->getSupportedToken('foo');
+
+ $this->assertSame($token, $provider->authenticate($token));
+ }
+
+ protected function getSupportedToken($key)
+ {
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken', array('getKey'), array(), '', false);
+ $token->expects($this->any())
+ ->method('getKey')
+ ->will($this->returnValue($key))
+ ;
+
+ return $token;
+ }
+
+ protected function getProvider($key)
+ {
+ return new AnonymousAuthenticationProvider($key);
+ }
+}
diff --git a/Core/Tests/Authentication/Provider/DaoAuthenticationProviderTest.php b/Core/Tests/Authentication/Provider/DaoAuthenticationProviderTest.php
new file mode 100644
index 0000000..ed4fe10
--- /dev/null
+++ b/Core/Tests/Authentication/Provider/DaoAuthenticationProviderTest.php
@@ -0,0 +1,300 @@
+<?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\Core\Tests\Authentication\Provider;
+
+use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
+
+use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
+
+class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
+ */
+ public function testRetrieveUserWhenProviderDoesNotReturnAnUserInterface()
+ {
+ $provider = $this->getProvider('fabien');
+ $method = new \ReflectionMethod($provider, 'retrieveUser');
+ $method->setAccessible(true);
+
+ $method->invoke($provider, 'fabien', $this->getSupportedToken());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
+ */
+ public function testRetrieveUserWhenUsernameIsNotFound()
+ {
+ $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
+ $userProvider->expects($this->once())
+ ->method('loadUserByUsername')
+ ->will($this->throwException($this->getMock('Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException', null, array(), '', false)))
+ ;
+
+ $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
+ $method = new \ReflectionMethod($provider, 'retrieveUser');
+ $method->setAccessible(true);
+
+ $method->invoke($provider, 'fabien', $this->getSupportedToken());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
+ */
+ public function testRetrieveUserWhenAnExceptionOccurs()
+ {
+ $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
+ $userProvider->expects($this->once())
+ ->method('loadUserByUsername')
+ ->will($this->throwException($this->getMock('RuntimeException', null, array(), '', false)))
+ ;
+
+ $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
+ $method = new \ReflectionMethod($provider, 'retrieveUser');
+ $method->setAccessible(true);
+
+ $method->invoke($provider, 'fabien', $this->getSupportedToken());
+ }
+
+ public function testRetrieveUserReturnsUserFromTokenOnReauthentication()
+ {
+ $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
+ $userProvider->expects($this->never())
+ ->method('loadUserByUsername')
+ ;
+
+ $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
+ $token = $this->getSupportedToken();
+ $token->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user))
+ ;
+
+ $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
+ $reflection = new \ReflectionMethod($provider, 'retrieveUser');
+ $reflection->setAccessible(true);
+ $result = $reflection->invoke($provider, null, $token);
+
+ $this->assertSame($user, $result);
+ }
+
+ public function testRetrieveUser()
+ {
+ $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
+
+ $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
+ $userProvider->expects($this->once())
+ ->method('loadUserByUsername')
+ ->will($this->returnValue($user))
+ ;
+
+ $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
+ $method = new \ReflectionMethod($provider, 'retrieveUser');
+ $method->setAccessible(true);
+
+ $this->assertSame($user, $method->invoke($provider, 'fabien', $this->getSupportedToken()));
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ */
+ public function testCheckAuthenticationWhenCredentialsAreEmpty()
+ {
+ $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
+ $encoder
+ ->expects($this->never())
+ ->method('isPasswordValid')
+ ;
+
+ $provider = $this->getProvider(false, false, $encoder);
+ $method = new \ReflectionMethod($provider, 'checkAuthentication');
+ $method->setAccessible(true);
+
+ $token = $this->getSupportedToken();
+ $token
+ ->expects($this->once())
+ ->method('getCredentials')
+ ->will($this->returnValue(''))
+ ;
+
+ $method->invoke(
+ $provider,
+ $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'),
+ $token
+ );
+ }
+
+ public function testCheckAuthenticationWhenCredentialsAre0()
+ {
+ $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
+ $encoder
+ ->expects($this->once())
+ ->method('isPasswordValid')
+ ->will($this->returnValue(true))
+ ;
+
+ $provider = $this->getProvider(false, false, $encoder);
+ $method = new \ReflectionMethod($provider, 'checkAuthentication');
+ $method->setAccessible(true);
+
+ $token = $this->getSupportedToken();
+ $token
+ ->expects($this->once())
+ ->method('getCredentials')
+ ->will($this->returnValue('0'))
+ ;
+
+ $method->invoke(
+ $provider,
+ $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'),
+ $token
+ );
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ */
+ public function testCheckAuthenticationWhenCredentialsAreNotValid()
+ {
+ $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
+ $encoder->expects($this->once())
+ ->method('isPasswordValid')
+ ->will($this->returnValue(false))
+ ;
+
+ $provider = $this->getProvider(false, false, $encoder);
+ $method = new \ReflectionMethod($provider, 'checkAuthentication');
+ $method->setAccessible(true);
+
+ $token = $this->getSupportedToken();
+ $token->expects($this->once())
+ ->method('getCredentials')
+ ->will($this->returnValue('foo'))
+ ;
+
+ $method->invoke($provider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'), $token);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ */
+ public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChanged()
+ {
+ $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
+ $user->expects($this->once())
+ ->method('getPassword')
+ ->will($this->returnValue('foo'))
+ ;
+
+ $token = $this->getSupportedToken();
+ $token->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $dbUser = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
+ $dbUser->expects($this->once())
+ ->method('getPassword')
+ ->will($this->returnValue('newFoo'))
+ ;
+
+ $provider = $this->getProvider(false, false, null);
+ $reflection = new \ReflectionMethod($provider, 'checkAuthentication');
+ $reflection->setAccessible(true);
+ $reflection->invoke($provider, $dbUser, $token);
+ }
+
+ public function testCheckAuthenticationWhenTokenNeedsReauthenticationWorksWithoutOriginalCredentials()
+ {
+ $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
+ $user->expects($this->once())
+ ->method('getPassword')
+ ->will($this->returnValue('foo'))
+ ;
+
+ $token = $this->getSupportedToken();
+ $token->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+
+ $dbUser = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
+ $dbUser->expects($this->once())
+ ->method('getPassword')
+ ->will($this->returnValue('foo'))
+ ;
+
+ $provider = $this->getProvider(false, false, null);
+ $reflection = new \ReflectionMethod($provider, 'checkAuthentication');
+ $reflection->setAccessible(true);
+ $reflection->invoke($provider, $dbUser, $token);
+ }
+
+ public function testCheckAuthentication()
+ {
+ $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
+ $encoder->expects($this->once())
+ ->method('isPasswordValid')
+ ->will($this->returnValue(true))
+ ;
+
+ $provider = $this->getProvider(false, false, $encoder);
+ $method = new \ReflectionMethod($provider, 'checkAuthentication');
+ $method->setAccessible(true);
+
+ $token = $this->getSupportedToken();
+ $token->expects($this->once())
+ ->method('getCredentials')
+ ->will($this->returnValue('foo'))
+ ;
+
+ $method->invoke($provider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'), $token);
+ }
+
+ protected function getSupportedToken()
+ {
+ $mock = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken', array('getCredentials', 'getUser', 'getProviderKey'), array(), '', false);
+ $mock
+ ->expects($this->any())
+ ->method('getProviderKey')
+ ->will($this->returnValue('key'))
+ ;
+
+ return $mock;
+ }
+
+ protected function getProvider($user = false, $userChecker = false, $passwordEncoder = null)
+ {
+ $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
+ if (false !== $user) {
+ $userProvider->expects($this->once())
+ ->method('loadUserByUsername')
+ ->will($this->returnValue($user))
+ ;
+ }
+
+ if (false === $userChecker) {
+ $userChecker = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface');
+ }
+
+ if (null === $passwordEncoder) {
+ $passwordEncoder = new PlaintextPasswordEncoder();
+ }
+
+ $encoderFactory = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface');
+ $encoderFactory
+ ->expects($this->any())
+ ->method('getEncoder')
+ ->will($this->returnValue($passwordEncoder))
+ ;
+
+ return new DaoAuthenticationProvider($userProvider, $userChecker, 'key', $encoderFactory);
+ }
+}
diff --git a/Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php b/Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php
new file mode 100644
index 0000000..522edb4
--- /dev/null
+++ b/Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php
@@ -0,0 +1,133 @@
+<?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\Core\Tests\Authentication\Provider;
+
+use Symfony\Component\Security\Core\Authentication\Provider\PreAuthenticatedAuthenticationProvider;
+
+class PreAuthenticatedAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testSupports()
+ {
+ $provider = $this->getProvider();
+
+ $this->assertTrue($provider->supports($this->getSupportedToken()));
+ $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
+
+ $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken')
+ ->disableOriginalConstructor()
+ ->getMock()
+ ;
+ $token
+ ->expects($this->once())
+ ->method('getProviderKey')
+ ->will($this->returnValue('foo'))
+ ;
+ $this->assertFalse($provider->supports($token));
+ }
+
+ public function testAuthenticateWhenTokenIsNotSupported()
+ {
+ $provider = $this->getProvider();
+
+ $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ */
+ public function testAuthenticateWhenNoUserIsSet()
+ {
+ $provider = $this->getProvider();
+ $provider->authenticate($this->getSupportedToken(''));
+ }
+
+ public function testAuthenticate()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->once())
+ ->method('getRoles')
+ ->will($this->returnValue(array()))
+ ;
+ $provider = $this->getProvider($user);
+
+ $token = $provider->authenticate($this->getSupportedToken('fabien', 'pass'));
+ $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken', $token);
+ $this->assertEquals('pass', $token->getCredentials());
+ $this->assertEquals('key', $token->getProviderKey());
+ $this->assertEquals(array(), $token->getRoles());
+ $this->assertEquals(array('foo' => 'bar'), $token->getAttributes(), '->authenticate() copies token attributes');
+ $this->assertSame($user, $token->getUser());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\LockedException
+ */
+ public function testAuthenticateWhenUserCheckerThrowsException()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+
+ $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
+ $userChecker->expects($this->once())
+ ->method('checkPostAuth')
+ ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\LockedException', null, array(), '', false)))
+ ;
+
+ $provider = $this->getProvider($user, $userChecker);
+
+ $provider->authenticate($this->getSupportedToken('fabien'));
+ }
+
+ protected function getSupportedToken($user = false, $credentials = false)
+ {
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken', array('getUser', 'getCredentials', 'getProviderKey'), array(), '', false);
+ if (false !== $user) {
+ $token->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user))
+ ;
+ }
+ if (false !== $credentials) {
+ $token->expects($this->once())
+ ->method('getCredentials')
+ ->will($this->returnValue($credentials))
+ ;
+ }
+
+ $token
+ ->expects($this->any())
+ ->method('getProviderKey')
+ ->will($this->returnValue('key'))
+ ;
+
+ $token->setAttributes(array('foo' => 'bar'));
+
+ return $token;
+ }
+
+ protected function getProvider($user = false, $userChecker = false)
+ {
+ $userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
+ if (false !== $user) {
+ $userProvider->expects($this->once())
+ ->method('loadUserByUsername')
+ ->will($this->returnValue($user))
+ ;
+ }
+
+ if (false === $userChecker) {
+ $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
+ }
+
+ return new PreAuthenticatedAuthenticationProvider($userProvider, $userChecker, 'key');
+ }
+}
diff --git a/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php b/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php
new file mode 100644
index 0000000..43da274
--- /dev/null
+++ b/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php
@@ -0,0 +1,111 @@
+<?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\Core\Tests\Authentication\Provider;
+
+use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider;
+use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
+use Symfony\Component\Security\Core\Role\Role;
+
+class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testSupports()
+ {
+ $provider = $this->getProvider();
+
+ $this->assertTrue($provider->supports($this->getSupportedToken()));
+ $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
+ }
+
+ public function testAuthenticateWhenTokenIsNotSupported()
+ {
+ $provider = $this->getProvider();
+
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $this->assertNull($provider->authenticate($token));
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ */
+ public function testAuthenticateWhenKeysDoNotMatch()
+ {
+ $provider = $this->getProvider(null, 'key1');
+ $token = $this->getSupportedToken(null, 'key2');
+
+ $provider->authenticate($token);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException
+ */
+ public function testAuthenticateWhenPostChecksFails()
+ {
+ $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
+ $userChecker->expects($this->once())
+ ->method('checkPostAuth')
+ ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\AccountExpiredException', null, array(), '', false)))
+ ;
+
+ $provider = $this->getProvider($userChecker);
+
+ $provider->authenticate($this->getSupportedToken());
+ }
+
+ public function testAuthenticate()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user->expects($this->exactly(2))
+ ->method('getRoles')
+ ->will($this->returnValue(array('ROLE_FOO')))
+ ;
+
+ $provider = $this->getProvider();
+
+ $token = $this->getSupportedToken($user);
+ $authToken = $provider->authenticate($token);
+
+ $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', $authToken);
+ $this->assertSame($user, $authToken->getUser());
+ $this->assertEquals(array(new Role('ROLE_FOO')), $authToken->getRoles());
+ $this->assertEquals('', $authToken->getCredentials());
+ }
+
+ protected function getSupportedToken($user = null, $key = 'test')
+ {
+ if (null === $user) {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->any())
+ ->method('getRoles')
+ ->will($this->returnValue(array()))
+ ;
+ }
+
+ $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', array('getProviderKey'), array($user, 'foo', $key));
+ $token
+ ->expects($this->once())
+ ->method('getProviderKey')
+ ->will($this->returnValue('foo'))
+ ;
+
+ return $token;
+ }
+
+ protected function getProvider($userChecker = null, $key = 'test')
+ {
+ if (null === $userChecker) {
+ $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
+ }
+
+ return new RememberMeAuthenticationProvider($userChecker, $key, 'foo');
+ }
+}
diff --git a/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php b/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php
new file mode 100644
index 0000000..c2b5781
--- /dev/null
+++ b/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php
@@ -0,0 +1,206 @@
+<?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\Core\Tests\Authentication\Provider;
+
+use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
+use Symfony\Component\Security\Core\Role\Role;
+use Symfony\Component\Security\Core\Exception\BadCredentialsException;
+
+class UserAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testSupports()
+ {
+ $provider = $this->getProvider();
+
+ $this->assertTrue($provider->supports($this->getSupportedToken()));
+ $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
+ }
+
+ public function testAuthenticateWhenTokenIsNotSupported()
+ {
+ $provider = $this->getProvider();
+
+ $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
+ */
+ public function testAuthenticateWhenUsernameIsNotFound()
+ {
+ $provider = $this->getProvider(false, false);
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false)))
+ ;
+
+ $provider->authenticate($this->getSupportedToken());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ */
+ public function testAuthenticateWhenUsernameIsNotFoundAndHideIsTrue()
+ {
+ $provider = $this->getProvider(false, true);
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false)))
+ ;
+
+ $provider->authenticate($this->getSupportedToken());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
+ */
+ public function testAuthenticateWhenProviderDoesNotReturnAnUserInterface()
+ {
+ $provider = $this->getProvider(false, true);
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->returnValue(null))
+ ;
+
+ $provider->authenticate($this->getSupportedToken());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\CredentialsExpiredException
+ */
+ public function testAuthenticateWhenPreChecksFails()
+ {
+ $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
+ $userChecker->expects($this->once())
+ ->method('checkPreAuth')
+ ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\CredentialsExpiredException', null, array(), '', false)))
+ ;
+
+ $provider = $this->getProvider($userChecker);
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\User\UserInterface')))
+ ;
+
+ $provider->authenticate($this->getSupportedToken());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException
+ */
+ public function testAuthenticateWhenPostChecksFails()
+ {
+ $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
+ $userChecker->expects($this->once())
+ ->method('checkPostAuth')
+ ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\AccountExpiredException', null, array(), '', false)))
+ ;
+
+ $provider = $this->getProvider($userChecker);
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\User\UserInterface')))
+ ;
+
+ $provider->authenticate($this->getSupportedToken());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ * @expectedExceptionMessage Bad credentials
+ */
+ public function testAuthenticateWhenPostCheckAuthenticationFails()
+ {
+ $provider = $this->getProvider();
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\User\UserInterface')))
+ ;
+ $provider->expects($this->once())
+ ->method('checkAuthentication')
+ ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\BadCredentialsException', null, array(), '', false)))
+ ;
+
+ $provider->authenticate($this->getSupportedToken());
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
+ * @expectedExceptionMessage Foo
+ */
+ public function testAuthenticateWhenPostCheckAuthenticationFailsWithHideFalse()
+ {
+ $provider = $this->getProvider(false, false);
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\User\UserInterface')))
+ ;
+ $provider->expects($this->once())
+ ->method('checkAuthentication')
+ ->will($this->throwException(new BadCredentialsException('Foo')))
+ ;
+
+ $provider->authenticate($this->getSupportedToken());
+ }
+
+ public function testAuthenticate()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user->expects($this->once())
+ ->method('getRoles')
+ ->will($this->returnValue(array('ROLE_FOO')))
+ ;
+
+ $provider = $this->getProvider();
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->returnValue($user))
+ ;
+
+ $token = $this->getSupportedToken();
+ $token->expects($this->once())
+ ->method('getCredentials')
+ ->will($this->returnValue('foo'))
+ ;
+
+ $authToken = $provider->authenticate($token);
+
+ $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $authToken);
+ $this->assertSame($user, $authToken->getUser());
+ $this->assertEquals(array(new Role('ROLE_FOO')), $authToken->getRoles());
+ $this->assertEquals('foo', $authToken->getCredentials());
+ $this->assertEquals(array('foo' => 'bar'), $authToken->getAttributes(), '->authenticate() copies token attributes');
+ }
+
+ protected function getSupportedToken()
+ {
+ $mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getProviderKey'), array(), '', false);
+ $mock
+ ->expects($this->any())
+ ->method('getProviderKey')
+ ->will($this->returnValue('key'))
+ ;
+
+ $mock->setAttributes(array('foo' => 'bar'));
+
+ return $mock;
+ }
+
+ protected function getProvider($userChecker = false, $hide = true)
+ {
+ if (false === $userChecker) {
+ $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
+ }
+
+ return $this->getMockForAbstractClass('Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider', array($userChecker, 'key', $hide));
+ }
+}
diff --git a/Core/Tests/Authentication/RememberMe/InMemoryTokenProviderTest.php b/Core/Tests/Authentication/RememberMe/InMemoryTokenProviderTest.php
new file mode 100644
index 0000000..3bdf38c
--- /dev/null
+++ b/Core/Tests/Authentication/RememberMe/InMemoryTokenProviderTest.php
@@ -0,0 +1,63 @@
+<?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\Core\Tests\Authentication\RememberMe;
+
+use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
+use Symfony\Component\Security\Core\Authentication\RememberMe\InMemoryTokenProvider;
+
+class InMemoryTokenProviderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testCreateNewToken()
+ {
+ $provider = new InMemoryTokenProvider();
+
+ $token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTime());
+ $provider->createNewToken($token);
+
+ $this->assertSame($provider->loadTokenBySeries('foo'), $token);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\TokenNotFoundException
+ */
+ public function testLoadTokenBySeriesThrowsNotFoundException()
+ {
+ $provider = new InMemoryTokenProvider();
+ $provider->loadTokenBySeries('foo');
+ }
+
+ public function testUpdateToken()
+ {
+ $provider = new InMemoryTokenProvider();
+
+ $token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTime());
+ $provider->createNewToken($token);
+ $provider->updateToken('foo', 'newFoo', $lastUsed = new \DateTime());
+ $token = $provider->loadTokenBySeries('foo');
+
+ $this->assertEquals('newFoo', $token->getTokenValue());
+ $this->assertSame($token->getLastUsed(), $lastUsed);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Security\Core\Exception\TokenNotFoundException
+ */
+ public function testDeleteToken()
+ {
+ $provider = new InMemoryTokenProvider();
+
+ $token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTime());
+ $provider->createNewToken($token);
+ $provider->deleteTokenBySeries('foo');
+ $provider->loadTokenBySeries('foo');
+ }
+}
diff --git a/Core/Tests/Authentication/RememberMe/PersistentTokenTest.php b/Core/Tests/Authentication/RememberMe/PersistentTokenTest.php
new file mode 100644
index 0000000..903c030
--- /dev/null
+++ b/Core/Tests/Authentication/RememberMe/PersistentTokenTest.php
@@ -0,0 +1,29 @@
+<?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\Core\Tests\Authentication\RememberMe;
+
+use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
+
+class PersistentTokenTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstructor()
+ {
+ $lastUsed = new \DateTime();
+ $token = new PersistentToken('fooclass', 'fooname', 'fooseries', 'footokenvalue', $lastUsed);
+
+ $this->assertEquals('fooclass', $token->getClass());
+ $this->assertEquals('fooname', $token->getUsername());
+ $this->assertEquals('fooseries', $token->getSeries());
+ $this->assertEquals('footokenvalue', $token->getTokenValue());
+ $this->assertSame($lastUsed, $token->getLastUsed());
+ }
+}
diff --git a/Core/Tests/Authentication/Token/AbstractTokenTest.php b/Core/Tests/Authentication/Token/AbstractTokenTest.php
new file mode 100644
index 0000000..928ee40
--- /dev/null
+++ b/Core/Tests/Authentication/Token/AbstractTokenTest.php
@@ -0,0 +1,244 @@
+<?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\Core\Tests\Authentication\Token;
+
+use Symfony\Component\Security\Core\Role\Role;
+
+class TestUser
+{
+ protected $name;
+
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+ public function __toString()
+ {
+ return $this->name;
+ }
+}
+
+class AbstractTokenTest extends \PHPUnit_Framework_TestCase
+{
+ public function testGetUsername()
+ {
+ $token = $this->getToken(array('ROLE_FOO'));
+ $token->setUser('fabien');
+ $this->assertEquals('fabien', $token->getUsername());
+
+ $token->setUser(new TestUser('fabien'));
+ $this->assertEquals('fabien', $token->getUsername());
+
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user->expects($this->once())->method('getUsername')->will($this->returnValue('fabien'));
+ $token->setUser($user);
+ $this->assertEquals('fabien', $token->getUsername());
+ }
+
+ public function testEraseCredentials()
+ {
+ $token = $this->getToken(array('ROLE_FOO'));
+
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user->expects($this->once())->method('eraseCredentials');
+ $token->setUser($user);
+
+ $token->eraseCredentials();
+ }
+
+ /**
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::serialize
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::unserialize
+ */
+ public function testSerialize()
+ {
+ $token = $this->getToken(array('ROLE_FOO'));
+ $token->setAttributes(array('foo' => 'bar'));
+
+ $uToken = unserialize(serialize($token));
+
+ $this->assertEquals($token->getRoles(), $uToken->getRoles());
+ $this->assertEquals($token->getAttributes(), $uToken->getAttributes());
+ }
+
+ /**
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::__construct
+ */
+ public function testConstructor()
+ {
+ $token = $this->getToken(array('ROLE_FOO'));
+ $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
+
+ $token = $this->getToken(array(new Role('ROLE_FOO')));
+ $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
+
+ $token = $this->getToken(array(new Role('ROLE_FOO'), 'ROLE_BAR'));
+ $this->assertEquals(array(new Role('ROLE_FOO'), new Role('ROLE_BAR')), $token->getRoles());
+ }
+
+ /**
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::isAuthenticated
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAuthenticated
+ */
+ public function testAuthenticatedFlag()
+ {
+ $token = $this->getToken();
+ $this->assertFalse($token->isAuthenticated());
+
+ $token->setAuthenticated(true);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setAuthenticated(false);
+ $this->assertFalse($token->isAuthenticated());
+ }
+
+ /**
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::getAttributes
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAttributes
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::hasAttribute
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::getAttribute
+ * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAttribute
+ */
+ public function testAttributes()
+ {
+ $attributes = array('foo' => 'bar');
+ $token = $this->getToken();
+ $token->setAttributes($attributes);
+
+ $this->assertEquals($attributes, $token->getAttributes(), '->getAttributes() returns the token attributes');
+ $this->assertEquals('bar', $token->getAttribute('foo'), '->getAttribute() returns the value of an attribute');
+ $token->setAttribute('foo', 'foo');
+ $this->assertEquals('foo', $token->getAttribute('foo'), '->setAttribute() changes the value of an attribute');
+ $this->assertTrue($token->hasAttribute('foo'), '->hasAttribute() returns true if the attribute is defined');
+ $this->assertFalse($token->hasAttribute('oof'), '->hasAttribute() returns false if the attribute is not defined');
+
+ try {
+ $token->getAttribute('foobar');
+ $this->fail('->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
+ } catch (\Exception $e) {
+ $this->assertInstanceOf('\InvalidArgumentException', $e, '->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
+ $this->assertEquals('This token has no "foobar" attribute.', $e->getMessage(), '->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
+ }
+ }
+
+ /**
+ * @dataProvider getUsers
+ */
+ public function testSetUser($user)
+ {
+ $token = $this->getToken();
+ $token->setUser($user);
+ $this->assertSame($user, $token->getUser());
+ }
+
+ public function getUsers()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $advancedUser = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
+
+ return array(
+ array($advancedUser),
+ array($user),
+ array(new TestUser('foo')),
+ array('foo'),
+ );
+ }
+
+ /**
+ * @dataProvider getUserChanges
+ */
+ public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser)
+ {
+ $token = $this->getToken();
+ $token->setAuthenticated(true);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setUser($firstUser);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setUser($secondUser);
+ $this->assertFalse($token->isAuthenticated());
+ }
+
+ public function getUserChanges()
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $advancedUser = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
+
+ return array(
+ array(
+ 'foo', 'bar',
+ ),
+ array(
+ 'foo', new TestUser('bar'),
+ ),
+ array(
+ 'foo', $user,
+ ),
+ array(
+ 'foo', $advancedUser
+ ),
+ array(
+ $user, 'foo'
+ ),
+ array(
+ $advancedUser, 'foo'
+ ),
+ array(
+ $user, new TestUser('foo'),
+ ),
+ array(
+ $advancedUser, new TestUser('foo'),
+ ),
+ array(
+ new TestUser('foo'), new TestUser('bar'),
+ ),
+ array(
+ new TestUser('foo'), 'bar',
+ ),
+ array(
+ new TestUser('foo'), $user,
+ ),
+ array(
+ new TestUser('foo'), $advancedUser,
+ ),
+ array(
+ $user, $advancedUser
+ ),
+ array(
+ $advancedUser, $user
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider getUsers
+ */
+ public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($user)
+ {
+ $token = $this->getToken();
+ $token->setAuthenticated(true);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setUser($user);
+ $this->assertTrue($token->isAuthenticated());
+
+ $token->setUser($user);
+ $this->assertTrue($token->isAuthenticated());
+ }
+
+ protected function getToken(array $roles = array())
+ {
+ return $this->getMockForAbstractClass('Symfony\Component\Security\Core\Authentication\Token\AbstractToken', array($roles));
+ }
+}
diff --git a/Core/Tests/Authentication/Token/AnonymousTokenTest.php b/Core/Tests/Authentication/Token/AnonymousTokenTest.php
new file mode 100644
index 0000000..b5cf006
--- /dev/null
+++ b/Core/Tests/Authentication/Token/AnonymousTokenTest.php
@@ -0,0 +1,45 @@
+<?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\Core\Tests\Authentication\Token;
+
+use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
+use Symfony\Component\Security\Core\Role\Role;
+
+class AnonymousTokenTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstructor()
+ {
+ $token = new AnonymousToken('foo', 'bar');
+ $this->assertTrue($token->isAuthenticated());
+
+ $token = new AnonymousToken('foo', 'bar', array('ROLE_FOO'));
+ $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
+ }
+
+ public function testGetKey()
+ {
+ $token = new AnonymousToken('foo', 'bar');
+ $this->assertEquals('foo', $token->getKey());
+ }
+
+ public function testGetCredentials()
+ {
+ $token = new AnonymousToken('foo', 'bar');
+ $this->assertEquals('', $token->getCredentials());
+ }
+
+ public function testGetUser()
+ {
+ $token = new AnonymousToken('foo', 'bar');
+ $this->assertEquals('bar', $token->getUser());
+ }
+}
diff --git a/Core/Tests/Authentication/Token/PreAuthenticatedTokenTest.php b/Core/Tests/Authentication/Token/PreAuthenticatedTokenTest.php
new file mode 100644
index 0000000..77d2608
--- /dev/null
+++ b/Core/Tests/Authentication/Token/PreAuthenticatedTokenTest.php
@@ -0,0 +1,48 @@
+<?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\Core\Tests\Authentication\Token;
+
+use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
+use Symfony\Component\Security\Core\Role\Role;
+
+class PreAuthenticatedTokenTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstructor()
+ {
+ $token = new PreAuthenticatedToken('foo', 'bar', 'key');
+ $this->assertFalse($token->isAuthenticated());
+
+ $token = new PreAuthenticatedToken('foo', 'bar', 'key', array('ROLE_FOO'));
+ $this->assertTrue($token->isAuthenticated());
+ $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
+ $this->assertEquals('key', $token->getProviderKey());
+ }
+
+ public function testGetCredentials()
+ {
+ $token = new PreAuthenticatedToken('foo', 'bar', 'key');
+ $this->assertEquals('bar', $token->getCredentials());
+ }
+
+ public function testGetUser()
+ {
+ $token = new PreAuthenticatedToken('foo', 'bar', 'key');
+ $this->assertEquals('foo', $token->getUser());
+ }
+
+ public function testEraseCredentials()
+ {
+ $token = new PreAuthenticatedToken('foo', 'bar', 'key');
+ $token->eraseCredentials();
+ $this->assertEquals('', $token->getCredentials());
+ }
+}
diff --git a/Core/Tests/Authentication/Token/RememerMeTokenTest.php b/Core/Tests/Authentication/Token/RememerMeTokenTest.php
new file mode 100644
index 0000000..60d88c2
--- /dev/null
+++ b/Core/Tests/Authentication/Token/RememerMeTokenTest.php
@@ -0,0 +1,83 @@
+<?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\Core\Tests\Authentication\Token;
+
+use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
+use Symfony\Component\Security\Core\Role\Role;
+
+class RememberMeTokenTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstructor()
+ {
+ $user = $this->getUser();
+ $token = new RememberMeToken($user, 'fookey', 'foo');
+
+ $this->assertEquals('fookey', $token->getProviderKey());
+ $this->assertEquals('foo', $token->getKey());
+ $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
+ $this->assertSame($user, $token->getUser());
+ $this->assertTrue($token->isAuthenticated());
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testConstructorKeyCannotBeNull()
+ {
+ new RememberMeToken(
+ $this->getUser(),
+ null,
+ null
+ );
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testConstructorKeyCannotBeEmptyString()
+ {
+ new RememberMeToken(
+ $this->getUser(),
+ '',
+ ''
+ );
+ }
+
+ /**
+ * @expectedException PHPUnit_Framework_Error
+ * @dataProvider getUserArguments
+ */
+ public function testConstructorUserCannotBeNull($user)
+ {
+ new RememberMeToken($user, 'foo', 'foo');
+ }
+
+ public function getUserArguments()
+ {
+ return array(
+ array(null),
+ array('foo'),
+ );
+ }
+
+ protected function getUser($roles = array('ROLE_FOO'))
+ {
+ $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $user
+ ->expects($this->once())
+ ->method('getRoles')
+ ->will($this->returnValue($roles))
+ ;
+
+ return $user;
+ }
+}
diff --git a/Core/Tests/Authentication/Token/UsernamePasswordTokenTest.php b/Core/Tests/Authentication/Token/UsernamePasswordTokenTest.php
new file mode 100644
index 0000000..99830c7
--- /dev/null
+++ b/Core/Tests/Authentication/Token/UsernamePasswordTokenTest.php
@@ -0,0 +1,58 @@
+<?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\Core\Tests\Authentication\Token;
+
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
+use Symfony\Component\Security\Core\Role\Role;
+
+class UsernamePasswordTokenTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstructor()
+ {
+ $token = new UsernamePasswordToken('foo', 'bar', 'key');
+ $this->assertFalse($token->isAuthenticated());
+
+ $token = new UsernamePasswordToken('foo', 'bar', 'key', array('ROLE_FOO'));
+ $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
+ $this->assertTrue($token->isAuthenticated());
+ $this->assertEquals('key', $token->getProviderKey());
+ }
+
+ /**
+ * @expectedException LogicException
+ */
+ public function testSetAuthenticatedToTrue()
+ {
+ $token = new UsernamePasswordToken('foo', 'bar', 'key');
+ $token->setAuthenticated(true);
+ }
+
+ public function testSetAuthenticatedToFalse()
+ {
+ $token = new UsernamePasswordToken('foo', 'bar', 'key');
+ $token->setAuthenticated(false);
+ $this->assertFalse($token->isAuthenticated());
+ }
+
+ public function testEraseCredentials()
+ {
+ $token = new UsernamePasswordToken('foo', 'bar', 'key');
+ $token->eraseCredentials();
+ $this->assertEquals('', $token->getCredentials());
+ }
+
+ public function testToString()
+ {
+ $token = new UsernamePasswordToken('foo', '', 'foo', array('A', 'B'));
+ $this->assertEquals('UsernamePasswordToken(user="foo", authenticated=true, roles="A, B")', (string) $token);
+ }
+}