8000 [HttpFoundation][FrameworkBundle] Separate out mock session storage a… · symfony/symfony@b12ece0 · GitHub
[go: up one dir, main page]

Skip to content

Commit b12ece0

Browse files
author
Drak
committed
[HttpFoundation][FrameworkBundle] Separate out mock session storage and stop polluting global namespace.
This makes mock sessions truly mock and not to interfere with global namespace. Add getters and setters for session name and ID.
1 parent d687801 commit b12ece0

File tree

15 files changed

+312
-76
lines changed

15 files changed

+312
-76
lines changed

src/Symfony/Bundle/FrameworkBundle/EventListener/SessionListener.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828
*/
2929
class SessionListener implements EventSubscriberInterface
3030
{
31+
/**
32+
* @var ContainerInterface
33+
*/
3134
private $container;
35+
36+
/**
37+
* @var boolean
38+
*/
3239
private $autoStart;
3340

3441
public function __construct(ContainerInterface $container, $autoStart = false)

src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ public function onKernelRequest(GetResponseEvent $event)
4343
}
4444

4545
// bootstrap the session
46-
if ($this->container->has('session')) {
47-
$this->container->get('session');
46+
if (!$this->container->has('session')) {
47+
return;
4848
}
4949

50+
$session = $this->container->get('session');
5051
$cookies = $event->getRequest()->cookies;
51-
if ($cookies->has(session_name())) {
52-
session_id($cookies->get(session_name()));
52+
if ($cookies->has($session->getName())) {
53+
$session->setId($cookies->get($session->getName()));
5354
} else {
54-
session_id('');
55+
$session->setId('');
5556
}
5657
}
5758

@@ -72,7 +73,7 @@ public function onKernelResponse(FilterResponseEvent $event)
7273

7374
$params = session_get_cookie_params();
7475

75-
$event->getResponse()->headers->setCookie(new Cookie(session_name(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
76+
$event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
7677
}
7778
}
7879

src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,13 @@ private function sessionMustBeSaved()
9494

9595
private function getSession()
9696
{
97-
return $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
97+
$mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
9898
->disableOriginalConstructor()
9999
->getMock();
100+
101+
// set return value for getName()
102+
$mock->expects($this->any())->method('getName')->will($this->returnValue('MOCKSESSID'));
103+
104+
return $mock;
100105
}
101106
}

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ public function getSession()
496496
public function hasPreviousSession()
497497
{
498498
// the check for $this->session avoids malicious users trying to fake a session cookie with proper name
499-
return $this->cookies->has(session_name()) && null !== $this->session;
499+
$sessionName = $this->hasSession() ? $this->session->getName() : null;
500+
return $this->cookies->has($sessionName) && $this->hasSession();
500501
}
501502

502503
/**

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
1818
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
1919
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
20+
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorage;
2021

2122
/**
2223
* Session.
@@ -140,19 +141,39 @@ public function save()
140141
}
141142

142143
/**
143-
* Returns the session ID
144-
*
145-
* @return mixed The session ID
146-
*
147-
* @api
144+
* {@inheritdoc}
148145
*/
149146
public function getId()
150147
{
151148
return $this->storage->getId();
152149
}
153150

154151
/**
155-
* Registers a SessionBagInterface with the sessio.
152+
* {@inheritdoc}
153+
*/
154+
public function setId($id)
155+
{
156+
$this->storage->setId($id);
157+
}
158+
159+
/**
160+
* {@inheritdoc}
161+
*/
162+
public function getName()
163+
{
164+
return $this->storage->getName();
165+
}
166+
167+
/**
168+
* {@inheritdoc}
169+
*/
170+
public function setName($name)
171+
{
172+
$this->storage->setName($name);
173+
}
174+
175+
/**
176+
* Registers a SessionBagInterface with the session.
156177
*
157178
* @param SessionBagInterface $bag
158179
*/

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,42 @@ interface SessionInterface
2929
*/
3030
function start();
3131

32+
/**
33+
* Returns the session ID.
34+
*
35+
* @return string The session ID.
36+
*
37+
* @api
38+
*/
39+
function getId();
40+
41+
/**
42+
* Sets the session ID
43+
*
44+
* @param string $id
45+
*
46< 10000 span class="diff-text-marker">+
* @api
47+
*/
48+
function setId($id);
49+
50+
/**
51+
* Returns the session name.
52+
*
53+
* @return mixed The session name.
54+
*
55+
* @api
56+
*/
57+
function getName();
58+
59+
/**
60+
* Sets the session name.
61+
*
62+
* @param string $name
63+
*
64+
* @api
65+
*/
66+
function setName($name);
67+
3268
/**
3369
* Invalidates the current session.
3470
*

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeFileSessionHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
1313

1414
/**
15-
* NativeFileSessionStorage.
15+
* NativeFileSessionHandler.
1616
*
1717
* Native session handler using PHP's built in file storage.
1818
*

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

Lines changed: 86 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Session\Storage;
1313

14-
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
14+
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
1515

1616
/**
1717
* MockArraySessionStorage mocks the session for unit tests.
@@ -25,21 +25,41 @@
2525
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
2626
* @author Drak <drak@zikula.org>
2727
*/
28-
class MockArraySessionStorage extends SessionStorage
28+
class MockArraySessionStorage implements SessionStorageInterface
2929
{
3030
/**
3131
* @var string
3232
*/
33-
protected $sessionId;
33+
protected $id = '';
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $name;
39+
40+
/**
41+
* @var boolean
42+
*/
43+
protected $started = false;
44+
45+
/**
46+
* @var boolean
47+
*/
48+
protected $closed = false;
3449

3550
/**
3651
* @var array
3752
*/
38-
protected $sessionData = array();
53+
protected $data = array();
3954

40-
public function __construct(array $options = array())
55+
/**
56+
* Constructor.
57+
*
58+
* @param string $name Session name
59+
*/
60+
public function __construct($name = 'MOCKSESSID')
4161
{
42-
parent::__construct($options, new NullSessionHandler());
62+
$this->name = $name;
4363
}
4464

4565
/**
@@ -49,7 +69,7 @@ public function __construct(array $options = array())
4969
*/
5070
public function setSessionData(array $array)
5171
{
52-
$this->sessionData = $array;
72+
$this->data = $array;
5373
}
5474

5575
/**
@@ -61,11 +81,11 @@ public function start()
6181
return true;
6282
}
6383

64-
$this->started = true;
65-
$this->loadSession($this->sessionData);
84+
if (empty($this->id)) {
85+
$this->id = $this->generateId();
86+
}
6687

67-
$this->sessionId = $this->generateSessionId();
68-
session_id($this->sessionId);
88+
$this->loadSession();
6989

7090
return true;
7191
}
@@ -80,8 +100,7 @@ public function regenerate($destroy = false)
80100
$this->start();
81101
}
82102

83-
$this->sessionId = $this->generateSessionId();
84-
session_id($this->sessionId);
103+
$this->id = $this->generateId();
85104

86105
return true;
87106
}
@@ -91,11 +110,35 @@ public function regenerate($destroy = false)
91110
*/
92111
public function getId()
93112
{
94-
if (!$this->started) {
95-
return '';
113+
return $this->id;
114+
}
115+
116+
/**
117+
* {@inheritdoc}
118+
*/
119+
public function setId($id)
120+
{
121+
if ($this->started) {
122+
throw new \LogicException('Cannot set session ID after the session has started.');
96123
}
97124

98-
return $this->sessionId;
125+
$this->id = $id;
126+
}
127+
128+
/**
129+
* {@inheritdoc}
130+
*/
131+
public function getName()
132+
{
133+
return $this->name;
134+
}
135+
136+
/**
137+
* {@inheritdoc}
138+
*/
139+
public function setName($name)
140+
{
141+
$this->name = $name;
99142
}
100143

101144
/**
@@ -118,10 +161,18 @@ public function clear()
118161
}
119162

120163
// clear out the session
121-
$this->sessionData = array();
164+
$this->data = array();
122165

123166
// reconnect the bags to the session
124-
$this->loadSession($this->sessionData);
167+
$this->loadSession();
168+
}
169+
170+
/**
171+
* {@inheritdoc}
172+
*/
173+
public function registerBag(SessionBagInterface $bag)
174+
{
175+
$this->bags[$bag->getName()] = $bag;
125176
}
126177

127178
/**
@@ -143,10 +194,25 @@ public function getBag($name)
143194
/**
144195
* Generates a session ID.
145196
*
197+
* This doesn't need to be particularly cryptographically secure since this is just
198+
* a mock.
199+
*
146200
* @return string
147201
*/
148-
protected function generateSessionId()
202+
protected function generateId()
203+
{
204+
return sha1(uniqid(mt_rand()));
205+
}
206+
207+
protected function loadSession()
149208
{
150-
return sha1(uniqid(mt_rand(), true));
209+
foreach ($this->bags as $bag) {
210+
$key = $bag->getStorageKey();
211+
$this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
212+
$bag->initialize($this->data[$key]);
213+
}
214+
215+
$this->started = true;
216+
$this->closed = false;
151217
}
152218
}

0 commit comments

Comments
 (0)
0