Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

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

Merged
merged 1 commit into from
Feb 19, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
abstract class AbstractTestSessionListener implements EventSubscriberInterface
{
private $sessionId;

public function onKernelRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
Expand All @@ -44,7 +46,8 @@ public function onKernelRequest(GetResponseEvent $event)
$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);
}
}

Expand All @@ -66,9 +69,10 @@ public function onKernelResponse(FilterResponseEvent $event)
$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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

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;
Expand Down Expand Up @@ -86,6 +88,22 @@ public function testEmptySessionDoesNotSendCookie()
$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();
Expand Down Expand Up @@ -150,6 +168,13 @@ private function sessionIsEmpty()
->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')
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.