diff options
Diffstat (limited to 'Tests/Core/Authentication')
15 files changed, 90 insertions, 1661 deletions
diff --git a/Tests/Core/Authentication/AuthenticationProviderManagerTest.php b/Tests/Core/Authentication/AuthenticationProviderManagerTest.php deleted file mode 100644 index 32e6cf7..0000000 --- a/Tests/Core/Authentication/AuthenticationProviderManagerTest.php +++ /dev/null @@ -1,138 +0,0 @@ -<?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\Core\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(), '', true))) - ; - } - - return $provider; - } -} diff --git a/Tests/Core/Authentication/AuthenticationTrustResolverTest.php b/Tests/Core/Authentication/AuthenticationTrustResolverTest.php deleted file mode 100644 index e2fc593..0000000 --- a/Tests/Core/Authentication/AuthenticationTrustResolverTest.php +++ /dev/null @@ -1,70 +0,0 @@ -<?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\Core\Authentication; - -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/Tests/Core/Authentication/Provider/AnonymousAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/AnonymousAuthenticationProviderTest.php deleted file mode 100644 index d0da147..0000000 --- a/Tests/Core/Authentication/Provider/AnonymousAuthenticationProviderTest.php +++ /dev/null @@ -1,66 +0,0 @@ -<?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\Core\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/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php deleted file mode 100644 index 18e9669..0000000 --- a/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php +++ /dev/null @@ -1,300 +0,0 @@ -<?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\Core\Authentication\Provider; - -use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder; -use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; - -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(new UsernameNotFoundException())) - ; - - $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(new \RuntimeException())) - ; - - $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(null, null, $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(null, null, $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(null, null, $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(); - $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(); - $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(null, null, $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 = null, $userChecker = null, $passwordEncoder = null) - { - $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface'); - if (null !== $user) { - $userProvider->expects($this->once()) - ->method('loadUserByUsername') - ->will($this->returnValue($user)) - ; - } - - if (null === $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/Tests/Core/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php deleted file mode 100644 index 17234b6..0000000 --- a/Tests/Core/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php +++ /dev/null @@ -1,134 +0,0 @@ -<?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\Core\Authentication\Provider; - -use Symfony\Component\Security\Core\Authentication\Provider\PreAuthenticatedAuthenticationProvider; -use Symfony\Component\Security\Core\Exception\LockedException; - -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(new LockedException())) - ; - - $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 = null, $userChecker = null) - { - $userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); - if (null !== $user) { - $userProvider->expects($this->once()) - ->method('loadUserByUsername') - ->will($this->returnValue($user)) - ; - } - - if (null === $userChecker) { - $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'); - } - - return new PreAuthenticatedAuthenticationProvider($userProvider, $userChecker, 'key'); - } -} diff --git a/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php deleted file mode 100644 index 54fb4ea..0000000 --- a/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php +++ /dev/null @@ -1,107 +0,0 @@ -<?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\Core\Authentication\Provider; - -use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider; -use Symfony\Component\Security\Core\Exception\DisabledException; -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\DisabledException - */ - public function testAuthenticateWhenPreChecksFails() - { - $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'); - $userChecker->expects($this->once()) - ->method('checkPreAuth') - ->will($this->throwException(new DisabledException())); - - $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/Tests/Core/Authentication/Provider/UserAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/UserAuthenticationProviderTest.php deleted file mode 100644 index 32f5b10..0000000 --- a/Tests/Core/Authentication/Provider/UserAuthenticationProviderTest.php +++ /dev/null @@ -1,250 +0,0 @@ -<?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\Core\Authentication\Provider; - -use Symfony\Component\Security\Core\Exception\AccountExpiredException; -use Symfony\Component\Security\Core\Exception\BadCredentialsException; -use Symfony\Component\Security\Core\Exception\CredentialsExpiredException; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; -use Symfony\Component\Security\Core\Role\Role; -use Symfony\Component\Security\Core\Role\SwitchUserRole; - -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(new UsernameNotFoundException())) - ; - - $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(new UsernameNotFoundException())) - ; - - $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(new CredentialsExpiredException())) - ; - - $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(new AccountExpiredException())) - ; - - $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(new BadCredentialsException())) - ; - - $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')) - ; - - $token->expects($this->once()) - ->method('getRoles') - ->will($this->returnValue(array())) - ; - - $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'); - } - - public function testAuthenticateWithPreservingRoleSwitchUserRole() - { - $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')) - ; - - $switchUserRole = new SwitchUserRole('foo', $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')); - $token->expects($this->once()) - ->method('getRoles') - ->will($this->returnValue(array($switchUserRole))) - ; - - $authToken = $provider->authenticate($token); - - $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $authToken); - $this->assertSame($user, $authToken->getUser()); - $this->assertContains(new Role('ROLE_FOO'), $authToken->getRoles(), '', false, false); - $this->assertContains($switchUserRole, $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', 'getRoles'), 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/Tests/Core/Authentication/RememberMe/InMemoryTokenProviderTest.php b/Tests/Core/Authentication/RememberMe/InMemoryTokenProviderTest.php deleted file mode 100644 index 1739714..0000000 --- a/Tests/Core/Authentication/RememberMe/InMemoryTokenProviderTest.php +++ /dev/null @@ -1,63 +0,0 @@ -<?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\Core\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/Tests/Core/Authentication/RememberMe/PersistentTokenTest.php b/Tests/Core/Authentication/RememberMe/PersistentTokenTest.php deleted file mode 100644 index 3903591..0000000 --- a/Tests/Core/Authentication/RememberMe/PersistentTokenTest.php +++ /dev/null @@ -1,29 +0,0 @@ -<?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\Core\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/Tests/Core/Authentication/Token/AbstractTokenTest.php b/Tests/Core/Authentication/Token/AbstractTokenTest.php deleted file mode 100644 index b8be628..0000000 --- a/Tests/Core/Authentication/Token/AbstractTokenTest.php +++ /dev/null @@ -1,287 +0,0 @@ -<?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\Core\Authentication\Token; - -use Symfony\Component\Security\Core\Authentication\Token\AbstractToken; -use Symfony\Component\Security\Core\Role\Role; -use Symfony\Component\Security\Core\Role\SwitchUserRole; - -class TestUser -{ - protected $name; - - public function __construct($name) - { - $this->name = $name; - } - - public function __toString() - { - return $this->name; - } -} - -class ConcreteToken extends AbstractToken -{ - private $credentials = 'credentials_value'; - - public function __construct($user, array $roles = array()) - { - parent::__construct($roles); - - $this->setUser($user); - } - - public function serialize() - { - return serialize(array($this->credentials, parent::serialize())); - } - - public function unserialize($serialized) - { - list($this->credentials, $parentStr) = unserialize($serialized); - parent::unserialize($parentStr); - } - - public function getCredentials() - { - } -} - -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()); - } - - public function testSerializeParent() - { - $user = new TestUser('fabien'); - $token = new ConcreteToken($user, array('ROLE_FOO')); - - $parentToken = new ConcreteToken($user, array(new SwitchUserRole('ROLE_PREVIOUS', $token))); - $uToken = unserialize(serialize($parentToken)); - - $this->assertEquals( - current($parentToken->getRoles())->getSource()->getUser(), - current($uToken->getRoles())->getSource()->getUser() - ); - } - - /** - * @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/Tests/Core/Authentication/Token/AnonymousTokenTest.php b/Tests/Core/Authentication/Token/AnonymousTokenTest.php deleted file mode 100644 index 135397b..0000000 --- a/Tests/Core/Authentication/Token/AnonymousTokenTest.php +++ /dev/null @@ -1,45 +0,0 @@ -<?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\Core\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/Tests/Core/Authentication/Token/PreAuthenticatedTokenTest.php b/Tests/Core/Authentication/Token/PreAuthenticatedTokenTest.php deleted file mode 100644 index 59a533a..0000000 --- a/Tests/Core/Authentication/Token/PreAuthenticatedTokenTest.php +++ /dev/null @@ -1,48 +0,0 @@ -<?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\Core\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/Tests/Core/Authentication/Token/RememberMeTokenTest.php b/Tests/Core/Authentication/Token/RememberMeTokenTest.php deleted file mode 100644 index 438c1d9..0000000 --- a/Tests/Core/Authentication/Token/RememberMeTokenTest.php +++ /dev/null @@ -1,66 +0,0 @@ -<?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\Core\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(), - '', - '' - ); - } - - 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/Tests/Core/Authentication/Token/UsernamePasswordTokenTest.php b/Tests/Core/Authentication/Token/UsernamePasswordTokenTest.php deleted file mode 100644 index 67f431f..0000000 --- a/Tests/Core/Authentication/Token/UsernamePasswordTokenTest.php +++ /dev/null @@ -1,58 +0,0 @@ -<?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\Core\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); - } -} diff --git a/Tests/Core/Authentication/Voter/AbstractVoterTest.php b/Tests/Core/Authentication/Voter/AbstractVoterTest.php new file mode 100644 index 0000000..c5e9466 --- /dev/null +++ b/Tests/Core/Authentication/Voter/AbstractVoterTest.php @@ -0,0 +1,90 @@ +<?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\Core\Authentication\Voter; + +use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter; + +/** + * @author Roman Marintšenko <inoryy@gmail.com> + */ +class AbstractVoterTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var AbstractVoter + */ + private $voter; + + private $token; + + protected function setUp() + { + $this->voter = new VoterFixture(); + + $tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); + $tokenMock + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue('user')); + + $this->token = $tokenMock; + } + + /** + * @dataProvider getData + */ + public function testVote($expectedVote, $object, $attributes, $message) + { + $this->assertEquals($expectedVote, $this->voter->vote($this->token, $object, $attributes), $message); + } + + public function getData() + { + return array( + array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'), + array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'), + array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'), + array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'), + array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'), + array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'), + array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'), + array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'), + ); + } +} + +class VoterFixture extends AbstractVoter +{ + protected function getSupportedClasses() + { + return array( + 'Symfony\Component\Security\Tests\Core\Authentication\Voter\ObjectFixture', + ); + } + + protected function getSupportedAttributes() + { + return array( 'foo', 'bar', 'baz'); + } + + protected function isGranted($attribute, $object, $user = null) + { + return $attribute === 'foo'; + } +} + +class ObjectFixture +{ +} + +class UnsupportedObjectFixture +{ +} |