[HttpKernel] Send new session cookie from AbstractTestSessionListener after session invalidation

This commit is contained in:
Remon van de Kamp 2018-02-12 23:59:35 +01:00 committed by Nicolas Grekas
parent fcca141059
commit 98f5d5354e
2 changed files with 31 additions and 2 deletions

View File

@ -29,6 +29,8 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*/
abstract class AbstractTestSessionListener implements EventSubscriberInterface
{
private $sessionId;
public function onKernelRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
@ -44,7 +46,8 @@ abstract class AbstractTestSessionListener implements EventSubscriberInterface
$cookies = $event->getRequest()->cookies;
if ($cookies->has($session->getName())) {
$session->setId($cookies->get($session->getName()));
$this->sessionId = $cookies->get($session->getName());
$session->setId($this->sessionId);
}
}
@ -66,9 +69,10 @@ abstract class AbstractTestSessionListener implements EventSubscriberInterface
$session->save();
}
if ($session instanceof Session ? !$session->isEmpty() : $wasStarted) {
if ($session instanceof Session ? !$session->isEmpty() || $session->getId() !== $this->sessionId : $wasStarted) {
$params = session_get_cookie_params();
$event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
$this->sessionId = $session->getId();
}
}

View File

@ -13,8 +13,10 @@ namespace Symfony\Component\HttpKernel\Tests\EventListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\EventListener\SessionListener;
@ -86,6 +88,22 @@ class TestSessionListenerTest extends TestCase
$this->assertSame(array(), $response->headers->getCookies());
}
public function testEmptySessionWithNewSessionIdDoesSendCookie()
{
$this->sessionHasBeenStarted();
$this->sessionIsEmpty();
$this->fixSessionId('456');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$request = Request::create('/', 'GET', array(), array(new Cookie('MOCKSESSID', '123')));
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
$this->listener->onKernelRequest($event);
$response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
$this->assertNotEmpty($response->headers->getCookies());
}
public function testUnstartedSessionIsNotSave()
{
$this->sessionHasNotBeenStarted();
@ -150,6 +168,13 @@ class TestSessionListenerTest extends TestCase
->will($this->returnValue(true));
}
private function fixSessionId($sessionId)
{
$this->session->expects($this->any())
->method('getId')
->will($this->returnValue($sessionId));
}
private function getSession()
{
$mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')