|
12 | 12 | namespace Symfony\Bundle\WebProfilerBundle\Tests\EventListener;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler; |
15 | 16 | use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener;
|
16 | 17 | use Symfony\Component\HttpFoundation\HeaderBag;
|
17 | 18 | use Symfony\Component\HttpFoundation\Request;
|
18 | 19 | use Symfony\Component\HttpFoundation\Response;
|
19 | 20 | use Symfony\Component\HttpFoundation\Session\Session;
|
| 21 | +use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector; |
20 | 22 | use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
21 | 23 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
22 | 24 | use Symfony\Component\HttpKernel\Kernel;
|
@@ -300,6 +302,48 @@ public function testThrowingErrorCleanup()
|
300 | 302 | $this->assertEquals('Exception: This multiline tabbed text should come out on a single plain line', $response->headers->get('X-Debug-Error'));
|
301 | 303 | }
|
302 | 304 |
|
| 305 | + public function testCspIsDisabledIfDumperWasUsed() |
| 306 | + { |
| 307 | + $response = new Response('<html><head></head><body></body></html>'); |
| 308 | + $response->headers->set('X-Debug-Token', 'xxxxxxxx'); |
| 309 | + |
| 310 | + $event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response); |
| 311 | + |
| 312 | + $cspHandler = $this->createMock(ContentSecurityPolicyHandler::class); |
| 313 | + $cspHandler->expects($this->once()) |
| 314 | + ->method('disableCsp'); |
| 315 | + $dumpDataCollector = $this->createMock(DumpDataCollector::class); |
| 316 | + $dumpDataCollector->expects($this->once()) |
| 317 | + ->method('getDumpsCount') |
| 318 | + ->willReturn(1); |
| 319 | + |
| 320 | + $listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', $cspHandler, $dumpDataCollector); |
| 321 | + $listener->onKernelResponse($event); |
| 322 | + |
| 323 | + $this->assertEquals("<html><head></head><body>\nWDT\n</body></html>", $response->getContent()); |
| 324 | + } |
| 325 | + |
| 326 | + public function testCspIsKeptEnabledIfDumperWasNotUsed() |
| 327 | + { |
| 328 | + $response = new Response('<html><head></head><body></body></html>'); |
| 329 | + $response->headers->set('X-Debug-Token', 'xxxxxxxx'); |
| 330 | + |
| 331 | + $event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response); |
| 332 | + |
| 333 | + $cspHandler = $this->createMock(ContentSecurityPolicyHandler::class); |
| 334 | + $cspHandler->expects($this->never()) |
| 335 | + ->method('disableCsp'); |
| 336 | + $dumpDataCollector = $this->createMock(DumpDataCollector::class); |
| 337 | + $dumpDataCollector->expects($this->once()) |
| 338 | + ->method('getDumpsCount') |
| 339 | + ->willReturn(0); |
| 340 | + |
| 341 | + $listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', $cspHandler, $dumpDataCollector); |
| 342 | + $listener->onKernelResponse($event); |
| 343 | + |
| 344 | + $this->assertEquals("<html><head></head><body>\nWDT\n</body></html>", $response->getContent()); |
| 345 | + } |
| 346 | + |
303 | 347 | protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html', $hasSession = true)
|
304 | 348 | {
|
305 | 349 | $request = $this->getMockBuilder(Request::class)->setMethods(['getSession', 'isXmlHttpRequest', 'getRequestFormat'])->disableOriginalConstructor()->getMock();
|
|
0 commit comments