Skip to content

Commit

Permalink
Change the allowed status codes for preview bar injection (see #1213)
Browse files Browse the repository at this point in the history
Description
-----------

Please refer to #1210

Commits
-------

44d0b0e Change allowed status codes for preview bar injection
  • Loading branch information
richardhj authored and leofeyer committed Jan 18, 2020
1 parent b6d81c4 commit 0230f5e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core-bundle/src/EventListener/PreviewToolbarListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __invoke(ResponseEvent $event): void
$response = $event->getResponse();

// Do not capture redirects, errors, or modify XML HTTP Requests
if ($response->isRedirection() || $response->isServerError() || $request->isXmlHttpRequest()) {
if ($request->isXmlHttpRequest() || !($response->isSuccessful() || $response->isClientError())) {
return;
}

Expand Down
55 changes: 52 additions & 3 deletions core-bundle/tests/EventListener/PreviewToolbarListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@ public function testDoesNotInjectTheToolbarOnContentDispositionAttachment(): voi
}

/**
* @dataProvider getRedirects
* @dataProvider getDisallowedStatusCodes
*/
public function testDoesNotInjectTheToolbarIntoARedirectResponse(int $statusCode, bool $hasSession): void
public function testDoesNotInjectToolbarOnDisallowedStatusCodes(int $statusCode, bool $hasSession): void
{
$response = new Response('<html><head></head><body></body></html>', $statusCode);
$response->headers->set('Content-Type', 'text/html; charset=utf-8');

$event = new ResponseEvent(
$this->createMock(HttpKernelInterface::class),
Expand All @@ -186,12 +187,60 @@ public function testDoesNotInjectTheToolbarIntoARedirectResponse(int $statusCode
$this->assertSame('<html><head></head><body></body></html>', $response->getContent());
}

public function getRedirects(): \Generator
public function getDisallowedStatusCodes(): \Generator
{
yield [100, true];
yield [301, true];
yield [302, true];
yield [500, true];
yield [100, false];
yield [301, false];
yield [302, false];
yield [302, false];
yield [500, false];
}

/**
* @dataProvider getAllowedStatusCodes
*/
public function testInjectsToolbarOnAllowedStatusCodes(int $statusCode, bool $hasSession): void
{
$response = new Response('<html><head></head><body></body></html>');
$response->headers->set('Content-Type', 'text/html; charset=utf-8');

$event = new ResponseEvent(
$this->createMock(HttpKernelInterface::class),
$this->getRequestMock(false, 'html', $hasSession),
HttpKernelInterface::MASTER_REQUEST,
$response
);

$listener = new PreviewToolbarListener(
'preview.php',
$this->mockScopeMatcher(),
$this->getTwigMock(),
$this->mockRouterWithContext()
);

$listener($event);

$this->assertSame("<html><head></head><body>\nCONTAO\n</body></html>", $response->getContent());
}

public function getAllowedStatusCodes(): \Generator
{
yield [200, true];
yield [201, true];
yield [202, true];
yield [401, true];
yield [403, true];
yield [404, true];
yield [200, false];
yield [201, false];
yield [202, false];
yield [401, false];
yield [403, false];
yield [404, false];
}

public function testDoesNotInjectTheToolbarIntoAnIncompleteHtmlResponse(): void
Expand Down

0 comments on commit 0230f5e

Please sign in to comment.