diff --git a/src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php b/src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php index 1229607db8..a6aca8f937 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php @@ -14,12 +14,15 @@ namespace Symfony\Component\Security\Http\Tests\Authenticator; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\BadCredentialsException; use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator; use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; use Symfony\Component\Security\Http\HttpUtils; +use Symfony\Component\Translation\Loader\ArrayLoader; +use Symfony\Component\Translation\Translator; class JsonLoginAuthenticatorTest extends TestCase { @@ -123,6 +126,27 @@ class JsonLoginAuthenticatorTest extends TestCase yield [$request, 'Invalid username.', BadCredentialsException::class]; } + public function testAuthenticationFailureWithoutTranslator() + { + $this->setUpAuthenticator(); + + $response = $this->authenticator->onAuthenticationFailure(new Request(), new AuthenticationException()); + $this->assertSame(['error' => 'An authentication exception occurred.'], json_decode($response->getContent(), true)); + } + + public function testAuthenticationFailureWithTranslator() + { + $translator = new Translator('en'); + $translator->addLoader('array', new ArrayLoader()); + $translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security'); + + $this->setUpAuthenticator(); + $this->authenticator->setTranslator($translator); + + $response = $this->authenticator->onAuthenticationFailure(new Request(), new AuthenticationException()); + $this->assertSame(['error' => 'foo'], json_decode($response->getContent(), true)); + } + private function setUpAuthenticator(array $options = []) { $this->authenticator = new JsonLoginAuthenticator(new HttpUtils(), $this->userProvider, null, null, $options); diff --git a/src/Symfony/Component/Security/Http/Tests/Firewall/UsernamePasswordJsonAuthenticationListenerTest.php b/src/Symfony/Component/Security/Http/Tests/Firewall/UsernamePasswordJsonAuthenticationListenerTest.php index b71d4fc490..cc31cc8d51 100644 --- a/src/Symfony/Component/Security/Http/Tests/Firewall/UsernamePasswordJsonAuthenticationListenerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Firewall/UsernamePasswordJsonAuthenticationListenerTest.php @@ -25,6 +25,8 @@ use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerI use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; use Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener; use Symfony\Component\Security\Http\HttpUtils; +use Symfony\Component\Translation\Loader\ArrayLoader; +use Symfony\Component\Translation\Translator; /** * @author Kévin Dunglas @@ -36,7 +38,7 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase */ private $listener; - private function createListener(array $options = [], $success = true, $matchCheckPath = true) + private function createListener(array $options = [], $success = true, $matchCheckPath = true, $withMockedHandler = true) { $tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); $httpUtils = $this->getMockBuilder(HttpUtils::class)->getMock(); @@ -55,10 +57,15 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase $authenticationManager->method('authenticate')->willThrowException(new AuthenticationException()); } - $authenticationSuccessHandler = $this->getMockBuilder(AuthenticationSuccessHandlerInterface::class)->getMock(); - $authenticationSuccessHandler->method('onAuthenticationSuccess')->willReturn(new Response('ok')); - $authenticationFailureHandler = $this->getMockBuilder(AuthenticationFailureHandlerInterface::class)->getMock(); - $authenticationFailureHandler->method('onAuthenticationFailure')->willReturn(new Response('ko')); + $authenticationSuccessHandler = null; + $authenticationFailureHandler = null; + + if ($withMockedHandler) { + $authenticationSuccessHandler = $this->getMockBuilder(AuthenticationSuccessHandlerInterface::class)->getMock(); + $authenticationSuccessHandler->method('onAuthenticationSuccess')->willReturn(new Response('ok')); + $authenticationFailureHandler = $this->getMockBuilder(AuthenticationFailureHandlerInterface::class)->getMock(); + $authenticationFailureHandler->method('onAuthenticationFailure')->willReturn(new Response('ko')); + } $this->listener = new UsernamePasswordJsonAuthenticationListener($tokenStorage, $authenticationManager, $httpUtils, 'providerKey', $authenticationSuccessHandler, $authenticationFailureHandler, $options); } @@ -86,12 +93,28 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase public function testHandleFailure() { - $this->createListener([], false); + $this->createListener([], false, true, false); $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}'); $event = new RequestEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST); ($this->listener)($event); - $this->assertEquals('ko', $event->getResponse()->getContent()); + $this->assertSame(['error' => 'An authentication exception occurred.'], json_decode($event->getResponse()->getContent(), true)); + } + + public function testTranslatedHandleFailure() + { + $translator = new Translator('en'); + $translator->addLoader('array', new ArrayLoader()); + $translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security'); + + $this->createListener([], false, true, false); + $this->listener->setTranslator($translator); + + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}'); + $event = new RequestEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST); + + ($this->listener)($event); + $this->assertSame(['error' => 'foo'], json_decode($event->getResponse()->getContent(), true)); } public function testUsePath() diff --git a/src/Symfony/Component/Security/Http/composer.json b/src/Symfony/Component/Security/Http/composer.json index cb924c6f1e..ac6f7f9417 100644 --- a/src/Symfony/Component/Security/Http/composer.json +++ b/src/Symfony/Component/Security/Http/composer.json @@ -27,6 +27,7 @@ "require-dev": { "symfony/routing": "^4.4|^5.0", "symfony/security-csrf": "^4.4|^5.0", + "symfony/translation": "^4.4|^5.0", "psr/log": "~1.0" }, "conflict": {