Skip to content

Commit

Permalink
Stop sessions from being created if not already started. Fixes luneti…
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilWhitworth committed Nov 27, 2017
1 parent 7d0b4d1 commit e6cd367
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
17 changes: 9 additions & 8 deletions LocaleGuesser/SessionLocaleGuesser.php
Expand Up @@ -57,16 +57,17 @@ public function __construct(Session $session, MetaValidator $metaValidator, $ses
*/
public function guessLocale(Request $request)
{
if ($this->session->has($this->sessionVariable)) {
$locale = $this->session->get($this->sessionVariable);
if (!$this->metaValidator->isAllowed($locale)) {
return false;
}
$this->identifiedLocale = $this->session->get($this->sessionVariable);
if ($this->session->isStarted()) {
if ($this->session->has($this->sessionVariable)) {
$locale = $this->session->get($this->sessionVariable);
if (!$this->metaValidator->isAllowed($locale)) {
return false;
}
$this->identifiedLocale = $this->session->get($this->sessionVariable);

return true;
return true;
}
}

return false;
}

Expand Down
37 changes: 37 additions & 0 deletions Tests/LocaleGuesser/SessionLocaleGuesserTest.php
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 e6cd367

Please sign in to comment.