forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationModelRuleGroupTest.php
More file actions
477 lines (394 loc) · 13.6 KB
/
ValidationModelRuleGroupTest.php
File metadata and controls
477 lines (394 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Models;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Model;
use PHPUnit\Framework\Attributes\Group;
use stdClass;
use Tests\Support\Config\Validation;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\SimpleEntity;
use Tests\Support\Models\ValidErrorsModel;
use Tests\Support\Models\ValidModelRuleGroup;
/**
* @internal
*/
#[Group('DatabaseLive')]
final class ValidationModelRuleGroupTest extends LiveModelTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->createModel(ValidModelRuleGroup::class);
}
protected function createModel(string $modelName, ?BaseConnection $db = null): Model
{
$config = new Validation();
$validation = new \CodeIgniter\Validation\Validation($config, service('renderer'));
$this->db = $db ?? $this->db;
$this->model = new $modelName($this->db, $validation);
return $this->model;
}
public function testValid(): void
{
$data = [
'name' => 'some name',
'description' => 'some great marketing stuff',
];
$this->assertIsInt($this->model->insert($data));
$errors = $this->model->errors();
$this->assertSame([], $errors);
}
public function testValidationBasics(): void
{
$data = [
'name' => null,
'description' => 'some great marketing stuff',
];
$this->assertFalse($this->model->insert($data));
$errors = $this->model->errors();
$this->assertSame('You forgot to name the baby.', $errors['name']);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5859
*/
public function testValidationTwice(): void
{
$data = [
'name' => null,
'description' => 'some great marketing stuff',
];
$this->assertFalse($this->model->insert($data));
$errors = $this->model->errors();
$this->assertSame('You forgot to name the baby.', $errors['name']);
$data = [
'name' => 'some name',
'description' => 'some great marketing stuff',
];
$this->assertIsInt($this->model->insert($data));
}
public function testValidationWithSetValidationRule(): void
{
$data = [
'name' => 'some name',
'description' => 'some great marketing stuff',
];
$this->model->setValidationRule('description', [
'rules' => 'required|min_length[50]',
'errors' => [
'min_length' => 'Description is too short baby.',
],
]);
$this->assertFalse($this->model->insert($data));
$errors = $this->model->errors();
$this->assertSame('Description is too short baby.', $errors['description']);
}
public function testValidationWithSetValidationRules(): void
{
$data = [
'name' => '',
'description' => 'some great marketing stuff',
];
$this->model->setValidationRules([
'name' => [
'rules' => 'required',
'errors' => [
'required' => 'Give me a name baby.',
],
],
'description' => [
'rules' => 'required|min_length[50]',
'errors' => [
'min_length' => 'Description is too short baby.',
],
],
]);
$this->assertFalse($this->model->insert($data));
$errors = $this->model->errors();
$this->assertSame('Give me a name baby.', $errors['name']);
$this->assertSame('Description is too short baby.', $errors['description']);
}
public function testValidationWithSetValidationMessage(): void
{
$data = [
'name' => null,
'description' => 'some great marketing stuff',
];
$this->model->setValidationMessage('name', [
'required' => 'Your baby name is missing.',
'min_length' => 'Too short, man!',
]);
$this->assertFalse($this->model->insert($data));
$errors = $this->model->errors();
$this->assertSame('Your baby name is missing.', $errors['name']);
}
public function testValidationPlaceholdersSuccess(): void
{
$data = [
'name' => 'abc',
'id' => 13,
'token' => 13,
];
$this->assertTrue($this->model->validate($data));
}
public function testValidationPlaceholdersFail(): void
{
$data = [
'name' => 'abc',
'id' => 13,
'token' => 12,
];
$this->assertFalse($this->model->validate($data));
}
public function testSkipValidation(): void
{
$data = [
'name' => '2',
'description' => 'some great marketing stuff',
];
$this->assertIsNumeric($this->model->skipValidation(true)->insert($data));
}
public function testCleanValidationRemovesAllWhenNoDataProvided(): void
{
$cleaner = self::getPrivateMethodInvoker($this->model, 'cleanValidationRules');
$rules = [
'name' => 'required',
'foo' => 'bar',
];
$rules = $cleaner($rules, []);
$this->assertEmpty($rules);
}
public function testCleanValidationRemovesOnlyForFieldsNotProvided(): void
{
$cleaner = self::getPrivateMethodInvoker($this->model, 'cleanValidationRules');
$rules = [
'name' => 'required',
'foo' => 'required',
];
$data = [
'foo' => 'bar',
];
$rules = $cleaner($rules, $data);
$this->assertArrayHasKey('foo', $rules);
$this->assertArrayNotHasKey('name', $rules);
}
public function testCleanValidationReturnsAllWhenAllExist(): void
{
$cleaner = self::getPrivateMethodInvoker($this->model, 'cleanValidationRules');
$rules = [
'name' => 'required',
'foo' => 'required',
];
$data = [
'foo' => 'bar',
'name' => null,
];
$rules = $cleaner($rules, $data);
$this->assertArrayHasKey('foo', $rules);
$this->assertArrayHasKey('name', $rules);
}
public function testValidationPassesWithMissingFields(): void
{
$data = [
'foo' => 'bar',
];
$result = $this->model->validate($data);
$this->assertTrue($result);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1584
*/
public function testUpdateWithValidation(): void
{
$data = [
'description' => 'This is a first test!',
'name' => 'valid',
'id' => 42,
'token' => 42,
];
$id = $this->model->insert($data);
$this->assertTrue((bool) $id);
$data['description'] = 'This is a second test!';
unset($data['name']);
$result = $this->model->update($id, $data);
$this->assertTrue($result);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1717
*/
public function testRequiredWithValidationEmptyString(): void
{
$this->assertFalse($this->model->insert(['name' => '']));
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1717
*/
public function testRequiredWithValidationNull(): void
{
$this->assertFalse($this->model->insert(['name' => null]));
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1717
*/
public function testRequiredWithValidationTrue(): void
{
$data = [
'name' => 'foobar',
'description' => 'just because we have to',
];
$this->assertNotFalse($this->model->insert($data));
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1574
*/
public function testValidationIncludingErrors(): void
{
$data = [
'description' => 'This is a first test!',
'name' => 'valid',
'id' => 42,
'token' => 42,
];
$this->createModel(ValidErrorsModel::class);
$id = $this->model->insert($data);
$this->assertFalse((bool) $id);
$this->assertSame('Minimum Length Error', $this->model->errors()['name']);
}
public function testValidationByObject(): void
{
$data = new stdClass();
$data->name = 'abc';
$data->id = '13';
$data->token = '13';
$this->assertTrue($this->model->validate($data));
}
public function testGetValidationRules(): void
{
$this->createModel(JobModel::class);
$this->setPrivateProperty($this->model, 'validationRules', ['description' => 'required']);
$rules = $this->model->getValidationRules();
$this->assertSame('required', $rules['description']);
}
public function testGetValidationMessages(): void
{
$jobData = [
[
'name' => 'Comedian',
'description' => null,
],
];
$this->createModel(JobModel::class);
$this->setPrivateProperty($this->model, 'validationRules', ['description' => 'required']);
$this->setPrivateProperty($this->model, 'validationMessages', ['description' => 'Description field is required.']);
$this->assertFalse($this->model->insertBatch($jobData));
$error = $this->model->getValidationMessages();
$this->assertSame('Description field is required.', $error['description']);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6577
*/
public function testUpdateEntityWithPropertyCleanValidationRulesTrueAndCallingCleanRulesFalse(): void
{
$model = new class () extends Model {
protected $table = 'test';
protected $allowedFields = ['field1', 'field2', 'field3', 'field4'];
protected $returnType = SimpleEntity::class;
protected $validationRules = [
'field1' => 'required_with[field2,field3,field4]',
'field2' => 'permit_empty',
'field3' => 'permit_empty',
'field4' => 'permit_empty',
];
};
// Simulate to get the entity from the database.
$entity = new SimpleEntity();
$entity->injectRawData([
'id' => '1',
'field1' => 'value1',
'field2' => 'value2',
'field3' => '',
'field4' => '',
]);
// Change field1 value.
$entity->field1 = '';
// Set $cleanValidationRules to false by cleanRules()
$model->cleanRules(false)->save($entity);
$errors = $model->errors();
$this->assertCount(1, $errors);
$this->assertSame(
'The field1 field is required when field2,field3,field4 is present.',
$errors['field1'],
);
}
public function testUpdateEntityWithPropertyCleanValidationRulesFalse(): void
{
$model = new class () extends Model {
protected $table = 'test';
protected $allowedFields = ['field1', 'field2', 'field3', 'field4'];
protected $returnType = SimpleEntity::class;
protected $validationRules = [
'field1' => 'required_with[field2,field3,field4]',
'field2' => 'permit_empty',
'field3' => 'permit_empty',
'field4' => 'permit_empty',
];
// Set to false.
protected $cleanValidationRules = false;
};
// Simulate to get the entity from the database.
$entity = new SimpleEntity();
$entity->injectRawData([
'id' => '1',
'field1' => 'value1',
'field2' => 'value2',
'field3' => '',
'field4' => '',
]);
// Change field1 value.
$entity->field1 = '';
$model->save($entity);
$errors = $model->errors();
$this->assertCount(1, $errors);
$this->assertSame(
'The field1 field is required when field2,field3,field4 is present.',
$errors['field1'],
);
}
public function testInsertEntityValidateEntireRules(): void
{
$model = new class () extends Model {
protected $table = 'test';
protected $allowedFields = ['field1', 'field2', 'field3', 'field4'];
protected $returnType = SimpleEntity::class;
protected $validationRules = [
'field1' => 'required',
'field2' => 'required',
'field3' => 'permit_empty',
'field4' => 'permit_empty',
];
};
$entity = new SimpleEntity();
$entity->injectRawData([
'field1' => 'value1',
// field2 is missing
'field3' => '',
'field4' => '',
]);
// Insert ignores $cleanValidationRules value.
$model->insert($entity);
$errors = $model->errors();
$this->assertCount(1, $errors);
$this->assertSame(
'The field2 field is required.',
$errors['field2'],
);
}
}