Skip to content

Commit

Permalink
Merge pull request #2927 from jlamim/bug_localizeroute
Browse files Browse the repository at this point in the history
Localization bug fix in localizeRoute method
  • Loading branch information
lonnieezell authored May 7, 2020
2 parents a5e3fa2 + 688fa33 commit 85cd56a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
}

//--------------------------------------------------------------------
Expand Down
84 changes: 84 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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
*/
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 85cd56a

Please sign in to comment.