Skip to content

Commit 69caabc

Browse files
Merge branch '6.0' into 6.1
* 6.0: fix merge [HttpKernel] Use the existing session id if available.
2 parents d76fa14 + 115620b commit 69caabc

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

EventListener/AbstractSessionListener.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ public function onKernelRequest(RequestEvent $event)
7070
$request->setSessionFactory(function () use (&$sess, $request) {
7171
if (!$sess) {
7272
$sess = $this->getSession();
73-
}
7473

75-
/*
76-
* For supporting sessions in php runtime with runners like roadrunner or swoole, the session
77-
* cookie needs to be read from the cookie bag and set on the session storage.
78-
*
79-
* Do not set it when a native php session is active.
80-
*/
81-
if ($sess && !$sess->isStarted() && \PHP_SESSION_ACTIVE !== session_status()) {
82-
$sessionId = $request->cookies->get($sess->getName(), '');
83-
$sess->setId($sessionId);
74+
/*
75+
* For supporting sessions in php runtime with runners like roadrunner or swoole, the session
76+
* cookie needs to be read from the cookie bag and set on the session storage.
77+
*
78+
* Do not set it when a native php session is active.
79+
*/
80+
if ($sess && !$sess->isStarted() && \PHP_SESSION_ACTIVE !== session_status()) {
81+
$sessionId = $sess->getId() ?: $request->cookies->get($sess->getName(), '');
82+
$sess->setId($sessionId);
83+
}
8484
}
8585

8686
return $sess;

Tests/EventListener/SessionListenerTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,40 @@ public function testSessionCookieNotWrittenCookieGiven()
211211
$this->assertCount(0, $cookies);
212212
}
213213

214+
/**
215+
* @runInSeparateProcess
216+
*/
217+
public function testNewSessionIdIsNotOverwritten()
218+
{
219+
$newSessionId = $this->createValidSessionId();
220+
221+
$this->assertNotEmpty($newSessionId);
222+
223+
$request = new Request();
224+
$request->cookies->set('PHPSESSID', 'OLD-SESSION-ID');
225+
226+
$listener = $this->createListener($request, new NativeSessionStorageFactory());
227+
228+
$kernel = $this->createMock(HttpKernelInterface::class);
229+
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
230+
231+
$session = $request->getSession();
232+
$this->assertSame($newSessionId, $session->getId());
233+
$session->set('hello', 'world');
234+
235+
$response = new Response();
236+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
237+
$this->assertSame($newSessionId, $session->getId());
238+
239+
$cookies = $response->headers->getCookies();
240+
241+
$this->assertCount(1, $cookies);
242+
$sessionCookie = $cookies[0];
243+
244+
$this->assertSame('PHPSESSID', $sessionCookie->getName());
245+
$this->assertSame($newSessionId, $sessionCookie->getValue());
246+
}
247+
214248
/**
215249
* @runInSeparateProcess
216250
*/
@@ -523,7 +557,7 @@ public function testUninitializedSessionWithoutInitializedSession()
523557
public function testSurrogateMainRequestIsPublic()
524558
{
525559
$session = $this->createMock(Session::class);
526-
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
560+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
527561
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
528562
$sessionFactory = $this->createMock(SessionFactory::class);
529563
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
@@ -563,7 +597,7 @@ public function testSurrogateMainRequestIsPublic()
563597
public function testGetSessionIsCalledOnce()
564598
{
565599
$session = $this->createMock(Session::class);
566-
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
600+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
567601
$sessionFactory = $this->createMock(SessionFactory::class);
568602
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
569603
$kernel = $this->createMock(KernelInterface::class);

0 commit comments

Comments
 (0)