Merge branch '4.0' into 4.1

* 4.0:
  [HttpFoundation] fix registration of session proxies
  failing test to reproduce session problem
  [HttpFoundation] fix session tracking counter
This commit is contained in:
Nicolas Grekas 2018-06-28 08:35:31 +02:00
commit 223cf06121
8 changed files with 84 additions and 12 deletions

View File

@ -43,6 +43,14 @@ class SessionController implements ContainerAwareInterface
return new Response(sprintf('Welcome back %s, nice to meet you.', $name));
}
public function cacheableAction()
{
$response = new Response('all good');
$response->setSharedMaxAge(100);
return $response;
}
public function logoutAction(Request $request)
{
$request->getSession()->invalidate();

View File

@ -2,6 +2,10 @@ session_welcome:
path: /session
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }
session_cacheable:
path: /cacheable
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::cacheableAction }
session_welcome_name:
path: /session/{name}
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }

View File

@ -126,6 +126,22 @@ class SessionTest extends WebTestCase
$this->assertContains('Welcome back client2, nice to meet you.', $crawler2->text());
}
/**
* @dataProvider getConfigs
*/
public function testCorrectCacheControlHeadersForCacheableAction($config, $insulate)
{
$client = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
if ($insulate) {
$client->insulate();
}
$client->request('GET', '/cacheable');
$response = $client->getResponse();
$this->assertSame('public, s-maxage=100', $response->headers->get('cache-control'));
}
public function getConfigs()
{
return array(

View File

@ -54,8 +54,6 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
*/
public function start()
{
++$this->usageIndex;
return $this->storage->start();
}
@ -160,7 +158,9 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
*/
public function isEmpty()
{
++$this->usageIndex;
if ($this->isStarted()) {
++$this->usageIndex;
}
foreach ($this->data as &$data) {
if (!empty($data)) {
return false;
@ -185,8 +185,6 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
*/
public function migrate($destroy = false, $lifetime = null)
{
++$this->usageIndex;
return $this->storage->regenerate($destroy, $lifetime);
}
@ -195,8 +193,6 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
*/
public function save()
{
++$this->usageIndex;
$this->storage->save();
}

View File

@ -44,6 +44,9 @@ final class SessionBagProxy implements SessionBagInterface
*/
public function isEmpty()
{
if (!isset($this->data[$this->bag->getStorageKey()])) {
return true;
}
++$this->usageIndex;
return empty($this->data[$this->bag->getStorageKey()]);
@ -81,8 +84,6 @@ final class SessionBagProxy implements SessionBagInterface
*/
public function clear()
{
++$this->usageIndex;
return $this->bag->clear();
}
}

View File

@ -407,8 +407,6 @@ class NativeSessionStorage implements SessionStorageInterface
}
if ($this->saveHandler instanceof SessionHandlerProxy) {
session_set_save_handler($this->saveHandler->getHandler(), false);
} elseif ($this->saveHandler instanceof \SessionHandlerInterface) {
session_set_save_handler($this->saveHandler, false);
}
}

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Proxy;
/**
* @author Drak <drak@zikula.org>
*/
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
{
protected $handler;
@ -82,4 +82,20 @@ class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterf
{
return (bool) $this->handler->gc($maxlifetime);
}
/**
* {@inheritdoc}
*/
public function validateId($sessionId)
{
return !$this->handler instanceof \SessionUpdateTimestampHandlerInterface || $this->handler->validateId($sessionId);
}
/**
* {@inheritdoc}
*/
public function updateTimestamp($sessionId, $data)
{
return $this->handler instanceof \SessionUpdateTimestampHandlerInterface ? $this->handler->updateTimestamp($sessionId, $data) : $this->write($sessionId, $data);
}
}

View File

@ -121,4 +121,37 @@ class SessionHandlerProxyTest extends TestCase
$this->proxy->gc(86400);
}
/**
* @requires PHPUnit 5.1
*/
public function testValidateId()
{
$mock = $this->getMockBuilder(array('SessionHandlerInterface', 'SessionUpdateTimestampHandlerInterface'))->getMock();
$mock->expects($this->once())
->method('validateId');
$proxy = new SessionHandlerProxy($mock);
$proxy->validateId('id');
$this->assertTrue($this->proxy->validateId('id'));
}
/**
* @requires PHPUnit 5.1
*/
public function testUpdateTimestamp()
{
$mock = $this->getMockBuilder(array('SessionHandlerInterface', 'SessionUpdateTimestampHandlerInterface'))->getMock();
$mock->expects($this->once())
->method('updateTimestamp');
$proxy = new SessionHandlerProxy($mock);
$proxy->updateTimestamp('id', 'data');
$this->mock->expects($this->once())
->method('write');
$this->proxy->updateTimestamp('id', 'data');
}
}