From 37917e688a84fc18dbe5aaaac800b2ebd23b039a Mon Sep 17 00:00:00 2001 From: Jonathan Lamim Date: Mon, 4 May 2020 08:30:37 -0300 Subject: [PATCH 1/2] Localization bug fix in localizeRoute method - fixes #2921 --- system/Router/RouteCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index cdb10074c2c7..3f1b0059ebfb 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1277,7 +1277,7 @@ public function reverseRoute(string $search, ...$params) */ protected function localizeRoute(string $route) :string { - return strtr($route, ['{locale}' => Services::language()->getLocale()]); + return strtr($route, ['{locale}' => Services::request()->getLocale()]); } //-------------------------------------------------------------------- From 688fa330f2ecd0231ef6540ccc29f6dc970eccfe Mon Sep 17 00:00:00 2001 From: Jonathan Lamim Date: Mon, 4 May 2020 16:00:48 -0300 Subject: [PATCH 2/2] Unit Tests --- tests/system/Router/RouteCollectionTest.php | 84 +++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 6c26f56e5f3d..aedfd8671cce 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -775,6 +775,19 @@ public function testReverseRoutingFindsSimpleMatch() //-------------------------------------------------------------------- + public function testReverseRoutingWithLocaleAndFindsSimpleMatch() + { + $routes = $this->getCollector(); + + $routes->add('{locale}/path/(:any)/to/(:num)', 'myController::goto/$1/$2'); + + $match = $routes->reverseRoute('myController::goto', 'string', 13); + + $this->assertEquals('/en/path/string/to/13', $match); + } + + //-------------------------------------------------------------------- + public function testReverseRoutingReturnsFalseWithBadParamCount() { $routes = $this->getCollector(); @@ -830,6 +843,17 @@ public function testNamedRoutes() //-------------------------------------------------------------------- + public function testNamedRoutesWithLocale() + { + $routes = $this->getCollector(); + + $routes->add('{locale}/users', 'Users::index', ['as' => 'namedRoute']); + + $this->assertEquals('/en/users', $routes->reverseRoute('namedRoute')); + } + + //-------------------------------------------------------------------- + public function testNamedRoutesFillInParams() { $routes = $this->getCollector(); @@ -843,6 +867,19 @@ public function testNamedRoutesFillInParams() //-------------------------------------------------------------------- + public function testNamedRoutesWithLocaleAndFillInParams() + { + $routes = $this->getCollector(); + + $routes->add('{locale}/path/(:any)/to/(:num)', 'myController::goto/$1/$2', ['as' => 'namedRoute']); + + $match = $routes->reverseRoute('namedRoute', 'string', 13); + + $this->assertEquals('/en/path/string/to/13', $match); + } + + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/642 */ @@ -871,6 +908,38 @@ public function testNamedRoutesWithSameURIDifferentMethods() //-------------------------------------------------------------------- + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/642 + */ + public function testNamedRoutesWithLocaleAndWithSameURIDifferentMethods() + { + $routes = $this->getCollector(); + + $routes->get('{locale}/user/insert', 'myController::goto/$1/$2', ['as' => 'namedRoute1']); + $routes->post( + '{locale}/user/insert', + function () { + }, + ['as' => 'namedRoute2'] + ); + $routes->put( + '{locale}/user/insert', + function () { + }, + ['as' => 'namedRoute3'] + ); + + $match1 = $routes->reverseRoute('namedRoute1'); + $match2 = $routes->reverseRoute('namedRoute2'); + $match3 = $routes->reverseRoute('namedRoute3'); + + $this->assertEquals('/en/user/insert', $match1); + $this->assertEquals('/en/user/insert', $match2); + $this->assertEquals('/en/user/insert', $match3); + } + + //-------------------------------------------------------------------- + public function testReverseRouteMatching() { $routes = $this->getCollector(); @@ -882,6 +951,21 @@ public function testReverseRouteMatching() $this->assertEquals('/test/1/2', $match); } + //-------------------------------------------------------------------- + + public function testReverseRouteMatchingWithLocale() + { + $routes = $this->getCollector(); + + $routes->get('{locale}/test/(:segment)/(:segment)', 'TestController::test/$1/$2', ['as' => 'testRouter']); + + $match = $routes->reverseRoute('testRouter', 1, 2); + + $this->assertEquals('/en/test/1/2', $match); + } + + //-------------------------------------------------------------------- + public function testAddRedirect() { $routes = $this->getCollector();