diff options
Diffstat (limited to 'Tests/Core')
38 files changed, 123 insertions, 3628 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 +{ +} diff --git a/Tests/Core/Authorization/AccessDecisionManagerTest.php b/Tests/Core/Authorization/AccessDecisionManagerTest.php deleted file mode 100644 index cbb39c4..0000000 --- a/Tests/Core/Authorization/AccessDecisionManagerTest.php +++ /dev/null @@ -1,190 +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\Authorization; - -use Symfony\Component\Security\Core\Authorization\AccessDecisionManager; -use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; - -class AccessDecisionManagerTest extends \PHPUnit_Framework_TestCase -{ - public function testSupportsClass() - { - $manager = new AccessDecisionManager(array( - $this->getVoterSupportsClass(true), - $this->getVoterSupportsClass(false), - )); - $this->assertTrue($manager->supportsClass('FooClass')); - - $manager = new AccessDecisionManager(array( - $this->getVoterSupportsClass(false), - $this->getVoterSupportsClass(false), - )); - $this->assertFalse($manager->supportsClass('FooClass')); - } - - public function testSupportsAttribute() - { - $manager = new AccessDecisionManager(array( - $this->getVoterSupportsAttribute(true), - $this->getVoterSupportsAttribute(false), - )); - $this->assertTrue($manager->supportsAttribute('foo')); - - $manager = new AccessDecisionManager(array( - $this->getVoterSupportsAttribute(false), - $this->getVoterSupportsAttribute(false), - )); - $this->assertFalse($manager->supportsAttribute('foo')); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testSetVotersEmpty() - { - $manager = new AccessDecisionManager(array()); - } - - /** - * @dataProvider getStrategyTests - */ - public function testStrategies($strategy, $voters, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions, $expected) - { - $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); - $manager = new AccessDecisionManager($voters, $strategy, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions); - - $this->assertSame($expected, $manager->decide($token, array('ROLE_FOO'))); - } - - /** - * @dataProvider getStrategiesWith2RolesTests - */ - public function testStrategiesWith2Roles($token, $strategy, $voter, $expected) - { - $manager = new AccessDecisionManager(array($voter), $strategy); - - $this->assertSame($expected, $manager->decide($token, array('ROLE_FOO', 'ROLE_BAR'))); - } - - public function getStrategiesWith2RolesTests() - { - $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); - - return array( - array($token, 'affirmative', $this->getVoter(VoterInterface::ACCESS_DENIED), false), - array($token, 'affirmative', $this->getVoter(VoterInterface::ACCESS_GRANTED), true), - - array($token, 'consensus', $this->getVoter(VoterInterface::ACCESS_DENIED), false), - array($token, 'consensus', $this->getVoter(VoterInterface::ACCESS_GRANTED), true), - - array($token, 'unanimous', $this->getVoterFor2Roles($token, VoterInterface::ACCESS_DENIED, VoterInterface::ACCESS_DENIED), false), - array($token, 'unanimous', $this->getVoterFor2Roles($token, VoterInterface::ACCESS_DENIED, VoterInterface::ACCESS_GRANTED), false), - array($token, 'unanimous', $this->getVoterFor2Roles($token, VoterInterface::ACCESS_GRANTED, VoterInterface::ACCESS_DENIED), false), - array($token, 'unanimous', $this->getVoterFor2Roles($token, VoterInterface::ACCESS_GRANTED, VoterInterface::ACCESS_GRANTED), true), - ); - } - - protected function getVoterFor2Roles($token, $vote1, $vote2) - { - $voter = $this->getMock('Symfony\Component\Security\Core\Authorization\Voter\VoterInterface'); - $voter->expects($this->exactly(2)) - ->method('vote') - ->will($this->returnValueMap(array( - array($token, null, array('ROLE_FOO'), $vote1), - array($token, null, array('ROLE_BAR'), $vote2), - ))) - ; - - return $voter; - } - - public function getStrategyTests() - { - return array( - // affirmative - array('affirmative', $this->getVoters(1, 0, 0), false, true, true), - array('affirmative', $this->getVoters(1, 2, 0), false, true, true), - array('affirmative', $this->getVoters(0, 1, 0), false, true, false), - array('affirmative', $this->getVoters(0, 0, 1), false, true, false), - array('affirmative', $this->getVoters(0, 0, 1), true, true, true), - - // consensus - array('consensus', $this->getVoters(1, 0, 0), false, true, true), - array('consensus', $this->getVoters(1, 2, 0), false, true, false), - array('consensus', $this->getVoters(2, 1, 0), false, true, true), - - array('consensus', $this->getVoters(0, 0, 1), false, true, false), - - array('consensus', $this->getVoters(0, 0, 1), true, true, true), - - array('consensus', $this->getVoters(2, 2, 0), false, true, true), - array('consensus', $this->getVoters(2, 2, 1), false, true, true), - - array('consensus', $this->getVoters(2, 2, 0), false, false, false), - array('consensus', $this->getVoters(2, 2, 1), false, false, false), - - // unanimous - array('unanimous', $this->getVoters(1, 0, 0), false, true, true), - array('unanimous', $this->getVoters(1, 0, 1), false, true, true), - array('unanimous', $this->getVoters(1, 1, 0), false, true, false), - - array('unanimous', $this->getVoters(0, 0, 2), false, true, false), - array('unanimous', $this->getVoters(0, 0, 2), true, true, true), - ); - } - - protected function getVoters($grants, $denies, $abstains) - { - $voters = array(); - for ($i = 0; $i < $grants; $i++) { - $voters[] = $this->getVoter(VoterInterface::ACCESS_GRANTED); - } - for ($i = 0; $i < $denies; $i++) { - $voters[] = $this->getVoter(VoterInterface::ACCESS_DENIED); - } - for ($i = 0; $i < $abstains; $i++) { - $voters[] = $this->getVoter(VoterInterface::ACCESS_ABSTAIN); - } - - return $voters; - } - - protected function getVoter($vote) - { - $voter = $this->getMock('Symfony\Component\Security\Core\Authorization\Voter\VoterInterface'); - $voter->expects($this->any()) - ->method('vote') - ->will($this->returnValue($vote)); - - return $voter; - } - - protected function getVoterSupportsClass($ret) - { - $voter = $this->getMock('Symfony\Component\Security\Core\Authorization\Voter\VoterInterface'); - $voter->expects($this->any()) - ->method('supportsClass') - ->will($this->returnValue($ret)); - - return $voter; - } - - protected function getVoterSupportsAttribute($ret) - { - $voter = $this->getMock('Symfony\Component\Security\Core\Authorization\Voter\VoterInterface'); - $voter->expects($this->any()) - ->method('supportsAttribute') - ->will($this->returnValue($ret)); - - return $voter; - } -} diff --git a/Tests/Core/Authorization/Voter/AuthenticatedVoterTest.php b/Tests/Core/Authorization/Voter/AuthenticatedVoterTest.php deleted file mode 100644 index b077712..0000000 --- a/Tests/Core/Authorization/Voter/AuthenticatedVoterTest.php +++ /dev/null @@ -1,78 +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\Authorization\Voter; - -use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver; -use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; -use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; - -class AuthenticatedVoterTest extends \PHPUnit_Framework_TestCase -{ - public function testSupportsClass() - { - $voter = new AuthenticatedVoter($this->getResolver()); - $this->assertTrue($voter->supportsClass('stdClass')); - } - - /** - * @dataProvider getVoteTests - */ - public function testVote($authenticated, $attributes, $expected) - { - $voter = new AuthenticatedVoter($this->getResolver()); - - $this->assertSame($expected, $voter->vote($this->getToken($authenticated), null, $attributes)); - } - - public function getVoteTests() - { - return array( - array('fully', array(), VoterInterface::ACCESS_ABSTAIN), - array('fully', array('FOO'), VoterInterface::ACCESS_ABSTAIN), - array('remembered', array(), VoterInterface::ACCESS_ABSTAIN), - array('remembered', array('FOO'), VoterInterface::ACCESS_ABSTAIN), - array('anonymously', array(), VoterInterface::ACCESS_ABSTAIN), - array('anonymously', array('FOO'), VoterInterface::ACCESS_ABSTAIN), - - array('fully', array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED), - array('remembered', array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED), - array('anonymously', array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED), - - array('fully', array('IS_AUTHENTICATED_REMEMBERED'), VoterInterface::ACCESS_GRANTED), - array('remembered', array('IS_AUTHENTICATED_REMEMBERED'), VoterInterface::ACCESS_GRANTED), - array('anonymously', array('IS_AUTHENTICATED_REMEMBERED'), VoterInterface::ACCESS_DENIED), - - array('fully', array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_GRANTED), - array('remembered', array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_DENIED), - array('anonymously', array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_DENIED), - ); - } - - protected function getResolver() - { - return new AuthenticationTrustResolver( - 'Symfony\\Component\\Security\\Core\\Authentication\\Token\\AnonymousToken', - 'Symfony\\Component\\Security\\Core\\Authentication\\Token\\RememberMeToken' - ); - } - - protected function getToken($authenticated) - { - if ('fully' === $authenticated) { - return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); - } elseif ('remembered' === $authenticated) { - return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', array('setPersistent'), array(), '', false); - } else { - return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken', null, array('', '')); - } - } -} diff --git a/Tests/Core/Authorization/Voter/RoleHierarchyVoterTest.php b/Tests/Core/Authorization/Voter/RoleHierarchyVoterTest.php deleted file mode 100644 index a50fa79..0000000 --- a/Tests/Core/Authorization/Voter/RoleHierarchyVoterTest.php +++ /dev/null @@ -1,36 +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\Authorization\Voter; - -use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter; -use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; -use Symfony\Component\Security\Core\Role\RoleHierarchy; - -class RoleHierarchyVoterTest extends RoleVoterTest -{ - /** - * @dataProvider getVoteTests - */ - public function testVote($roles, $attributes, $expected) - { - $voter = new RoleHierarchyVoter(new RoleHierarchy(array('ROLE_FOO' => array('ROLE_FOOBAR')))); - - $this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes)); - } - - public function getVoteTests() - { - return array_merge(parent::getVoteTests(), array( - array(array('ROLE_FOO'), array('ROLE_FOOBAR'), VoterInterface::ACCESS_GRANTED), - )); - } -} diff --git a/Tests/Core/Authorization/Voter/RoleVoterTest.php b/Tests/Core/Authorization/Voter/RoleVoterTest.php deleted file mode 100644 index 8a5cdc5..0000000 --- a/Tests/Core/Authorization/Voter/RoleVoterTest.php +++ /dev/null @@ -1,61 +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\Authorization\Voter; - -use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter; -use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; -use Symfony\Component\Security\Core\Role\Role; - -class RoleVoterTest extends \PHPUnit_Framework_TestCase -{ - public function testSupportsClass() - { - $voter = new RoleVoter(); - - $this->assertTrue($voter->supportsClass('Foo')); - } - - /** - * @dataProvider getVoteTests - */ - public function testVote($roles, $attributes, $expected) - { - $voter = new RoleVoter(); - - $this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes)); - } - - public function getVoteTests() - { - return array( - array(array(), array(), VoterInterface::ACCESS_ABSTAIN), - array(array(), array('FOO'), VoterInterface::ACCESS_ABSTAIN), - array(array(), array('ROLE_FOO'), VoterInterface::ACCESS_DENIED), - array(array('ROLE_FOO'), array('ROLE_FOO'), VoterInterface::ACCESS_GRANTED), - array(array('ROLE_FOO'), array('FOO', 'ROLE_FOO'), VoterInterface::ACCESS_GRANTED), - array(array('ROLE_BAR', 'ROLE_FOO'), array('ROLE_FOO'), VoterInterface::ACCESS_GRANTED), - ); - } - - protected function getToken(array $roles) - { - foreach ($roles as $i => $role) { - $roles[$i] = new Role($role); - } - $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); - $token->expects($this->once()) - ->method('getRoles') - ->will($this->returnValue($roles)); - - return $token; - } -} diff --git a/Tests/Core/Encoder/BCryptPasswordEncoderTest.php b/Tests/Core/Encoder/BCryptPasswordEncoderTest.php deleted file mode 100644 index 61e2afe..0000000 --- a/Tests/Core/Encoder/BCryptPasswordEncoderTest.php +++ /dev/null @@ -1,90 +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\Encoder; - -use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder; - -/** - * @author Elnur Abdurrakhimov <elnur@elnur.pro> - */ -class BCryptPasswordEncoderTest extends \PHPUnit_Framework_TestCase -{ - const PASSWORD = 'password'; - const BYTES = '0123456789abcdef'; - const VALID_COST = '04'; - - /** - * @expectedException \InvalidArgumentException - */ - public function testCostBelowRange() - { - new BCryptPasswordEncoder(3); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testCostAboveRange() - { - new BCryptPasswordEncoder(32); - } - - public function testCostInRange() - { - for ($cost = 4; $cost <= 31; $cost++) { - new BCryptPasswordEncoder($cost); - } - } - - public function testResultLength() - { - $this->skipIfPhpVersionIsNotSupported(); - - $encoder = new BCryptPasswordEncoder(self::VALID_COST); - $result = $encoder->encodePassword(self::PASSWORD, null); - $this->assertEquals(60, strlen($result)); - } - - public function testValidation() - { - $this->skipIfPhpVersionIsNotSupported(); - - $encoder = new BCryptPasswordEncoder(self::VALID_COST); - $result = $encoder->encodePassword(self::PASSWORD, null); - $this->assertTrue($encoder->isPasswordValid($result, self::PASSWORD, null)); - $this->assertFalse($encoder->isPasswordValid($result, 'anotherPassword', null)); - } - - private function skipIfPhpVersionIsNotSupported() - { - if (PHP_VERSION_ID < 50307) { - $this->markTestSkipped('Requires PHP >= 5.3.7'); - } - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException - */ - public function testEncodePasswordLength() - { - $encoder = new BCryptPasswordEncoder(self::VALID_COST); - - $encoder->encodePassword(str_repeat('a', 5000), 'salt'); - } - - public function testCheckPasswordLength() - { - $encoder = new BCryptPasswordEncoder(self::VALID_COST); - - $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt')); - } -} diff --git a/Tests/Core/Encoder/BasePasswordEncoderTest.php b/Tests/Core/Encoder/BasePasswordEncoderTest.php deleted file mode 100644 index 702efb0..0000000 --- a/Tests/Core/Encoder/BasePasswordEncoderTest.php +++ /dev/null @@ -1,101 +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\Encoder; - -use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder; - -class PasswordEncoder extends BasePasswordEncoder -{ - public function encodePassword($raw, $salt) - { - } - - public function isPasswordValid($encoded, $raw, $salt) - { - } -} - -class BasePasswordEncoderTest extends \PHPUnit_Framework_TestCase -{ - public function testComparePassword() - { - $this->assertTrue($this->invokeComparePasswords('password', 'password')); - $this->assertFalse($this->invokeComparePasswords('password', 'foo')); - } - - public function testDemergePasswordAndSalt() - { - $this->assertEquals(array('password', 'salt'), $this->invokeDemergePasswordAndSalt('password{salt}')); - $this->assertEquals(array('password', ''), $this->invokeDemergePasswordAndSalt('password')); - $this->assertEquals(array('', ''), $this->invokeDemergePasswordAndSalt('')); - } - - public function testMergePasswordAndSalt() - { - $this->assertEquals('password{salt}', $this->invokeMergePasswordAndSalt('password', 'salt')); - $this->assertEquals('password', $this->invokeMergePasswordAndSalt('password', '')); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testMergePasswordAndSaltWithException() - { - $this->invokeMergePasswordAndSalt('password', '{foo}'); - } - - public function testIsPasswordTooLong() - { - $this->assertTrue($this->invokeIsPasswordTooLong(str_repeat('a', 10000))); - $this->assertFalse($this->invokeIsPasswordTooLong(str_repeat('a', 10))); - } - - protected function invokeDemergePasswordAndSalt($password) - { - $encoder = new PasswordEncoder(); - $r = new \ReflectionObject($encoder); - $m = $r->getMethod('demergePasswordAndSalt'); - $m->setAccessible(true); - - return $m->invoke($encoder, $password); - } - - protected function invokeMergePasswordAndSalt($password, $salt) - { - $encoder = new PasswordEncoder(); - $r = new \ReflectionObject($encoder); - $m = $r->getMethod('mergePasswordAndSalt'); - $m->setAccessible(true); - - return $m->invoke($encoder, $password, $salt); - } - - protected function invokeComparePasswords($p1, $p2) - { - $encoder = new PasswordEncoder(); - $r = new \ReflectionObject($encoder); - $m = $r->getMethod('comparePasswords'); - $m->setAccessible(true); - - return $m->invoke($encoder, $p1, $p2); - } - - protected function invokeIsPasswordTooLong($p) - { - $encoder = new PasswordEncoder(); - $r = new \ReflectionObject($encoder); - $m = $r->getMethod('isPasswordTooLong'); - $m->setAccessible(true); - - return $m->invoke($encoder, $p); - } -} diff --git a/Tests/Core/Encoder/EncoderFactoryTest.php b/Tests/Core/Encoder/EncoderFactoryTest.php deleted file mode 100644 index 85d4e91..0000000 --- a/Tests/Core/Encoder/EncoderFactoryTest.php +++ /dev/null @@ -1,104 +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\Encoder; - -use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; -use Symfony\Component\Security\Core\Encoder\EncoderFactory; -use Symfony\Component\Security\Core\User\User; -use Symfony\Component\Security\Core\User\UserInterface; - -class EncoderFactoryTest extends \PHPUnit_Framework_TestCase -{ - public function testGetEncoderWithMessageDigestEncoder() - { - $factory = new EncoderFactory(array('Symfony\Component\Security\Core\User\UserInterface' => array( - 'class' => 'Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder', - 'arguments' => array('sha512', true, 5), - ))); - - $encoder = $factory->getEncoder($this->getMock('Symfony\Component\Security\Core\User\UserInterface')); - $expectedEncoder = new MessageDigestPasswordEncoder('sha512', true, 5); - - $this->assertEquals($expectedEncoder->encodePassword('foo', 'moo'), $encoder->encodePassword('foo', 'moo')); - } - - public function testGetEncoderWithService() - { - $factory = new EncoderFactory(array( - 'Symfony\Component\Security\Core\User\UserInterface' => new MessageDigestPasswordEncoder('sha1'), - )); - - $encoder = $factory->getEncoder($this->getMock('Symfony\Component\Security\Core\User\UserInterface')); - $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); - $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); - - $encoder = $factory->getEncoder(new User('user', 'pass')); - $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); - $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); - } - - public function testGetEncoderWithClassName() - { - $factory = new EncoderFactory(array( - 'Symfony\Component\Security\Core\User\UserInterface' => new MessageDigestPasswordEncoder('sha1'), - )); - - $encoder = $factory->getEncoder('Symfony\Component\Security\Tests\Core\Encoder\SomeChildUser'); - $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); - $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); - } - - public function testGetEncoderConfiguredForConcreteClassWithService() - { - $factory = new EncoderFactory(array( - 'Symfony\Component\Security\Core\User\User' => new MessageDigestPasswordEncoder('sha1'), - )); - - $encoder = $factory->getEncoder(new User('user', 'pass')); - $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); - $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); - } - - public function testGetEncoderConfiguredForConcreteClassWithClassName() - { - $factory = new EncoderFactory(array( - 'Symfony\Component\Security\Tests\Core\Encoder\SomeUser' => new MessageDigestPasswordEncoder('sha1'), - )); - - $encoder = $factory->getEncoder('Symfony\Component\Security\Tests\Core\Encoder\SomeChildUser'); - $expectedEncoder = new MessageDigestPasswordEncoder('sha1'); - $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', '')); - } -} - -class SomeUser implements UserInterface -{ - public function getRoles() - { - } - public function getPassword() - { - } - public function getSalt() - { - } - public function getUsername() - { - } - public function eraseCredentials() - { - } -} - -class SomeChildUser extends SomeUser -{ -} diff --git a/Tests/Core/Encoder/MessageDigestPasswordEncoderTest.php b/Tests/Core/Encoder/MessageDigestPasswordEncoderTest.php deleted file mode 100644 index f37d3bc..0000000 --- a/Tests/Core/Encoder/MessageDigestPasswordEncoderTest.php +++ /dev/null @@ -1,62 +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\Encoder; - -use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; - -class MessageDigestPasswordEncoderTest extends \PHPUnit_Framework_TestCase -{ - public function testIsPasswordValid() - { - $encoder = new MessageDigestPasswordEncoder('sha256', false, 1); - - $this->assertTrue($encoder->isPasswordValid(hash('sha256', 'password'), 'password', '')); - } - - public function testEncodePassword() - { - $encoder = new MessageDigestPasswordEncoder('sha256', false, 1); - $this->assertSame(hash('sha256', 'password'), $encoder->encodePassword('password', '')); - - $encoder = new MessageDigestPasswordEncoder('sha256', true, 1); - $this->assertSame(base64_encode(hash('sha256', 'password', true)), $encoder->encodePassword('password', '')); - - $encoder = new MessageDigestPasswordEncoder('sha256', false, 2); - $this->assertSame(hash('sha256', hash('sha256', 'password', true).'password'), $encoder->encodePassword('password', '')); - } - - /** - * @expectedException \LogicException - */ - public function testEncodePasswordAlgorithmDoesNotExist() - { - $encoder = new MessageDigestPasswordEncoder('foobar'); - $encoder->encodePassword('password', ''); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException - */ - public function testEncodePasswordLength() - { - $encoder = new MessageDigestPasswordEncoder(); - - $encoder->encodePassword(str_repeat('a', 5000), 'salt'); - } - - public function testCheckPasswordLength() - { - $encoder = new MessageDigestPasswordEncoder(); - - $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt')); - } -} diff --git a/Tests/Core/Encoder/Pbkdf2PasswordEncoderTest.php b/Tests/Core/Encoder/Pbkdf2PasswordEncoderTest.php deleted file mode 100644 index ca16f02..0000000 --- a/Tests/Core/Encoder/Pbkdf2PasswordEncoderTest.php +++ /dev/null @@ -1,62 +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\Encoder; - -use Symfony\Component\Security\Core\Encoder\Pbkdf2PasswordEncoder; - -class Pbkdf2PasswordEncoderTest extends \PHPUnit_Framework_TestCase -{ - public function testIsPasswordValid() - { - $encoder = new Pbkdf2PasswordEncoder('sha256', false, 1, 40); - - $this->assertTrue($encoder->isPasswordValid('c1232f10f62715fda06ae7c0a2037ca19b33cf103b727ba56d870c11f290a2ab106974c75607c8a3', 'password', '')); - } - - public function testEncodePassword() - { - $encoder = new Pbkdf2PasswordEncoder('sha256', false, 1, 40); - $this->assertSame('c1232f10f62715fda06ae7c0a2037ca19b33cf103b727ba56d870c11f290a2ab106974c75607c8a3', $encoder->encodePassword('password', '')); - - $encoder = new Pbkdf2PasswordEncoder('sha256', true, 1, 40); - $this->assertSame('wSMvEPYnFf2gaufAogN8oZszzxA7cnulbYcMEfKQoqsQaXTHVgfIow==', $encoder->encodePassword('password', '')); - - $encoder = new Pbkdf2PasswordEncoder('sha256', false, 2, 40); - $this->assertSame('8bc2f9167a81cdcfad1235cd9047f1136271c1f978fcfcb35e22dbeafa4634f6fd2214218ed63ebb', $encoder->encodePassword('password', '')); - } - - /** - * @expectedException \LogicException - */ - public function testEncodePasswordAlgorithmDoesNotExist() - { - $encoder = new Pbkdf2PasswordEncoder('foobar'); - $encoder->encodePassword('password', ''); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException - */ - public function testEncodePasswordLength() - { - $encoder = new Pbkdf2PasswordEncoder('foobar'); - - $encoder->encodePassword(str_repeat('a', 5000), 'salt'); - } - - public function testCheckPasswordLength() - { - $encoder = new Pbkdf2PasswordEncoder('foobar'); - - $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt')); - } -} diff --git a/Tests/Core/Encoder/PlaintextPasswordEncoderTest.php b/Tests/Core/Encoder/PlaintextPasswordEncoderTest.php deleted file mode 100644 index 8b1b888..0000000 --- a/Tests/Core/Encoder/PlaintextPasswordEncoderTest.php +++ /dev/null @@ -1,56 +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\Encoder; - -use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder; - -class PlaintextPasswordEncoderTest extends \PHPUnit_Framework_TestCase -{ - public function testIsPasswordValid() - { - $encoder = new PlaintextPasswordEncoder(); - - $this->assertTrue($encoder->isPasswordValid('foo', 'foo', '')); - $this->assertFalse($encoder->isPasswordValid('bar', 'foo', '')); - $this->assertFalse($encoder->isPasswordValid('FOO', 'foo', '')); - - $encoder = new PlaintextPasswordEncoder(true); - - $this->assertTrue($encoder->isPasswordValid('foo', 'foo', '')); - $this->assertFalse($encoder->isPasswordValid('bar', 'foo', '')); - $this->assertTrue($encoder->isPasswordValid('FOO', 'foo', '')); - } - - public function testEncodePassword() - { - $encoder = new PlaintextPasswordEncoder(); - - $this->assertSame('foo', $encoder->encodePassword('foo', '')); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException - */ - public function testEncodePasswordLength() - { - $encoder = new PlaintextPasswordEncoder(); - - $encoder->encodePassword(str_repeat('a', 5000), 'salt'); - } - - public function testCheckPasswordLength() - { - $encoder = new PlaintextPasswordEncoder(); - - $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt')); - } -} diff --git a/Tests/Core/LegacySecurityContextInterfaceTest.php b/Tests/Core/LegacySecurityContextInterfaceTest.php new file mode 100644 index 0000000..3fad2b1 --- /dev/null +++ b/Tests/Core/LegacySecurityContextInterfaceTest.php @@ -0,0 +1,33 @@ +<?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; + +use Symfony\Component\Security\Core\SecurityContextInterface; +use Symfony\Component\Security\Core\Security; + +/** + * @group legacy + */ +class LegacySecurityContextInterfaceTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test if the BC Layer is working as intended + */ + public function testConstantSync() + { + $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); + + $this->assertSame(Security::ACCESS_DENIED_ERROR, SecurityContextInterface::ACCESS_DENIED_ERROR); + $this->assertSame(Security::AUTHENTICATION_ERROR, SecurityContextInterface::AUTHENTICATION_ERROR); + $this->assertSame(Security::LAST_USERNAME, SecurityContextInterface::LAST_USERNAME); + } +} diff --git a/Tests/Core/Role/RoleHierarchyTest.php b/Tests/Core/Role/RoleHierarchyTest.php deleted file mode 100644 index a98aed6..0000000 --- a/Tests/Core/Role/RoleHierarchyTest.php +++ /dev/null @@ -1,32 +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\Role; - -use Symfony\Component\Security\Core\Role\RoleHierarchy; -use Symfony\Component\Security\Core\Role\Role; - -class RoleHierarchyTest extends \PHPUnit_Framework_TestCase -{ - public function testGetReachableRoles() - { - $role = new RoleHierarchy(array( - 'ROLE_ADMIN' => array('ROLE_USER'), - 'ROLE_SUPER_ADMIN' => array('ROLE_ADMIN', 'ROLE_FOO'), - )); - - $this->assertEquals(array(new Role('ROLE_USER')), $role->getReachableRoles(array(new Role('ROLE_USER')))); - $this->assertEquals(array(new Role('ROLE_FOO')), $role->getReachableRoles(array(new Role('ROLE_FOO')))); - $this->assertEquals(array(new Role('ROLE_ADMIN'), new Role('ROLE_USER')), $role->getReachableRoles(array(new Role('ROLE_ADMIN')))); - $this->assertEquals(array(new Role('ROLE_FOO'), new Role('ROLE_ADMIN'), new Role('ROLE_USER')), $role->getReachableRoles(array(new Role('ROLE_FOO'), new Role('ROLE_ADMIN')))); - $this->assertEquals(array(new Role('ROLE_SUPER_ADMIN'), new Role('ROLE_ADMIN'), new Role('ROLE_FOO'), new Role('ROLE_USER')), $role->getReachableRoles(array(new Role('ROLE_SUPER_ADMIN')))); - } -} diff --git a/Tests/Core/Role/RoleTest.php b/Tests/Core/Role/RoleTest.php deleted file mode 100644 index e2e7ca8..0000000 --- a/Tests/Core/Role/RoleTest.php +++ /dev/null @@ -1,24 +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\Role; - -use Symfony\Component\Security\Core\Role\Role; - -class RoleTest extends \PHPUnit_Framework_TestCase -{ - public function testGetRole() - { - $role = new Role('FOO'); - - $this->assertEquals('FOO', $role->getRole()); - } -} diff --git a/Tests/Core/Role/SwitchUserRoleTest.php b/Tests/Core/Role/SwitchUserRoleTest.php deleted file mode 100644 index bf9b173..0000000 --- a/Tests/Core/Role/SwitchUserRoleTest.php +++ /dev/null @@ -1,31 +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\Role; - -use Symfony\Component\Security\Core\Role\SwitchUserRole; - -class SwitchUserRoleTest extends \PHPUnit_Framework_TestCase -{ - public function testGetSource() - { - $role = new SwitchUserRole('FOO', $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')); - - $this->assertSame($token, $role->getSource()); - } - - public function testGetRole() - { - $role = new SwitchUserRole('FOO', $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')); - - $this->assertEquals('FOO', $role->getRole()); - } -} diff --git a/Tests/Core/SecurityContextTest.php b/Tests/Core/SecurityContextTest.php deleted file mode 100644 index 124ebf9..0000000 --- a/Tests/Core/SecurityContextTest.php +++ /dev/null @@ -1,92 +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; - -use Symfony\Component\Security\Core\SecurityContext; - -class SecurityContextTest extends \PHPUnit_Framework_TestCase -{ - public function testVoteAuthenticatesTokenIfNecessary() - { - $authManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'); - $decisionManager = $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface'); - - $context = new SecurityContext($authManager, $decisionManager); - $context->setToken($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')); - - $authManager - ->expects($this->once()) - ->method('authenticate') - ->with($this->equalTo($token)) - ->will($this->returnValue($newToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'))) - ; - - $decisionManager - ->expects($this->once()) - ->method('decide') - ->will($this->returnValue(true)) - ; - - $this->assertTrue($context->isGranted('foo')); - $this->assertSame($newToken, $context->getToken()); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException - */ - public function testVoteWithoutAuthenticationToken() - { - $context = new SecurityContext( - $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'), - $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface') - ); - - $context->isGranted('ROLE_FOO'); - } - - public function testIsGranted() - { - $manager = $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface'); - $manager->expects($this->once())->method('decide')->will($this->returnValue(false)); - $context = new SecurityContext($this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'), $manager); - $context->setToken($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')); - $token - ->expects($this->once()) - ->method('isAuthenticated') - ->will($this->returnValue(true)) - ; - $this->assertFalse($context->isGranted('ROLE_FOO')); - - $manager = $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface'); - $manager->expects($this->once())->method('decide')->will($this->returnValue(true)); - $context = new SecurityContext($this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'), $manager); - $context->setToken($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')); - $token - ->expects($this->once()) - ->method('isAuthenticated') - ->will($this->returnValue(true)) - ; - $this->assertTrue($context->isGranted('ROLE_FOO')); - } - - public function testGetSetToken() - { - $context = new SecurityContext( - $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'), - $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface') - ); - $this->assertNull($context->getToken()); - - $context->setToken($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')); - $this->assertSame($token, $context->getToken()); - } -} diff --git a/Tests/Core/User/ChainUserProviderTest.php b/Tests/Core/User/ChainUserProviderTest.php deleted file mode 100644 index 9d38a4c..0000000 --- a/Tests/Core/User/ChainUserProviderTest.php +++ /dev/null @@ -1,183 +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\User; - -use Symfony\Component\Security\Core\Exception\UnsupportedUserException; -use Symfony\Component\Security\Core\User\ChainUserProvider; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; - -class ChainUserProviderTest extends \PHPUnit_Framework_TestCase -{ - public function testLoadUserByUsername() - { - $provider1 = $this->getProvider(); - $provider1 - ->expects($this->once()) - ->method('loadUserByUsername') - ->with($this->equalTo('foo')) - ->will($this->throwException(new UsernameNotFoundException('not found'))) - ; - - $provider2 = $this->getProvider(); - $provider2 - ->expects($this->once()) - ->method('loadUserByUsername') - ->with($this->equalTo('foo')) - ->will($this->returnValue($account = $this->getAccount())) - ; - - $provider = new ChainUserProvider(array($provider1, $provider2)); - $this->assertSame($account, $provider->loadUserByUsername('foo')); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException - */ - public function testLoadUserByUsernameThrowsUsernameNotFoundException() - { - $provider1 = $this->getProvider(); - $provider1 - ->expects($this->once()) - ->method('loadUserByUsername') - ->with($this->equalTo('foo')) - ->will($this->throwException(new UsernameNotFoundException('not found'))) - ; - - $provider2 = $this->getProvider(); - $provider2 - ->expects($this->once()) - ->method('loadUserByUsername') - ->with($this->equalTo('foo')) - ->will($this->throwException(new UsernameNotFoundException('not found'))) - ; - - $provider = new ChainUserProvider(array($provider1, $provider2)); - $provider->loadUserByUsername('foo'); - } - - public function testRefreshUser() - { - $provider1 = $this->getProvider(); - $provider1 - ->expects($this->once()) - ->method('refreshUser') - ->will($this->throwException(new UnsupportedUserException('unsupported'))) - ; - - $provider2 = $this->getProvider(); - $provider2 - ->expects($this->once()) - ->method('refreshUser') - ->will($this->returnValue($account = $this->getAccount())) - ; - - $provider = new ChainUserProvider(array($provider1, $provider2)); - $this->assertSame($account, $provider->refreshUser($this->getAccount())); - } - - public function testRefreshUserAgain() - { - $provider1 = $this->getProvider(); - $provider1 - ->expects($this->once()) - ->method('refreshUser') - ->will($this->throwException(new UsernameNotFoundException('not found'))) - ; - - $provider2 = $this->getProvider(); - $provider2 - ->expects($this->once()) - ->method('refreshUser') - ->will($this->returnValue($account = $this->getAccount())) - ; - - $provider = new ChainUserProvider(array($provider1, $provider2)); - $this->assertSame($account, $provider->refreshUser($this->getAccount())); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException - */ - public function testRefreshUserThrowsUnsupportedUserException() - { - $provider1 = $this->getProvider(); - $provider1 - ->expects($this->once()) - ->method('refreshUser') - ->will($this->throwException(new UnsupportedUserException('unsupported'))) - ; - - $provider2 = $this->getProvider(); - $provider2 - ->expects($this->once()) - ->method('refreshUser') - ->will($this->throwException(new UnsupportedUserException('unsupported'))) - ; - - $provider = new ChainUserProvider(array($provider1, $provider2)); - $provider->refreshUser($this->getAccount()); - } - - public function testSupportsClass() - { - $provider1 = $this->getProvider(); - $provider1 - ->expects($this->once()) - ->method('supportsClass') - ->with($this->equalTo('foo')) - ->will($this->returnValue(false)) - ; - - $provider2 = $this->getProvider(); - $provider2 - ->expects($this->once()) - ->method('supportsClass') - ->with($this->equalTo('foo')) - ->will($this->returnValue(true)) - ; - - $provider = new ChainUserProvider(array($provider1, $provider2)); - $this->assertTrue($provider->supportsClass('foo')); - } - - public function testSupportsClassWhenNotSupported() - { - $provider1 = $this->getProvider(); - $provider1 - ->expects($this->once()) - ->method('supportsClass') - ->with($this->equalTo('foo')) - ->will($this->returnValue(false)) - ; - - $provider2 = $this->getProvider(); - $provider2 - ->expects($this->once()) - ->method('supportsClass') - ->with($this->equalTo('foo')) - ->will($this->returnValue(false)) - ; - - $provider = new ChainUserProvider(array($provider1, $provider2)); - $this->assertFalse($provider->supportsClass('foo')); - } - - protected function getAccount() - { - return $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); - } - - protected function getProvider() - { - return $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); - } -} diff --git a/Tests/Core/User/InMemoryUserProviderTest.php b/Tests/Core/User/InMemoryUserProviderTest.php deleted file mode 100644 index 826e390..0000000 --- a/Tests/Core/User/InMemoryUserProviderTest.php +++ /dev/null @@ -1,62 +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\User; - -use Symfony\Component\Security\Core\User\InMemoryUserProvider; -use Symfony\Component\Security\Core\User\User; - -class InMemoryUserProviderTest extends \PHPUnit_Framework_TestCase -{ - public function testConstructor() - { - $provider = new InMemoryUserProvider(array( - 'fabien' => array( - 'password' => 'foo', - 'enabled' => false, - 'roles' => array('ROLE_USER'), - ), - )); - - $user = $provider->loadUserByUsername('fabien'); - $this->assertEquals('foo', $user->getPassword()); - $this->assertEquals(array('ROLE_USER'), $user->getRoles()); - $this->assertFalse($user->isEnabled()); - } - - public function testCreateUser() - { - $provider = new InMemoryUserProvider(); - $provider->createUser(new User('fabien', 'foo')); - - $user = $provider->loadUserByUsername('fabien'); - $this->assertEquals('foo', $user->getPassword()); - } - - /** - * @expectedException \LogicException - */ - public function testCreateUserAlreadyExist() - { - $provider = new InMemoryUserProvider(); - $provider->createUser(new User('fabien', 'foo')); - $provider->createUser(new User('fabien', 'foo')); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException - */ - public function testLoadUserByUsernameDoesNotExist() - { - $provider = new InMemoryUserProvider(); - $provider->loadUserByUsername('fabien'); - } -} diff --git a/Tests/Core/User/UserCheckerTest.php b/Tests/Core/User/UserCheckerTest.php deleted file mode 100644 index dca6311..0000000 --- a/Tests/Core/User/UserCheckerTest.php +++ /dev/null @@ -1,108 +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\User; - -use Symfony\Component\Security\Core\User\UserChecker; - -class UserCheckerTest extends \PHPUnit_Framework_TestCase -{ - public function testCheckPostAuthNotAdvancedUserInterface() - { - $checker = new UserChecker(); - - $this->assertNull($checker->checkPostAuth($this->getMock('Symfony\Component\Security\Core\User\UserInterface'))); - } - - public function testCheckPostAuthPass() - { - $checker = new UserChecker(); - - $account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface'); - $account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(true)); - - $this->assertNull($checker->checkPostAuth($account)); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\CredentialsExpiredException - */ - public function testCheckPostAuthCredentialsExpired() - { - $checker = new UserChecker(); - - $account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface'); - $account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(false)); - - $checker->checkPostAuth($account); - } - - public function testCheckPreAuthNotAdvancedUserInterface() - { - $checker = new UserChecker(); - - $this->assertNull($checker->checkPreAuth($this->getMock('Symfony\Component\Security\Core\User\UserInterface'))); - } - - public function testCheckPreAuthPass() - { - $checker = new UserChecker(); - - $account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface'); - $account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true)); - $account->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); - $account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(true)); - - $this->assertNull($checker->checkPreAuth($account)); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\LockedException - */ - public function testCheckPreAuthAccountLocked() - { - $checker = new UserChecker(); - - $account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface'); - $account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(false)); - - $checker->checkPreAuth($account); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\DisabledException - */ - public function testCheckPreAuthDisabled() - { - $checker = new UserChecker(); - - $account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface'); - $account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true)); - $account->expects($this->once())->method('isEnabled')->will($this->returnValue(false)); - - $checker->checkPreAuth($account); - } - - /** - * @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException - */ - public function testCheckPreAuthAccountExpired() - { - $checker = new UserChecker(); - - $account = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface'); - $account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true)); - $account->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); - $account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(false)); - - $checker->checkPreAuth($account); - } -} diff --git a/Tests/Core/User/UserTest.php b/Tests/Core/User/UserTest.php deleted file mode 100644 index d05f491..0000000 --- a/Tests/Core/User/UserTest.php +++ /dev/null @@ -1,126 +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\User; - -use Symfony\Component\Security\Core\User\User; - -class UserTest extends \PHPUnit_Framework_TestCase -{ - /** - * @covers Symfony\Component\Security\Core\User\User::__construct - * @expectedException \InvalidArgumentException - */ - public function testConstructorException() - { - new User('', 'superpass'); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::__construct - * @covers Symfony\Component\Security\Core\User\User::getRoles - */ - public function testGetRoles() - { - $user = new User('fabien', 'superpass'); - $this->assertEquals(array(), $user->getRoles()); - - $user = new User('fabien', 'superpass', array('ROLE_ADMIN')); - $this->assertEquals(array('ROLE_ADMIN'), $user->getRoles()); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::__construct - * @covers Symfony\Component\Security\Core\User\User::getPassword - */ - public function testGetPassword() - { - $user = new User('fabien', 'superpass'); - $this->assertEquals('superpass', $user->getPassword()); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::__construct - * @covers Symfony\Component\Security\Core\User\User::getUsername - */ - public function testGetUsername() - { - $user = new User('fabien', 'superpass'); - $this->assertEquals('fabien', $user->getUsername()); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::getSalt - */ - public function testGetSalt() - { - $user = new User('fabien', 'superpass'); - $this->assertEquals('', $user->getSalt()); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::isAccountNonExpired - */ - public function testIsAccountNonExpired() - { - $user = new User('fabien', 'superpass'); - $this->assertTrue($user->isAccountNonExpired()); - - $user = new User('fabien', 'superpass', array(), true, false); - $this->assertFalse($user->isAccountNonExpired()); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::isCredentialsNonExpired - */ - public function testIsCredentialsNonExpired() - { - $user = new User('fabien', 'superpass'); - $this->assertTrue($user->isCredentialsNonExpired()); - - $user = new User('fabien', 'superpass', array(), true, true, false); - $this->assertFalse($user->isCredentialsNonExpired()); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::isAccountNonLocked - */ - public function testIsAccountNonLocked() - { - $user = new User('fabien', 'superpass'); - $this->assertTrue($user->isAccountNonLocked()); - - $user = new User('fabien', 'superpass', array(), true, true, true, false); - $this->assertFalse($user->isAccountNonLocked()); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::isEnabled - */ - public function testIsEnabled() - { - $user = new User('fabien', 'superpass'); - $this->assertTrue($user->isEnabled()); - - $user = new User('fabien', 'superpass', array(), false); - $this->assertFalse($user->isEnabled()); - } - - /** - * @covers Symfony\Component\Security\Core\User\User::eraseCredentials - */ - public function testEraseCredentials() - { - $user = new User('fabien', 'superpass'); - $user->eraseCredentials(); - $this->assertEquals('superpass', $user->getPassword()); - } -} diff --git a/Tests/Core/Util/ClassUtilsTest.php b/Tests/Core/Util/ClassUtilsTest.php deleted file mode 100644 index 8359236..0000000 --- a/Tests/Core/Util/ClassUtilsTest.php +++ /dev/null @@ -1,50 +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\Util -{ - use Symfony\Component\Security\Core\Util\ClassUtils; - - class ClassUtilsTest extends \PHPUnit_Framework_TestCase - { - public static function dataGetClass() - { - return array( - array('stdClass', 'stdClass'), - array('Symfony\Component\Security\Core\Util\ClassUtils', 'Symfony\Component\Security\Core\Util\ClassUtils'), - array('MyProject\Proxies\__CG__\stdClass', 'stdClass'), - array('MyProject\Proxies\__CG__\OtherProject\Proxies\__CG__\stdClass', 'stdClass'), - array('MyProject\Proxies\__CG__\Symfony\Component\Security\Tests\Core\Util\ChildObject', 'Symfony\Component\Security\Tests\Core\Util\ChildObject'), - array(new TestObject(), 'Symfony\Component\Security\Tests\Core\Util\TestObject'), - array(new \Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Core\Util\TestObject(), 'Symfony\Component\Security\Tests\Core\Util\TestObject'), - ); - } - - /** - * @dataProvider dataGetClass - */ - public function testGetRealClass($object, $expectedClassName) - { - $this->assertEquals($expectedClassName, ClassUtils::getRealClass($object)); - } - } - - class TestObject - { - } -} - -namespace Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Tests\Core\Util -{ - class TestObject extends \Symfony\Component\Security\Tests\Core\Util\TestObject - { - } -} diff --git a/Tests/Core/Util/SecureRandomTest.php b/Tests/Core/Util/SecureRandomTest.php deleted file mode 100644 index 316b049..0000000 --- a/Tests/Core/Util/SecureRandomTest.php +++ /dev/null @@ -1,201 +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\Util; - -use Symfony\Component\Security\Core\Util\SecureRandom; - -class SecureRandomTest extends \PHPUnit_Framework_TestCase -{ - /** - * T1: Monobit test. - * - * @dataProvider getSecureRandoms - */ - public function testMonobit($secureRandom) - { - $nbOnBits = substr_count($this->getBitSequence($secureRandom, 20000), '1'); - $this->assertTrue($nbOnBits > 9654 && $nbOnBits < 10346, 'Monobit test failed, number of turned on bits: '.$nbOnBits); - } - - /** - * T2: Chi-square test with 15 degrees of freedom (chi-Quadrat-Anpassungstest). - * - * @dataProvider getSecureRandoms - */ - public function testPoker($secureRandom) - { - $b = $this->getBitSequence($secureRandom, 20000); - $c = array(); - for ($i = 0; $i <= 15; $i++) { - $c[$i] = 0; - } - - for ($j = 1; $j <= 5000; $j++) { - $k = 4 * $j - 1; - ++$c[8 * $b[$k - 3] + 4 * $b[$k - 2] + 2 * $b[$k - 1] + $b[$k]]; - } - - $f = 0; - for ($i = 0; $i <= 15; $i++) { - $f += $c[$i] * $c[$i]; - } - - $Y = 16 / 5000 * $f - 5000; - - $this->assertTrue($Y > 1.03 && $Y < 57.4, 'Poker test failed, Y = '.$Y); - } - - /** - * Run test. - * - * @dataProvider getSecureRandoms - */ - public function testRun($secureRandom) - { - $b = $this->getBitSequence($secureRandom, 20000); - - $runs = array(); - for ($i = 1; $i <= 6; $i++) { - $runs[$i] = 0; - } - - $addRun = function ($run) use (&$runs) { - if ($run > 6) { - $run = 6; - } - - ++$runs[$run]; - }; - - $currentRun = 0; - $lastBit = null; - for ($i = 0; $i < 20000; $i++) { - if ($lastBit === $b[$i]) { - ++$currentRun; - } else { - if ($currentRun > 0) { - $addRun($currentRun); - } - - $lastBit = $b[$i]; - $currentRun = 0; - } - } - if ($currentRun > 0) { - $addRun($currentRun); - } - - $this->assertTrue($runs[1] > 2267 && $runs[1] < 2733, 'Runs of length 1 outside of defined interval: '.$runs[1]); - $this->assertTrue($runs[2] > 1079 && $runs[2] < 1421, 'Runs of length 2 outside of defined interval: '.$runs[2]); - $this->assertTrue($runs[3] > 502 && $runs[3] < 748, 'Runs of length 3 outside of defined interval: '.$runs[3]); - $this->assertTrue($runs[4] > 233 && $runs[4] < 402, 'Runs of length 4 outside of defined interval: '.$runs[4]); - $this->assertTrue($runs[5] > 90 && $runs[5] < 223, 'Runs of length 5 outside of defined interval: '.$runs[5]); - $this->assertTrue($runs[6] > 90 && $runs[6] < 233, 'Runs of length 6 outside of defined interval: '.$runs[6]); - } - - /** - * Long-run test. - * - * @dataProvider getSecureRandoms - */ - public function testLongRun($secureRandom) - { - $b = $this->getBitSequence($secureRandom, 20000); - - $longestRun = $currentRun = 0; - $lastBit = null; - for ($i = 0; $i < 20000; $i++) { - if ($lastBit === $b[$i]) { - ++$currentRun; - } else { - if ($currentRun > $longestRun) { - $longestRun = $currentRun; - } - $lastBit = $b[$i]; - $currentRun = 0; - } - } - if ($currentRun > $longestRun) { - $longestRun = $currentRun; - } - - $this->assertTrue($longestRun < 34, 'Failed longest run test: '.$longestRun); - } - - /** - * Serial Correlation (Autokorrelationstest). - * - * @dataProvider getSecureRandoms - */ - public function testSerialCorrelation($secureRandom) - { - $shift = rand(1, 5000); - $b = $this->getBitSequence($secureRandom, 20000); - - $Z = 0; - for ($i = 0; $i < 5000; $i++) { - $Z += $b[$i] === $b[$i + $shift] ? 1 : 0; - } - - $this->assertTrue($Z > 2326 && $Z < 2674, 'Failed serial correlation test: '.$Z); - } - - public function getSecureRandoms() - { - $secureRandoms = array(); - - // only add if openssl is indeed present - $secureRandom = new SecureRandom(); - if ($this->hasOpenSsl($secureRandom)) { - $secureRandoms[] = array($secureRandom); - } - - // no-openssl with custom seed provider - $secureRandom = new SecureRandom(sys_get_temp_dir().'/_sf2.seed'); - $this->disableOpenSsl($secureRandom); - $secureRandoms[] = array($secureRandom); - - return $secureRandoms; - } - - protected function disableOpenSsl($secureRandom) - { - $ref = new \ReflectionProperty($secureRandom, 'useOpenSsl'); - $ref->setAccessible(true); - $ref->setValue($secureRandom, false); - $ref->setAccessible(false); - } - - protected function hasOpenSsl($secureRandom) - { - $ref = new \ReflectionProperty($secureRandom, 'useOpenSsl'); - $ref->setAccessible(true); - - $ret = $ref->getValue($secureRandom); - - $ref->setAccessible(false); - - return $ret; - } - - private function getBitSequence($secureRandom, $length) - { - $bitSequence = ''; - for ($i = 0; $i < $length; $i += 40) { - $value = unpack('H*', $secureRandom->nextBytes(5)); - $value = str_pad(base_convert($value[1], 16, 2), 40, '0', STR_PAD_LEFT); - $bitSequence .= $value; - } - - return substr($bitSequence, 0, $length); - } -} diff --git a/Tests/Core/Util/StringUtilsTest.php b/Tests/Core/Util/StringUtilsTest.php deleted file mode 100644 index 3b18d48..0000000 --- a/Tests/Core/Util/StringUtilsTest.php +++ /dev/null @@ -1,61 +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\Util; - -use Symfony\Component\Security\Core\Util\StringUtils; - -/** - * Data from PHP.net's hash_equals tests. - */ -class StringUtilsTest extends \PHPUnit_Framework_TestCase -{ - public function dataProviderTrue() - { - return array( - array('same', 'same'), - array('', ''), - array(123, 123), - array(null, ''), - array(null, null), - ); - } - - public function dataProviderFalse() - { - return array( - array('not1same', 'not2same'), - array('short', 'longer'), - array('longer', 'short'), - array('', 'notempty'), - array('notempty', ''), - array(123, 'NaN'), - array('NaN', 123), - array(null, 123), - ); - } - - /** - * @dataProvider dataProviderTrue - */ - public function testEqualsTrue($known, $user) - { - $this->assertTrue(StringUtils::equals($known, $user)); - } - - /** - * @dataProvider dataProviderFalse - */ - public function testEqualsFalse($known, $user) - { - $this->assertFalse(StringUtils::equals($known, $user)); - } -} diff --git a/Tests/Core/Validator/Constraints/UserPasswordValidatorTest.php b/Tests/Core/Validator/Constraints/UserPasswordValidatorTest.php deleted file mode 100644 index 4c420c7..0000000 --- a/Tests/Core/Validator/Constraints/UserPasswordValidatorTest.php +++ /dev/null @@ -1,157 +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\Validator\Constraints; - -use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; -use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator; - -class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase -{ - const PASSWORD_VALID = true; - const PASSWORD_INVALID = false; - - protected $context; - - protected function setUp() - { - $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); - } - - protected function tearDown() - { - $this->context = null; - } - - public function testPasswordIsValid() - { - $user = $this->createUser(); - $securityContext = $this->createSecurityContext($user); - - $encoder = $this->createPasswordEncoder(static::PASSWORD_VALID); - $encoderFactory = $this->createEncoderFactory($encoder); - - $validator = new UserPasswordValidator($securityContext, $encoderFactory); - $validator->initialize($this->context); - - $this - ->context - ->expects($this->never()) - ->method('addViolation') - ; - - $validator->validate('secret', new UserPassword()); - } - - public function testPasswordIsNotValid() - { - $user = $this->createUser(); - $securityContext = $this->createSecurityContext($user); - - $encoder = $this->createPasswordEncoder(static::PASSWORD_INVALID); - $encoderFactory = $this->createEncoderFactory($encoder); - - $validator = new UserPasswordValidator($securityContext, $encoderFactory); - $validator->initialize($this->context); - - $this - ->context - ->expects($this->once()) - ->method('addViolation') - ; - - $validator->validate('secret', new UserPassword()); - } - - public function testUserIsNotValid() - { - $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); - - $user = $this->getMock('Foo\Bar\User'); - $encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'); - $securityContext = $this->createSecurityContext($user); - - $validator = new UserPasswordValidator($securityContext, $encoderFactory); - $validator->initialize($this->context); - $validator->validate('secret', new UserPassword()); - } - - protected function createUser() - { - $mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); - - $mock - ->expects($this->once()) - ->method('getPassword') - ->will($this->returnValue('s3Cr3t')) - ; - - $mock - ->expects($this->once()) - ->method('getSalt') - ->will($this->returnValue('^S4lt$')) - ; - - return $mock; - } - - protected function createPasswordEncoder($isPasswordValid = true) - { - $mock = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface'); - - $mock - ->expects($this->once()) - ->method('isPasswordValid') - ->will($this->returnValue($isPasswordValid)) - ; - - return $mock; - } - - protected function createEncoderFactory($encoder = null) - { - $mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'); - - $mock - ->expects($this->once()) - ->method('getEncoder') - ->will($this->returnValue($encoder)) - ; - - return $mock; - } - - protected function createSecurityContext($user = null) - { - $token = $this->createAuthenticationToken($user); - - $mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'); - $mock - ->expects($this->once()) - ->method('getToken') - ->will($this->returnValue($token)) - ; - - return $mock; - } - - protected function createAuthenticationToken($user = null) - { - $mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); - $mock - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)) - ; - - return $mock; - } -} |