Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / dom-crawler / Tests / FormTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DomCrawler\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DomCrawler\Form;
16 use Symfony\Component\DomCrawler\FormFieldRegistry;
17
18 class FormTest extends TestCase
19 {
20     public static function setUpBeforeClass()
21     {
22         // Ensure that the private helper class FormFieldRegistry is loaded
23         class_exists('Symfony\\Component\\DomCrawler\\Form');
24     }
25
26     public function testConstructorThrowsExceptionIfTheNodeHasNoFormAncestor()
27     {
28         $dom = new \DOMDocument();
29         $dom->loadHTML('
30             <html>
31                 <input type="submit" />
32                 <form>
33                     <input type="foo" />
34                 </form>
35                 <button />
36             </html>
37         ');
38
39         $nodes = $dom->getElementsByTagName('input');
40
41         try {
42             $form = new Form($nodes->item(0), 'http://example.com');
43             $this->fail('__construct() throws a \\LogicException if the node has no form ancestor');
44         } catch (\LogicException $e) {
45             $this->assertTrue(true, '__construct() throws a \\LogicException if the node has no form ancestor');
46         }
47
48         try {
49             $form = new Form($nodes->item(1), 'http://example.com');
50             $this->fail('__construct() throws a \\LogicException if the input type is not submit, button, or image');
51         } catch (\LogicException $e) {
52             $this->assertTrue(true, '__construct() throws a \\LogicException if the input type is not submit, button, or image');
53         }
54
55         $nodes = $dom->getElementsByTagName('button');
56
57         try {
58             $form = new Form($nodes->item(0), 'http://example.com');
59             $this->fail('__construct() throws a \\LogicException if the node has no form ancestor');
60         } catch (\LogicException $e) {
61             $this->assertTrue(true, '__construct() throws a \\LogicException if the node has no form ancestor');
62         }
63     }
64
65     /**
66      * __construct() should throw \\LogicException if the form attribute is invalid.
67      *
68      * @expectedException \LogicException
69      */
70     public function testConstructorThrowsExceptionIfNoRelatedForm()
71     {
72         $dom = new \DOMDocument();
73         $dom->loadHTML('
74             <html>
75                 <form id="bar">
76                     <input type="submit" form="nonexistent" />
77                 </form>
78                 <input type="text" form="nonexistent" />
79                 <button />
80             </html>
81         ');
82
83         $nodes = $dom->getElementsByTagName('input');
84
85         $form = new Form($nodes->item(0), 'http://example.com');
86         $form = new Form($nodes->item(1), 'http://example.com');
87     }
88
89     public function testConstructorLoadsOnlyFieldsOfTheRightForm()
90     {
91         $dom = $this->createTestMultipleForm();
92
93         $nodes = $dom->getElementsByTagName('form');
94         $buttonElements = $dom->getElementsByTagName('button');
95
96         $form = new Form($nodes->item(0), 'http://example.com');
97         $this->assertCount(3, $form->all());
98
99         $form = new Form($buttonElements->item(1), 'http://example.com');
100         $this->assertCount(5, $form->all());
101     }
102
103     public function testConstructorHandlesFormAttribute()
104     {
105         $dom = $this->createTestHtml5Form();
106
107         $inputElements = $dom->getElementsByTagName('input');
108         $buttonElements = $dom->getElementsByTagName('button');
109
110         // Tests if submit buttons are correctly assigned to forms
111         $form1 = new Form($buttonElements->item(1), 'http://example.com');
112         $this->assertSame($dom->getElementsByTagName('form')->item(0), $form1->getFormNode(), 'HTML5-compliant form attribute handled incorrectly');
113
114         $form1 = new Form($inputElements->item(3), 'http://example.com');
115         $this->assertSame($dom->getElementsByTagName('form')->item(0), $form1->getFormNode(), 'HTML5-compliant form attribute handled incorrectly');
116
117         $form2 = new Form($buttonElements->item(0), 'http://example.com');
118         $this->assertSame($dom->getElementsByTagName('form')->item(1), $form2->getFormNode(), 'HTML5-compliant form attribute handled incorrectly');
119     }
120
121     public function testConstructorHandlesFormValues()
122     {
123         $dom = $this->createTestHtml5Form();
124
125         $inputElements = $dom->getElementsByTagName('input');
126         $buttonElements = $dom->getElementsByTagName('button');
127
128         $form1 = new Form($inputElements->item(3), 'http://example.com');
129         $form2 = new Form($buttonElements->item(0), 'http://example.com');
130
131         // Tests if form values are correctly assigned to forms
132         $values1 = array(
133             'apples' => array('1', '2'),
134             'form_name' => 'form-1',
135             'button_1' => 'Capture fields',
136             'outer_field' => 'success',
137         );
138         $values2 = array(
139             'oranges' => array('1', '2', '3'),
140             'form_name' => 'form_2',
141             'button_2' => '',
142             'app_frontend_form_type_contact_form_type' => array('contactType' => '', 'firstName' => 'John'),
143         );
144
145         $this->assertEquals($values1, $form1->getPhpValues(), 'HTML5-compliant form attribute handled incorrectly');
146         $this->assertEquals($values2, $form2->getPhpValues(), 'HTML5-compliant form attribute handled incorrectly');
147     }
148
149     public function testMultiValuedFields()
150     {
151         $form = $this->createForm('<form>
152             <input type="text" name="foo[4]" value="foo" disabled="disabled" />
153             <input type="text" name="foo" value="foo" disabled="disabled" />
154             <input type="text" name="foo[2]" value="foo" disabled="disabled" />
155             <input type="text" name="foo[]" value="foo" disabled="disabled" />
156             <input type="text" name="bar[foo][]" value="foo" disabled="disabled" />
157             <input type="text" name="bar[foo][foobar]" value="foo" disabled="disabled" />
158             <input type="submit" />
159         </form>
160         ');
161
162         $this->assertEquals(
163             array_keys($form->all()),
164             array('foo[2]', 'foo[3]', 'bar[foo][0]', 'bar[foo][foobar]')
165         );
166
167         $this->assertEquals($form->get('foo[2]')->getValue(), 'foo');
168         $this->assertEquals($form->get('foo[3]')->getValue(), 'foo');
169         $this->assertEquals($form->get('bar[foo][0]')->getValue(), 'foo');
170         $this->assertEquals($form->get('bar[foo][foobar]')->getValue(), 'foo');
171
172         $form['foo[2]'] = 'bar';
173         $form['foo[3]'] = 'bar';
174
175         $this->assertEquals($form->get('foo[2]')->getValue(), 'bar');
176         $this->assertEquals($form->get('foo[3]')->getValue(), 'bar');
177
178         $form['bar'] = array('foo' => array('0' => 'bar', 'foobar' => 'foobar'));
179
180         $this->assertEquals($form->get('bar[foo][0]')->getValue(), 'bar');
181         $this->assertEquals($form->get('bar[foo][foobar]')->getValue(), 'foobar');
182     }
183
184     /**
185      * @dataProvider provideInitializeValues
186      */
187     public function testConstructor($message, $form, $values)
188     {
189         $form = $this->createForm('<form>'.$form.'</form>');
190         $this->assertEquals(
191             $values,
192             array_map(
193                 function ($field) {
194                     $class = get_class($field);
195
196                     return array(substr($class, strrpos($class, '\\') + 1), $field->getValue());
197                 },
198                 $form->all()
199             ),
200             '->getDefaultValues() '.$message
201         );
202     }
203
204     public function provideInitializeValues()
205     {
206         return array(
207             array(
208                 'does not take into account input fields without a name attribute',
209                 '<input type="text" value="foo" />
210                  <input type="submit" />',
211                 array(),
212             ),
213             array(
214                 'does not take into account input fields with an empty name attribute value',
215                 '<input type="text" name="" value="foo" />
216                  <input type="submit" />',
217                 array(),
218             ),
219             array(
220                 'takes into account disabled input fields',
221                 '<input type="text" name="foo" value="foo" disabled="disabled" />
222                  <input type="submit" />',
223                 array('foo' => array('InputFormField', 'foo')),
224             ),
225             array(
226                 'appends the submitted button value',
227                 '<input type="submit" name="bar" value="bar" />',
228                 array('bar' => array('InputFormField', 'bar')),
229             ),
230             array(
231                 'appends the submitted button value for Button element',
232                 '<button type="submit" name="bar" value="bar">Bar</button>',
233                 array('bar' => array('InputFormField', 'bar')),
234             ),
235             array(
236                 'appends the submitted button value but not other submit buttons',
237                 '<input type="submit" name="bar" value="bar" />
238                  <input type="submit" name="foobar" value="foobar" />',
239                  array('foobar' => array('InputFormField', 'foobar')),
240             ),
241             array(
242                 'turns an image input into x and y fields',
243                 '<input type="image" name="bar" />',
244                 array('bar.x' => array('InputFormField', '0'), 'bar.y' => array('InputFormField', '0')),
245             ),
246             array(
247                 'returns textareas',
248                 '<textarea name="foo">foo</textarea>
249                  <input type="submit" />',
250                  array('foo' => array('TextareaFormField', 'foo')),
251             ),
252             array(
253                 'returns inputs',
254                 '<input type="text" name="foo" value="foo" />
255                  <input type="submit" />',
256                  array('foo' => array('InputFormField', 'foo')),
257             ),
258             array(
259                 'returns checkboxes',
260                 '<input type="checkbox" name="foo" value="foo" checked="checked" />
261                  <input type="submit" />',
262                  array('foo' => array('ChoiceFormField', 'foo')),
263             ),
264             array(
265                 'returns not-checked checkboxes',
266                 '<input type="checkbox" name="foo" value="foo" />
267                  <input type="submit" />',
268                  array('foo' => array('ChoiceFormField', false)),
269             ),
270             array(
271                 'returns radio buttons',
272                 '<input type="radio" name="foo" value="foo" />
273                  <input type="radio" name="foo" value="bar" checked="bar" />
274                  <input type="submit" />',
275                  array('foo' => array('ChoiceFormField', 'bar')),
276             ),
277             array(
278                 'returns file inputs',
279                 '<input type="file" name="foo" />
280                  <input type="submit" />',
281                  array('foo' => array('FileFormField', array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))),
282             ),
283         );
284     }
285
286     public function testGetFormNode()
287     {
288         $dom = new \DOMDocument();
289         $dom->loadHTML('<html><form><input type="submit" /></form></html>');
290
291         $form = new Form($dom->getElementsByTagName('input')->item(0), 'http://example.com');
292
293         $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
294     }
295
296     public function testGetFormNodeFromNamedForm()
297     {
298         $dom = new \DOMDocument();
299         $dom->loadHTML('<html><form name="my_form"><input type="submit" /></form></html>');
300
301         $form = new Form($dom->getElementsByTagName('form')->item(0), 'http://example.com');
302
303         $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
304     }
305
306     public function testGetMethod()
307     {
308         $form = $this->createForm('<form><input type="submit" /></form>');
309         $this->assertEquals('GET', $form->getMethod(), '->getMethod() returns get if no method is defined');
310
311         $form = $this->createForm('<form method="post"><input type="submit" /></form>');
312         $this->assertEquals('POST', $form->getMethod(), '->getMethod() returns the method attribute value of the form');
313
314         $form = $this->createForm('<form method="post"><input type="submit" /></form>', 'put');
315         $this->assertEquals('PUT', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
316
317         $form = $this->createForm('<form method="post"><input type="submit" /></form>', 'delete');
318         $this->assertEquals('DELETE', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
319
320         $form = $this->createForm('<form method="post"><input type="submit" /></form>', 'patch');
321         $this->assertEquals('PATCH', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
322     }
323
324     public function testGetMethodWithOverride()
325     {
326         $form = $this->createForm('<form method="get"><input type="submit" formmethod="post" /></form>');
327         $this->assertEquals('POST', $form->getMethod(), '->getMethod() returns the method attribute value of the form');
328     }
329
330     public function testGetSetValue()
331     {
332         $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
333
334         $this->assertEquals('foo', $form['foo']->getValue(), '->offsetGet() returns the value of a form field');
335
336         $form['foo'] = 'bar';
337
338         $this->assertEquals('bar', $form['foo']->getValue(), '->offsetSet() changes the value of a form field');
339
340         try {
341             $form['foobar'] = 'bar';
342             $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
343         } catch (\InvalidArgumentException $e) {
344             $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
345         }
346
347         try {
348             $form['foobar'];
349             $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
350         } catch (\InvalidArgumentException $e) {
351             $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
352         }
353     }
354
355     public function testDisableValidation()
356     {
357         $form = $this->createForm('<form>
358             <select name="foo[bar]">
359                 <option value="bar">bar</option>
360             </select>
361             <select name="foo[baz]">
362                 <option value="foo">foo</option>
363             </select>
364             <input type="submit" />
365         </form>');
366
367         $form->disableValidation();
368
369         $form['foo[bar]']->select('foo');
370         $form['foo[baz]']->select('bar');
371         $this->assertEquals('foo', $form['foo[bar]']->getValue(), '->disableValidation() disables validation of all ChoiceFormField.');
372         $this->assertEquals('bar', $form['foo[baz]']->getValue(), '->disableValidation() disables validation of all ChoiceFormField.');
373     }
374
375     public function testOffsetUnset()
376     {
377         $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
378         unset($form['foo']);
379         $this->assertArrayNotHasKey('foo', $form, '->offsetUnset() removes a field');
380     }
381
382     public function testOffsetExists()
383     {
384         $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
385
386         $this->assertArrayHasKey('foo', $form, '->offsetExists() return true if the field exists');
387         $this->assertArrayNotHasKey('bar', $form, '->offsetExists() return false if the field does not exist');
388     }
389
390     public function testGetValues()
391     {
392         $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><select multiple="multiple" name="baz[]"></select><input type="submit" /></form>');
393         $this->assertEquals(array('foo[bar]' => 'foo', 'bar' => 'bar', 'baz' => array()), $form->getValues(), '->getValues() returns all form field values');
394
395         $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
396         $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include not-checked checkboxes');
397
398         $form = $this->createForm('<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
399         $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include file input fields');
400
401         $form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
402         $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
403     }
404
405     public function testSetValues()
406     {
407         $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" checked="checked" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
408         $form->setValues(array('foo' => false, 'bar' => 'foo'));
409         $this->assertEquals(array('bar' => 'foo'), $form->getValues(), '->setValues() sets the values of fields');
410     }
411
412     public function testMultiselectSetValues()
413     {
414         $form = $this->createForm('<form><select multiple="multiple" name="multi"><option value="foo">foo</option><option value="bar">bar</option></select><input type="submit" /></form>');
415         $form->setValues(array('multi' => array('foo', 'bar')));
416         $this->assertEquals(array('multi' => array('foo', 'bar')), $form->getValues(), '->setValue() sets the values of select');
417     }
418
419     public function testGetPhpValues()
420     {
421         $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
422         $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');
423
424         $form = $this->createForm('<form><input type="text" name="fo.o[ba.r]" value="foo" /><input type="text" name="ba r" value="bar" /><input type="submit" /></form>');
425         $this->assertEquals(array('fo.o' => array('ba.r' => 'foo'), 'ba r' => 'bar'), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names');
426
427         $form = $this->createForm('<form><input type="text" name="fo.o[ba.r][]" value="foo" /><input type="text" name="fo.o[ba.r][ba.z]" value="bar" /><input type="submit" /></form>');
428         $this->assertEquals(array('fo.o' => array('ba.r' => array('foo', 'ba.z' => 'bar'))), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names recursively');
429
430         $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><select multiple="multiple" name="baz[]"></select><input type="submit" /></form>');
431         $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), "->getPhpValues() doesn't return empty values");
432     }
433
434     public function testGetFiles()
435     {
436         $form = $this->createForm('<form><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
437         $this->assertEquals(array(), $form->getFiles(), '->getFiles() returns an empty array if method is get');
438
439         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
440         $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for POST');
441
442         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'put');
443         $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PUT');
444
445         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'delete');
446         $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for DELETE');
447
448         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'patch');
449         $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PATCH');
450
451         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
452         $this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
453     }
454
455     public function testGetPhpFiles()
456     {
457         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
458         $this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');
459
460         $form = $this->createForm('<form method="post"><input type="file" name="f.o o[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
461         $this->assertEquals(array('f.o o' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() preserves periods and spaces in names');
462
463         $form = $this->createForm('<form method="post"><input type="file" name="f.o o[bar][ba.z]" /><input type="file" name="f.o o[bar][]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
464         $this->assertEquals(array('f.o o' => array('bar' => array('ba.z' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0), array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)))), $form->getPhpFiles(), '->getPhpFiles() preserves periods and spaces in names recursively');
465
466         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
467         $files = $form->getPhpFiles();
468
469         $this->assertSame(0, $files['foo']['bar']['size'], '->getPhpFiles() converts size to int');
470         $this->assertSame(4, $files['foo']['bar']['error'], '->getPhpFiles() converts error to int');
471
472         $form = $this->createForm('<form method="post"><input type="file" name="size[error]" /><input type="text" name="error" value="error" /><input type="submit" /></form>');
473         $this->assertEquals(array('size' => array('error' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() int conversion does not collide with file names');
474     }
475
476     /**
477      * @dataProvider provideGetUriValues
478      */
479     public function testGetUri($message, $form, $values, $uri, $method = null)
480     {
481         $form = $this->createForm($form, $method);
482         $form->setValues($values);
483
484         $this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
485     }
486
487     public function testGetBaseUri()
488     {
489         $dom = new \DOMDocument();
490         $dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
491
492         $nodes = $dom->getElementsByTagName('input');
493         $form = new Form($nodes->item($nodes->length - 1), 'http://www.foo.com/');
494         $this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
495     }
496
497     public function testGetUriWithAnchor()
498     {
499         $form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com/id/123');
500
501         $this->assertEquals('http://example.com/id/123#foo', $form->getUri());
502     }
503
504     public function testGetUriActionAbsolute()
505     {
506         $formHtml = '<form id="login_form" action="https://login.foo.com/login.php?login_attempt=1" method="POST"><input type="text" name="foo" value="foo" /><input type="submit" /></form>';
507
508         $form = $this->createForm($formHtml);
509         $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
510
511         $form = $this->createForm($formHtml, null, 'https://login.foo.com');
512         $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
513
514         $form = $this->createForm($formHtml, null, 'https://login.foo.com/bar/');
515         $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
516
517         // The action URI haven't the same domain Host have an another domain as Host
518         $form = $this->createForm($formHtml, null, 'https://www.foo.com');
519         $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
520
521         $form = $this->createForm($formHtml, null, 'https://www.foo.com/bar/');
522         $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
523     }
524
525     public function testGetUriAbsolute()
526     {
527         $form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
528         $this->assertEquals('http://localhost/foo/foo', $form->getUri(), '->getUri() returns absolute URIs');
529
530         $form = $this->createForm('<form action="/foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
531         $this->assertEquals('http://localhost/foo', $form->getUri(), '->getUri() returns absolute URIs');
532     }
533
534     public function testGetUriWithOnlyQueryString()
535     {
536         $form = $this->createForm('<form action="?get=param"><input type="submit" /></form>', null, 'http://localhost/foo/bar');
537         $this->assertEquals('http://localhost/foo/bar?get=param', $form->getUri(), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
538     }
539
540     public function testGetUriWithoutAction()
541     {
542         $form = $this->createForm('<form><input type="submit" /></form>', null, 'http://localhost/foo/bar');
543         $this->assertEquals('http://localhost/foo/bar', $form->getUri(), '->getUri() returns path if no action defined');
544     }
545
546     public function testGetUriWithActionOverride()
547     {
548         $form = $this->createForm('<form action="/foo"><button type="submit" formaction="/bar" /></form>', null, 'http://localhost/foo/');
549         $this->assertEquals('http://localhost/bar', $form->getUri(), '->getUri() returns absolute URIs');
550     }
551
552     public function provideGetUriValues()
553     {
554         return array(
555             array(
556                 'returns the URI of the form',
557                 '<form action="/foo"><input type="submit" /></form>',
558                 array(),
559                 '/foo',
560             ),
561             array(
562                 'appends the form values if the method is get',
563                 '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
564                 array(),
565                 '/foo?foo=foo',
566             ),
567             array(
568                 'appends the form values and merges the submitted values',
569                 '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
570                 array('foo' => 'bar'),
571                 '/foo?foo=bar',
572             ),
573             array(
574                 'does not append values if the method is post',
575                 '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
576                 array(),
577                 '/foo',
578             ),
579             array(
580                 'does not append values if the method is patch',
581                 '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
582                 array(),
583                 '/foo',
584                 'PUT',
585             ),
586             array(
587                 'does not append values if the method is delete',
588                 '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
589                 array(),
590                 '/foo',
591                 'DELETE',
592             ),
593             array(
594                 'does not append values if the method is put',
595                 '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
596                 array(),
597                 '/foo',
598                 'PATCH',
599             ),
600             array(
601                 'appends the form values to an existing query string',
602                 '<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
603                 array(),
604                 '/foo?bar=bar&foo=foo',
605             ),
606             array(
607                 'replaces query values with the form values',
608                 '<form action="/foo?bar=bar"><input type="text" name="bar" value="foo" /><input type="submit" /></form>',
609                 array(),
610                 '/foo?bar=foo',
611             ),
612             array(
613                 'returns an empty URI if the action is empty',
614                 '<form><input type="submit" /></form>',
615                 array(),
616                 '/',
617             ),
618             array(
619                 'appends the form values even if the action is empty',
620                 '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
621                 array(),
622                 '/?foo=foo',
623             ),
624             array(
625                 'chooses the path if the action attribute value is a sharp (#)',
626                 '<form action="#" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
627                 array(),
628                 '/#',
629             ),
630         );
631     }
632
633     public function testHas()
634     {
635         $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
636
637         $this->assertFalse($form->has('foo'), '->has() returns false if a field is not in the form');
638         $this->assertTrue($form->has('bar'), '->has() returns true if a field is in the form');
639     }
640
641     public function testRemove()
642     {
643         $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
644         $form->remove('bar');
645         $this->assertFalse($form->has('bar'), '->remove() removes a field');
646     }
647
648     public function testGet()
649     {
650         $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
651
652         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Field\\InputFormField', $form->get('bar'), '->get() returns the field object associated with the given name');
653
654         try {
655             $form->get('foo');
656             $this->fail('->get() throws an \InvalidArgumentException if the field does not exist');
657         } catch (\InvalidArgumentException $e) {
658             $this->assertTrue(true, '->get() throws an \InvalidArgumentException if the field does not exist');
659         }
660     }
661
662     public function testAll()
663     {
664         $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
665
666         $fields = $form->all();
667         $this->assertCount(1, $fields, '->all() return an array of form field objects');
668         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Field\\InputFormField', $fields['bar'], '->all() return an array of form field objects');
669     }
670
671     public function testSubmitWithoutAFormButton()
672     {
673         $dom = new \DOMDocument();
674         $dom->loadHTML('
675             <html>
676                 <form>
677                     <input type="foo" />
678                 </form>
679             </html>
680         ');
681
682         $nodes = $dom->getElementsByTagName('form');
683         $form = new Form($nodes->item(0), 'http://example.com');
684         $this->assertSame($nodes->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
685     }
686
687     public function testTypeAttributeIsCaseInsensitive()
688     {
689         $form = $this->createForm('<form method="post"><input type="IMAGE" name="example" /></form>');
690         $this->assertTrue($form->has('example.x'), '->has() returns true if the image input was correctly turned into an x and a y fields');
691         $this->assertTrue($form->has('example.y'), '->has() returns true if the image input was correctly turned into an x and a y fields');
692     }
693
694     public function testFormFieldRegistryAcceptAnyNames()
695     {
696         $field = $this->getFormFieldMock('[t:dbt%3adate;]data_daterange_enddate_value');
697
698         $registry = new FormFieldRegistry();
699         $registry->add($field);
700         $this->assertEquals($field, $registry->get('[t:dbt%3adate;]data_daterange_enddate_value'));
701         $registry->set('[t:dbt%3adate;]data_daterange_enddate_value', null);
702
703         $form = $this->createForm('<form><input type="text" name="[t:dbt%3adate;]data_daterange_enddate_value" value="bar" /><input type="submit" /></form>');
704         $form['[t:dbt%3adate;]data_daterange_enddate_value'] = 'bar';
705
706         $registry->remove('[t:dbt%3adate;]data_daterange_enddate_value');
707     }
708
709     /**
710      * @expectedException \InvalidArgumentException
711      */
712     public function testFormFieldRegistryGetThrowAnExceptionWhenTheFieldDoesNotExist()
713     {
714         $registry = new FormFieldRegistry();
715         $registry->get('foo');
716     }
717
718     /**
719      * @expectedException \InvalidArgumentException
720      */
721     public function testFormFieldRegistrySetThrowAnExceptionWhenTheFieldDoesNotExist()
722     {
723         $registry = new FormFieldRegistry();
724         $registry->set('foo', null);
725     }
726
727     public function testFormFieldRegistryHasReturnsTrueWhenTheFQNExists()
728     {
729         $registry = new FormFieldRegistry();
730         $registry->add($this->getFormFieldMock('foo[bar]'));
731
732         $this->assertTrue($registry->has('foo'));
733         $this->assertTrue($registry->has('foo[bar]'));
734         $this->assertFalse($registry->has('bar'));
735         $this->assertFalse($registry->has('foo[foo]'));
736     }
737
738     public function testFormRegistryFieldsCanBeRemoved()
739     {
740         $registry = new FormFieldRegistry();
741         $registry->add($this->getFormFieldMock('foo'));
742         $registry->remove('foo');
743         $this->assertFalse($registry->has('foo'));
744     }
745
746     public function testFormRegistrySupportsMultivaluedFields()
747     {
748         $registry = new FormFieldRegistry();
749         $registry->add($this->getFormFieldMock('foo[]'));
750         $registry->add($this->getFormFieldMock('foo[]'));
751         $registry->add($this->getFormFieldMock('bar[5]'));
752         $registry->add($this->getFormFieldMock('bar[]'));
753         $registry->add($this->getFormFieldMock('bar[baz]'));
754
755         $this->assertEquals(
756             array('foo[0]', 'foo[1]', 'bar[5]', 'bar[6]', 'bar[baz]'),
757             array_keys($registry->all())
758         );
759     }
760
761     public function testFormRegistrySetValues()
762     {
763         $registry = new FormFieldRegistry();
764         $registry->add($f2 = $this->getFormFieldMock('foo[2]'));
765         $registry->add($f3 = $this->getFormFieldMock('foo[3]'));
766         $registry->add($fbb = $this->getFormFieldMock('foo[bar][baz]'));
767
768         $f2
769             ->expects($this->exactly(2))
770             ->method('setValue')
771             ->with(2)
772         ;
773
774         $f3
775             ->expects($this->exactly(2))
776             ->method('setValue')
777             ->with(3)
778         ;
779
780         $fbb
781             ->expects($this->exactly(2))
782             ->method('setValue')
783             ->with('fbb')
784         ;
785
786         $registry->set('foo[2]', 2);
787         $registry->set('foo[3]', 3);
788         $registry->set('foo[bar][baz]', 'fbb');
789
790         $registry->set('foo', array(
791             2 => 2,
792             3 => 3,
793             'bar' => array(
794                 'baz' => 'fbb',
795              ),
796         ));
797     }
798
799     /**
800      * @expectedException \InvalidArgumentException
801      * @expectedExceptionMessage Cannot set value on a compound field "foo[bar]".
802      */
803     public function testFormRegistrySetValueOnCompoundField()
804     {
805         $registry = new FormFieldRegistry();
806         $registry->add($this->getFormFieldMock('foo[bar][baz]'));
807
808         $registry->set('foo[bar]', 'fbb');
809     }
810
811     /**
812      * @expectedException \InvalidArgumentException
813      * @expectedExceptionMessage Unreachable field "0"
814      */
815     public function testFormRegistrySetArrayOnNotCompoundField()
816     {
817         $registry = new FormFieldRegistry();
818         $registry->add($this->getFormFieldMock('bar'));
819
820         $registry->set('bar', array('baz'));
821     }
822
823     public function testDifferentFieldTypesWithSameName()
824     {
825         $dom = new \DOMDocument();
826         $dom->loadHTML('
827             <html>
828                 <body>
829                     <form action="/">
830                         <input type="hidden" name="option" value="default">
831                         <input type="radio" name="option" value="A">
832                         <input type="radio" name="option" value="B">
833                         <input type="hidden" name="settings[1]" value="0">
834                         <input type="checkbox" name="settings[1]" value="1" id="setting-1">
835                         <button>klickme</button>
836                     </form>
837                 </body>
838             </html>
839         ');
840         $form = new Form($dom->getElementsByTagName('form')->item(0), 'http://example.com');
841
842         $this->assertInstanceOf('Symfony\Component\DomCrawler\Field\ChoiceFormField', $form->get('option'));
843     }
844
845     protected function getFormFieldMock($name, $value = null)
846     {
847         $field = $this
848             ->getMockBuilder('Symfony\\Component\\DomCrawler\\Field\\FormField')
849             ->setMethods(array('getName', 'getValue', 'setValue', 'initialize'))
850             ->disableOriginalConstructor()
851             ->getMock()
852         ;
853
854         $field
855             ->expects($this->any())
856             ->method('getName')
857             ->will($this->returnValue($name))
858         ;
859
860         $field
861             ->expects($this->any())
862             ->method('getValue')
863             ->will($this->returnValue($value))
864         ;
865
866         return $field;
867     }
868
869     protected function createForm($form, $method = null, $currentUri = null)
870     {
871         $dom = new \DOMDocument();
872         $dom->loadHTML('<html>'.$form.'</html>');
873
874         $xPath = new \DOMXPath($dom);
875         $nodes = $xPath->query('//input | //button');
876
877         if (null === $currentUri) {
878             $currentUri = 'http://example.com/';
879         }
880
881         return new Form($nodes->item($nodes->length - 1), $currentUri, $method);
882     }
883
884     protected function createTestHtml5Form()
885     {
886         $dom = new \DOMDocument();
887         $dom->loadHTML('
888         <html>
889             <h1>Hello form</h1>
890             <form id="form-1" action="" method="POST">
891                 <div><input type="checkbox" name="apples[]" value="1" checked /></div>
892                 <input form="form_2" type="checkbox" name="oranges[]" value="1" checked />
893                 <div><label></label><input form="form-1" type="hidden" name="form_name" value="form-1" /></div>
894                 <input form="form-1" type="submit" name="button_1" value="Capture fields" />
895                 <button form="form_2" type="submit" name="button_2">Submit form_2</button>
896             </form>
897             <input form="form-1" type="checkbox" name="apples[]" value="2" checked />
898             <form id="form_2" action="" method="POST">
899                 <div><div><input type="checkbox" name="oranges[]" value="2" checked />
900                 <input type="checkbox" name="oranges[]" value="3" checked /></div></div>
901                 <input form="form_2" type="hidden" name="form_name" value="form_2" />
902                 <input form="form-1" type="hidden" name="outer_field" value="success" />
903                 <button form="form-1" type="submit" name="button_3">Submit from outside the form</button>
904                 <div>
905                     <label for="app_frontend_form_type_contact_form_type_contactType">Message subject</label>
906                     <div>
907                         <select name="app_frontend_form_type_contact_form_type[contactType]" id="app_frontend_form_type_contact_form_type_contactType"><option selected="selected" value="">Please select subject</option><option id="1">Test type</option></select>
908                     </div>
909                 </div>
910                 <div>
911                     <label for="app_frontend_form_type_contact_form_type_firstName">Firstname</label>
912                     <input type="text" name="app_frontend_form_type_contact_form_type[firstName]" value="John" id="app_frontend_form_type_contact_form_type_firstName"/>
913                 </div>
914             </form>
915             <button />
916         </html>');
917
918         return $dom;
919     }
920
921     protected function createTestMultipleForm()
922     {
923         $dom = new \DOMDocument();
924         $dom->loadHTML('
925         <html>
926             <h1>Hello form</h1>
927             <form action="" method="POST">
928                 <div><input type="checkbox" name="apples[]" value="1" checked /></div>
929                 <input type="checkbox" name="oranges[]" value="1" checked />
930                 <div><label></label><input type="hidden" name="form_name" value="form-1" /></div>
931                 <input type="submit" name="button_1" value="Capture fields" />
932                 <button type="submit" name="button_2">Submit form_2</button>
933             </form>
934             <form action="" method="POST">
935                 <div><div><input type="checkbox" name="oranges[]" value="2" checked />
936                 <input type="checkbox" name="oranges[]" value="3" checked /></div></div>
937                 <input type="hidden" name="form_name" value="form_2" />
938                 <input type="hidden" name="outer_field" value="success" />
939                 <button type="submit" name="button_3">Submit from outside the form</button>
940             </form>
941             <button />
942         </html>');
943
944         return $dom;
945     }
946
947     public function testgetPhpValuesWithEmptyTextarea()
948     {
949         $dom = new \DOMDocument();
950         $dom->loadHTML('
951               <html>
952                   <form>
953                       <textarea name="example"></textarea>
954                   </form>
955               </html>
956           ');
957
958         $nodes = $dom->getElementsByTagName('form');
959         $form = new Form($nodes->item(0), 'http://example.com');
960         $this->assertEquals($form->getPhpValues(), array('example' => ''));
961     }
962 }