From 46c199dd65396498fc5186d1c8b68bc58d3e7397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20L=C3=B6fgren?= Date: Tue, 29 Apr 2014 15:34:07 +0200 Subject: [PATCH 1/2] Use request path info as fallback --- .../LocaleGuesser/UrlLocaleGuesser.php | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Kunstmaan/LanguageChooserBundle/LocaleGuesser/UrlLocaleGuesser.php b/src/Kunstmaan/LanguageChooserBundle/LocaleGuesser/UrlLocaleGuesser.php index f54556b0a6..aae2cbd47c 100644 --- a/src/Kunstmaan/LanguageChooserBundle/LocaleGuesser/UrlLocaleGuesser.php +++ b/src/Kunstmaan/LanguageChooserBundle/LocaleGuesser/UrlLocaleGuesser.php @@ -39,15 +39,23 @@ public function __construct(MetaValidator $metaValidator) public function guessLocale(Request $request) { $localeValidator = $this->metaValidator; - if ($path = $request->attributes->get('path')) { - $parts = array_filter(explode("/", $path)); - $locale = array_shift($parts); - if ($localeValidator->isAllowed($locale)) { - $this->identifiedLocale = $locale; + $path = $request->getPathInfo(); + if ($request->attributes->has('path')) { + $path = $request->attributes->get('path'); + } + + if (!$path) { + return false; + } + + $parts = array_filter(explode("/", $path)); + $locale = array_shift($parts); + + if ($localeValidator->isAllowed($locale)) { + $this->identifiedLocale = $locale; - return true; - } + return true; } return false; From dc7e0d48a2c550c7adf366bb7e6ac3270cb426ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20L=C3=B6fgren?= Date: Tue, 29 Apr 2014 16:12:22 +0200 Subject: [PATCH 2/2] Add tests for Url locale guesser --- .../LocaleGuesser/UrlLocaleGuesserTest.php | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/Kunstmaan/LanguageChooserBundle/Tests/LocaleGuesser/UrlLocaleGuesserTest.php diff --git a/src/Kunstmaan/LanguageChooserBundle/Tests/LocaleGuesser/UrlLocaleGuesserTest.php b/src/Kunstmaan/LanguageChooserBundle/Tests/LocaleGuesser/UrlLocaleGuesserTest.php new file mode 100644 index 0000000000..95f11f51e4 --- /dev/null +++ b/src/Kunstmaan/LanguageChooserBundle/Tests/LocaleGuesser/UrlLocaleGuesserTest.php @@ -0,0 +1,117 @@ +metaValidator = $this->getMockBuilder('Lunetics\LocaleBundle\Validator\MetaValidator') + ->disableOriginalConstructor() + ->getMock(); + + $this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') + ->disableOriginalConstructor() + ->getMock(); + + $this->request->attributes = new ParameterBag(); + + $this->object = new UrlLocaleGuesser($this->metaValidator); + } + + /** + * @covers Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser::guessLocale + */ + public function testFoundLocalePathAttribute() + { + $this->metaValidator->expects($this->once()) + ->method('isAllowed') + ->will($this->returnValue(true)); + + $this->request->expects($this->once()) + ->method('getPathInfo') + ->will($this->returnValue('/de/some-path')); + + $this->request->attributes->set('path', '/de/some-path'); + + $this->assertTrue($this->object->guessLocale($this->request)); + $this->assertEquals('de', $this->object->getIdentifiedLocale()); + } + + /** + * @covers Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser::guessLocale + */ + public function testFoundLocalePathInfo() + { + $this->metaValidator->expects($this->once()) + ->method('isAllowed') + ->will($this->returnValue(true)); + + $this->request->expects($this->once()) + ->method('getPathInfo') + ->will($this->returnValue('/fr/some-other-path')); + + $this->assertTrue($this->object->guessLocale($this->request)); + $this->assertEquals('fr', $this->object->getIdentifiedLocale()); + } + + /** + * @covers Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser::guessLocale + */ + public function testNullPath() + { + $this->metaValidator->expects($this->never()) + ->method('isAllowed'); + + $this->request->expects($this->once()) + ->method('getPathInfo') + ->will($this->returnValue(null)); + + $this->assertFalse($this->object->guessLocale($this->request)); + $this->assertEquals(null, $this->object->getIdentifiedLocale()); + } + + /** + * @covers Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser::guessLocale + */ + public function testInvalidLocaleFound() + { + $this->metaValidator->expects($this->once()) + ->method('isAllowed') + ->will($this->returnValue(false)); + + $this->request->expects($this->once()) + ->method('getPathInfo') + ->will($this->returnValue('/en/another-path')); + + $this->assertFalse($this->object->guessLocale($this->request)); + $this->assertEquals(null, $this->object->getIdentifiedLocale()); + } +}