1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionException; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\Form; |
11
|
|
|
use SilverStripe\Forms\RequiredFields; |
12
|
|
|
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator; |
13
|
|
|
use SilverStripe\Forms\TextField; |
14
|
|
|
use SilverStripe\Forms\CompositeValidator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @package framework |
18
|
|
|
* @subpackage tests |
19
|
|
|
*/ |
20
|
|
|
class CompositeValidatorTest extends SapphireTest |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Common method for setting up form, since that will always be a dependency for the validator. |
24
|
|
|
* |
25
|
|
|
* @param array $fieldNames |
26
|
|
|
* @return Form |
27
|
|
|
*/ |
28
|
|
|
protected function getForm(array $fieldNames = []): Form |
29
|
|
|
{ |
30
|
|
|
// Setup field list now. We're only worried about names right now |
31
|
|
|
$fieldList = new FieldList(); |
32
|
|
|
|
33
|
|
|
foreach ($fieldNames as $name) { |
34
|
|
|
$fieldList->add(new TextField($name)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return new Form(Controller::curr(), "testForm", $fieldList, new FieldList([/* no actions */])); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testAddValidator(): void |
41
|
|
|
{ |
42
|
|
|
$compositeValidator = new CompositeValidator(); |
43
|
|
|
$compositeValidator->addValidator(new RequiredFields()); |
44
|
|
|
$compositeValidator->addValidator(new RequiredFields()); |
45
|
|
|
|
46
|
|
|
$this->assertCount(2, $compositeValidator->getValidators()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws ReflectionException |
51
|
|
|
*/ |
52
|
|
|
public function testSetForm(): void |
53
|
|
|
{ |
54
|
|
|
$form = $this->getForm(); |
55
|
|
|
|
56
|
|
|
$reflectionClass = new ReflectionClass(CompositeValidator::class); |
57
|
|
|
$property = $reflectionClass->getProperty('form'); |
58
|
|
|
$property->setAccessible(true); |
59
|
|
|
|
60
|
|
|
$compositeValidator = new CompositeValidator(); |
61
|
|
|
$validator = new TestValidator(); |
62
|
|
|
|
63
|
|
|
$compositeValidator->addValidator($validator); |
64
|
|
|
|
65
|
|
|
$compositeValidator->setForm($form); |
66
|
|
|
|
67
|
|
|
$this->assertNotNull($property->getValue($compositeValidator)); |
68
|
|
|
$this->assertCount(1, $compositeValidator->getValidators()); |
69
|
|
|
|
70
|
|
|
foreach ($compositeValidator->getValidators() as $validator) { |
71
|
|
|
/** @var TestValidator $validator */ |
72
|
|
|
$this->assertNotNull($property->getValue($validator)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testGetValidatorsByType(): void |
77
|
|
|
{ |
78
|
|
|
$compositeValidator = new CompositeValidator(); |
79
|
|
|
$compositeValidator->addValidator(new RequiredFields()); |
80
|
|
|
$compositeValidator->addValidator(new TestValidator()); |
81
|
|
|
$compositeValidator->addValidator(new RequiredFields()); |
82
|
|
|
$compositeValidator->addValidator(new TestValidator()); |
83
|
|
|
|
84
|
|
|
$this->assertCount(4, $compositeValidator->getValidators()); |
85
|
|
|
$this->assertCount(2, $compositeValidator->getValidatorsByType(RequiredFields::class)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testRemoveValidatorsByType(): void |
89
|
|
|
{ |
90
|
|
|
$compositeValidator = new CompositeValidator(); |
91
|
|
|
$compositeValidator->addValidator(new RequiredFields()); |
92
|
|
|
$compositeValidator->addValidator(new TestValidator()); |
93
|
|
|
$compositeValidator->addValidator(new RequiredFields()); |
94
|
|
|
$compositeValidator->addValidator(new TestValidator()); |
95
|
|
|
|
96
|
|
|
$this->assertCount(4, $compositeValidator->getValidators()); |
97
|
|
|
|
98
|
|
|
$compositeValidator->removeValidatorsByType(RequiredFields::class); |
99
|
|
|
$this->assertCount(2, $compositeValidator->getValidators()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testCanBeCached(): void |
103
|
|
|
{ |
104
|
|
|
$compositeValidator = new CompositeValidator(); |
105
|
|
|
$compositeValidator->addValidator(new RequiredFields()); |
106
|
|
|
|
107
|
|
|
$this->assertTrue($compositeValidator->canBeCached()); |
108
|
|
|
|
109
|
|
|
$compositeValidator = new CompositeValidator(); |
110
|
|
|
$compositeValidator->addValidator(new RequiredFields(['Foor'])); |
111
|
|
|
|
112
|
|
|
$this->assertFalse($compositeValidator->canBeCached()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testFieldIsRequired(): void |
116
|
|
|
{ |
117
|
|
|
$compositeValidator = new CompositeValidator(); |
118
|
|
|
|
119
|
|
|
$fieldNames = [ |
120
|
|
|
'Title', |
121
|
|
|
'Content', |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
$requiredFieldsFirst = new RequiredFields( |
125
|
|
|
[ |
126
|
|
|
$fieldNames[0], |
127
|
|
|
] |
128
|
|
|
); |
129
|
|
|
$requiredFieldsSecond = new RequiredFields( |
130
|
|
|
[ |
131
|
|
|
$fieldNames[1], |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$compositeValidator->addValidator($requiredFieldsFirst); |
136
|
|
|
$compositeValidator->addValidator($requiredFieldsSecond); |
137
|
|
|
|
138
|
|
|
foreach ($fieldNames as $field) { |
139
|
|
|
$this->assertTrue( |
140
|
|
|
$compositeValidator->fieldIsRequired($field), |
141
|
|
|
sprintf('Failed to find "%s" field in required list', $field) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testValidate(): void |
147
|
|
|
{ |
148
|
|
|
$compositeValidator = new CompositeValidator(); |
149
|
|
|
// Add two separate validators, each with one required field |
150
|
|
|
$compositeValidator->addValidator(new RequiredFields(['Foo'])); |
151
|
|
|
$compositeValidator->addValidator(new RequiredFields(['Bar'])); |
152
|
|
|
|
153
|
|
|
// Setup a form with the fields/data we're testing (a form is a dependency for validation right now) |
154
|
|
|
// We'll add three empty fields, but only two of them should be required |
155
|
|
|
$data = [ |
156
|
|
|
'Foo' => '', |
157
|
|
|
'Bar' => '', |
158
|
|
|
'FooBar' => '', |
159
|
|
|
]; |
160
|
|
|
// We only care right now about the fields we've got setup in this array |
161
|
|
|
$form = $this->getForm(array_keys($data)); |
162
|
|
|
$form->disableSecurityToken(); |
163
|
|
|
// Setup validator now that we've got our form |
164
|
|
|
$form->setValidator($compositeValidator); |
165
|
|
|
// Put data into the form so the validator can pull it back out again |
166
|
|
|
$form->loadDataFrom($data); |
167
|
|
|
|
168
|
|
|
$result = $form->validationResult(); |
169
|
|
|
$this->assertFalse($result->isValid()); |
170
|
|
|
$this->assertCount(2, $result->getMessages()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testRemoveValidation(): void |
174
|
|
|
{ |
175
|
|
|
$compositeValidator = new CompositeValidator(); |
176
|
|
|
$compositeValidator->addValidator(new TestValidator()); |
177
|
|
|
|
178
|
|
|
// Setup a form with the fields/data we're testing (a form is a dependency for validation right now) |
179
|
|
|
$data = [ |
180
|
|
|
'Foo' => '', |
181
|
|
|
]; |
182
|
|
|
// We only care right now about the fields we've got setup in this array |
183
|
|
|
$form = $this->getForm(array_keys($data)); |
184
|
|
|
$form->disableSecurityToken(); |
185
|
|
|
// Setup validator now that we've got our form |
186
|
|
|
$form->setValidator($compositeValidator); |
187
|
|
|
// Put data into the form so the validator can pull it back out again |
188
|
|
|
$form->loadDataFrom($data); |
189
|
|
|
|
190
|
|
|
$result = $form->validationResult(); |
191
|
|
|
$this->assertFalse($result->isValid()); |
192
|
|
|
$this->assertCount(1, $result->getMessages()); |
193
|
|
|
|
194
|
|
|
// Make sure it doesn't fail after removing validation AND has no errors (since calling validate should |
195
|
|
|
// reset errors) |
196
|
|
|
$compositeValidator->removeValidation(); |
197
|
|
|
$result = $form->validationResult(); |
198
|
|
|
$this->assertTrue($result->isValid()); |
199
|
|
|
$this->assertEmpty($result->getMessages()); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|