/
FormTest.php
417 lines (332 loc) · 12.3 KB
/
FormTest.php
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
<?php
namespace Symfony\Tests\Components\Form;
require_once __DIR__ . '/Fixtures/Author.php';
require_once __DIR__ . '/Fixtures/TestField.php';
use Symfony\Components\Form\Form;
use Symfony\Components\Form\Field;
use Symfony\Components\Form\HiddenField;
use Symfony\Components\Form\FieldGroup;
use Symfony\Components\Form\HtmlGenerator;
use Symfony\Components\Form\PropertyPath;
use Symfony\Components\File\UploadedFile;
use Symfony\Components\Validator\ConstraintViolation;
use Symfony\Components\Validator\ConstraintViolationList;
use Symfony\Tests\Components\Form\Fixtures\Author;
use Symfony\Tests\Components\Form\Fixtures\TestField;
class FormTest_PreconfiguredForm extends Form
{
protected function configure()
{
$this->add(new Field('firstName'));
}
}
class TestSetDataBeforeConfigureForm extends Form
{
protected $testCase;
protected $object;
public function __construct($testCase, $name, $object, $validator)
{
$this->testCase = $testCase;
$this->object = $object;
parent::__construct($name, $object, $validator);
}
protected function configure()
{
$this->testCase->assertEquals($this->object, $this->getData());
}
}
class FormTest extends \PHPUnit_Framework_TestCase
{
protected $validator;
protected $form;
protected function setUp()
{
Form::disableDefaultCsrfProtection();
Form::setDefaultCsrfSecret(null);
$this->validator = $this->createMockValidator();
$this->form = new Form('author', new Author(), $this->validator);
}
public function testConstructInitializesObject()
{
$this->assertEquals(new Author(), $this->form->getData());
}
public function testSetDataBeforeConfigure()
{
new TestSetDataBeforeConfigureForm($this, 'author', new Author(), $this->validator);
}
public function testIsCsrfProtected()
{
$this->assertFalse($this->form->isCsrfProtected());
$this->form->enableCsrfProtection();
$this->assertTrue($this->form->isCsrfProtected());
$this->form->disableCsrfProtection();
$this->assertFalse($this->form->isCsrfProtected());
}
public function testNoCsrfProtectionByDefault()
{
$form = new Form('author', new Author(), $this->validator);
$this->assertFalse($form->isCsrfProtected());
}
public function testDefaultCsrfProtectionCanBeEnabled()
{
Form::enableDefaultCsrfProtection();
$form = new Form('author', new Author(), $this->validator);
$this->assertTrue($form->isCsrfProtected());
}
public function testGeneratedCsrfSecretByDefault()
{
$form = new Form('author', new Author(), $this->validator);
$this->assertTrue(strlen($form->getCsrfSecret()) >= 32);
}
public function testDefaultCsrfSecretCanBeSet()
{
Form::setDefaultCsrfSecret('foobar');
$form = new Form('author', new Author(), $this->validator);
$this->assertEquals('foobar', $form->getCsrfSecret());
}
public function testDefaultCsrfFieldNameCanBeSet()
{
Form::setDefaultCsrfFieldName('foobar');
$form = new Form('author', new Author(), $this->validator);
$this->assertEquals('foobar', $form->getCsrfFieldName());
}
public function testCsrfProtectedFormsHaveExtraField()
{
$this->form->enableCsrfProtection();
$this->assertTrue($this->form->has($this->form->getCsrfFieldName()));
$field = $this->form->get($this->form->getCsrfFieldName());
$this->assertTrue($field instanceof HiddenField);
$this->assertGreaterThanOrEqual(32, strlen($field->getDisplayedData()));
}
public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
{
$this->form->bind(array());
$this->assertTrue($this->form->isCsrfTokenValid());
}
public function testIsCsrfTokenValidPasses()
{
$this->form->enableCsrfProtection();
$field = $this->form->getCsrfFieldName();
$token = $this->form->get($field)->getDisplayedData();
$this->form->bind(array($field => $token));
$this->assertTrue($this->form->isCsrfTokenValid());
}
public function testIsCsrfTokenValidFails()
{
$this->form->enableCsrfProtection();
$field = $this->form->getCsrfFieldName();
$this->form->bind(array($field => 'foobar'));
$this->assertFalse($this->form->isCsrfTokenValid());
}
public function testDefaultLocaleCanBeSet()
{
Form::setDefaultLocale('de-DE-1996');
$form = new Form('author', new Author(), $this->validator);
$field = $this->getMock('Symfony\Components\Form\Field', array(), array(), '', false, false);
$field->expects($this->any())
->method('getKey')
->will($this->returnValue('firstName'));
$field->expects($this->once())
->method('setLocale')
->with($this->equalTo('de-DE-1996'));
$form->add($field);
}
public function testDefaultTranslatorCanBeSet()
{
$translator = $this->getMock('Symfony\Components\I18N\TranslatorInterface');
Form::setDefaultTranslator($translator);
$form = new Form('author', new Author(), $this->validator);
$field = $this->getMock('Symfony\Components\Form\Field', array(), array(), '', false, false);
$field->expects($this->any())
->method('getKey')
->will($this->returnValue('firstName'));
$field->expects($this->once())
->method('setTranslator')
->with($this->equalTo($translator));
$form->add($field);
}
public function testValidationGroupsCanBeSet()
{
$form = new Form('author', new Author(), $this->validator);
$this->assertNull($form->getValidationGroups());
$form->setValidationGroups('group');
$this->assertEquals(array('group'), $form->getValidationGroups());
$form->setValidationGroups(array('group1', 'group2'));
$this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
$form->setValidationGroups(null);
$this->assertNull($form->getValidationGroups());
}
public function testBindUsesValidationGroups()
{
$field = $this->createMockField('firstName');
$form = new Form('author', new Author(), $this->validator);
$form->add($field);
$form->setValidationGroups('group');
$this->validator->expects($this->once())
->method('validate')
->with($this->equalTo($form), $this->equalTo(array('group')));
$form->bind(array()); // irrelevant
}
public function testBindConvertsUploadedFiles()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
$field = $this->createMockField('file');
$field->expects($this->once())
->method('bind')
->with($this->equalTo($file));
$form = new Form('author', new Author(), $this->validator);
$form->add($field);
// test
$form->bind(array(), array('file' => array(
'name' => basename($tmpFile),
'type' => 'text/plain',
'tmp_name' => $tmpFile,
'error' => 0,
'size' => 100
)));
}
public function testBindConvertsUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
$field = $this->createMockField('file');
$field->expects($this->once())
->method('bind')
->with($this->equalTo($file));
$form = new Form('author', new Author(), $this->validator);
$form->add($field);
// test
$form->bind(array(), array(
'name' => array(
'file' => basename($tmpFile),
),
'type' => array(
'file' => 'text/plain',
),
'tmp_name' => array(
'file' => $tmpFile,
),
'error' => array(
'file' => 0,
),
'size' => array(
'file' => 100,
),
));
}
public function testBindConvertsNestedUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
$file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
$group = $this->getMock(
'Symfony\Components\Form\FieldGroup',
array('bind'),
array('child', array('property_path' => null))
);
$group->expects($this->once())
->method('bind')
->with($this->equalTo(array('file' => $file)));
$form = new Form('author', new Author(), $this->validator);
$form->add($group);
// test
$form->bind(array(), array(
'name' => array(
'child' => array('file' => basename($tmpFile)),
),
'type' => array(
'child' => array('file' => 'text/plain'),
),
'tmp_name' => array(
'child' => array('file' => $tmpFile),
),
'error' => array(
'child' => array('file' => 0),
),
'size' => array(
'child' => array('file' => 100),
),
));
}
public function testMultipartFormsWithoutParentsRequireFiles()
{
$form = new Form('author', new Author(), $this->validator);
$form->add($this->createMultipartMockField('file'));
$this->setExpectedException('InvalidArgumentException');
// should be given in second argument
$form->bind(array('file' => 'test.txt'));
}
public function testMultipartFormsWithParentsRequireNoFiles()
{
$form = new Form('author', new Author(), $this->validator);
$form->add($this->createMultipartMockField('file'));
$form->setParent($this->createMockField('group'));
// files are expected to be converted by the parent
$form->bind(array('file' => 'test.txt'));
}
public function testRenderFormTagProducesValidXhtml()
{
$form = new Form('author', new Author(), $this->validator);
$this->assertEquals('<form action="url" method="post">', $form->renderFormTag('url'));
}
public function testSetCharsetAdjustsGenerator()
{
$form = $this->getMock(
'Symfony\Components\Form\Form',
array('setGenerator'),
array(),
'',
false // don't call original constructor
);
$form->expects($this->once())
->method('setGenerator')
->with($this->equalTo(new HtmlGenerator('iso-8859-1')));
$form->setCharset('iso-8859-1');
}
protected function createMockField($key)
{
$field = $this->getMock(
'Symfony\Components\Form\FieldInterface',
array(),
array(),
'',
false, // don't use constructor
false // don't call parent::__clone
);
$field->expects($this->any())
->method('getKey')
->will($this->returnValue($key));
return $field;
}
protected function createMockFieldGroup($key)
{
$field = $this->getMock(
'Symfony\Components\Form\FieldGroup',
array(),
array(),
'',
false, // don't use constructor
false // don't call parent::__clone
);
$field->expects($this->any())
->method('getKey')
->will($this->returnValue($key));
return $field;
}
protected function createMultipartMockField($key)
{
$field = $this->createMockField($key);
$field->expects($this->any())
->method('isMultipart')
->will($this->returnValue(true));
return $field;
}
protected function createTempFile()
{
return tempnam(sys_get_temp_dir(), 'FormTest');
}
protected function createMockValidator()
{
return $this->getMock('Symfony\Components\Validator\ValidatorInterface');
}
}