Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d30df7

Browse files
bug #28433 [HttpFoundation] Allow reuse of Session between requests if ID did not change (tgalopin)
This PR was merged into the 2.8 branch. Discussion ---------- [HttpFoundation] Allow reuse of Session between requests if ID did not change | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13450 | License | MIT | Doc PR | - I stumbled upon the issue from #13450 in a more simple case than what was exposed in the issue. From my understanding, the problem arises when the session is used between an access to the session and a functional test request: because the session was accessed (usually using the container directly), the session has started and the following request fails. This PR checks whether the ID was actually regenerated before throwing (if a setId is called with the same ID, it is the same request context, it shouldn't throw IMO). Not sure I understood everything correctly though, feel free to fix it for me if needed. Commits ------- fd30f4a Allow reuse of Session between requests
2 parents 5a10f2d + fd30f4a commit 5d30df7

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ public function getId()
178178
*/
179179
public function setId($id)
180180
{
181-
$this->storage->setId($id);
181+
if ($this->storage->getId() !== $id) {
182+
$this->storage->setId($id);
183+
}
182184
}
183185

184186
/**

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ public function testSetId()
7070
$this->assertEquals('0123456789abcdef', $this->session->getId());
7171
}
7272

73+
public function testSetIdAfterStart()
74+
{
75+
$this->session->start();
76+
$id = $this->session->getId();
77+
78+
$e = null;
79+
try {
80+
$this->session->setId($id);
81+
} catch (\Exception $e) {
82+
}
83+
84+
$this->assertNull($e);
85+
86+
try {
87+
$this->session->setId('different');
88+
} catch (\Exception $e) {
89+
}
90+
91+
$this->assertInstanceOf('\LogicException', $e);
92+
}
93+
7394
public function testSetName()
7495
{
7596
$this->assertEquals('MOCKSESSID', $this->session->getName());

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function setUp()
4848
$this->data = array(
4949
$this->attributes->getStorageKey() => array('foo' => 'bar'),
5050
$this->flashes->getStorageKey() => array('notice' => 'hello'),
51-
);
51+
);
5252

5353
$this->storage = new MockArraySessionStorage();
5454
$this->storage->registerBag($this->flashes);

0 commit comments

Comments
 (0)