[HttpKernel] Send new session cookie from AbstractTestSessionListener after session invalidation
This commit is contained in:
parent
fcca141059
commit
98f5d5354e
@ -29,6 +29,8 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|||||||
*/
|
*/
|
||||||
abstract class AbstractTestSessionListener implements EventSubscriberInterface
|
abstract class AbstractTestSessionListener implements EventSubscriberInterface
|
||||||
{
|
{
|
||||||
|
private $sessionId;
|
||||||
|
|
||||||
public function onKernelRequest(GetResponseEvent $event)
|
public function onKernelRequest(GetResponseEvent $event)
|
||||||
{
|
{
|
||||||
if (!$event->isMasterRequest()) {
|
if (!$event->isMasterRequest()) {
|
||||||
@ -44,7 +46,8 @@ abstract class AbstractTestSessionListener implements EventSubscriberInterface
|
|||||||
$cookies = $event->getRequest()->cookies;
|
$cookies = $event->getRequest()->cookies;
|
||||||
|
|
||||||
if ($cookies->has($session->getName())) {
|
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();
|
$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();
|
$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']));
|
$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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,8 +13,10 @@ namespace Symfony\Component\HttpKernel\Tests\EventListener;
|
|||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
|
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Cookie;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
||||||
use Symfony\Component\HttpKernel\EventListener\SessionListener;
|
use Symfony\Component\HttpKernel\EventListener\SessionListener;
|
||||||
@ -86,6 +88,22 @@ class TestSessionListenerTest extends TestCase
|
|||||||
$this->assertSame(array(), $response->headers->getCookies());
|
$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()
|
public function testUnstartedSessionIsNotSave()
|
||||||
{
|
{
|
||||||
$this->sessionHasNotBeenStarted();
|
$this->sessionHasNotBeenStarted();
|
||||||
@ -150,6 +168,13 @@ class TestSessionListenerTest extends TestCase
|
|||||||
->will($this->returnValue(true));
|
->will($this->returnValue(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function fixSessionId($sessionId)
|
||||||
|
{
|
||||||
|
$this->session->expects($this->any())
|
||||||
|
->method('getId')
|
||||||
|
->will($this->returnValue($sessionId));
|
||||||
|
}
|
||||||
|
|
||||||
private function getSession()
|
private function getSession()
|
||||||
{
|
{
|
||||||
$mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
|
$mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
|
||||||
|
Reference in New Issue
Block a user