1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DroneTest\Validator; |
12
|
|
|
|
13
|
|
|
use Drone\Dom\Element\ElementFactory; |
14
|
|
|
use Drone\Validator\FormValidator; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class FormValidatorTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Tests form validation fail |
21
|
|
|
* |
22
|
|
|
* @return null |
23
|
|
|
*/ |
24
|
|
|
public function testFormValidationFail() |
25
|
|
|
{ |
26
|
|
|
$form = ElementFactory::create('FORM', [ |
27
|
|
|
"action" => 'someurl', |
28
|
|
|
"method" => 'post' |
29
|
|
|
], [ |
30
|
|
|
"input" => [ |
31
|
|
|
"username" => [ |
32
|
|
|
"type" => 'text', |
33
|
|
|
"maxlength" => 15, |
34
|
|
|
"minlength" => 5 |
35
|
|
|
], |
36
|
|
|
"email" => [ |
37
|
|
|
"type" => 'email', |
38
|
|
|
"maxlength" => 15, |
39
|
|
|
"minlength" => 5 |
40
|
|
|
], |
41
|
|
|
"password" => [ |
42
|
|
|
"type" => 'password', |
43
|
|
|
"maxlength" => 15, |
44
|
|
|
"minlength" => 5 |
45
|
|
|
] |
46
|
|
|
] |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$form->fill([ |
|
|
|
|
50
|
|
|
"username" => 'jobs', # wrong because of minlength attr |
51
|
|
|
"password" => 'jVi7Qp4X', |
52
|
|
|
"email" => 'j@' # wrong because of type and minlength attr |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$validator = new FormValidator($form, 'fr'); |
56
|
|
|
$validator->validate(); |
57
|
|
|
|
58
|
|
|
# testing validation |
59
|
|
|
$this->assertNotTrue($validator->isValid()); |
60
|
|
|
|
61
|
|
|
$errors = $validator->getErrors(); |
62
|
|
|
|
63
|
|
|
# testing error labeled |
64
|
|
|
$this->assertArrayHasKey("username", $errors); |
65
|
|
|
$this->assertArrayHasKey("email", $errors); |
66
|
|
|
|
67
|
|
|
# testing error grouping |
68
|
|
|
$this->assertEquals(1, count($errors["username"])); |
69
|
|
|
$this->assertEquals(2, count($errors["email"])); |
70
|
|
|
|
71
|
|
|
# testing locale |
72
|
|
|
$this->assertEquals("L'entrée contient moins de 5 caractères", $errors["username"][0]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tests form validation success |
77
|
|
|
* |
78
|
|
|
* @return null |
79
|
|
|
*/ |
80
|
|
|
public function testFormValidationSuccess() |
81
|
|
|
{ |
82
|
|
|
$form = ElementFactory::create('FORM', [ |
83
|
|
|
"action" => 'someurl', |
84
|
|
|
"method" => 'post' |
85
|
|
|
], [ |
86
|
|
|
"input" => [ |
87
|
|
|
"username" => [ |
88
|
|
|
"type" => 'text', |
89
|
|
|
"maxlength" => 15, |
90
|
|
|
"minlength" => 5 |
91
|
|
|
], |
92
|
|
|
"email" => [ |
93
|
|
|
"type" => 'email', |
94
|
|
|
"maxlength" => 16, |
95
|
|
|
"minlength" => 5 |
96
|
|
|
], |
97
|
|
|
"password" => [ |
98
|
|
|
"type" => 'password', |
99
|
|
|
"maxlength" => 15, |
100
|
|
|
"minlength" => 5 |
101
|
|
|
] |
102
|
|
|
] |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$form->fill([ |
106
|
|
|
"username" => 'steave.jobs', |
107
|
|
|
"password" => 'jVi7Qp4X', |
108
|
|
|
"email" => '[email protected]' |
109
|
|
|
]); |
110
|
|
|
|
111
|
|
|
$validator = new FormValidator($form, 'en'); |
112
|
|
|
$validator->validate(); |
113
|
|
|
|
114
|
|
|
# testing validation |
115
|
|
|
$this->assertTrue($validator->isValid()); |
116
|
|
|
|
117
|
|
|
$errors = $validator->getErrors(); |
118
|
|
|
$this->assertEquals(0, count($errors)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Tests form validation success when an element is or not required |
123
|
|
|
* |
124
|
|
|
* @return null |
125
|
|
|
*/ |
126
|
|
|
public function testFormValidationForRequiredAndNotRequired() |
127
|
|
|
{ |
128
|
|
|
/* |
129
|
|
|
* Not required element not filled |
130
|
|
|
*/ |
131
|
|
|
|
132
|
|
|
$form = ElementFactory::create('FORM', [ |
133
|
|
|
"action" => 'someurl', |
134
|
|
|
"method" => 'post' |
135
|
|
|
], [ |
136
|
|
|
"input" => [ |
137
|
|
|
"username" => [ |
138
|
|
|
"type" => 'text', |
139
|
|
|
"maxlength" => 15, |
140
|
|
|
"minlength" => 5, |
141
|
|
|
"required" => false # could be ommited, this is the default behaviour |
142
|
|
|
], |
143
|
|
|
] |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
$validator = new FormValidator($form, 'en'); |
147
|
|
|
$validator->validate(); |
148
|
|
|
|
149
|
|
|
# testing validation, no element has been declared as 'required' |
150
|
|
|
$this->assertTrue($validator->isValid()); |
151
|
|
|
|
152
|
|
|
$errors = $validator->getErrors(); |
153
|
|
|
$this->assertEquals(0, count($errors)); |
154
|
|
|
|
155
|
|
|
/* |
156
|
|
|
* Required element not filled |
157
|
|
|
*/ |
158
|
|
|
|
159
|
|
|
$form = ElementFactory::create('FORM', [ |
160
|
|
|
"action" => 'someurl', |
161
|
|
|
"method" => 'post' |
162
|
|
|
], [ |
163
|
|
|
"input" => [ |
164
|
|
|
"username" => [ |
165
|
|
|
"type" => 'text', |
166
|
|
|
"maxlength" => 15, |
167
|
|
|
"minlength" => 5, |
168
|
|
|
"required" => true |
169
|
|
|
], |
170
|
|
|
] |
171
|
|
|
]); |
172
|
|
|
|
173
|
|
|
$validator = new FormValidator($form, 'en'); |
174
|
|
|
$validator->validate(); |
175
|
|
|
|
176
|
|
|
# testing validation, the element has been declared as 'required' |
177
|
|
|
$this->assertNotTrue($validator->isValid()); |
178
|
|
|
|
179
|
|
|
$errors = $validator->getErrors(); |
180
|
|
|
$this->assertEquals(1, count($errors)); |
181
|
|
|
|
182
|
|
|
/* |
183
|
|
|
* Not Required element filled |
184
|
|
|
*/ |
185
|
|
|
|
186
|
|
|
$form = ElementFactory::create('FORM', [ |
187
|
|
|
"action" => 'someurl', |
188
|
|
|
"method" => 'post' |
189
|
|
|
], [ |
190
|
|
|
"input" => [ |
191
|
|
|
"username" => [ |
192
|
|
|
"type" => 'text', |
193
|
|
|
"maxlength" => 15, |
194
|
|
|
"minlength" => 5, |
195
|
|
|
"required" => false |
196
|
|
|
], |
197
|
|
|
] |
198
|
|
|
]); |
199
|
|
|
|
200
|
|
|
$form->fill([ |
201
|
|
|
"username" => 'jobs', # wrong because of minlength attr |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
$validator = new FormValidator($form, 'en'); |
205
|
|
|
$validator->validate(); |
206
|
|
|
|
207
|
|
|
# testing validation, no element has been declared as 'required' but is fille, so it'll be validated |
208
|
|
|
$this->assertNotTrue($validator->isValid()); |
209
|
|
|
|
210
|
|
|
$errors = $validator->getErrors(); |
211
|
|
|
$this->assertEquals(1, count($errors)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Tests form validation for n-dimensional arrays |
216
|
|
|
* |
217
|
|
|
* An n-dimensional array is created by PHP when the form control has a name like |
218
|
|
|
* name='product[]' |
219
|
|
|
* name='productdetail[0][]' |
220
|
|
|
* |
221
|
|
|
* @return null |
222
|
|
|
*/ |
223
|
|
|
public function testFormValidationWithNdimensionalArrays() |
224
|
|
|
{ |
225
|
|
|
$form = ElementFactory::create('FORM', [ |
226
|
|
|
"action" => 'someurl', |
227
|
|
|
"method" => 'post' |
228
|
|
|
], [ |
229
|
|
|
"input" => [ |
230
|
|
|
# in this case product name |
231
|
|
|
"product" => [ |
232
|
|
|
"type" => 'text', |
233
|
|
|
"maxlength" => 15, |
234
|
|
|
"minlength" => 5 |
235
|
|
|
], |
236
|
|
|
"price" => [ |
237
|
|
|
"type" => 'number', |
238
|
|
|
"min" => 1, |
239
|
|
|
] |
240
|
|
|
] |
241
|
|
|
]); |
242
|
|
|
|
243
|
|
|
$form->fill([ |
244
|
|
|
"product" => [ |
245
|
|
|
"optical mouse", |
246
|
|
|
"-", # wrong name |
247
|
|
|
"78" # another wrong name |
248
|
|
|
], |
249
|
|
|
"price" => [ |
250
|
|
|
10, |
251
|
|
|
0, # wrong price |
252
|
|
|
0 # another wrong price |
253
|
|
|
] |
254
|
|
|
]); |
255
|
|
|
|
256
|
|
|
$validator = new FormValidator($form, 'en'); |
257
|
|
|
$validator->validate(); |
258
|
|
|
|
259
|
|
|
# testing validation |
260
|
|
|
$this->assertNotTrue($validator->isValid()); |
261
|
|
|
|
262
|
|
|
$errors = $validator->getErrors(); |
263
|
|
|
|
264
|
|
|
# testing error labeled |
265
|
|
|
$this->assertArrayHasKey("product", $errors); |
266
|
|
|
$this->assertArrayHasKey("price", $errors); |
267
|
|
|
|
268
|
|
|
# testing error grouping |
269
|
|
|
$this->assertEquals(2, count($errors["product"])); |
270
|
|
|
$this->assertEquals(2, count($errors["price"])); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Tests for Zend validations |
275
|
|
|
* |
276
|
|
|
* @return null |
277
|
|
|
*/ |
278
|
|
|
public function testZendValidations() |
279
|
|
|
{ |
280
|
|
|
$form = ElementFactory::create('FORM', [ |
281
|
|
|
"action" => 'someurl', |
282
|
|
|
"method" => 'post' |
283
|
|
|
], [ |
284
|
|
|
"input" => [ |
285
|
|
|
"username" => [ |
286
|
|
|
"type" => 'text', |
287
|
|
|
"maxlength" => 15, |
288
|
|
|
"minlength" => 5, |
289
|
|
|
"data-validators" => [ |
290
|
|
|
"Alnum" => ["allowWhiteSpace" => false] |
291
|
|
|
] |
292
|
|
|
], |
293
|
|
|
"type" => [ |
294
|
|
|
"type" => 'text', |
295
|
|
|
"data-validators" => [ |
296
|
|
|
"InArray" => ["haystack" => ['admin', 'guest']] |
297
|
|
|
] |
298
|
|
|
], |
299
|
|
|
"password" => [ |
300
|
|
|
"type" => 'password', |
301
|
|
|
"maxlength" => 15, |
302
|
|
|
"minlength" => 5, |
303
|
|
|
] |
304
|
|
|
] |
305
|
|
|
]); |
306
|
|
|
|
307
|
|
|
$form->fill([ |
308
|
|
|
"username" => 'steave jobs', # wrong because of minlength attr |
309
|
|
|
"password" => 'jVi7Qp4X', |
310
|
|
|
"type" => 'moderator' # wrong because moderator is not a valid needle |
311
|
|
|
]); |
312
|
|
|
|
313
|
|
|
$validator = new FormValidator($form, 'en'); |
314
|
|
|
$validator->validate(); |
315
|
|
|
|
316
|
|
|
# testing validation |
317
|
|
|
$this->assertNotTrue($validator->isValid()); |
318
|
|
|
|
319
|
|
|
$errors = $validator->getErrors(); |
320
|
|
|
|
321
|
|
|
# testing error labeled |
322
|
|
|
$this->assertArrayHasKey("username", $errors); |
323
|
|
|
$this->assertArrayHasKey("type", $errors); |
324
|
|
|
|
325
|
|
|
# testing error grouping |
326
|
|
|
$this->assertEquals(1, count($errors["username"])); |
327
|
|
|
$this->assertEquals(1, count($errors["type"])); |
328
|
|
|
} |
329
|
|
|
} |