forked from craftcms/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueValidatorTest.php
More file actions
97 lines (86 loc) · 3.57 KB
/
Copy pathUniqueValidatorTest.php
File metadata and controls
97 lines (86 loc) · 3.57 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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace crafttests\unit\validators;
use craft\models\UserGroup;
use craft\records\UserGroup as UserGroupRecord;
use craft\test\TestCase;
use craft\validators\UniqueValidator;
use crafttests\fixtures\UserGroupsFixture;
use yii\db\ActiveQueryInterface;
/**
* Class UniqueValidatorTest.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @author Global Network Group | Giel Tettelaar <giel@yellowflash.net>
* @since 4.5.0
*/
class UniqueValidatorTest extends TestCase
{
/**
* @var UniqueValidator
*/
protected UniqueValidator $uniqueValidator;
public function _fixtures(): array
{
return [
'user-groups' => [
'class' => UserGroupsFixture::class,
],
];
}
/**
* @dataProvider uniqueValidatorValidateDataProvider
* @param string $attribute
* @param int|null $id
* @param array $config
* @param bool $mustValidate
* @param array $modelConfig
*/
public function testValidation(string $attribute, ?int $id = null, array $config = [], bool $mustValidate = true, array $modelConfig = []): void
{
$uniqueValidator = new UniqueValidator($config);
if ($id) {
$model = \Craft::$app->getUserGroups()->getGroupById($id);
if (!empty($modelConfig)) {
$model->setAttributes($modelConfig);
}
} else {
$model = new UserGroup($modelConfig);
}
$uniqueValidator->validateAttribute($model, $attribute);
if ($mustValidate) {
self::assertArrayNotHasKey($attribute, $model->getErrors());
} else {
self::assertArrayHasKey($attribute, $model->getErrors());
}
}
/**
* @return array
*/
public static function uniqueValidatorValidateDataProvider(): array
{
return [
'existing' => ['handle', 1000, ['targetClass' => UserGroupRecord::class], true],
'unique' => ['handle', 1000, ['targetClass' => UserGroupRecord::class], true, ['handle' => 'group99']],
'not-unique' => ['handle', 1000, ['targetClass' => UserGroupRecord::class], false, ['handle' => 'group2']],
'new-unique' => ['handle', null, ['targetClass' => UserGroupRecord::class], true, ['handle' => 'group99']],
'new-not-unique' => ['handle', null, ['targetClass' => UserGroupRecord::class], false, ['handle' => 'group1']],
// Add extra filter to make sure it passes validation
'not-unique-extra-filter' => ['handle', 1000, ['targetClass' => UserGroupRecord::class, 'filter' => ['not', ['description' => null]]], true, ['handle' => 'group2']],
'not-unique-closure-filter' => ['handle', 1000, ['targetClass' => UserGroupRecord::class, 'filter' => fn(ActiveQueryInterface $query) => $query->andWhere(['not', ['description' => null]])], true, ['handle' => 'group2']],
'new-not-unique-extra-filter' => ['handle', null, ['targetClass' => UserGroupRecord::class, 'filter' => ['not', ['description' => null]]], true, ['handle' => 'group2']],
'new-not-unique-closure-filter' => ['handle', null, ['targetClass' => UserGroupRecord::class, 'filter' => fn(ActiveQueryInterface $query) => $query->andWhere(['not', ['description' => null]])], true, ['handle' => 'group2']],
];
}
/**
* @inheritdoc
*/
protected function _before(): void
{
$this->uniqueValidator = new UniqueValidator();
}
}