Skip to content

Commit bc61694

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.6
2 parents 7c0a539 + 5a340d0 commit bc61694

File tree

4 files changed

+184
-25
lines changed

4 files changed

+184
-25
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12847,24 +12847,6 @@
1284712847
'count' => 1,
1284812848
'path' => __DIR__ . '/tests/system/ControllerTest.php',
1284912849
];
12850-
$ignoreErrors[] = [
12851-
// identifier: missingType.property
12852-
'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:128\\:\\:\\$signup has no type specified\\.$#',
12853-
'count' => 1,
12854-
'path' => __DIR__ . '/tests/system/ControllerTest.php',
12855-
];
12856-
$ignoreErrors[] = [
12857-
// identifier: missingType.property
12858-
'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:128\\:\\:\\$signup_errors has no type specified\\.$#',
12859-
'count' => 1,
12860-
'path' => __DIR__ . '/tests/system/ControllerTest.php',
12861-
];
12862-
$ignoreErrors[] = [
12863-
// identifier: missingType.property
12864-
'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:151\\:\\:\\$signup has no type specified\\.$#',
12865-
'count' => 1,
12866-
'path' => __DIR__ . '/tests/system/ControllerTest.php',
12867-
];
1286812850
$ignoreErrors[] = [
1286912851
// identifier: argument.type
1287012852
'message' => '#^Parameter \\#1 \\$cookies of class CodeIgniter\\\\Cookie\\\\CookieStore constructor expects array\\<CodeIgniter\\\\Cookie\\\\Cookie\\>, array\\<int, DateTimeImmutable\\> given\\.$#',

system/Validation/StrictRules/Rules.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ public function differs(
4848
return $str !== dot_array_search($otherField, $data);
4949
}
5050

51-
if (! array_key_exists($field, $data)) {
51+
if (! array_key_exists($otherField, $data)) {
5252
return false;
5353
}
5454

55-
if (! array_key_exists($otherField, $data)) {
55+
if (str_contains($field, '.')) {
56+
if (! ArrayHelper::dotKeyExists($field, $data)) {
57+
return false;
58+
}
59+
} elseif (! array_key_exists($field, $data)) {
5660
return false;
5761
}
5862

@@ -281,11 +285,15 @@ public function matches(
281285
return $str === dot_array_search($otherField, $data);
282286
}
283287

284-
if (! array_key_exists($field, $data)) {
288+
if (! array_key_exists($otherField, $data)) {
285289
return false;
286290
}
287291

288-
if (! array_key_exists($otherField, $data)) {
292+
if (str_contains($field, '.')) {
293+
if (! ArrayHelper::dotKeyExists($field, $data)) {
294+
return false;
295+
}
296+
} elseif (! array_key_exists($field, $data)) {
289297
return false;
290298
}
291299

tests/system/ControllerTest.php

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,17 @@ public function testValidateWithStringRulesNotFound(): void
126126
public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig(): void
127127
{
128128
$validation = new class () extends ValidationConfig {
129-
public $signup = [
129+
/**
130+
* @var array<string, string>
131+
*/
132+
public array $signup = [
130133
'username' => 'required',
131134
];
132-
public $signup_errors = [
135+
136+
/**
137+
* @var array<string, array<string, string>>
138+
*/
139+
public array $signup_errors = [
133140
'username' => [
134141
'required' => 'You must choose a username.',
135142
],
@@ -149,7 +156,10 @@ public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig
149156
public function testValidateWithStringRulesFoundUseMessagesParameter(): void
150157
{
151158
$validation = new class () extends ValidationConfig {
152-
public $signup = [
159+
/**
160+
* @var array<string, string>
161+
*/
162+
public array $signup = [
153163
'username' => 'required',
154164
];
155165
};
@@ -191,6 +201,77 @@ public function testValidateData(): void
191201
);
192202
}
193203

204+
public function testValidateDataWithCustomErrorMessage(): void
205+
{
206+
// make sure we can instantiate one
207+
$this->controller = new Controller();
208+
$this->controller->initController($this->request, $this->response, $this->logger);
209+
210+
$method = $this->getPrivateMethodInvoker($this->controller, 'validateData');
211+
212+
$data = [
213+
'username' => 'a',
214+
'password' => '123',
215+
];
216+
$rules = [
217+
'username' => 'required|min_length[3]',
218+
'password' => 'required|min_length[10]',
219+
];
220+
$errors = [
221+
'username' => [
222+
'required' => 'Please fill "{field}".',
223+
'min_length' => '"{field}" must be {param} letters or longer.',
224+
],
225+
];
226+
$this->assertFalse($method($data, $rules, $errors));
227+
$this->assertSame(
228+
'"username" must be 3 letters or longer.',
229+
Services::validation()->getError('username')
230+
);
231+
$this->assertSame(
232+
'The password field must be at least 10 characters in length.',
233+
Services::validation()->getError('password')
234+
);
235+
}
236+
237+
public function testValidateDataWithCustomErrorMessageLabeledStyle(): void
238+
{
239+
// make sure we can instantiate one
240+
$this->controller = new Controller();
241+
$this->controller->initController($this->request, $this->response, $this->logger);
242+
243+
$method = $this->getPrivateMethodInvoker($this->controller, 'validateData');
244+
245+
$data = [
246+
'username' => 'a',
247+
'password' => '123',
248+
];
249+
$rules = [
250+
'username' => [
251+
'label' => 'Username',
252+
'rules' => 'required|min_length[3]',
253+
'errors' => [
254+
'required' => 'Please fill "{field}".',
255+
'min_length' => '"{field}" must be {param} letters or longer.',
256+
],
257+
],
258+
'password' => [
259+
'required|min_length[10]',
260+
'label' => 'Password',
261+
'rules' => 'required|min_length[10]',
262+
],
263+
];
264+
$this->assertFalse($method($data, $rules));
265+
$this->assertSame(
266+
'"Username" must be 3 letters or longer.',
267+
Services::validation()->getError('username')
268+
);
269+
$this->assertSame(
270+
'The Password field must be at least 10 characters in length.',
271+
Services::validation()->getError('password')
272+
);
273+
}
274+
194275
public function testHelpers(): void
195276
{
196277
$this->controller = new class () extends Controller {

tests/system/Validation/RulesTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,50 @@ public function testMatchesNested(array $data, bool $expected): void
336336
$this->assertSame($expected, $this->validation->run($data));
337337
}
338338

339+
public function testMatchesWithDotArrayPass(): void
340+
{
341+
$rules = [
342+
'name' => 'permit_empty',
343+
'emailAddress' => 'permit_empty|valid_email',
344+
'alias.*' => 'permit_empty|matches[name]',
345+
];
346+
$this->validation->setRules($rules);
347+
348+
$data = [
349+
'name' => 'Princess Peach',
350+
'emailAddress' => '[email protected]',
351+
'alias' => [
352+
'Princess Peach',
353+
'Princess Peach',
354+
],
355+
];
356+
$this->assertTrue($this->validation->run($data));
357+
}
358+
359+
public function testMatchesWithDotArrayFail(): void
360+
{
361+
$rules = [
362+
'name' => 'permit_empty',
363+
'emailAddress' => 'permit_empty|valid_email',
364+
'alias.*' => 'permit_empty|matches[name]',
365+
];
366+
$this->validation->setRules($rules);
367+
368+
$data = [
369+
'name' => 'Princess Peach',
370+
'emailAddress' => '[email protected]',
371+
'alias' => [
372+
'Princess ',
373+
'Princess Peach',
374+
],
375+
];
376+
$this->assertFalse($this->validation->run($data));
377+
$this->assertSame(
378+
['alias.0' => 'The alias.* field does not match the name field.'],
379+
$this->validation->getErrors()
380+
);
381+
}
382+
339383
public static function provideMatchesNestedCases(): iterable
340384
{
341385
yield from [
@@ -373,6 +417,50 @@ public function testDiffersNested(array $data, bool $expected): void
373417
$this->assertSame(! $expected, $this->validation->run($data));
374418
}
375419

420+
public function testDiffersWithDotArrayPass(): void
421+
{
422+
$rules = [
423+
'name' => 'permit_empty',
424+
'emailAddress' => 'permit_empty|valid_email',
425+
'alias.*' => 'permit_empty|differs[name]',
426+
];
427+
$this->validation->setRules($rules);
428+
429+
$data = [
430+
'name' => 'Princess Peach',
431+
'emailAddress' => '[email protected]',
432+
'alias' => [
433+
'Princess Toadstool',
434+
'Peach',
435+
],
436+
];
437+
$this->assertTrue($this->validation->run($data));
438+
}
439+
440+
public function testDiffersWithDotArrayFail(): void
441+
{
442+
$rules = [
443+
'name' => 'permit_empty',
444+
'emailAddress' => 'permit_empty|valid_email',
445+
'alias.*' => 'permit_empty|differs[name]',
446+
];
447+
$this->validation->setRules($rules);
448+
449+
$data = [
450+
'name' => 'Princess Peach',
451+
'emailAddress' => '[email protected]',
452+
'alias' => [
453+
'Princess Toadstool',
454+
'Princess Peach',
455+
],
456+
];
457+
$this->assertFalse($this->validation->run($data));
458+
$this->assertSame(
459+
['alias.1' => 'The alias.* field must differ from the name field.'],
460+
$this->validation->getErrors()
461+
);
462+
}
463+
376464
#[DataProvider('provideEquals')]
377465
public function testEquals(array $data, string $param, bool $expected): void
378466
{

0 commit comments

Comments
 (0)