Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/85'
Browse files Browse the repository at this point in the history
Close #85
  • Loading branch information
weierophinney committed Nov 29, 2017
2 parents 01b228b + 053fca6 commit 8552a16
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- [#85](https://github.com/zendframework/zend-session/pull/85) fixes an issue
with how the expiration seconds are handled when a long-running request
occurs. Previously, when called, it would use the value of
`$_SERVER['REQUEST_TIME']` to calculate the expiration time; this would cause
failures if the expiration seconds had been reached by the time the value was
set. It now correctly uses the current `time()`.

- [#99](https://github.com/zendframework/zend-session/pull/99) fixes how
`Zend\Session\Config\SessionConfig` handles attaching save handlers to ensure
it will honor any handlers registered with the PHP engine (e.g., redis,
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public function getIterator()
public function setExpirationSeconds($ttl, $vars = null)
{
$storage = $this->getStorage();
$ts = $_SERVER['REQUEST_TIME'] + $ttl;
$ts = time() + $ttl;
if (is_scalar($vars) && null !== $vars) {
$vars = (array) $vars;
}
Expand Down
28 changes: 22 additions & 6 deletions test/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,24 @@ public function testContainerWritesToStorage()

public function testSettingExpirationSecondsUpdatesStorageMetadataForFullContainer()
{
$currentTimestamp = time();
$this->container->setExpirationSeconds(3600);
$storage = $this->manager->getStorage();
$metadata = $storage->getMetadata($this->container->getName());
$this->assertArrayHasKey('EXPIRE', $metadata);
$this->assertEquals($_SERVER['REQUEST_TIME'] + 3600, $metadata['EXPIRE']);
$this->assertEquals($currentTimestamp + 3600, $metadata['EXPIRE']);
}

public function testSettingExpirationSecondsForIndividualKeyUpdatesStorageMetadataForThatKey()
{
$this->container->foo = 'bar';
$currentTimestamp = time();
$this->container->setExpirationSeconds(3600, 'foo');
$storage = $this->manager->getStorage();
$metadata = $storage->getMetadata($this->container->getName());
$this->assertArrayHasKey('EXPIRE_KEYS', $metadata);
$this->assertArrayHasKey('foo', $metadata['EXPIRE_KEYS']);
$this->assertEquals($_SERVER['REQUEST_TIME'] + 3600, $metadata['EXPIRE_KEYS']['foo']);
$this->assertEquals($currentTimestamp + 3600, $metadata['EXPIRE_KEYS']['foo']);
}

public function testMultipleCallsToExpirationSecondsAggregates()
Expand All @@ -217,15 +219,29 @@ public function testMultipleCallsToExpirationSecondsAggregates()
$this->container->bar = 'baz';
$this->container->baz = 'bat';
$this->container->bat = 'bas';
$currentTimestamp = time();
$this->container->setExpirationSeconds(3600);
$this->container->setExpirationSeconds(1800, 'foo');
$this->container->setExpirationSeconds(900, ['baz', 'bat']);
$storage = $this->manager->getStorage();
$metadata = $storage->getMetadata($this->container->getName());
$this->assertEquals($_SERVER['REQUEST_TIME'] + 1800, $metadata['EXPIRE_KEYS']['foo']);
$this->assertEquals($_SERVER['REQUEST_TIME'] + 900, $metadata['EXPIRE_KEYS']['baz']);
$this->assertEquals($_SERVER['REQUEST_TIME'] + 900, $metadata['EXPIRE_KEYS']['bat']);
$this->assertEquals($_SERVER['REQUEST_TIME'] + 3600, $metadata['EXPIRE']);
$this->assertEquals($currentTimestamp + 1800, $metadata['EXPIRE_KEYS']['foo']);
$this->assertEquals($currentTimestamp + 900, $metadata['EXPIRE_KEYS']['baz']);
$this->assertEquals($currentTimestamp + 900, $metadata['EXPIRE_KEYS']['bat']);
$this->assertEquals($currentTimestamp + 3600, $metadata['EXPIRE']);
}

public function testSettingExpirationSecondsUsesCurrentTime()
{
sleep(3);
$this->container->setExpirationSeconds(2);
$this->container->foo = 'bar';

// Simulate a second request: overwrite the request time with current time()
$_SERVER['REQUEST_TIME'] = time();
$_SERVER['REQUEST_TIME_FLOAT'] = microtime(true);

$this->assertEquals('bar', $this->container->foo);
}

public function testPassingUnsetKeyToSetExpirationSecondsDoesNothing()
Expand Down

0 comments on commit 8552a16

Please sign in to comment.