Skip to content

Commit

Permalink
Merge pull request #103 from kokspflanze/feature/default_locale
Browse files Browse the repository at this point in the history
Don't redirect to default locale
  • Loading branch information
svycka authored Mar 21, 2019
2 parents 2a8a0a6 + e6d494d commit bf7a75e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/SlmLocale/Strategy/UriPathStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class UriPathStrategy extends AbstractStrategy
protected $aliases;
protected $redirect_to_canonical;
protected $sl;
/** @var string|null */
protected $default;
/**
* @var SimpleRouteStack
*/
Expand All @@ -74,6 +76,9 @@ public function setOptions(array $options = [])
if (array_key_exists('redirect_to_canonical', $options)) {
$this->redirect_to_canonical = (bool) $options['redirect_to_canonical'];
}
if (array_key_exists('default', $options)) {
$this->default = (string) $options['default'];
}
}

protected function redirectWhenFound()
Expand Down Expand Up @@ -124,7 +129,7 @@ public function found(LocaleEvent $event)
}

$locale = $event->getLocale();
if (null === $locale) {
if (null === $locale || (null !== $this->default && $locale === $this->default)) {
return;
}

Expand Down Expand Up @@ -196,7 +201,13 @@ public function assemble(LocaleEvent $event)
// Remove first part
array_shift($parts);

$path = $base . '/' . $locale . '/' . implode('/', $parts);
if ($locale === $this->default) {
$locale = '';
} else {
$locale .= '/';
}

$path = $base . '/' . $locale . implode('/', $parts);
$uri->setPath($path);

return $uri;
Expand Down
72 changes: 72 additions & 0 deletions tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,42 @@ public function testFoundSetsBaseUrl()
$this->assertEquals($expected, $actual);
}

public function testFoundSetsBaseUrlWithDefault()
{
$request = new HttpRequest();
$request->setUri('http://example.com/');

$this->event->setLocale('en');
$this->event->setRequest($request);
$this->event->setResponse(new HttpResponse());

$this->strategy->setOptions([
'default' => 'en',
]);
$this->strategy->found($this->event);

$actual = $this->router->getBaseUrl();
$this->assertNull($actual);
}

public function testFoundSetsBaseUrlWithDefaultNotMatch()
{
$request = new HttpRequest();
$request->setUri('http://example.com/fr');

$this->event->setLocale('fr');
$this->event->setRequest($request);
$this->event->setResponse(new HttpResponse());

$this->strategy->setOptions([
'default' => 'en',
]);
$this->strategy->found($this->event);

$actual = $this->router->getBaseUrl();
$this->assertSame('/fr', $actual);
}

public function testFoundAppendsExistingBaseUrl()
{
$this->router->setBaseUrl('/some/deep/installation/path');
Expand Down Expand Up @@ -348,6 +384,42 @@ public function testAssembleWorksWithAliasesToo()
$this->assertEquals($expected, $actual);
}

public function testAssembleWithDefault()
{
$uri = new Uri('/nl/foo/bar/baz');

$this->event->setLocale('en');
$this->event->setUri($uri);

$this->strategy->setOptions([
'default' => 'en',
]);
$this->strategy->assemble($this->event);

$expected = '/foo/bar/baz';
$actual = $this->event->getUri()->getPath();

$this->assertSame($expected, $actual);
}

public function testAssembleWithDefaultNotMatching()
{
$uri = new Uri('/nl/foo/bar/baz');

$this->event->setLocale('en');
$this->event->setUri($uri);

$this->strategy->setOptions([
'default' => 'fr',
]);
$this->strategy->assemble($this->event);

$expected = '/en/foo/bar/baz';
$actual = $this->event->getUri()->getPath();

$this->assertSame($expected, $actual);
}

protected function getPluginManager($console = false)
{
$sl = new ServiceManager();
Expand Down

0 comments on commit bf7a75e

Please sign in to comment.