Skip to content

Commit

Permalink
Merge pull request #199 from NeilWhitworth/master
Browse files Browse the repository at this point in the history
Stop sessions from being created if not already started. Fixes #198
  • Loading branch information
lunetics authored Feb 3, 2018
2 parents 285f787 + 31e4074 commit 11de2c1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions LocaleGuesser/SessionLocaleGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function __construct(Session $session, MetaValidator $metaValidator, $ses
*/
public function guessLocale(Request $request)
{
if (!$this->session->isStarted()) {
return false;
}
if ($this->session->has($this->sessionVariable)) {
$locale = $this->session->get($this->sessionVariable);
if (!$this->metaValidator->isAllowed($locale)) {
Expand Down
37 changes: 37 additions & 0 deletions Tests/LocaleGuesser/SessionLocaleGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ public function testSetSessionLocale()
$this->assertAttributeContains($locale, 'session', $guesser);
}


public function testLocaleIsNotRetrievedFromSessionIfNotStarted()
{
$request = $this->getRequestNoSessionLocale();
$metaValidator = $this->getMetaValidatorMock();
$expectation = $metaValidator->expects($this->never())
->method('isAllowed');
$this->setMultipleMatching($expectation, array('ru'), array(false));

$guesser = $this->getGuesser($request->getSession(), $metaValidator);
$guesser->guessLocale($request);
$this->assertFalse($guesser->getIdentifiedLocale());
}

public function testSessionIsNotAutomaticalyStarted()
{
$request = $this->getRequestNoSessionLocale();
$metaValidator = $this->getMetaValidatorMock();
$session = $request->getSession();

$guesser = $this->getGuesser($request->getSession(), $metaValidator);
$guesser->guessLocale($request);
$this->assertFalse($session->isStarted());
}


private function getGuesser($session = null, $metaValidator = null)
{
if (null === $session) {
Expand All @@ -84,6 +110,17 @@ private function getGuesser($session = null, $metaValidator = null)
return new SessionLocaleGuesser($session, $metaValidator);
}


private function getRequestNoSessionLocale()
{
$session = new Session(new MockArraySessionStorage());
$request = Request::create('/');
$request->setSession($session);
$request->headers->set('Accept-language', 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4');

return $request;
}

private function getRequestWithSessionLocale($locale = 'ru')
{
$session = new Session(new MockArraySessionStorage());
Expand Down

0 comments on commit 11de2c1

Please sign in to comment.