Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / ExceptionListenerTest.php
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\HttpKernel\Tests\EventListener;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\EventDispatcher\EventDispatcher;
16 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17 use Symfony\Component\HttpKernel\HttpKernelInterface;
18 use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
19 use Symfony\Component\HttpKernel\KernelEvents;
20 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
21 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
22 use Symfony\Component\HttpFoundation\Request;
23 use Symfony\Component\HttpFoundation\Response;
24 use Symfony\Component\HttpKernel\Tests\Logger;
25
26 /**
27  * ExceptionListenerTest.
28  *
29  * @author Robert Schönthal <seroscho@googlemail.com>
30  *
31  * @group time-sensitive
32  */
33 class ExceptionListenerTest extends TestCase
34 {
35     public function testConstruct()
36     {
37         $logger = new TestLogger();
38         $l = new ExceptionListener('foo', $logger);
39
40         $_logger = new \ReflectionProperty(get_class($l), 'logger');
41         $_logger->setAccessible(true);
42         $_controller = new \ReflectionProperty(get_class($l), 'controller');
43         $_controller->setAccessible(true);
44
45         $this->assertSame($logger, $_logger->getValue($l));
46         $this->assertSame('foo', $_controller->getValue($l));
47     }
48
49     /**
50      * @dataProvider provider
51      */
52     public function testHandleWithoutLogger($event, $event2)
53     {
54         $this->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
55
56         $l = new ExceptionListener('foo');
57         $l->onKernelException($event);
58
59         $this->assertEquals(new Response('foo'), $event->getResponse());
60
61         try {
62             $l->onKernelException($event2);
63             $this->fail('RuntimeException expected');
64         } catch (\RuntimeException $e) {
65             $this->assertSame('bar', $e->getMessage());
66             $this->assertSame('foo', $e->getPrevious()->getMessage());
67         }
68     }
69
70     /**
71      * @dataProvider provider
72      */
73     public function testHandleWithLogger($event, $event2)
74     {
75         $logger = new TestLogger();
76
77         $l = new ExceptionListener('foo', $logger);
78         $l->onKernelException($event);
79
80         $this->assertEquals(new Response('foo'), $event->getResponse());
81
82         try {
83             $l->onKernelException($event2);
84             $this->fail('RuntimeException expected');
85         } catch (\RuntimeException $e) {
86             $this->assertSame('bar', $e->getMessage());
87             $this->assertSame('foo', $e->getPrevious()->getMessage());
88         }
89
90         $this->assertEquals(3, $logger->countErrors());
91         $this->assertCount(3, $logger->getLogs('critical'));
92     }
93
94     public function provider()
95     {
96         if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
97             return array(array(null, null));
98         }
99
100         $request = new Request();
101         $exception = new \Exception('foo');
102         $event = new GetResponseForExceptionEvent(new TestKernel(), $request, HttpKernelInterface::MASTER_REQUEST, $exception);
103         $event2 = new GetResponseForExceptionEvent(new TestKernelThatThrowsException(), $request, HttpKernelInterface::MASTER_REQUEST, $exception);
104
105         return array(
106             array($event, $event2),
107         );
108     }
109
110     public function testSubRequestFormat()
111     {
112         $listener = new ExceptionListener('foo', $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock());
113
114         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
115         $kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
116             return new Response($request->getRequestFormat());
117         }));
118
119         $request = Request::create('/');
120         $request->setRequestFormat('xml');
121
122         $event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new \Exception('foo'));
123         $listener->onKernelException($event);
124
125         $response = $event->getResponse();
126         $this->assertEquals('xml', $response->getContent());
127     }
128
129     public function testCSPHeaderIsRemoved()
130     {
131         $dispatcher = new EventDispatcher();
132         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
133         $kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
134             return new Response($request->getRequestFormat());
135         }));
136
137         $listener = new ExceptionListener('foo', $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(), true);
138
139         $dispatcher->addSubscriber($listener);
140
141         $request = Request::create('/');
142         $event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new \Exception('foo'));
143         $dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
144
145         $response = new Response('', 200, array('content-security-policy' => "style-src 'self'"));
146         $this->assertTrue($response->headers->has('content-security-policy'));
147
148         $event = new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response);
149         $dispatcher->dispatch(KernelEvents::RESPONSE, $event);
150
151         $this->assertFalse($response->headers->has('content-security-policy'), 'CSP header has been removed');
152         $this->assertFalse($dispatcher->hasListeners(KernelEvents::RESPONSE), 'CSP removal listener has been removed');
153     }
154
155     public function testNullController()
156     {
157         $listener = new ExceptionListener(null);
158         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
159         $kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
160             $controller = $request->attributes->get('_controller');
161
162             return $controller();
163         }));
164         $request = Request::create('/');
165         $event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new \Exception('foo'));
166
167         $listener->onKernelException($event);
168
169         $this->assertContains('Whoops, looks like something went wrong.', $event->getResponse()->getContent());
170     }
171 }
172
173 class TestLogger extends Logger implements DebugLoggerInterface
174 {
175     public function countErrors()
176     {
177         return count($this->logs['critical']);
178     }
179 }
180
181 class TestKernel implements HttpKernelInterface
182 {
183     public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
184     {
185         return new Response('foo');
186     }
187 }
188
189 class TestKernelThatThrowsException implements HttpKernelInterface
190 {
191     public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
192     {
193         throw new \RuntimeException('bar');
194     }
195 }