Skip to content

Commit 090f309

Browse files
Merge branch '5.4' into 6.0
* 5.4: [HttpKernel] Use the existing session id if available.
2 parents 094f340 + 6adf696 commit 090f309

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
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: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,40 @@ public function testSessionCookieNotWrittenCookieGiven()
213213
$this->assertCount(0, $cookies);
214214
}
215215

216+
/**
217+
* @runInSeparateProcess
218+
*/
219+
public function testNewSessionIdIsNotOverwritten()
220+
{
221+
$newSessionId = $this->createValidSessionId();
222+
223+
$this->assertNotEmpty($newSessionId);
224+
225+
$request = new Request();
226+
$request->cookies->set('PHPSESSID', 'OLD-SESSION-ID');
227+
228+
$listener = $this->createListener($request, new NativeSessionStorageFactory());
229+
230+
$kernel = $this->createMock(HttpKernelInterface::class);
231+
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
232+
233+
$session = $request->getSession();
234+
$this->assertSame($newSessionId, $session->getId());
235+
$session->set('hello', 'world');
236+
237+
$response = new Response();
238+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
239+
$this->assertSame($newSessionId, $session->getId());
240+
241+
$cookies = $response->headers->getCookies();
242+
243+
$this->assertCount(1, $cookies);
244+
$sessionCookie = $cookies[0];
245+
246+
$this->assertSame('PHPSESSID', $sessionCookie->getName());
247+
$this->assertSame($newSessionId, $sessionCookie->getValue());
248+
}
249+
216250
/**
217251
* @runInSeparateProcess
218252
*/
@@ -525,7 +559,7 @@ public function testUninitializedSessionWithoutInitializedSession()
525559
public function testSurrogateMainRequestIsPublic()
526560
{
527561
$session = $this->createMock(Session::class);
528-
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
562+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
529563
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
530564
$sessionFactory = $this->createMock(SessionFactory::class);
531565
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
@@ -565,9 +599,14 @@ public function testSurrogateMainRequestIsPublic()
565599
public function testGetSessionIsCalledOnce()
566600
{
567601
$session = $this->createMock(Session::class);
602+
<<<<<<< HEAD
568603
$session->expects($this->exactly(2))->method('getName')->willReturn('PHPSESSID');
569604
$sessionFactory = $this->createMock(SessionFactory::class);
570605
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
606+
=======
607+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
608+
$sessionStorage = $this->createMock(NativeSessionStorage::class);
609+
>>>>>>> 5.4
571610
$kernel = $this->createMock(KernelInterface::class);
572611

573612
$requestStack = new RequestStack();

0 commit comments

Comments
 (0)