From 7554783573d633a83be40fbbb2afa6c9392e9155 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Mon, 3 Jun 2019 22:43:49 -0500 Subject: [PATCH 1/2] Zero params should be passed through when routing. Fixes #2032 --- system/Router/Router.php | 4 +++- tests/system/Router/RouterTest.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/system/Router/Router.php b/system/Router/Router.php index 01e8a940e2c7..843e8427d17c 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -568,7 +568,9 @@ public function autoRoute(string $uri) */ protected function validateRequest(array $segments): array { - $segments = array_filter($segments); + $segments = array_filter($segments, function ($segment) { + return ! empty($segment) || ($segment !== '0' || $segment !== 0); + }); $segments = array_values($segments); $c = count($segments); diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index f850e15e2d6b..1ecf4d0eaf1d 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -490,4 +490,23 @@ public function testTranslateURIDashesForAutoRoute() } //-------------------------------------------------------------------- + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/2032 + */ + public function testAutoRouteMatchesZeroParams() + { + $router = new Router($this->collection, $this->request); + + $router->autoRoute('myController/someMethod/0/abc'); + + $this->assertEquals('MyController', $router->controllerName()); + $this->assertEquals('someMethod', $router->methodName()); + + $expected = [ + '0', + 'abc', + ]; + $this->assertEquals($expected, $router->params()); + } } From 92ec0804468fea6bd24aa9569bd7fcc4d1f34b32 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Thu, 6 Jun 2019 23:25:16 -0500 Subject: [PATCH 2/2] Fix test that seems irrelevant... --- system/Test/FeatureTestCase.php | 4 ++-- tests/system/Test/FeatureTestCaseTest.php | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/system/Test/FeatureTestCase.php b/system/Test/FeatureTestCase.php index 83ddc8e24e51..229cfe31f900 100644 --- a/system/Test/FeatureTestCase.php +++ b/system/Test/FeatureTestCase.php @@ -151,7 +151,8 @@ public function skipEvents() public function call(string $method, string $path, array $params = null) { // Simulate having a blank session - $_SESSION = []; + $_SESSION = []; + $_SERVER['REQUEST_METHOD'] = $method; $request = $this->setupRequest($method, $path, $params); $request = $this->populateGlobals($method, $request, $params); @@ -165,7 +166,6 @@ public function call(string $method, string $path, array $params = null) // Make sure any other classes that might call the request // instance get the right one. Services::injectMock('request', $request); - $_SERVER['REQUEST_METHOD'] = $method; $response = $this->app ->setRequest($request) diff --git a/tests/system/Test/FeatureTestCaseTest.php b/tests/system/Test/FeatureTestCaseTest.php index e54539235241..a0cb01772943 100644 --- a/tests/system/Test/FeatureTestCaseTest.php +++ b/tests/system/Test/FeatureTestCaseTest.php @@ -137,10 +137,18 @@ function () { public function testSession() { - $response = $this->withSession([ - 'fruit' => 'apple', - 'greeting' => 'hello', - ])->get('home'); + $response = $this->withRoutes([ + [ + 'get', + 'home', + function () { + return 'Home'; + }, + ], + ])->withSession([ + 'fruit' => 'apple', + 'greeting' => 'hello', + ])->get('home'); $response->assertSessionHas('fruit', 'apple'); $response->assertSessionMissing('popcorn');