From 1695f823b61ae8f9347a0159c77c210fde6326d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Mon, 17 Apr 2017 18:16:13 +0200 Subject: [PATCH] Issue #105 Add tests for URL params --- UrlManager.php | 2 +- tests/RedirectTest.php | 90 ++++++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 3 ++ tests/UrlManagerTest.php | 2 + 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/UrlManager.php b/UrlManager.php index 9487986..cbf2a2c 100644 --- a/UrlManager.php +++ b/UrlManager.php @@ -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 diff --git a/tests/RedirectTest.php b/tests/RedirectTest.php index 3df9958..603bd1e 100644 --- a/tests/RedirectTest.php +++ b/tests/RedirectTest.php @@ -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']]], + ], ], ], @@ -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']]], + ], ], ], @@ -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']]], + ], ], ], [ @@ -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']]], + ], ], ], @@ -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', ], ], [ @@ -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', ], ], @@ -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', ], ], [ @@ -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', ], ], ]; diff --git a/tests/TestCase.php b/tests/TestCase.php index 4cd98fc..73748ab 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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 = [ diff --git a/tests/UrlManagerTest.php b/tests/UrlManagerTest.php index c67a752..d0e5006 100644 --- a/tests/UrlManagerTest.php +++ b/tests/UrlManagerTest.php @@ -166,6 +166,7 @@ public function testDoesNothingIfInvalidLanguageInCookie() 'languages' => ['en-US', 'en', 'de'], ]); $this->mockRequest('/site/page'); + $this->assertTrue(true); } public function testDoesNothingIfInvalidLanguageInSession() @@ -176,6 +177,7 @@ public function testDoesNothingIfInvalidLanguageInSession() 'languages' => ['en-US', 'en', 'de'], ]); $this->mockRequest('/site/page'); + $this->assertTrue(true); }