From 6c8660a4c153c0a63bb4823c4b36e9c3ab2efa27 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Mon, 11 Sep 2023 18:40:51 +0800 Subject: [PATCH 01/20] fix: Fix the filters are executed even when controller does not exist with Auto Routing (Legacy). --- system/CodeIgniter.php | 20 +++++++++++++++++++- tests/system/CodeIgniterTest.php | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 0698d74fbccd..69822bb2d9d6 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -416,7 +416,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache $uri = $this->request->getPath(); - if ($this->enableFilters) { + if ($this->enableFilters && $this->checkControllerNotFoundBeforeFilter()) { // Start up the filters $filters = Services::filters(); @@ -1110,4 +1110,22 @@ protected function outputBufferingEnd(): string return $buffer; } + + /** + * Check whether the controller is not found before the before filter. + */ + protected function checkControllerNotFoundBeforeFilter(): bool + { + // Check whether the controller type is Closure. + if (is_object($this->controller) && (get_class($this->controller) === 'Closure')) { + return true; + } + + // Try to autoload the class + if (! class_exists($this->controller, true) || $this->method[0] === '_') { + throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); + } + + return true; + } } diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index b7b5e092e609..d059f3c72576 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -13,6 +13,7 @@ use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\ConfigException; +use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\HTTP\Response; use CodeIgniter\Router\Exceptions\RedirectException; use CodeIgniter\Router\RouteCollection; @@ -926,4 +927,24 @@ public static function providePageCacheWithCacheQueryString(): iterable '$cacheQueryString=array' => [['important_parameter'], 3, $testingUrls], ]; } + + /** + * See https://github.com/codeigniter4/CodeIgniter4/issues/7205 + */ + public function testRunControllerNotFoundBeforeFilter(): void + { + $_SERVER['argv'] = ['index.php']; + $_SERVER['argc'] = 1; + + $_SERVER['REQUEST_URI'] = '/cannotFound'; + $_SERVER['SCRIPT_NAME'] = '/index.php'; + + // Inject mock router. + $routes = Services::routes(); + $routes->setAutoRoute(true); + + $this->expectException(PageNotFoundException::class); + + $this->codeigniter->run($routes); + } } From a207d000d859cbe351ba5aa23c1e3e21a340c19e Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Tue, 12 Sep 2023 01:46:44 +0800 Subject: [PATCH 02/20] test: add the before filter for the case. --- tests/system/CodeIgniterTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index d059f3c72576..505d1f5e5a7b 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -26,6 +26,7 @@ use Config\Modules; use Config\Routing; use Tests\Support\Filters\Customfilter; +use Tests\Support\Filters\RedirectFilter; /** * @runTestsInSeparateProcesses @@ -943,6 +944,12 @@ public function testRunControllerNotFoundBeforeFilter(): void $routes = Services::routes(); $routes->setAutoRoute(true); + // Inject the before filter. + $filterConfig = config('Filters'); + $filterConfig->aliases['redirectFilter'] = RedirectFilter::class; + $filterConfig->globals['before'] = ['redirectFilter']; + Services::filters($filterConfig); + $this->expectException(PageNotFoundException::class); $this->codeigniter->run($routes); From 818e0b6dad2981e250886845555c0f6d3c972084 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Tue, 12 Sep 2023 20:30:25 +0800 Subject: [PATCH 03/20] fix: Move the condition into `AutoRouter.php` from `CodeIgniter.php`. --- system/CodeIgniter.php | 20 +------------------- system/Router/AutoRouter.php | 5 +++++ 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 69822bb2d9d6..0698d74fbccd 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -416,7 +416,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache $uri = $this->request->getPath(); - if ($this->enableFilters && $this->checkControllerNotFoundBeforeFilter()) { + if ($this->enableFilters) { // Start up the filters $filters = Services::filters(); @@ -1110,22 +1110,4 @@ protected function outputBufferingEnd(): string return $buffer; } - - /** - * Check whether the controller is not found before the before filter. - */ - protected function checkControllerNotFoundBeforeFilter(): bool - { - // Check whether the controller type is Closure. - if (is_object($this->controller) && (get_class($this->controller) === 'Closure')) { - return true; - } - - // Try to autoload the class - if (! class_exists($this->controller, true) || $this->method[0] === '_') { - throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); - } - - return true; - } } diff --git a/system/Router/AutoRouter.php b/system/Router/AutoRouter.php index f7cd02a3d329..918653633037 100644 --- a/system/Router/AutoRouter.php +++ b/system/Router/AutoRouter.php @@ -174,6 +174,11 @@ public function getRoute(string $uri, string $httpVerb): array ); } + // Check if the controller exists. + if (! class_exists($this->controller, true) || $this->method[0] === '_') { + throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); + } + return [$this->directory, $this->controllerName(), $this->methodName(), $params]; } From a2dfa699922f1bd23bcfdc30b573dbb8591593c8 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Mon, 9 Oct 2023 17:25:12 +0800 Subject: [PATCH 04/20] refactor: change the way to handle pagenotfound exception. --- system/Router/AutoRouter.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/system/Router/AutoRouter.php b/system/Router/AutoRouter.php index 918653633037..9ea45ea86e13 100644 --- a/system/Router/AutoRouter.php +++ b/system/Router/AutoRouter.php @@ -159,6 +159,8 @@ public function getRoute(string $uri, string $httpVerb): array $file = APPPATH . 'Controllers/' . $this->directory . $controllerName . '.php'; if (is_file($file)) { include_once $file; + } else { + throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); } // Ensure the controller stores the fully-qualified class name @@ -174,11 +176,6 @@ public function getRoute(string $uri, string $httpVerb): array ); } - // Check if the controller exists. - if (! class_exists($this->controller, true) || $this->method[0] === '_') { - throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); - } - return [$this->directory, $this->controllerName(), $this->methodName(), $params]; } From 07656b123f85579525ca2d348ff572780bc67951 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Mon, 9 Oct 2023 21:24:32 +0800 Subject: [PATCH 05/20] test: fix some test cases. --- tests/system/Router/RouterTest.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index d0c9b5a664ab..5a0d647b01c4 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -212,10 +212,14 @@ public function testAutoRouteFindsControllerWithFileAndMethod(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - $router->autoRoute('myController/someMethod'); + copy(TESTPATH . 'system/Router/Controllers/MyController.php', APPPATH . 'Controllers/Mycontroller.php'); - $this->assertSame('MyController', $router->controllerName()); - $this->assertSame('someMethod', $router->methodName()); + $router->autoRoute('Mycontroller/getSomemethod'); + + unlink(APPPATH . 'Controllers/Mycontroller.php'); + + $this->assertSame('Mycontroller', $router->controllerName()); + $this->assertSame('getSomemethod', $router->methodName()); } public function testAutoRouteFindsControllerWithFile(): void @@ -223,9 +227,13 @@ public function testAutoRouteFindsControllerWithFile(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - $router->autoRoute('myController'); + copy(TESTPATH . 'system/Router/Controllers/MyController.php', APPPATH . 'Controllers/Mycontroller.php'); - $this->assertSame('MyController', $router->controllerName()); + $router->autoRoute('mycontroller'); + + unlink(APPPATH . 'Controllers/Mycontroller.php'); + + $this->assertSame('Mycontroller', $router->controllerName()); $this->assertSame('index', $router->methodName()); } @@ -236,12 +244,16 @@ public function testAutoRouteFindsControllerWithSubfolder(): void mkdir(APPPATH . 'Controllers/Subfolder'); - $router->autoRoute('subfolder/myController/someMethod'); + copy(TESTPATH . 'system/Router/Controllers/Subfolder/MyController.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php'); + + $router->autoRoute('subfolder/Mycontroller/getSomemethod'); + + unlink(APPPATH . 'Controllers/Subfolder/Mycontroller.php'); rmdir(APPPATH . 'Controllers/Subfolder'); - $this->assertSame('MyController', $router->controllerName()); - $this->assertSame('someMethod', $router->methodName()); + $this->assertSame('Mycontroller', $router->controllerName()); + $this->assertSame('getSomemethod', $router->methodName()); } public function testAutoRouteFindsDashedSubfolder(): void From 1c9c0261d8b8fb75f6974e71e4aefc000ea0ef42 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 11 Oct 2023 15:47:32 +0800 Subject: [PATCH 06/20] test: fix the test cases error. --- .../system/Router/Controllers/Admin_user.php | 21 +++++++ tests/system/Router/Controllers/Test.php | 21 +++++++ .../foo/bar/baz/Some_controller.php | 21 +++++++ .../system/Router/Controllers/\316\246.php" | 21 +++++++ .../Router/Controllers/\316\246/Home.php" | 21 +++++++ tests/system/Router/RouterTest.php | 62 ++++++++++++++----- 6 files changed, 150 insertions(+), 17 deletions(-) create mode 100644 tests/system/Router/Controllers/Admin_user.php create mode 100644 tests/system/Router/Controllers/Test.php create mode 100644 tests/system/Router/Controllers/foo/bar/baz/Some_controller.php create mode 100644 "tests/system/Router/Controllers/\316\246.php" create mode 100644 "tests/system/Router/Controllers/\316\246/Home.php" diff --git a/tests/system/Router/Controllers/Admin_user.php b/tests/system/Router/Controllers/Admin_user.php new file mode 100644 index 000000000000..0614fff87254 --- /dev/null +++ b/tests/system/Router/Controllers/Admin_user.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Router\Controllers; + +use CodeIgniter\Controller; + +class Admin_user extends Controller +{ + public function show_list(): void + { + } +} diff --git a/tests/system/Router/Controllers/Test.php b/tests/system/Router/Controllers/Test.php new file mode 100644 index 000000000000..f9e6cc37eed6 --- /dev/null +++ b/tests/system/Router/Controllers/Test.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Router\Controllers; + +use CodeIgniter\Controller; + +class Test extends Controller +{ + public function test(): void + { + } +} diff --git a/tests/system/Router/Controllers/foo/bar/baz/Some_controller.php b/tests/system/Router/Controllers/foo/bar/baz/Some_controller.php new file mode 100644 index 000000000000..0ec6f4a3be2b --- /dev/null +++ b/tests/system/Router/Controllers/foo/bar/baz/Some_controller.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Router\Controllers\foo\bar\baz; + +use CodeIgniter\Controller; + +class Some_controller extends Controller +{ + public function some_method($first = ''): void + { + } +} diff --git "a/tests/system/Router/Controllers/\316\246.php" "b/tests/system/Router/Controllers/\316\246.php" new file mode 100644 index 000000000000..253cd438c58f --- /dev/null +++ "b/tests/system/Router/Controllers/\316\246.php" @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Router\Controllers; + +use CodeIgniter\Controller; + +class Φ extends Controller +{ + public function getIndex(): void + { + } +} diff --git "a/tests/system/Router/Controllers/\316\246/Home.php" "b/tests/system/Router/Controllers/\316\246/Home.php" new file mode 100644 index 000000000000..fd0024f6879e --- /dev/null +++ "b/tests/system/Router/Controllers/\316\246/Home.php" @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Router\Controllers\Φ; + +use CodeIgniter\Controller; + +class Home extends Controller +{ + public function getIndex(): void + { + } +} diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 5a0d647b01c4..02483d3f4fd0 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -201,8 +201,12 @@ public function testAutoRouteFindsDefaultControllerAndMethod(): void $this->collection->setDefaultMethod('test'); $router = new Router($this->collection, $this->request); + copy(TESTPATH . 'system/Router/Controllers/Test.php', APPPATH . 'Controllers/Test.php'); + $router->autoRoute('/'); + unlink(APPPATH . 'Controllers/Test.php'); + $this->assertSame('Test', $router->controllerName()); $this->assertSame('test', $router->methodName()); } @@ -212,9 +216,9 @@ public function testAutoRouteFindsControllerWithFileAndMethod(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/MyController.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); - $router->autoRoute('Mycontroller/getSomemethod'); + $router->autoRoute('mycontroller/getSomemethod'); unlink(APPPATH . 'Controllers/Mycontroller.php'); @@ -227,7 +231,7 @@ public function testAutoRouteFindsControllerWithFile(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/MyController.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('mycontroller'); @@ -244,9 +248,9 @@ public function testAutoRouteFindsControllerWithSubfolder(): void mkdir(APPPATH . 'Controllers/Subfolder'); - copy(TESTPATH . 'system/Router/Controllers/Subfolder/MyController.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/Subfolder/Mycontroller.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php'); - $router->autoRoute('subfolder/Mycontroller/getSomemethod'); + $router->autoRoute('subfolder/mycontroller/getSomemethod'); unlink(APPPATH . 'Controllers/Subfolder/Mycontroller.php'); @@ -263,9 +267,11 @@ public function testAutoRouteFindsDashedSubfolder(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); + copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Mycontroller.php', APPPATH . 'Controllers/Dash_folder/Mycontroller.php'); $router->autoRoute('dash-folder/mycontroller/somemethod'); + unlink(APPPATH . 'Controllers/Dash_folder/Mycontroller.php'); rmdir(APPPATH . 'Controllers/Dash_folder'); $this->assertSame('Dash_folder/', $router->directory()); @@ -280,16 +286,16 @@ public function testAutoRouteFindsDashedController(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - file_put_contents(APPPATH . 'Controllers/Dash_folder/Dash_controller.php', ''); + copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); - $router->autoRoute('dash-folder/dash-controller/somemethod'); + $router->autoRoute('dash-folder/dash-controller/getSomemethod'); unlink(APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); rmdir(APPPATH . 'Controllers/Dash_folder'); $this->assertSame('Dash_folder/', $router->directory()); $this->assertSame('Dash_controller', $router->controllerName()); - $this->assertSame('somemethod', $router->methodName()); + $this->assertSame('getSomemethod', $router->methodName()); } public function testAutoRouteFindsDashedMethod(): void @@ -299,16 +305,16 @@ public function testAutoRouteFindsDashedMethod(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - file_put_contents(APPPATH . 'Controllers/Dash_folder/Dash_controller.php', ''); + copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); - $router->autoRoute('dash-folder/dash-controller/dash-method'); + $router->autoRoute('dash-folder/dash-controller/getDash_method'); unlink(APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); rmdir(APPPATH . 'Controllers/Dash_folder'); $this->assertSame('Dash_folder/', $router->directory()); $this->assertSame('Dash_controller', $router->controllerName()); - $this->assertSame('dash_method', $router->methodName()); + $this->assertSame('getDash_method', $router->methodName()); } public function testAutoRouteFindsDefaultDashFolder(): void @@ -318,9 +324,11 @@ public function testAutoRouteFindsDefaultDashFolder(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); + copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Home.php', APPPATH . 'Controllers/Dash_folder/Home.php'); $router->autoRoute('dash-folder'); + unlink(APPPATH . 'Controllers/Dash_folder/Home.php'); rmdir(APPPATH . 'Controllers/Dash_folder'); $this->assertSame('Dash_folder/', $router->directory()); @@ -335,9 +343,11 @@ public function testAutoRouteFindsMByteDir(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Φ'); + copy(TESTPATH . 'system/Router/Controllers/Φ/Home.php', APPPATH . 'Controllers/Φ/Home.php'); $router->autoRoute('Φ'); + unlink(APPPATH . 'Controllers/Φ/Home.php'); rmdir(APPPATH . 'Controllers/Φ'); $this->assertSame('Φ/', $router->directory()); @@ -351,11 +361,11 @@ public function testAutoRouteFindsMByteController(): void $router = new Router($this->collection, $this->request); $router->setTranslateURIDashes(true); - file_put_contents(APPPATH . 'Controllers/Φ', ''); + copy(TESTPATH . 'system/Router/Controllers/Φ.php', APPPATH . 'Controllers/Φ.php'); $router->autoRoute('Φ'); - unlink(APPPATH . 'Controllers/Φ'); + unlink(APPPATH . 'Controllers/Φ.php'); $this->assertSame('Φ', $router->controllerName()); $this->assertSame('index', $router->methodName()); @@ -744,7 +754,11 @@ public function testTranslateURIDashesForAutoRoute(): void $router = new Router($this->collection, $this->request); $router->setTranslateURIDashes(true); - $router->autoRoute('admin-user/show-list'); + copy(TESTPATH . 'system/Router/Controllers/Admin_user.php', APPPATH . 'Controllers/Admin_user.php'); + + $router->autoRoute('admin_user/show_list'); + + unlink(APPPATH . 'Controllers/Admin_user.php'); $this->assertSame('Admin_user', $router->controllerName()); $this->assertSame('show_list', $router->methodName()); @@ -758,10 +772,14 @@ public function testAutoRouteMatchesZeroParams(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - $router->autoRoute('myController/someMethod/0/abc'); + copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); - $this->assertSame('MyController', $router->controllerName()); - $this->assertSame('someMethod', $router->methodName()); + $router->autoRoute('mycontroller/getSomemethod/0/abc'); + + $this->assertSame('Mycontroller', $router->controllerName()); + $this->assertSame('getSomemethod', $router->methodName()); + + unlink(APPPATH . 'Controllers/Mycontroller.php'); $expected = [ '0', @@ -829,9 +847,19 @@ public function testRouterPriorDirectory(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); + mkdir(APPPATH . 'Controllers/foo'); + mkdir(APPPATH . 'Controllers/foo/bar'); + mkdir(APPPATH . 'Controllers/foo/bar/baz'); + copy(TESTPATH . 'system/Router/Controllers/foo/bar/baz/Some_controller.php', APPPATH . 'Controllers/foo/bar/baz/Some_controller.php'); + $router->setDirectory('foo/bar/baz', false, true); $router->handle('Some_controller/some_method/param1/param2/param3'); + unlink(APPPATH . 'Controllers/foo/bar/baz/Some_controller.php'); + rmdir(APPPATH . 'Controllers/foo/bar/baz'); + rmdir(APPPATH . 'Controllers/foo/bar'); + rmdir(APPPATH . 'Controllers/foo'); + $this->assertSame('foo/bar/baz/', $router->directory()); $this->assertSame('Some_controller', $router->controllerName()); $this->assertSame('some_method', $router->methodName()); From 8bf95da3b63f0e32a859d035e56aee1422d7c68f Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 11 Oct 2023 16:02:28 +0800 Subject: [PATCH 07/20] fix: fix the coding style. --- tests/system/Router/Controllers/Test.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/system/Router/Controllers/Test.php b/tests/system/Router/Controllers/Test.php index f9e6cc37eed6..934859fa5147 100644 --- a/tests/system/Router/Controllers/Test.php +++ b/tests/system/Router/Controllers/Test.php @@ -13,7 +13,10 @@ use CodeIgniter\Controller; -class Test extends Controller +/** + * @internal + */ +final class Test extends Controller { public function test(): void { From e3e46b588b9173eb870c3d1e86d46b2b33941059 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 11 Oct 2023 16:17:48 +0800 Subject: [PATCH 08/20] fix: fix the test cases error. --- tests/system/Router/Controllers/Product.php | 21 +++++++++++++++++++++ tests/system/Router/RouteCollectionTest.php | 8 ++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/system/Router/Controllers/Product.php diff --git a/tests/system/Router/Controllers/Product.php b/tests/system/Router/Controllers/Product.php new file mode 100644 index 000000000000..4b557dcb25ab --- /dev/null +++ b/tests/system/Router/Controllers/Product.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Router\Controllers; + +use CodeIgniter\Controller; + +class Product extends Controller +{ + public function index(): void + { + } +} diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index d6b7e0dfe6ac..c4852006977c 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -1778,9 +1778,13 @@ public function testAutoRoutesControllerNameReturnsFQCN($namespace): void $routes->setAutoRoute(true); $routes->setDefaultNamespace($namespace); + copy(TESTPATH . 'system/Router/Controllers/Product.php', APPPATH . 'Controllers/Product.php'); + $router = new Router($routes, Services::request()); $router->handle('/product'); + unlink(APPPATH . 'Controllers/Product.php'); + $this->assertSame('\App\\Controllers\\Product', $router->controllerName()); } @@ -1797,9 +1801,13 @@ public function testRoutesControllerNameReturnsFQCN($namespace): void $routes->setDefaultNamespace($namespace); $routes->get('/product', 'Product'); + copy(TESTPATH . 'system/Router/Controllers/Product.php', APPPATH . 'Controllers/Product.php'); + $router = new Router($routes, Services::request()); $router->handle('/product'); + unlink(APPPATH . 'Controllers/Product.php'); + $this->assertSame('\App\\Controllers\\Product', $router->controllerName()); } From 7444b9f9aef6fb7fb85123fd85a2803e5d1e53e6 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 11 Oct 2023 17:42:34 +0800 Subject: [PATCH 09/20] test: refactor the testcase of `testAutoRouteFindsDefaultControllerAndMethod`. --- tests/system/Router/Controllers/Test.php | 24 ------------------------ tests/system/Router/RouterTest.php | 12 ++++++------ 2 files changed, 6 insertions(+), 30 deletions(-) delete mode 100644 tests/system/Router/Controllers/Test.php diff --git a/tests/system/Router/Controllers/Test.php b/tests/system/Router/Controllers/Test.php deleted file mode 100644 index 934859fa5147..000000000000 --- a/tests/system/Router/Controllers/Test.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -namespace CodeIgniter\Router\Controllers; - -use CodeIgniter\Controller; - -/** - * @internal - */ -final class Test extends Controller -{ - public function test(): void - { - } -} diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 02483d3f4fd0..8c890025bef4 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -197,18 +197,18 @@ public function testClosures(): void public function testAutoRouteFindsDefaultControllerAndMethod(): void { $this->collection->setAutoRoute(true); - $this->collection->setDefaultController('Test'); - $this->collection->setDefaultMethod('test'); + $this->collection->setDefaultController('Mycontroller'); + $this->collection->setDefaultMethod('getSomemethod'); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/Test.php', APPPATH . 'Controllers/Test.php'); + copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('/'); - unlink(APPPATH . 'Controllers/Test.php'); + unlink(APPPATH . 'Controllers/Mycontroller.php'); - $this->assertSame('Test', $router->controllerName()); - $this->assertSame('test', $router->methodName()); + $this->assertSame('Mycontroller', $router->controllerName()); + $this->assertSame('getSomemethod', $router->methodName()); } public function testAutoRouteFindsControllerWithFileAndMethod(): void From e5a3d12f95b3c98794b25fd5d13b24465caaf843 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Thu, 12 Oct 2023 01:03:50 +0800 Subject: [PATCH 10/20] test: fix the test cases error. --- .../Controllers/{ => App}/Admin_user.php | 2 +- .../App/Dash_folder/Dash_controller.php | 25 ++++++++++++++++++ .../Controllers/App/Dash_folder/Home.php | 2 +- .../App/Dash_folder/Mycontroller.php | 21 +++++++++++++++ .../Router/Controllers/App/Mycontroller.php | 25 ++++++++++++++++++ .../App/Subfolder/Mycontroller.php | 21 +++++++++++++++ .../{ => App}/foo/bar/baz/Some_controller.php | 0 .../Router/Controllers/App/\316\246.php" | 2 +- .../Router/Controllers/App/\316\246/Home.php" | 21 +++++++++++++++ tests/system/Router/RouterTest.php | 26 +++++++++---------- 10 files changed, 129 insertions(+), 16 deletions(-) rename tests/system/Router/Controllers/{ => App}/Admin_user.php (89%) create mode 100644 tests/system/Router/Controllers/App/Dash_folder/Dash_controller.php rename "tests/system/Router/Controllers/\316\246/Home.php" => tests/system/Router/Controllers/App/Dash_folder/Home.php (89%) create mode 100644 tests/system/Router/Controllers/App/Dash_folder/Mycontroller.php create mode 100644 tests/system/Router/Controllers/App/Mycontroller.php create mode 100644 tests/system/Router/Controllers/App/Subfolder/Mycontroller.php rename tests/system/Router/Controllers/{ => App}/foo/bar/baz/Some_controller.php (100%) rename "tests/system/Router/Controllers/\316\246.php" => "tests/system/Router/Controllers/App/\316\246.php" (89%) create mode 100644 "tests/system/Router/Controllers/App/\316\246/Home.php" diff --git a/tests/system/Router/Controllers/Admin_user.php b/tests/system/Router/Controllers/App/Admin_user.php similarity index 89% rename from tests/system/Router/Controllers/Admin_user.php rename to tests/system/Router/Controllers/App/Admin_user.php index 0614fff87254..62e154410955 100644 --- a/tests/system/Router/Controllers/Admin_user.php +++ b/tests/system/Router/Controllers/App/Admin_user.php @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Router\Controllers; +namespace App\Controllers; use CodeIgniter\Controller; diff --git a/tests/system/Router/Controllers/App/Dash_folder/Dash_controller.php b/tests/system/Router/Controllers/App/Dash_folder/Dash_controller.php new file mode 100644 index 000000000000..c474aff32e6e --- /dev/null +++ b/tests/system/Router/Controllers/App/Dash_folder/Dash_controller.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace App\Controllers\Dash_folder; + +use CodeIgniter\Controller; + +class Dash_controller extends Controller +{ + public function getSomemethod(): void + { + } + + public function getDash_method(): void + { + } +} diff --git "a/tests/system/Router/Controllers/\316\246/Home.php" b/tests/system/Router/Controllers/App/Dash_folder/Home.php similarity index 89% rename from "tests/system/Router/Controllers/\316\246/Home.php" rename to tests/system/Router/Controllers/App/Dash_folder/Home.php index fd0024f6879e..20b28e109721 100644 --- "a/tests/system/Router/Controllers/\316\246/Home.php" +++ b/tests/system/Router/Controllers/App/Dash_folder/Home.php @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Router\Controllers\Φ; +namespace App\Controllers\Dash_folder; use CodeIgniter\Controller; diff --git a/tests/system/Router/Controllers/App/Dash_folder/Mycontroller.php b/tests/system/Router/Controllers/App/Dash_folder/Mycontroller.php new file mode 100644 index 000000000000..2aef903bc891 --- /dev/null +++ b/tests/system/Router/Controllers/App/Dash_folder/Mycontroller.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace App\Controllers\Dash_folder; + +use CodeIgniter\Controller; + +class Mycontroller extends Controller +{ + public function getSomemethod(): void + { + } +} diff --git a/tests/system/Router/Controllers/App/Mycontroller.php b/tests/system/Router/Controllers/App/Mycontroller.php new file mode 100644 index 000000000000..d107198c78d4 --- /dev/null +++ b/tests/system/Router/Controllers/App/Mycontroller.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace App\Controllers; + +use CodeIgniter\Controller; + +class Mycontroller extends Controller +{ + public function getIndex(): void + { + } + + public function getSomemethod($first = ''): void + { + } +} diff --git a/tests/system/Router/Controllers/App/Subfolder/Mycontroller.php b/tests/system/Router/Controllers/App/Subfolder/Mycontroller.php new file mode 100644 index 000000000000..1da9d80843b3 --- /dev/null +++ b/tests/system/Router/Controllers/App/Subfolder/Mycontroller.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace App\Controllers\Subfolder; + +use CodeIgniter\Controller; + +class Mycontroller extends Controller +{ + public function getSomemethod(): void + { + } +} diff --git a/tests/system/Router/Controllers/foo/bar/baz/Some_controller.php b/tests/system/Router/Controllers/App/foo/bar/baz/Some_controller.php similarity index 100% rename from tests/system/Router/Controllers/foo/bar/baz/Some_controller.php rename to tests/system/Router/Controllers/App/foo/bar/baz/Some_controller.php diff --git "a/tests/system/Router/Controllers/\316\246.php" "b/tests/system/Router/Controllers/App/\316\246.php" similarity index 89% rename from "tests/system/Router/Controllers/\316\246.php" rename to "tests/system/Router/Controllers/App/\316\246.php" index 253cd438c58f..a48ba76f77cd 100644 --- "a/tests/system/Router/Controllers/\316\246.php" +++ "b/tests/system/Router/Controllers/App/\316\246.php" @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Router\Controllers; +namespace App\Controllers; use CodeIgniter\Controller; diff --git "a/tests/system/Router/Controllers/App/\316\246/Home.php" "b/tests/system/Router/Controllers/App/\316\246/Home.php" new file mode 100644 index 000000000000..3ee7e7aea271 --- /dev/null +++ "b/tests/system/Router/Controllers/App/\316\246/Home.php" @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace App\Controllers\Φ; + +use CodeIgniter\Controller; + +class Home extends Controller +{ + public function getIndex(): void + { + } +} diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 8c890025bef4..b97d6fd98bda 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -201,7 +201,7 @@ public function testAutoRouteFindsDefaultControllerAndMethod(): void $this->collection->setDefaultMethod('getSomemethod'); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('/'); @@ -216,7 +216,7 @@ public function testAutoRouteFindsControllerWithFileAndMethod(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('mycontroller/getSomemethod'); @@ -231,7 +231,7 @@ public function testAutoRouteFindsControllerWithFile(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('mycontroller'); @@ -248,7 +248,7 @@ public function testAutoRouteFindsControllerWithSubfolder(): void mkdir(APPPATH . 'Controllers/Subfolder'); - copy(TESTPATH . 'system/Router/Controllers/Subfolder/Mycontroller.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Subfolder/Mycontroller.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php'); $router->autoRoute('subfolder/mycontroller/getSomemethod'); @@ -267,7 +267,7 @@ public function testAutoRouteFindsDashedSubfolder(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Mycontroller.php', APPPATH . 'Controllers/Dash_folder/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Dash_folder/Mycontroller.php', APPPATH . 'Controllers/Dash_folder/Mycontroller.php'); $router->autoRoute('dash-folder/mycontroller/somemethod'); @@ -286,7 +286,7 @@ public function testAutoRouteFindsDashedController(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); $router->autoRoute('dash-folder/dash-controller/getSomemethod'); @@ -305,7 +305,7 @@ public function testAutoRouteFindsDashedMethod(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); $router->autoRoute('dash-folder/dash-controller/getDash_method'); @@ -324,7 +324,7 @@ public function testAutoRouteFindsDefaultDashFolder(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Home.php', APPPATH . 'Controllers/Dash_folder/Home.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Dash_folder/Home.php', APPPATH . 'Controllers/Dash_folder/Home.php'); $router->autoRoute('dash-folder'); @@ -343,7 +343,7 @@ public function testAutoRouteFindsMByteDir(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Φ'); - copy(TESTPATH . 'system/Router/Controllers/Φ/Home.php', APPPATH . 'Controllers/Φ/Home.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Φ/Home.php', APPPATH . 'Controllers/Φ/Home.php'); $router->autoRoute('Φ'); @@ -361,7 +361,7 @@ public function testAutoRouteFindsMByteController(): void $router = new Router($this->collection, $this->request); $router->setTranslateURIDashes(true); - copy(TESTPATH . 'system/Router/Controllers/Φ.php', APPPATH . 'Controllers/Φ.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Φ.php', APPPATH . 'Controllers/Φ.php'); $router->autoRoute('Φ'); @@ -754,7 +754,7 @@ public function testTranslateURIDashesForAutoRoute(): void $router = new Router($this->collection, $this->request); $router->setTranslateURIDashes(true); - copy(TESTPATH . 'system/Router/Controllers/Admin_user.php', APPPATH . 'Controllers/Admin_user.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Admin_user.php', APPPATH . 'Controllers/Admin_user.php'); $router->autoRoute('admin_user/show_list'); @@ -772,7 +772,7 @@ public function testAutoRouteMatchesZeroParams(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('mycontroller/getSomemethod/0/abc'); @@ -850,7 +850,7 @@ public function testRouterPriorDirectory(): void mkdir(APPPATH . 'Controllers/foo'); mkdir(APPPATH . 'Controllers/foo/bar'); mkdir(APPPATH . 'Controllers/foo/bar/baz'); - copy(TESTPATH . 'system/Router/Controllers/foo/bar/baz/Some_controller.php', APPPATH . 'Controllers/foo/bar/baz/Some_controller.php'); + copy(TESTPATH . 'system/Router/Controllers/App/foo/bar/baz/Some_controller.php', APPPATH . 'Controllers/foo/bar/baz/Some_controller.php'); $router->setDirectory('foo/bar/baz', false, true); $router->handle('Some_controller/some_method/param1/param2/param3'); From d68a9b0e0b72ab5f5f7880e4863f884f010d37aa Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Thu, 12 Oct 2023 10:13:39 +0800 Subject: [PATCH 11/20] fix: change the namespace for the mock controllers. --- tests/system/Router/Controllers/{ => App}/Product.php | 2 +- .../Router/Controllers/App/foo/bar/baz/Some_controller.php | 2 +- tests/system/Router/RouteCollectionTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename tests/system/Router/Controllers/{ => App}/Product.php (89%) diff --git a/tests/system/Router/Controllers/Product.php b/tests/system/Router/Controllers/App/Product.php similarity index 89% rename from tests/system/Router/Controllers/Product.php rename to tests/system/Router/Controllers/App/Product.php index 4b557dcb25ab..88951b8d90c1 100644 --- a/tests/system/Router/Controllers/Product.php +++ b/tests/system/Router/Controllers/App/Product.php @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Router\Controllers; +namespace App\Controllers; use CodeIgniter\Controller; diff --git a/tests/system/Router/Controllers/App/foo/bar/baz/Some_controller.php b/tests/system/Router/Controllers/App/foo/bar/baz/Some_controller.php index 0ec6f4a3be2b..25fe509c56cc 100644 --- a/tests/system/Router/Controllers/App/foo/bar/baz/Some_controller.php +++ b/tests/system/Router/Controllers/App/foo/bar/baz/Some_controller.php @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Router\Controllers\foo\bar\baz; +namespace App\Controllers\foo\bar\baz; use CodeIgniter\Controller; diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index c4852006977c..e7cf778a4b6a 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -1778,7 +1778,7 @@ public function testAutoRoutesControllerNameReturnsFQCN($namespace): void $routes->setAutoRoute(true); $routes->setDefaultNamespace($namespace); - copy(TESTPATH . 'system/Router/Controllers/Product.php', APPPATH . 'Controllers/Product.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Product.php', APPPATH . 'Controllers/Product.php'); $router = new Router($routes, Services::request()); $router->handle('/product'); @@ -1801,7 +1801,7 @@ public function testRoutesControllerNameReturnsFQCN($namespace): void $routes->setDefaultNamespace($namespace); $routes->get('/product', 'Product'); - copy(TESTPATH . 'system/Router/Controllers/Product.php', APPPATH . 'Controllers/Product.php'); + copy(TESTPATH . 'system/Router/Controllers/App/Product.php', APPPATH . 'Controllers/Product.php'); $router = new Router($routes, Services::request()); $router->handle('/product'); From 0154206640461a9f4ff65d3da363106ea7f76dc6 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Thu, 12 Oct 2023 12:28:22 +0800 Subject: [PATCH 12/20] test: change the mock controller place. --- .../_controller}/Admin_user.php | 0 .../Dash_folder/Dash_controller.php | 0 .../_controller}/Dash_folder/Home.php | 0 .../_controller}/Dash_folder/Mycontroller.php | 0 .../_controller}/Mycontroller.php | 0 .../App => _support/_controller}/Product.php | 0 .../_controller}/Subfolder/Mycontroller.php | 0 .../foo/bar/baz/Some_controller.php | 0 .../_support/_controller/\316\246.php" | 0 .../_support/_controller/\316\246/Home.php" | 0 tests/system/Router/RouteCollectionTest.php | 4 +-- tests/system/Router/RouterTest.php | 26 +++++++++---------- 12 files changed, 15 insertions(+), 15 deletions(-) rename tests/{system/Router/Controllers/App => _support/_controller}/Admin_user.php (100%) rename tests/{system/Router/Controllers/App => _support/_controller}/Dash_folder/Dash_controller.php (100%) rename tests/{system/Router/Controllers/App => _support/_controller}/Dash_folder/Home.php (100%) rename tests/{system/Router/Controllers/App => _support/_controller}/Dash_folder/Mycontroller.php (100%) rename tests/{system/Router/Controllers/App => _support/_controller}/Mycontroller.php (100%) rename tests/{system/Router/Controllers/App => _support/_controller}/Product.php (100%) rename tests/{system/Router/Controllers/App => _support/_controller}/Subfolder/Mycontroller.php (100%) rename tests/{system/Router/Controllers/App => _support/_controller}/foo/bar/baz/Some_controller.php (100%) rename "tests/system/Router/Controllers/App/\316\246.php" => "tests/_support/_controller/\316\246.php" (100%) rename "tests/system/Router/Controllers/App/\316\246/Home.php" => "tests/_support/_controller/\316\246/Home.php" (100%) diff --git a/tests/system/Router/Controllers/App/Admin_user.php b/tests/_support/_controller/Admin_user.php similarity index 100% rename from tests/system/Router/Controllers/App/Admin_user.php rename to tests/_support/_controller/Admin_user.php diff --git a/tests/system/Router/Controllers/App/Dash_folder/Dash_controller.php b/tests/_support/_controller/Dash_folder/Dash_controller.php similarity index 100% rename from tests/system/Router/Controllers/App/Dash_folder/Dash_controller.php rename to tests/_support/_controller/Dash_folder/Dash_controller.php diff --git a/tests/system/Router/Controllers/App/Dash_folder/Home.php b/tests/_support/_controller/Dash_folder/Home.php similarity index 100% rename from tests/system/Router/Controllers/App/Dash_folder/Home.php rename to tests/_support/_controller/Dash_folder/Home.php diff --git a/tests/system/Router/Controllers/App/Dash_folder/Mycontroller.php b/tests/_support/_controller/Dash_folder/Mycontroller.php similarity index 100% rename from tests/system/Router/Controllers/App/Dash_folder/Mycontroller.php rename to tests/_support/_controller/Dash_folder/Mycontroller.php diff --git a/tests/system/Router/Controllers/App/Mycontroller.php b/tests/_support/_controller/Mycontroller.php similarity index 100% rename from tests/system/Router/Controllers/App/Mycontroller.php rename to tests/_support/_controller/Mycontroller.php diff --git a/tests/system/Router/Controllers/App/Product.php b/tests/_support/_controller/Product.php similarity index 100% rename from tests/system/Router/Controllers/App/Product.php rename to tests/_support/_controller/Product.php diff --git a/tests/system/Router/Controllers/App/Subfolder/Mycontroller.php b/tests/_support/_controller/Subfolder/Mycontroller.php similarity index 100% rename from tests/system/Router/Controllers/App/Subfolder/Mycontroller.php rename to tests/_support/_controller/Subfolder/Mycontroller.php diff --git a/tests/system/Router/Controllers/App/foo/bar/baz/Some_controller.php b/tests/_support/_controller/foo/bar/baz/Some_controller.php similarity index 100% rename from tests/system/Router/Controllers/App/foo/bar/baz/Some_controller.php rename to tests/_support/_controller/foo/bar/baz/Some_controller.php diff --git "a/tests/system/Router/Controllers/App/\316\246.php" "b/tests/_support/_controller/\316\246.php" similarity index 100% rename from "tests/system/Router/Controllers/App/\316\246.php" rename to "tests/_support/_controller/\316\246.php" diff --git "a/tests/system/Router/Controllers/App/\316\246/Home.php" "b/tests/_support/_controller/\316\246/Home.php" similarity index 100% rename from "tests/system/Router/Controllers/App/\316\246/Home.php" rename to "tests/_support/_controller/\316\246/Home.php" diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index e7cf778a4b6a..8c3a897eac46 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -1778,7 +1778,7 @@ public function testAutoRoutesControllerNameReturnsFQCN($namespace): void $routes->setAutoRoute(true); $routes->setDefaultNamespace($namespace); - copy(TESTPATH . 'system/Router/Controllers/App/Product.php', APPPATH . 'Controllers/Product.php'); + copy(TESTPATH . '_support/_controller/Product.php', APPPATH . 'Controllers/Product.php'); $router = new Router($routes, Services::request()); $router->handle('/product'); @@ -1801,7 +1801,7 @@ public function testRoutesControllerNameReturnsFQCN($namespace): void $routes->setDefaultNamespace($namespace); $routes->get('/product', 'Product'); - copy(TESTPATH . 'system/Router/Controllers/App/Product.php', APPPATH . 'Controllers/Product.php'); + copy(TESTPATH . '_support/_controller/Product.php', APPPATH . 'Controllers/Product.php'); $router = new Router($routes, Services::request()); $router->handle('/product'); diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index b97d6fd98bda..60dd669172e5 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -201,7 +201,7 @@ public function testAutoRouteFindsDefaultControllerAndMethod(): void $this->collection->setDefaultMethod('getSomemethod'); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/App/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . '_support/_controller/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('/'); @@ -216,7 +216,7 @@ public function testAutoRouteFindsControllerWithFileAndMethod(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/App/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . '_support/_controller/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('mycontroller/getSomemethod'); @@ -231,7 +231,7 @@ public function testAutoRouteFindsControllerWithFile(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/App/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . '_support/_controller/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('mycontroller'); @@ -248,7 +248,7 @@ public function testAutoRouteFindsControllerWithSubfolder(): void mkdir(APPPATH . 'Controllers/Subfolder'); - copy(TESTPATH . 'system/Router/Controllers/App/Subfolder/Mycontroller.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php'); + copy(TESTPATH . '_support/_controller/Subfolder/Mycontroller.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php'); $router->autoRoute('subfolder/mycontroller/getSomemethod'); @@ -267,7 +267,7 @@ public function testAutoRouteFindsDashedSubfolder(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - copy(TESTPATH . 'system/Router/Controllers/App/Dash_folder/Mycontroller.php', APPPATH . 'Controllers/Dash_folder/Mycontroller.php'); + copy(TESTPATH . '_support/_controller/Dash_folder/Mycontroller.php', APPPATH . 'Controllers/Dash_folder/Mycontroller.php'); $router->autoRoute('dash-folder/mycontroller/somemethod'); @@ -286,7 +286,7 @@ public function testAutoRouteFindsDashedController(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - copy(TESTPATH . 'system/Router/Controllers/App/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); + copy(TESTPATH . '_support/_controller/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); $router->autoRoute('dash-folder/dash-controller/getSomemethod'); @@ -305,7 +305,7 @@ public function testAutoRouteFindsDashedMethod(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - copy(TESTPATH . 'system/Router/Controllers/App/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); + copy(TESTPATH . '_support/_controller/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); $router->autoRoute('dash-folder/dash-controller/getDash_method'); @@ -324,7 +324,7 @@ public function testAutoRouteFindsDefaultDashFolder(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Dash_folder'); - copy(TESTPATH . 'system/Router/Controllers/App/Dash_folder/Home.php', APPPATH . 'Controllers/Dash_folder/Home.php'); + copy(TESTPATH . '_support/_controller/Dash_folder/Home.php', APPPATH . 'Controllers/Dash_folder/Home.php'); $router->autoRoute('dash-folder'); @@ -343,7 +343,7 @@ public function testAutoRouteFindsMByteDir(): void $router->setTranslateURIDashes(true); mkdir(APPPATH . 'Controllers/Φ'); - copy(TESTPATH . 'system/Router/Controllers/App/Φ/Home.php', APPPATH . 'Controllers/Φ/Home.php'); + copy(TESTPATH . '_support/_controller/Φ/Home.php', APPPATH . 'Controllers/Φ/Home.php'); $router->autoRoute('Φ'); @@ -361,7 +361,7 @@ public function testAutoRouteFindsMByteController(): void $router = new Router($this->collection, $this->request); $router->setTranslateURIDashes(true); - copy(TESTPATH . 'system/Router/Controllers/App/Φ.php', APPPATH . 'Controllers/Φ.php'); + copy(TESTPATH . '_support/_controller/Φ.php', APPPATH . 'Controllers/Φ.php'); $router->autoRoute('Φ'); @@ -754,7 +754,7 @@ public function testTranslateURIDashesForAutoRoute(): void $router = new Router($this->collection, $this->request); $router->setTranslateURIDashes(true); - copy(TESTPATH . 'system/Router/Controllers/App/Admin_user.php', APPPATH . 'Controllers/Admin_user.php'); + copy(TESTPATH . '_support/_controller/Admin_user.php', APPPATH . 'Controllers/Admin_user.php'); $router->autoRoute('admin_user/show_list'); @@ -772,7 +772,7 @@ public function testAutoRouteMatchesZeroParams(): void $this->collection->setAutoRoute(true); $router = new Router($this->collection, $this->request); - copy(TESTPATH . 'system/Router/Controllers/App/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); + copy(TESTPATH . '_support/_controller/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php'); $router->autoRoute('mycontroller/getSomemethod/0/abc'); @@ -850,7 +850,7 @@ public function testRouterPriorDirectory(): void mkdir(APPPATH . 'Controllers/foo'); mkdir(APPPATH . 'Controllers/foo/bar'); mkdir(APPPATH . 'Controllers/foo/bar/baz'); - copy(TESTPATH . 'system/Router/Controllers/App/foo/bar/baz/Some_controller.php', APPPATH . 'Controllers/foo/bar/baz/Some_controller.php'); + copy(TESTPATH . '_support/_controller/foo/bar/baz/Some_controller.php', APPPATH . 'Controllers/foo/bar/baz/Some_controller.php'); $router->setDirectory('foo/bar/baz', false, true); $router->handle('Some_controller/some_method/param1/param2/param3'); From 4a2e99c24a5241b16dac843cf6bc8158af048e37 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Sat, 14 Oct 2023 16:47:56 +0800 Subject: [PATCH 13/20] style: fix the coding style. --- tests/system/Router/RouteCollectionTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 8c3a897eac46..985326dcf66d 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Router; +use App\Controllers\Product; use CodeIgniter\Config\Services; use CodeIgniter\controller; use CodeIgniter\Exceptions\PageNotFoundException; @@ -1785,7 +1786,7 @@ public function testAutoRoutesControllerNameReturnsFQCN($namespace): void unlink(APPPATH . 'Controllers/Product.php'); - $this->assertSame('\App\\Controllers\\Product', $router->controllerName()); + $this->assertSame('\\' . Product::class, $router->controllerName()); } /** @@ -1808,7 +1809,7 @@ public function testRoutesControllerNameReturnsFQCN($namespace): void unlink(APPPATH . 'Controllers/Product.php'); - $this->assertSame('\App\\Controllers\\Product', $router->controllerName()); + $this->assertSame('\\' . Product::class, $router->controllerName()); } public function testRoutePriorityDetected(): void From fde42ba117c8357e47b490b96d5b209253687173 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Tue, 17 Oct 2023 12:57:51 +0800 Subject: [PATCH 14/20] docs: add the notice for the user guide. --- user_guide_src/source/incoming/controllers.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/user_guide_src/source/incoming/controllers.rst b/user_guide_src/source/incoming/controllers.rst index 8bc9e90d581f..15b3f8821616 100644 --- a/user_guide_src/source/incoming/controllers.rst +++ b/user_guide_src/source/incoming/controllers.rst @@ -412,6 +412,8 @@ without route definitions. The auto-routing is disabled by default. .. important:: Auto Routing (Legacy) routes a HTTP request with **any** HTTP method to a controller method. +.. important:: If Auto Routing (Legacy) doesn't find the controller, it will throw page not found exception before the filter executes. + Consider this URI:: example.com/index.php/helloworld/ From ca438973c948d2506847fbf9dd97ec241c8edc39 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Tue, 17 Oct 2023 13:03:20 +0800 Subject: [PATCH 15/20] docs: add the version. --- user_guide_src/source/incoming/controllers.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/incoming/controllers.rst b/user_guide_src/source/incoming/controllers.rst index 15b3f8821616..058267ccbb18 100644 --- a/user_guide_src/source/incoming/controllers.rst +++ b/user_guide_src/source/incoming/controllers.rst @@ -412,7 +412,7 @@ without route definitions. The auto-routing is disabled by default. .. important:: Auto Routing (Legacy) routes a HTTP request with **any** HTTP method to a controller method. -.. important:: If Auto Routing (Legacy) doesn't find the controller, it will throw page not found exception before the filter executes. +.. important:: If Auto Routing (Legacy) doesn't find the controller, it will throw page not found exception before the filter executes in version **4.5**. Consider this URI:: From ac4b77bae3421e2c1e005c9e4200ba9a27aaa73b Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 18 Oct 2023 21:17:30 +0800 Subject: [PATCH 16/20] docs: add the upgrade file. --- user_guide_src/source/changelogs/v4.4.2.rst | 3 +++ user_guide_src/source/changelogs/v4.5.0.rst | 2 ++ user_guide_src/source/installation/upgrade_450.rst | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.4.2.rst b/user_guide_src/source/changelogs/v4.4.2.rst index eb146ef07c0c..03297a695ee5 100644 --- a/user_guide_src/source/changelogs/v4.4.2.rst +++ b/user_guide_src/source/changelogs/v4.4.2.rst @@ -9,6 +9,9 @@ Release Date: October 19, 2023 :local: :depth: 3 +BREAKING +******** + Message Changes *************** diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index 2b6fc7299231..9181ba9a831f 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -18,6 +18,8 @@ Highlights BREAKING ******** +- **AutoRouting Legacy:** Add a condition to check if the controller exists. + Behavior Changes ================ diff --git a/user_guide_src/source/installation/upgrade_450.rst b/user_guide_src/source/installation/upgrade_450.rst index 56b1f37c011e..c40a93e27609 100644 --- a/user_guide_src/source/installation/upgrade_450.rst +++ b/user_guide_src/source/installation/upgrade_450.rst @@ -132,6 +132,11 @@ Removed Deprecated Items Some deprecated items have been removed. If you extend these classes and are using them, upgrade your code. See :ref:`ChangeLog ` for details. +Auto Routing (Legacy) +======================= + +If the auto routing cannot found the controller, it will throw page not found exception before the filter executed. + Breaking Enhancements ********************* From b9dc457ae14520f8a2326243c5ae389cd8504db8 Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Wed, 18 Oct 2023 21:20:29 +0800 Subject: [PATCH 17/20] style: change the coding style. --- system/Router/AutoRouter.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/system/Router/AutoRouter.php b/system/Router/AutoRouter.php index 9ea45ea86e13..c35e375cfa2f 100644 --- a/system/Router/AutoRouter.php +++ b/system/Router/AutoRouter.php @@ -157,12 +157,13 @@ public function getRoute(string $uri, string $httpVerb): array // Load the file so that it's available for CodeIgniter. $file = APPPATH . 'Controllers/' . $this->directory . $controllerName . '.php'; - if (is_file($file)) { - include_once $file; - } else { + + if (! is_file($file)) { throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); } + include_once $file; + // Ensure the controller stores the fully-qualified class name // We have to check for a length over 1, since by default it will be '\' if (strpos($this->controller, '\\') === false && strlen($this->defaultNamespace) > 1) { From 25e3900d304d43e37f7dd259ed9e8f40affdd1ca Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Tue, 24 Oct 2023 13:58:46 +0800 Subject: [PATCH 18/20] docs: modify the upgrade docs. --- user_guide_src/source/changelogs/v4.5.0.rst | 4 ++-- .../source/incoming/controllers.rst | 4 +++- .../source/installation/upgrade_450.rst | 19 ++++++++++++++----- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index 9181ba9a831f..5100ded28a89 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -18,8 +18,6 @@ Highlights BREAKING ******** -- **AutoRouting Legacy:** Add a condition to check if the controller exists. - Behavior Changes ================ @@ -45,6 +43,8 @@ class, and even if it were extended, there is no way to replace it. Others ------ +- **AutoRouting Legacy:** Changed so that a ``PageNotFoundException`` is thrown + if the controller corresponding to the request URI does not exist. - **Logger:** The :php:func:`log_message()` function and the logger methods in ``CodeIgniter\Log\Logger`` now do not return ``bool`` values. The return types have been fixed to ``void`` to follow the PSR-3 interface. diff --git a/user_guide_src/source/incoming/controllers.rst b/user_guide_src/source/incoming/controllers.rst index 058267ccbb18..f53609023c52 100644 --- a/user_guide_src/source/incoming/controllers.rst +++ b/user_guide_src/source/incoming/controllers.rst @@ -412,7 +412,9 @@ without route definitions. The auto-routing is disabled by default. .. important:: Auto Routing (Legacy) routes a HTTP request with **any** HTTP method to a controller method. -.. important:: If Auto Routing (Legacy) doesn't find the controller, it will throw page not found exception before the filter executes in version **4.5**. +.. important:: Since v4.5.0, if Auto Routing (Legacy) doesn't find the controller, + it will throw ``PageNotFoundException`` exception before the Controller Filters + execute. Consider this URI:: diff --git a/user_guide_src/source/installation/upgrade_450.rst b/user_guide_src/source/installation/upgrade_450.rst index c40a93e27609..efba63f8afe0 100644 --- a/user_guide_src/source/installation/upgrade_450.rst +++ b/user_guide_src/source/installation/upgrade_450.rst @@ -119,12 +119,26 @@ has been removed. If you extneds ``BaseModel``, implement the ``getIdValue()`` method in the child class. +<<<<<<< HEAD Factories ========= :doc:`../concepts/factories` has been changed to a final class. In the unlikely event, you have inherited the Factories, stop inheriting and copy the code into your Factories class. +======= +Auto Routing (Legacy) +===================== + +In previous versions, the controller filters might be executed even when the +corresponding controller was not found. + +This bug has been fixed and now a ``PageNotFoundException`` will be thrown and +the filters will not be executed if the controller is not found. + +If you have code that depends on this bug, for example if you expect global filters +to be executed even for non-existent pages, please add the necessary routes. +>>>>>>> 60f7e69cf4 (docs: modify the upgrade docs.) Removed Deprecated Items ======================== @@ -132,11 +146,6 @@ Removed Deprecated Items Some deprecated items have been removed. If you extend these classes and are using them, upgrade your code. See :ref:`ChangeLog ` for details. -Auto Routing (Legacy) -======================= - -If the auto routing cannot found the controller, it will throw page not found exception before the filter executed. - Breaking Enhancements ********************* From 3ecdc1b9b377d7270fc7c7fc3ab6b01779f91bbc Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Tue, 24 Oct 2023 14:14:58 +0800 Subject: [PATCH 19/20] docs: resovle the conflict --- user_guide_src/source/installation/upgrade_450.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/user_guide_src/source/installation/upgrade_450.rst b/user_guide_src/source/installation/upgrade_450.rst index efba63f8afe0..04607dde6489 100644 --- a/user_guide_src/source/installation/upgrade_450.rst +++ b/user_guide_src/source/installation/upgrade_450.rst @@ -119,14 +119,13 @@ has been removed. If you extneds ``BaseModel``, implement the ``getIdValue()`` method in the child class. -<<<<<<< HEAD Factories ========= :doc:`../concepts/factories` has been changed to a final class. In the unlikely event, you have inherited the Factories, stop inheriting and copy the code into your Factories class. -======= + Auto Routing (Legacy) ===================== @@ -138,7 +137,6 @@ the filters will not be executed if the controller is not found. If you have code that depends on this bug, for example if you expect global filters to be executed even for non-existent pages, please add the necessary routes. ->>>>>>> 60f7e69cf4 (docs: modify the upgrade docs.) Removed Deprecated Items ======================== From 7d2ae12b58553f5d7a02bedf92246edde9b94d7b Mon Sep 17 00:00:00 2001 From: ping-yee <611077101@mail.nknu.edu.tw> Date: Tue, 24 Oct 2023 14:21:24 +0800 Subject: [PATCH 20/20] docs: remove the extra docs. --- user_guide_src/source/changelogs/v4.4.2.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.4.2.rst b/user_guide_src/source/changelogs/v4.4.2.rst index 03297a695ee5..eb146ef07c0c 100644 --- a/user_guide_src/source/changelogs/v4.4.2.rst +++ b/user_guide_src/source/changelogs/v4.4.2.rst @@ -9,9 +9,6 @@ Release Date: October 19, 2023 :local: :depth: 3 -BREAKING -******** - Message Changes ***************