Skip to content

Commit 53d3a13

Browse files
committed
Do not use session if not previously started
1 parent 052917f commit 53d3a13

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

Router/DefaultLocaleResolver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public function resolveLocale(Request $request, array $availableLocales)
4949
// check if a session exists, and if it contains a locale
5050
if ($request->hasPreviousSession()) {
5151
$session = $request->getSession();
52-
if ($session->has('_locale')) {
52+
53+
// Do not use session if not already started
54+
if ($session->isStarted() && $session->has('_locale')) {
5355
return $session->get('_locale');
5456
}
5557
}
@@ -74,4 +76,4 @@ public function resolveLocale(Request $request, array $availableLocales)
7476

7577
return null;
7678
}
77-
}
79+
}

Tests/Router/DefaultLocaleResolverTest.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,25 @@ public function getResolutionTests()
2727
$tests[] = array(Request::create('/?hl=de'), array('foo'), 'de', 'Query parameter is selected');
2828
$tests[] = array(Request::create('/?hl=de', 'GET', array(), array('hl' => 'en')), array('foo'), 'de', 'Query parameter has precedence before cookie');
2929

30-
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
31-
$session->expects($this->any())
32-
->method('has')
33-
->with('_locale')
34-
->will($this->returnValue(true));
35-
$session->expects($this->any())
36-
->method('get')
37-
->with('_locale')
38-
->will($this->returnValue('fr'));
39-
$session->expects($this->any())
40-
->method('getName')
41-
->will($this->returnValue('SESS'));
42-
4330
$tests[] = array($request = Request::create('/?hl=de', 'GET', array(), array('SESS' => 'foo')), array('foo'), 'de', 'Query parameter has precedence before session');
31+
$session = $this->createSession();
4432
$request->setSession($session);
4533

4634
$tests[] = array($request = Request::create('/', 'GET', array(), array('SESS' => 'foo')), array('foo'), 'fr', 'Session is used');
35+
$session = $this->createSession();
36+
$request->setSession($session);
37+
38+
$tests[] = array($request = Request::create('/', 'GET', array(), array('SESS' => 'foo')), array('foo'), null, 'No session');
39+
40+
$tests[] = array($request = Request::create('/', 'GET', array(), array('SESS' => 'foo')), array('foo'), 'fr', 'Session is not started');
41+
$session = $this->createSession();
42+
$session->expects($this->any())
43+
->method('isStarted')
44+
->will($this->returnValue(false));
4745
$request->setSession($session);
4846

4947
$tests[] = array($request = Request::create('/', 'GET', array(), array('hl' => 'es', 'SESS' => 'foo')), array('foo'), 'fr', 'Session has precedence before cookie.');
48+
$session = $this->createSession();
5049
$request->setSession($session);
5150

5251
$tests[] = array(Request::create('/', 'GET', array(), array('hl' => 'es')), array('foo'), 'es', 'Cookie is used');
@@ -65,4 +64,25 @@ protected function setUp()
6564
'bar' => 'de',
6665
));
6766
}
68-
}
67+
68+
private function createSession()
69+
{
70+
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
71+
$session->expects($this->any())
72+
->method('has')
73+
->with('_locale')
74+
->will($this->returnValue(true));
75+
$session->expects($this->any())
76+
->method('get')
77+
->with('_locale')
78+
->will($this->returnValue('fr'));
79+
$session->expects($this->any())
80+
->method('getName')
81+
->will($this->returnValue('SESS'));
82+
$session->expects($this->any())
83+
->method('isStarted')
84+
->will($this->returnValue(true));
85+
86+
return $session;
87+
}
88+
}

0 commit comments

Comments
 (0)