Skip to content

Commit

Permalink
Do not use session if not previously started
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelVella committed Jul 24, 2018
1 parent 052917f commit d95962c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
6 changes: 4 additions & 2 deletions Router/DefaultLocaleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand All @@ -74,4 +76,4 @@ public function resolveLocale(Request $request, array $availableLocales)

return null;
}
}
}
48 changes: 34 additions & 14 deletions Tests/Router/DefaultLocaleResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -65,4 +64,25 @@ protected function setUp()
'bar' => 'de',
));
}
}

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;
}
}

0 comments on commit d95962c

Please sign in to comment.