Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into 4.6
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 6, 2024
2 parents 7c0a539 + 5a340d0 commit bc61694
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 25 deletions.
18 changes: 0 additions & 18 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -12847,24 +12847,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\\<CodeIgniter\\\\Cookie\\\\Cookie\\>, array\\<int, DateTimeImmutable\\> given\\.$#',
Expand Down
16 changes: 12 additions & 4 deletions system/Validation/StrictRules/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
87 changes: 84 additions & 3 deletions tests/system/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,17 @@ public function testValidateWithStringRulesNotFound(): void
public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig(): void
{
$validation = new class () extends ValidationConfig {
public $signup = [
/**
* @var array<string, string>
*/
public array $signup = [
'username' => 'required',
];
public $signup_errors = [

/**
* @var array<string, array<string, string>>
*/
public array $signup_errors = [
'username' => [
'required' => 'You must choose a username.',
],
Expand All @@ -149,7 +156,10 @@ public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig
public function testValidateWithStringRulesFoundUseMessagesParameter(): void
{
$validation = new class () extends ValidationConfig {
public $signup = [
/**
* @var array<string, string>
*/
public array $signup = [
'username' => 'required',
];
};
Expand Down Expand Up @@ -191,6 +201,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 {
Expand Down
88 changes: 88 additions & 0 deletions tests/system/Validation/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit bc61694

Please sign in to comment.