Skip to content

Commit 223cf06

Browse files
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
2 parents 9604e69 + 9aecd2b commit 223cf06

File tree

8 files changed

+84
-12
lines changed

8 files changed

+84
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public function welcomeAction(Request $request, $name = null)
4343
return new Response(sprintf('Welcome back %s, nice to meet you.', $name));
4444
}
4545

46+
public function cacheableAction()
47+
{
48+
$response = new Response('all good');
49+
$response->setSharedMaxAge(100);
50+
51+
return $response;
52+
}
53+
4654
public function logoutAction(Request $request)
4755
{
4856
$request->getSession()->invalidate();

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ session_welcome:
22
path: /session
33
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }
44

5+
session_cacheable:
6+
path: /cacheable
7+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::cacheableAction }
8+
59
session_welcome_name:
610
path: /session/{name}
711
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ public function testTwoClients($config, $insulate)
126126
$this->assertContains('Welcome back client2, nice to meet you.', $crawler2->text());
127127
}
128128

129+
/**
130+
* @dataProvider getConfigs
131+
*/
132+
public function testCorrectCacheControlHeadersForCacheableAction($config, $insulate)
133+
{
134+
$client = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
135+
if ($insulate) {
136+
$client->insulate();
137+
}
138+
139+
$client->request('GET', '/cacheable');
140+
141+
$response = $client->getResponse();
142+
$this->assertSame('public, s-maxage=100', $response->headers->get('cache-control'));
143+
}
144+
129145
public function getConfigs()
130146
{
131147
return array(

src/Symfony/Component/HttpFoundation/Session/Session.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public function __construct(SessionStorageInterface $storage = null, AttributeBa
5454
*/
5555
public function start()
5656
{
57-
++$this->usageIndex;
58-
5957
return $this->storage->start();
6058
}
6159

@@ -160,7 +158,9 @@ public function getUsageIndex()
160158
*/
161159
public function isEmpty()
162160
{
163-
++$this->usageIndex;
161+
if ($this->isStarted()) {
162+
++$this->usageIndex;
163+
}
164164
foreach ($this->data as &$data) {
165165
if (!empty($data)) {
166166
return false;
@@ -185,8 +185,6 @@ public function invalidate($lifetime = null)
185185
*/
186186
public function migrate($destroy = false, $lifetime = null)
187187
{
188-
++$this->usageIndex;
189-
190188
return $this->storage->regenerate($destroy, $lifetime);
191189
}
192190

@@ -195,8 +193,6 @@ public function migrate($destroy = false, $lifetime = null)
195193
*/
196194
public function save()
197195
{
198-
++$this->usageIndex;
199-
200196
$this->storage->save();
201197
}
202198

src/Symfony/Component/HttpFoundation/Session/SessionBagProxy.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function getBag()
4444
*/
4545
public function isEmpty()
4646
{
47+
if (!isset($this->data[$this->bag->getStorageKey()])) {
48+
return true;
49+
}
4750
++$this->usageIndex;
4851

4952
return empty($this->data[$this->bag->getStorageKey()]);
@@ -81,8 +84,6 @@ public function getStorageKey()
8184
*/
8285
public function clear()
8386
{
84-
++$this->usageIndex;
85-
8687
return $this->bag->clear();
8788
}
8889
}

src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,6 @@ public function setSaveHandler($saveHandler = null)
407407
}
408408

409409
if ($this->saveHandler instanceof SessionHandlerProxy) {
410-
session_set_save_handler($this->saveHandler->getHandler(), false);
411-
} elseif ($this->saveHandler instanceof \SessionHandlerInterface) {
412410
session_set_save_handler($this->saveHandler, false);
413411
}
414412
}

src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @author Drak <drak@zikula.org>
1616
*/
17-
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface
17+
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
1818
{
1919
protected $handler;
2020

@@ -82,4 +82,20 @@ public function gc($maxlifetime)
8282
{
8383
return (bool) $this->handler->gc($maxlifetime);
8484
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
public function validateId($sessionId)
90+
{
91+
return !$this->handler instanceof \SessionUpdateTimestampHandlerInterface || $this->handler->validateId($sessionId);
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
public function updateTimestamp($sessionId, $data)
98+
{
99+
return $this->handler instanceof \SessionUpdateTimestampHandlerInterface ? $this->handler->updateTimestamp($sessionId, $data) : $this->write($sessionId, $data);
100+
}
85101
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Proxy/SessionHandlerProxyTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,37 @@ public function testGc()
121121

122122
$this->proxy->gc(86400);
123123
}
124+
125+
/**
126+
* @requires PHPUnit 5.1
127+
*/
128+
public function testValidateId()
129+
{
130+
$mock = $this->getMockBuilder(array('SessionHandlerInterface', 'SessionUpdateTimestampHandlerInterface'))->getMock();
131+
$mock->expects($this->once())
132+
->method('validateId');
133+
134+
$proxy = new SessionHandlerProxy($mock);
135+
$proxy->validateId('id');
136+
137+
$this->assertTrue($this->proxy->validateId('id'));
138+
}
139+
140+
/**
141+
* @requires PHPUnit 5.1
142+
*/
143+
public function testUpdateTimestamp()
144+
{
145+
$mock = $this->getMockBuilder(array('SessionHandlerInterface', 'SessionUpdateTimestampHandlerInterface'))->getMock();
146+
$mock->expects($this->once())
147+
->method('updateTimestamp');
148+
149+
$proxy = new SessionHandlerProxy($mock);
150+
$proxy->updateTimestamp('id', 'data');
151+
152+
$this->mock->expects($this->once())
153+
->method('write');
154+
155+
$this->proxy->updateTimestamp('id', 'data');
156+
}
124157
}

0 commit comments

Comments
 (0)