Skip to content

Commit

Permalink
[HttpFoundation] Fixed session migration with custom cookie lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Guite authored and nicolas-grekas committed Apr 5, 2020
1 parent 4d440be commit 9a692b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Session/Storage/NativeSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function start()

// ok to try and start the session
if (!session_start()) {
throw new \RuntimeException('Failed to start the session');
throw new \RuntimeException('Failed to start the session.');
}

if (null !== $this->emulateSameSite) {
Expand Down Expand Up @@ -213,8 +213,10 @@ public function regenerate($destroy = false, $lifetime = null)
return false;
}

if (null !== $lifetime) {
if (null !== $lifetime && $lifetime != ini_get('session.cookie_lifetime')) {
$this->save();
ini_set('session.cookie_lifetime', $lifetime);
$this->start();
}

if ($destroy) {
Expand Down Expand Up @@ -314,7 +316,7 @@ public function registerBag(SessionBagInterface $bag)
public function getBag($name)
{
if (!isset($this->bags[$name])) {
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
}

if (!$this->started && $this->saveHandler->isActive()) {
Expand Down
13 changes: 13 additions & 0 deletions Tests/Session/Storage/NativeSessionStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ public function testRegenerateDestroy()
$this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
}

public function testRegenerateWithCustomLifetime()
{
$storage = $this->getStorage();
$storage->start();
$id = $storage->getId();
$lifetime = 999999;
$storage->getBag('attributes')->set('legs', 11);
$storage->regenerate(false, $lifetime);
$this->assertNotEquals($id, $storage->getId());
$this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
$this->assertEquals($lifetime, ini_get('session.cookie_lifetime'));
}

public function testSessionGlobalIsUpToDateAfterIdRegeneration()
{
$storage = $this->getStorage();
Expand Down

0 comments on commit 9a692b6

Please sign in to comment.