From 98f5d5354e319d5dc8959a02d31a90df0e083adf Mon Sep 17 00:00:00 2001 From: Remon van de Kamp Date: Mon, 12 Feb 2018 23:59:35 +0100 Subject: [PATCH] [HttpKernel] Send new session cookie from AbstractTestSessionListener after session invalidation --- .../AbstractTestSessionListener.php | 8 ++++-- .../EventListener/TestSessionListenerTest.php | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php index 5f0ea5c0a9..0a153dd943 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php @@ -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(); } } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php index 4452f48771..0a2263d5a8 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php @@ -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')