Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the allowed status codes for preview bar injection #1213

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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