|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Tests\Http\Firewall; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
| 17 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 18 | +use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; |
| 19 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 20 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 21 | +use Symfony\Component\Security\Core\SecurityContextInterface; |
| 22 | +use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface; |
| 23 | +use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
| 24 | +use Symfony\Component\Security\Http\Firewall\ExceptionListener; |
| 25 | +use Symfony\Component\Security\Http\HttpUtils; |
| 26 | + |
| 27 | +class ExceptionListenerTest extends \PHPUnit_Framework_TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @dataProvider getAuthenticationExceptionProvider |
| 31 | + */ |
| 32 | + public function testAuthenticationExceptionWithoutEntryPoint(\Exception $exception, \Exception $eventException = null) |
| 33 | + { |
| 34 | + $event = $this->createEvent($exception); |
| 35 | + |
| 36 | + $listener = $this->createExceptionListener(); |
| 37 | + $listener->onKernelException($event); |
| 38 | + |
| 39 | + $this->assertNull($event->getResponse()); |
| 40 | + $this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @dataProvider getAuthenticationExceptionProvider |
| 45 | + */ |
| 46 | + public function testAuthenticationExceptionWithEntryPoint(\Exception $exception, \Exception $eventException = null) |
| 47 | + { |
| 48 | + $event = $this->createEvent($exception = new AuthenticationException()); |
| 49 | + |
| 50 | + $listener = $this->createExceptionListener(null, null, null, $this->createEntryPoint()); |
| 51 | + $listener->onKernelException($event); |
| 52 | + |
| 53 | + $this->assertEquals('OK', $event->getResponse()->getContent()); |
| 54 | + $this->assertSame($exception, $event->getException()); |
| 55 | + } |
| 56 | + |
| 57 | + public function getAuthenticationExceptionProvider() |
| 58 | + { |
| 59 | + return array( |
| 60 | + array(new AuthenticationException()), |
| 61 | + array(new \LogicException('random', 0, $e = new AuthenticationException()), $e), |
| 62 | + array(new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AuthenticationException())), $e), |
| 63 | + array(new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AccessDeniedException())), $e), |
| 64 | + array(new AuthenticationException('random', 0, new \LogicException())), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @dataProvider getAccessDeniedExceptionProvider |
| 70 | + */ |
| 71 | + public function testAccessDeniedExceptionFullFledgedAndWithoutAccessDeniedHandlerAndWithoutErrorPage(\Exception $exception, \Exception $eventException = null) |
| 72 | + { |
| 73 | + $event = $this->createEvent($exception); |
| 74 | + |
| 75 | + $listener = $this->createExceptionListener(null, $this->createTrustResolver(true)); |
| 76 | + $listener->onKernelException($event); |
| 77 | + |
| 78 | + $this->assertNull($event->getResponse()); |
| 79 | + $this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @dataProvider getAccessDeniedExceptionProvider |
| 84 | + */ |
| 85 | + public function testAccessDeniedExceptionFullFledgedAndWithoutAccessDeniedHandlerAndWithErrorPage(\Exception $exception, \Exception $eventException = null) |
| 86 | + { |
| 87 | + $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); |
| 88 | + $kernel->expects($this->once())->method('handle')->will($this->returnValue(new Response('error'))); |
| 89 | + |
| 90 | + $event = $this->createEvent($exception, $kernel); |
| 91 | + |
| 92 | + $httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils'); |
| 93 | + $httpUtils->expects($this->once())->method('createRequest')->will($this->returnValue(Request::create('/error'))); |
| 94 | + |
| 95 | + $listener = $this->createExceptionListener(null, $this->createTrustResolver(true), $httpUtils, null, '/error'); |
| 96 | + $listener->onKernelException($event); |
| 97 | + |
| 98 | + $this->assertEquals('error', $event->getResponse()->getContent()); |
| 99 | + $this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @dataProvider getAccessDeniedExceptionProvider |
| 104 | + */ |
| 105 | + public function testAccessDeniedExceptionFullFledgedAndWithAccessDeniedHandlerAndWithoutErrorPage(\Exception $exception, \Exception $eventException = null) |
| 106 | + { |
| 107 | + $event = $this->createEvent($exception); |
| 108 | + |
| 109 | + $accessDeniedHandler = $this->getMock('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface'); |
| 110 | + $accessDeniedHandler->expects($this->once())->method('handle')->will($this->returnValue(new Response('error'))); |
| 111 | + |
| 112 | + $listener = $this->createExceptionListener(null, $this->createTrustResolver(true), null, null, null, $accessDeniedHandler); |
| 113 | + $listener->onKernelException($event); |
| 114 | + |
| 115 | + $this->assertEquals('error', $event->getResponse()->getContent()); |
| 116 | + $this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @dataProvider getAccessDeniedExceptionProvider |
| 121 | + */ |
| 122 | + public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \Exception $eventException = null) |
| 123 | + { |
| 124 | + $event = $this->createEvent($exception); |
| 125 | + |
| 126 | + $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'); |
| 127 | + $context->expects($this->once())->method('getToken')->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'))); |
| 128 | + |
| 129 | + $listener = $this->createExceptionListener($context, $this->createTrustResolver(false), null, $this->createEntryPoint()); |
| 130 | + $listener->onKernelException($event); |
| 131 | + |
| 132 | + $this->assertEquals('OK', $event->getResponse()->getContent()); |
| 133 | + $this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious()); |
| 134 | + } |
| 135 | + |
| 136 | + public function getAccessDeniedExceptionProvider() |
| 137 | + { |
| 138 | + return array( |
| 139 | + array(new AccessDeniedException()), |
| 140 | + array(new \LogicException('random', 0, $e = new AccessDeniedException()), $e), |
| 141 | + array(new \LogicException('random', 0, $e = new AccessDeniedException('embed', new AccessDeniedException())), $e), |
| 142 | + array(new \LogicException('random', 0, $e = new AccessDeniedException('embed', new AuthenticationException())), $e), |
| 143 | + array(new AccessDeniedException('random', new \LogicException())), |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + private function createEntryPoint() |
| 148 | + { |
| 149 | + $entryPoint = $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface'); |
| 150 | + $entryPoint->expects($this->once())->method('start')->will($this->returnValue(new Response('OK'))); |
| 151 | + |
| 152 | + return $entryPoint; |
| 153 | + } |
| 154 | + |
| 155 | + private function createTrustResolver($fullFledged) |
| 156 | + { |
| 157 | + $trustResolver = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface'); |
| 158 | + $trustResolver->expects($this->once())->method('isFullFledged')->will($this->returnValue($fullFledged)); |
| 159 | + |
| 160 | + return $trustResolver; |
| 161 | + } |
| 162 | + |
| 163 | + private function createEvent(\Exception $exception, $kernel = null) |
| 164 | + { |
| 165 | + if (null === $kernel) { |
| 166 | + $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); |
| 167 | + } |
| 168 | + |
| 169 | + $event = new GetResponseForExceptionEvent($kernel, Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $exception); |
| 170 | + |
| 171 | + // FIXME: to be removed in 2.4 |
| 172 | + $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
| 173 | + $event->setDispatcher($dispatcher); |
| 174 | + |
| 175 | + return $event; |
| 176 | + } |
| 177 | + |
| 178 | + private function createExceptionListener(SecurityContextInterface $context = null, AuthenticationTrustResolverInterface $trustResolver = null, HttpUtils $httpUtils = null, AuthenticationEntryPointInterface $authenticationEntryPoint = null, $errorPage = null, AccessDeniedHandlerInterface $accessDeniedHandler = null) |
| 179 | + { |
| 180 | + return new ExceptionListener( |
| 181 | + $context ? $context : $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'), |
| 182 | + $trustResolver ? $trustResolver : $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface'), |
| 183 | + $httpUtils ? $httpUtils : $this->getMock('Symfony\Component\Security\Http\HttpUtils'), |
| 184 | + 'key', |
| 185 | + $authenticationEntryPoint, |
| 186 | + $errorPage, |
| 187 | + $accessDeniedHandler |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
0 commit comments