Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use session if not previously started #226

Open
wants to merge 1 commit into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}