From 3143cf342a4a130e29d6f9d6043c7c2cc97b7608 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 3 Aug 2024 08:33:43 +0900 Subject: [PATCH 1/5] test: add property types --- tests/system/ControllerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/system/ControllerTest.php b/tests/system/ControllerTest.php index 7cbe664aabc0..b028729cd387 100644 --- a/tests/system/ControllerTest.php +++ b/tests/system/ControllerTest.php @@ -126,10 +126,10 @@ public function testValidateWithStringRulesNotFound(): void public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig(): void { $validation = new class () extends ValidationConfig { - public $signup = [ + public array $signup = [ 'username' => 'required', ]; - public $signup_errors = [ + public array $signup_errors = [ 'username' => [ 'required' => 'You must choose a username.', ], @@ -149,7 +149,7 @@ public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig public function testValidateWithStringRulesFoundUseMessagesParameter(): void { $validation = new class () extends ValidationConfig { - public $signup = [ + public array $signup = [ 'username' => 'required', ]; }; From c408acf473e83e492a89f212ab3245a7112a2b51 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 3 Aug 2024 09:08:49 +0900 Subject: [PATCH 2/5] test: add tests for validateData() with custom error messages --- tests/system/ControllerTest.php | 71 +++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/system/ControllerTest.php b/tests/system/ControllerTest.php index b028729cd387..c28de171d85b 100644 --- a/tests/system/ControllerTest.php +++ b/tests/system/ControllerTest.php @@ -191,6 +191,77 @@ public function testValidateData(): void ); } + public function testValidateDataWithCustomErrorMessage(): void + { + // make sure we can instantiate one + $this->controller = new Controller(); + $this->controller->initController($this->request, $this->response, $this->logger); + + $method = $this->getPrivateMethodInvoker($this->controller, 'validateData'); + + $data = [ + 'username' => 'a', + 'password' => '123', + ]; + $rules = [ + 'username' => 'required|min_length[3]', + 'password' => 'required|min_length[10]', + ]; + $errors = [ + 'username' => [ + 'required' => 'Please fill "{field}".', + 'min_length' => '"{field}" must be {param} letters or longer.', + ], + ]; + $this->assertFalse($method($data, $rules, $errors)); + $this->assertSame( + '"username" must be 3 letters or longer.', + Services::validation()->getError('username') + ); + $this->assertSame( + 'The password field must be at least 10 characters in length.', + Services::validation()->getError('password') + ); + } + + public function testValidateDataWithCustomErrorMessageLabeledStyle(): void + { + // make sure we can instantiate one + $this->controller = new Controller(); + $this->controller->initController($this->request, $this->response, $this->logger); + + $method = $this->getPrivateMethodInvoker($this->controller, 'validateData'); + + $data = [ + 'username' => 'a', + 'password' => '123', + ]; + $rules = [ + 'username' => [ + 'label' => 'Username', + 'rules' => 'required|min_length[3]', + 'errors' => [ + 'required' => 'Please fill "{field}".', + 'min_length' => '"{field}" must be {param} letters or longer.', + ], + ], + 'password' => [ + 'required|min_length[10]', + 'label' => 'Password', + 'rules' => 'required|min_length[10]', + ], + ]; + $this->assertFalse($method($data, $rules)); + $this->assertSame( + '"Username" must be 3 letters or longer.', + Services::validation()->getError('username') + ); + $this->assertSame( + 'The Password field must be at least 10 characters in length.', + Services::validation()->getError('password') + ); + } + public function testHelpers(): void { $this->controller = new class () extends Controller { From ab82f418a7ec3ae766cbe28eb10b5c30bf390420 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 3 Aug 2024 10:04:26 +0900 Subject: [PATCH 3/5] test: add tests for differs and matches --- tests/system/Validation/RulesTest.php | 88 +++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index 7c69fa24838e..c39304461d61 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -336,6 +336,50 @@ public function testMatchesNested(array $data, bool $expected): void $this->assertSame($expected, $this->validation->run($data)); } + public function testMatchesWithDotArrayPass(): void + { + $rules = [ + 'name' => 'permit_empty', + 'emailAddress' => 'permit_empty|valid_email', + 'alias.*' => 'permit_empty|matches[name]', + ]; + $this->validation->setRules($rules); + + $data = [ + 'name' => 'Princess Peach', + 'emailAddress' => 'valid@example.com', + 'alias' => [ + 'Princess Peach', + 'Princess Peach', + ], + ]; + $this->assertTrue($this->validation->run($data)); + } + + public function testMatchesWithDotArrayFail(): void + { + $rules = [ + 'name' => 'permit_empty', + 'emailAddress' => 'permit_empty|valid_email', + 'alias.*' => 'permit_empty|matches[name]', + ]; + $this->validation->setRules($rules); + + $data = [ + 'name' => 'Princess Peach', + 'emailAddress' => 'valid@example.com', + 'alias' => [ + 'Princess ', + 'Princess Peach', + ], + ]; + $this->assertFalse($this->validation->run($data)); + $this->assertSame( + ['alias.0' => 'The alias.* field does not match the name field.'], + $this->validation->getErrors() + ); + } + public static function provideMatchesNestedCases(): iterable { yield from [ @@ -373,6 +417,50 @@ public function testDiffersNested(array $data, bool $expected): void $this->assertSame(! $expected, $this->validation->run($data)); } + public function testDiffersWithDotArrayPass(): void + { + $rules = [ + 'name' => 'permit_empty', + 'emailAddress' => 'permit_empty|valid_email', + 'alias.*' => 'permit_empty|differs[name]', + ]; + $this->validation->setRules($rules); + + $data = [ + 'name' => 'Princess Peach', + 'emailAddress' => 'valid@example.com', + 'alias' => [ + 'Princess Toadstool', + 'Peach', + ], + ]; + $this->assertTrue($this->validation->run($data)); + } + + public function testDiffersWithDotArrayFail(): void + { + $rules = [ + 'name' => 'permit_empty', + 'emailAddress' => 'permit_empty|valid_email', + 'alias.*' => 'permit_empty|differs[name]', + ]; + $this->validation->setRules($rules); + + $data = [ + 'name' => 'Princess Peach', + 'emailAddress' => 'valid@example.com', + 'alias' => [ + 'Princess Toadstool', + 'Princess Peach', + ], + ]; + $this->assertFalse($this->validation->run($data)); + $this->assertSame( + ['alias.1' => 'The alias.* field must differ from the name field.'], + $this->validation->getErrors() + ); + } + #[DataProvider('provideEquals')] public function testEquals(array $data, string $param, bool $expected): void { From 156e45bab1ed27523d995fc0a7a106e2087a0b26 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 3 Aug 2024 10:04:59 +0900 Subject: [PATCH 4/5] fix: rule `differs` and `matches` with dot array --- system/Validation/StrictRules/Rules.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/system/Validation/StrictRules/Rules.php b/system/Validation/StrictRules/Rules.php index e4fc4fc91142..ec02a4e0a0ac 100644 --- a/system/Validation/StrictRules/Rules.php +++ b/system/Validation/StrictRules/Rules.php @@ -48,11 +48,15 @@ public function differs( return $str !== dot_array_search($otherField, $data); } - if (! array_key_exists($field, $data)) { + if (! array_key_exists($otherField, $data)) { return false; } - if (! array_key_exists($otherField, $data)) { + if (str_contains($field, '.')) { + if (! ArrayHelper::dotKeyExists($field, $data)) { + return false; + } + } elseif (! array_key_exists($field, $data)) { return false; } @@ -281,11 +285,15 @@ public function matches( return $str === dot_array_search($otherField, $data); } - if (! array_key_exists($field, $data)) { + if (! array_key_exists($otherField, $data)) { return false; } - if (! array_key_exists($otherField, $data)) { + if (str_contains($field, '.')) { + if (! ArrayHelper::dotKeyExists($field, $data)) { + return false; + } + } elseif (! array_key_exists($field, $data)) { return false; } From 72c92e0dcb6ae3c691bd8e1b115e8fd5ea598622 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 3 Aug 2024 10:13:27 +0900 Subject: [PATCH 5/5] test: add @var for PHPStan --- phpstan-baseline.php | 18 ------------------ tests/system/ControllerTest.php | 10 ++++++++++ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index c49b156548eb..2fd4d79af642 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -12955,24 +12955,6 @@ 'count' => 1, 'path' => __DIR__ . '/tests/system/ControllerTest.php', ]; -$ignoreErrors[] = [ - // identifier: missingType.property - 'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:128\\:\\:\\$signup has no type specified\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/tests/system/ControllerTest.php', -]; -$ignoreErrors[] = [ - // identifier: missingType.property - 'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:128\\:\\:\\$signup_errors has no type specified\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/tests/system/ControllerTest.php', -]; -$ignoreErrors[] = [ - // identifier: missingType.property - 'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:151\\:\\:\\$signup has no type specified\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/tests/system/ControllerTest.php', -]; $ignoreErrors[] = [ // identifier: argument.type 'message' => '#^Parameter \\#1 \\$cookies of class CodeIgniter\\\\Cookie\\\\CookieStore constructor expects array\\, array\\ given\\.$#', diff --git a/tests/system/ControllerTest.php b/tests/system/ControllerTest.php index c28de171d85b..add07fc20dd4 100644 --- a/tests/system/ControllerTest.php +++ b/tests/system/ControllerTest.php @@ -126,9 +126,16 @@ public function testValidateWithStringRulesNotFound(): void public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig(): void { $validation = new class () extends ValidationConfig { + /** + * @var array + */ public array $signup = [ 'username' => 'required', ]; + + /** + * @var array> + */ public array $signup_errors = [ 'username' => [ 'required' => 'You must choose a username.', @@ -149,6 +156,9 @@ public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig public function testValidateWithStringRulesFoundUseMessagesParameter(): void { $validation = new class () extends ValidationConfig { + /** + * @var array + */ public array $signup = [ 'username' => 'required', ];