From 2dce190939f869a8ac471ca1d25bd8707faf6363 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Apr 2020 17:11:32 +0700 Subject: [PATCH] test Controller::validate() with string rules --- system/Controller.php | 2 +- tests/system/ControllerTest.php | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/system/Controller.php b/system/Controller.php index 72ed399c826d..1bb35ae322dd 100644 --- a/system/Controller.php +++ b/system/Controller.php @@ -194,7 +194,7 @@ protected function validate($rules, array $messages = []): bool // If you replace the $rules array with the name of the group if (is_string($rules)) { - $validation = new \Config\Validation(); + $validation = config('Validation'); // If the rule wasn't found in the \Config\Validation, we // should throw an exception so the developer can find it. diff --git a/tests/system/ControllerTest.php b/tests/system/ControllerTest.php index 590697cd2e64..d12c9cb09497 100644 --- a/tests/system/ControllerTest.php +++ b/tests/system/ControllerTest.php @@ -4,6 +4,7 @@ use Config\App; use CodeIgniter\HTTP\UserAgent; use CodeIgniter\Test\Mock\MockCodeIgniter; +use CodeIgniter\Validation\Exceptions\ValidationException; /** * Exercise our core Controller class. @@ -102,6 +103,59 @@ public function testValidate() $this->assertFalse($method([])); } + public function testValidateWithStringRulesNotFound() + { + $this->expectException(ValidationException::class); + + // make sure we can instantiate one + $this->controller = new Controller(); + $this->controller->initController($this->request, $this->response, $this->logger); + + $method = $this->getPrivateMethodInvoker($this->controller, 'validate'); + $this->assertFalse($method('signup')); + } + + public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig() + { + $validation = config('Validation'); + $validation->signup = [ + 'username' => 'required', + ]; + $validation->signup_errors = [ + 'username' => [ + 'required' => 'You must choose a username.', + ], + ]; + + // make sure we can instantiate one + $this->controller = new Controller(); + $this->controller->initController($this->request, $this->response, $this->logger); + + $method = $this->getPrivateMethodInvoker($this->controller, 'validate'); + $this->assertFalse($method('signup')); + $this->assertEquals('You must choose a username.', Services::validation()->getError()); + } + + public function testValidateWithStringRulesFoundUseMessagesParameter() + { + $validation = config('Validation'); + $validation->signup = [ + 'username' => 'required', + ]; + + // make sure we can instantiate one + $this->controller = new Controller(); + $this->controller->initController($this->request, $this->response, $this->logger); + + $method = $this->getPrivateMethodInvoker($this->controller, 'validate'); + $this->assertFalse($method('signup', [ + 'username' => [ + 'required' => 'You must choose a username.', + ], + ])); + $this->assertEquals('You must choose a username.', Services::validation()->getError()); + } + //-------------------------------------------------------------------- public function testHelpers() {