Skip to content

Commit

Permalink
Issue #105 Add tests for URL params
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Apr 17, 2017
1 parent f31a579 commit 1695f82
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ protected function redirectToLanguage($language)
array_unshift($params, $route);
$url = $this->createUrl($params);
// Required to prevent double slashes on generated URLs
if ($this->suffix==='/' && $route==='') {
if ($this->suffix==='/' && $route==='' && count($params)===1) {
$url = rtrim($url, '/').'/';
}
// Prevent redirects to same URL which could happen in certain
Expand Down
90 changes: 90 additions & 0 deletions tests/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ class RedirectTest extends TestCase
'/slash/' => false,
'/en/slash/' => '/slash/',
'/de/slash/' => false,

// Params
'/en?a=b' => '/?a=b',
'/en/site/page?a=b' => '/site/page?a=b',
'/en/custom?a=b' => '/custom?a=b',
'/en/slash/?a=b' => '/slash/?a=b',
'/site/page?a=b' => [
['/de/site/page?a=b', 'request' => ['acceptableLanguages' => ['de']]],
],
],
],

Expand Down Expand Up @@ -168,6 +177,15 @@ class RedirectTest extends TestCase
'/slash/' => '/en/slash/',
'/en/slash/' => false,
'/de/slash/' => false,

// Params
'/?a=b' => '/en?a=b',
'/site/page?a=b' => '/en/site/page?a=b',
'/custom?a=b' => '/en/custom?a=b',
'/slash/?a=b' => '/en/slash/?a=b',
'/site/page?a=b' => [
['/de/site/page?a=b', 'request' => ['acceptableLanguages' => ['de']]],
],
],
],

Expand Down Expand Up @@ -276,6 +294,15 @@ class RedirectTest extends TestCase
'/noslash' => false,
'/en/noslash' => '/noslash',
'/de/noslash' => false,

// Params
'/en?a=b' => '/?a=b',
'/en/site/page/?a=b' => '/site/page/?a=b',
'/en/custom/?a=b' => '/custom/?a=b',
'/en/noslash?a=b' => '/noslash?a=b',
'/site/page/?a=b' => [
['/de/site/page/?a=b', 'request' => ['acceptableLanguages' => ['de']]],
],
],
],
[
Expand Down Expand Up @@ -321,6 +348,15 @@ class RedirectTest extends TestCase
'/noslash' => '/en/noslash',
'/en/noslash' => false,
'/de/noslash' => false,

// Params
'/?a=b' => '/en/?a=b',
'/site/page/?a=b' => '/en/site/page/?a=b',
'/custom/?a=b' => '/en/custom/?a=b',
'/noslash?a=b' => '/en/noslash?a=b',
'/site/page/?a=b' => [
['/de/site/page/?a=b', 'request' => ['acceptableLanguages' => ['de']]],
],
],
],

Expand Down Expand Up @@ -368,6 +404,21 @@ class RedirectTest extends TestCase
'/en/noslash/' => '/noslash',
'/de/noslash' => false,
'/de/noslash/' => '/de/noslash',

// Params
'/site/page?a=b' => '/site/page/?a=b',
'/de?a=b' => '/de/?a=b',
'/de/site/login?a=b' => '/de/site/login/?a=b',
'/en/site/login?a=b' => '/site/login/?a=b',
'/en/site/login/?a=b' => '/site/login/?a=b',
'/custom?a=b' => '/custom/?a=b',
'/en/custom?a=b' => '/custom/?a=b',
'/en/custom/?a=b' => '/custom/?a=b',
'/de/custom?a=b' => '/de/custom/?a=b',
'/noslash/?a=b' => '/noslash?a=b',
'/en/noslash?a=b' => '/noslash?a=b',
'/en/noslash/?a=b' => '/noslash?a=b',
'/de/noslash/?a=b' => '/de/noslash?a=b',
],
],
[
Expand Down Expand Up @@ -412,6 +463,21 @@ class RedirectTest extends TestCase
'/en/slash/' => '/slash/',
'/de/slash' => '/de/slash/',
'/de/slash/' => false,

// Params
'/site/page/?a=b' => '/site/page?a=b',
'/de/?a=b' => '/de?a=b', // normalizer
'/de/site/login/?a=b' => '/de/site/login?a=b', // normalizer
'/en/site/login/?a=b' => '/site/login?a=b', // normalizer
'/en/site/login?a=b' => '/site/login?a=b', // localeurls
'/custom/?a=b' => '/custom?a=b',
'/en/custom?a=b' => '/custom?a=b',
'/en/custom/?a=b' => '/custom?a=b',
'/de/custom/?a=b' => '/de/custom?a=b',
'/slash?a=b' => '/slash/?a=b',
'/en/slash?a=b' => '/slash/?a=b',
'/en/slash/?a=b' => '/slash/?a=b',
'/de/slash?a=b' => '/de/slash/?a=b',
],
],

Expand Down Expand Up @@ -459,6 +525,18 @@ class RedirectTest extends TestCase
'/en/noslash/' => '/en/noslash',
'/de/noslash' => false,
'/de/noslash/' => '/de/noslash',

// Params
'?a=b' => '/en/?a=b',
'/site/page?a=b' => '/en/site/page/?a=b',
'/custom?a=b' => '/en/custom/?a=b',
'/custom/?a=b' => '/en/custom/?a=b',
'/en/custom?a=b' => '/en/custom/?a=b',
'/de/custom?a=b' => '/de/custom/?a=b',
'/noslash?a=b' => '/en/noslash?a=b',
'/noslash/?a=b' => '/en/noslash?a=b',
'/en/noslash/?a=b' => '/en/noslash?a=b',
'/de/noslash/?a=b' => '/de/noslash?a=b',
],
],
[
Expand Down Expand Up @@ -503,6 +581,18 @@ class RedirectTest extends TestCase
'/en/slash/' => false,
'/de/slash' => '/de/slash/',
'/de/slash/' => false,

// Params
'/?a=b' => '/en?a=b',
'/site/page/?a=b' => '/en/site/page?a=b',
'/en/site/page/?a=b' => '/en/site/page?a=b',
'/custom?a=b' => '/en/custom?a=b',
'/custom/?a=b' => '/en/custom?a=b',
'/de/custom/?a=b' => '/de/custom?a=b',
'/slash?a=b' => '/en/slash/?a=b',
'/slash/?a=b' => '/en/slash/?a=b',
'/en/slash?a=b' => '/en/slash/?a=b',
'/de/slash?a=b' => '/de/slash/?a=b',
],
],
];
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected function mockRequest($url, $config = [])
$parts = explode('?', $url);
if (isset($parts[1])) {
$_SERVER['QUERY_STRING'] = $parts[1];
parse_str($parts[1], $_GET);
} else {
$_GET = [];
}
if ($config!==[]) {
$config = [
Expand Down
2 changes: 2 additions & 0 deletions tests/UrlManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public function testDoesNothingIfInvalidLanguageInCookie()
'languages' => ['en-US', 'en', 'de'],
]);
$this->mockRequest('/site/page');
$this->assertTrue(true);
}

public function testDoesNothingIfInvalidLanguageInSession()
Expand All @@ -176,6 +177,7 @@ public function testDoesNothingIfInvalidLanguageInSession()
'languages' => ['en-US', 'en', 'de'],
]);
$this->mockRequest('/site/page');
$this->assertTrue(true);
}


Expand Down

0 comments on commit 1695f82

Please sign in to comment.