forked from craftcms/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordValidatorTest.php
More file actions
169 lines (149 loc) · 4.73 KB
/
Copy pathPasswordValidatorTest.php
File metadata and controls
169 lines (149 loc) · 4.73 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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace crafttests\unit\validators;
use Craft;
use craft\test\mockclasses\models\ExampleModel;
use craft\test\TestCase;
use craft\validators\UserPasswordValidator;
use TypeError;
use UnitTester;
/**
* Class PasswordValidatorTest.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @author Global Network Group | Giel Tettelaar <giel@yellowflash.net>
* @since 3.2
*/
class PasswordValidatorTest extends TestCase
{
/**
* @var UnitTester
*/
protected UnitTester $tester;
/**
* @var UserPasswordValidator
*/
protected UserPasswordValidator $passwordValidator;
/**
* @var ExampleModel
*/
protected ExampleModel $model;
/**
* @dataProvider passwordValidationDataProvider
* @param string $inputValue
* @param bool $mustValidate
* @param string|null $currentPass
*/
public function testValidation(string $inputValue, bool $mustValidate, string $currentPass = null): void
{
$this->model->exampleParam = $inputValue;
if ($currentPass) {
$this->passwordValidator->currentPassword = $currentPass;
}
$this->passwordValidator->validateAttribute($this->model, 'exampleParam');
if ($mustValidate) {
self::assertArrayNotHasKey('exampleParam', $this->model->getErrors());
} else {
self::assertArrayHasKey('exampleParam', $this->model->getErrors());
}
}
/**
* @dataProvider customConfigDataProvider
* @param mixed $input
* @param bool $mustValidate
* @param int $min
* @param int $max
*/
public function testCustomConfig(mixed $input, bool $mustValidate, int $min, int $max): void
{
$passVal = new UserPasswordValidator(['min' => $min, 'max' => $max]);
$this->model->exampleParam = $input;
$passVal->validateAttribute($this->model, 'exampleParam');
if ($mustValidate) {
self::assertArrayNotHasKey('exampleParam', $this->model->getErrors());
} else {
self::assertArrayHasKey('exampleParam', $this->model->getErrors());
}
}
/**
* @dataProvider forceDiffValidationDataProvider
* @param bool $mustValidate
* @param string $input
* @param string $currentPassword
*/
public function testForceDiffValidation(bool $mustValidate, string $input, string $currentPassword): void
{
$this->passwordValidator->forceDifferent = true;
$this->passwordValidator->currentPassword = Craft::$app->getSecurity()->hashPassword($currentPassword);
$this->model->exampleParam = $input;
$this->passwordValidator->validateAttribute($this->model, 'exampleParam');
if ($mustValidate) {
self::assertArrayNotHasKey('exampleParam', $this->model->getErrors());
} else {
self::assertArrayHasKey('exampleParam', $this->model->getErrors());
}
}
public function testToStringExpectException(): void
{
$passVal = $this->passwordValidator;
$this->tester->expectThrowable(TypeError::class, function() use ($passVal) {
/** @phpstan-ignore-next-line */
$passVal->isEmpty = 'craft_increment';
$passVal->isEmpty(1);
});
}
/**
* @return array
*/
public static function passwordValidationDataProvider(): array
{
return [
['22', false],
['123456', true],
['!@#$%^&*()', true],
['161charsoaudsoidsaiadsjdsapoisajdpodsapaasdjosadojdsaodsapojdaposjosdakshjdsahksakhjhsadskajaskjhsadkdsakdsjhadsahkksadhdaskldskldslkdaslkadslkdsalkdsalkdsalkdsa', false],
];
}
/**
* @return array
*/
public static function customConfigDataProvider(): array
{
return [
['password', false, 0, 0],
['3', false, 2, 0],
['123', true, 3, 3],
['123', true, 2, 3],
['123', true, 3, 4],
['', true, -1, 0],
[null, false, -1, 0],
];
}
/**
* @return array
*/
public static function forceDiffValidationDataProvider(): array
{
return [
[false, 'test', 'test'],
[false, '', ''],
// Not 6 chars
[false, 'test', 'difftest'],
[true, 'onetwothreefourfivesix', 'onetwothreefourfivesixseven'],
// Spaces?
[true, ' ', ' '],
];
}
/**
* @inheritdoc
*/
protected function _before(): void
{
$this->passwordValidator = new UserPasswordValidator();
$this->model = new ExampleModel();
}
}