Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-clear the session form data (see #1550).
Description ----------- See #1389. Commits ------- da6bfc5 Auto-clear the session form data (see #1389). 6847201 Fix a comment and add the unit tests.
- Loading branch information
Showing
4 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
|
||
class ClearFormDataListener | ||
{ | ||
/** | ||
* Clear the Contao form data if not a POST request. | ||
* | ||
* @param FilterResponseEvent $event | ||
*/ | ||
public function onKernelResponse(FilterResponseEvent $event): void | ||
{ | ||
if (!$event->isMasterRequest()) { | ||
return; | ||
} | ||
|
||
$request = $event->getRequest(); | ||
|
||
if ($request->isMethod('POST')) { | ||
return; | ||
} | ||
|
||
if (null === ($session = $request->getSession()) || !$session->isStarted()) { | ||
return; | ||
} | ||
|
||
unset($_SESSION['FORM_DATA']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Tests\EventListener; | ||
|
||
use Contao\CoreBundle\EventListener\ClearFormDataListener; | ||
use Contao\CoreBundle\Tests\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
class ClearFormDataListenerTest extends TestCase | ||
{ | ||
public function testCanBeInstantiated(): void | ||
{ | ||
$listener = new ClearFormDataListener(); | ||
|
||
$this->assertInstanceOf('Contao\CoreBundle\EventListener\ClearFormDataListener', $listener); | ||
} | ||
|
||
public function testClearsTheFormData(): void | ||
{ | ||
$session = $this->createMock(Session::class); | ||
|
||
$session | ||
->expects($this->once()) | ||
->method('isStarted') | ||
->willReturn(true) | ||
; | ||
|
||
$request = new Request(); | ||
$request->setSession($session); | ||
|
||
$event = new FilterResponseEvent( | ||
$this->createMock(KernelInterface::class), | ||
$request, | ||
HttpKernelInterface::MASTER_REQUEST, | ||
new Response() | ||
); | ||
|
||
$_SESSION['FORM_DATA'] = ['foo' => 'bar']; | ||
|
||
$listener = new ClearFormDataListener(); | ||
$listener->onKernelResponse($event); | ||
|
||
$this->assertArrayNotHasKey('FORM_DATA', $_SESSION); | ||
} | ||
|
||
public function testDoesNotClearTheFormDataUponSubrequests(): void | ||
{ | ||
$request = $this->createMock(Request::class); | ||
|
||
$request | ||
->expects($this->never()) | ||
->method('isMethod') | ||
; | ||
|
||
$event = new FilterResponseEvent( | ||
$this->createMock(KernelInterface::class), | ||
$request, | ||
HttpKernelInterface::SUB_REQUEST, | ||
new Response() | ||
); | ||
|
||
$listener = new ClearFormDataListener(); | ||
$listener->onKernelResponse($event); | ||
} | ||
|
||
public function testDoesNotClearTheFormDataUponPostRequests(): void | ||
{ | ||
$session = $this->createMock(Session::class); | ||
|
||
$session | ||
->expects($this->never()) | ||
->method('isStarted') | ||
; | ||
|
||
$request = new Request(); | ||
$request->setSession($session); | ||
$request->setMethod('POST'); | ||
|
||
$event = new FilterResponseEvent( | ||
$this->createMock(KernelInterface::class), | ||
$request, | ||
HttpKernelInterface::MASTER_REQUEST, | ||
new Response() | ||
); | ||
|
||
$listener = new ClearFormDataListener(); | ||
$listener->onKernelResponse($event); | ||
} | ||
|
||
public function testDoesNotClearTheFormDataIfTheSessionIsNotStarted(): void | ||
{ | ||
$session = $this->createMock(Session::class); | ||
|
||
$session | ||
->expects($this->once()) | ||
->method('isStarted') | ||
->willReturn(false) | ||
; | ||
|
||
$request = new Request(); | ||
$request->setSession($session); | ||
|
||
$event = new FilterResponseEvent( | ||
$this->createMock(KernelInterface::class), | ||
$request, | ||
HttpKernelInterface::MASTER_REQUEST, | ||
new Response() | ||
); | ||
|
||
$_SESSION['FORM_DATA'] = ['foo' => 'bar']; | ||
|
||
$listener = new ClearFormDataListener(); | ||
$listener->onKernelResponse($event); | ||
|
||
$this->assertSame(['foo' => 'bar'], $_SESSION['FORM_DATA']); | ||
} | ||
} |