diff --git a/Router/DefaultLocaleResolver.php b/Router/DefaultLocaleResolver.php index 9bb5406..af97776 100755 --- a/Router/DefaultLocaleResolver.php +++ b/Router/DefaultLocaleResolver.php @@ -49,7 +49,9 @@ public function resolveLocale(Request $request, array $availableLocales) // check if a session exists, and if it contains a locale if ($request->hasPreviousSession()) { $session = $request->getSession(); - if ($session->has('_locale')) { + + // Do not use session if not already started + if ($session->isStarted() && $session->has('_locale')) { return $session->get('_locale'); } } @@ -74,4 +76,4 @@ public function resolveLocale(Request $request, array $availableLocales) return null; } -} \ No newline at end of file +} diff --git a/Tests/Functional/TestBundle/Resources/views/default/index.html.twig b/Tests/Functional/TestBundle/Resources/views/Default/index.html.twig similarity index 100% rename from Tests/Functional/TestBundle/Resources/views/default/index.html.twig rename to Tests/Functional/TestBundle/Resources/views/Default/index.html.twig diff --git a/Tests/Router/DefaultLocaleResolverTest.php b/Tests/Router/DefaultLocaleResolverTest.php index 0f17bdb..0fb604c 100755 --- a/Tests/Router/DefaultLocaleResolverTest.php +++ b/Tests/Router/DefaultLocaleResolverTest.php @@ -27,26 +27,25 @@ public function getResolutionTests() $tests[] = array(Request::create('/?hl=de'), array('foo'), 'de', 'Query parameter is selected'); $tests[] = array(Request::create('/?hl=de', 'GET', array(), array('hl' => 'en')), array('foo'), 'de', 'Query parameter has precedence before cookie'); - $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); - $session->expects($this->any()) - ->method('has') - ->with('_locale') - ->will($this->returnValue(true)); - $session->expects($this->any()) - ->method('get') - ->with('_locale') - ->will($this->returnValue('fr')); - $session->expects($this->any()) - ->method('getName') - ->will($this->returnValue('SESS')); - $tests[] = array($request = Request::create('/?hl=de', 'GET', array(), array('SESS' => 'foo')), array('foo'), 'de', 'Query parameter has precedence before session'); + $session = $this->createSession(); $request->setSession($session); $tests[] = array($request = Request::create('/', 'GET', array(), array('SESS' => 'foo')), array('foo'), 'fr', 'Session is used'); + $session = $this->createSession(); + $request->setSession($session); + + $tests[] = array($request = Request::create('/', 'GET', array(), array('SESS' => 'foo')), array('foo'), null, 'No session'); + + $tests[] = array($request = Request::create('/', 'GET', array(), array('SESS' => 'foo')), array('foo'), 'fr', 'Session is not started'); + $session = $this->createSession(); + $session->expects($this->any()) + ->method('isStarted') + ->will($this->returnValue(false)); $request->setSession($session); $tests[] = array($request = Request::create('/', 'GET', array(), array('hl' => 'es', 'SESS' => 'foo')), array('foo'), 'fr', 'Session has precedence before cookie.'); + $session = $this->createSession(); $request->setSession($session); $tests[] = array(Request::create('/', 'GET', array(), array('hl' => 'es')), array('foo'), 'es', 'Cookie is used'); @@ -65,4 +64,25 @@ protected function setUp() 'bar' => 'de', )); } -} \ No newline at end of file + + private function createSession() + { + $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); + $session->expects($this->any()) + ->method('has') + ->with('_locale') + ->will($this->returnValue(true)); + $session->expects($this->any()) + ->method('get') + ->with('_locale') + ->will($this->returnValue('fr')); + $session->expects($this->any()) + ->method('getName') + ->will($this->returnValue('SESS')); + $session->expects($this->any()) + ->method('isStarted') + ->will($this->returnValue(true)); + + return $session; + } +}