4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DomCrawler\Form;
16 use Symfony\Component\DomCrawler\FormFieldRegistry;
18 class FormTest extends TestCase
20 public static function setUpBeforeClass()
22 // Ensure that the private helper class FormFieldRegistry is loaded
23 class_exists('Symfony\\Component\\DomCrawler\\Form');
26 public function testConstructorThrowsExceptionIfTheNodeHasNoFormAncestor()
28 $dom = new \DOMDocument();
31 <input type="submit" />
39 $nodes = $dom->getElementsByTagName('input');
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');
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');
55 $nodes = $dom->getElementsByTagName('button');
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');
66 * __construct() should throw \\LogicException if the form attribute is invalid.
68 * @expectedException \LogicException
70 public function testConstructorThrowsExceptionIfNoRelatedForm()
72 $dom = new \DOMDocument();
76 <input type="submit" form="nonexistent" />
78 <input type="text" form="nonexistent" />
83 $nodes = $dom->getElementsByTagName('input');
85 $form = new Form($nodes->item(0), 'http://example.com');
86 $form = new Form($nodes->item(1), 'http://example.com');
89 public function testConstructorLoadsOnlyFieldsOfTheRightForm()
91 $dom = $this->createTestMultipleForm();
93 $nodes = $dom->getElementsByTagName('form');
94 $buttonElements = $dom->getElementsByTagName('button');
96 $form = new Form($nodes->item(0), 'http://example.com');
97 $this->assertCount(3, $form->all());
99 $form = new Form($buttonElements->item(1), 'http://example.com');
100 $this->assertCount(5, $form->all());
103 public function testConstructorHandlesFormAttribute()
105 $dom = $this->createTestHtml5Form();
107 $inputElements = $dom->getElementsByTagName('input');
108 $buttonElements = $dom->getElementsByTagName('button');
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');
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');
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');
121 public function testConstructorHandlesFormValues()
123 $dom = $this->createTestHtml5Form();
125 $inputElements = $dom->getElementsByTagName('input');
126 $buttonElements = $dom->getElementsByTagName('button');
128 $form1 = new Form($inputElements->item(3), 'http://example.com');
129 $form2 = new Form($buttonElements->item(0), 'http://example.com');
131 // Tests if form values are correctly assigned to forms
133 'apples' => array('1', '2'),
134 'form_name' => 'form-1',
135 'button_1' => 'Capture fields',
136 'outer_field' => 'success',
139 'oranges' => array('1', '2', '3'),
140 'form_name' => 'form_2',
142 'app_frontend_form_type_contact_form_type' => array('contactType' => '', 'firstName' => 'John'),
145 $this->assertEquals($values1, $form1->getPhpValues(), 'HTML5-compliant form attribute handled incorrectly');
146 $this->assertEquals($values2, $form2->getPhpValues(), 'HTML5-compliant form attribute handled incorrectly');
149 public function testMultiValuedFields()
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" />
163 array_keys($form->all()),
164 array('foo[2]', 'foo[3]', 'bar[foo][0]', 'bar[foo][foobar]')
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');
172 $form['foo[2]'] = 'bar';
173 $form['foo[3]'] = 'bar';
175 $this->assertEquals($form->get('foo[2]')->getValue(), 'bar');
176 $this->assertEquals($form->get('foo[3]')->getValue(), 'bar');
178 $form['bar'] = array('foo' => array('0' => 'bar', 'foobar' => 'foobar'));
180 $this->assertEquals($form->get('bar[foo][0]')->getValue(), 'bar');
181 $this->assertEquals($form->get('bar[foo][foobar]')->getValue(), 'foobar');
185 * @dataProvider provideInitializeValues
187 public function testConstructor($message, $form, $values)
189 $form = $this->createForm('<form>'.$form.'</form>');
194 $class = get_class($field);
196 return array(substr($class, strrpos($class, '\\') + 1), $field->getValue());
200 '->getDefaultValues() '.$message
204 public function provideInitializeValues()
208 'does not take into account input fields without a name attribute',
209 '<input type="text" value="foo" />
210 <input type="submit" />',
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" />',
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')),
226 'appends the submitted button value',
227 '<input type="submit" name="bar" value="bar" />',
228 array('bar' => array('InputFormField', 'bar')),
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')),
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')),
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')),
248 '<textarea name="foo">foo</textarea>
249 <input type="submit" />',
250 array('foo' => array('TextareaFormField', 'foo')),
254 '<input type="text" name="foo" value="foo" />
255 <input type="submit" />',
256 array('foo' => array('InputFormField', 'foo')),
259 'returns checkboxes',
260 '<input type="checkbox" name="foo" value="foo" checked="checked" />
261 <input type="submit" />',
262 array('foo' => array('ChoiceFormField', 'foo')),
265 'returns not-checked checkboxes',
266 '<input type="checkbox" name="foo" value="foo" />
267 <input type="submit" />',
268 array('foo' => array('ChoiceFormField', false)),
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')),
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))),
286 public function testGetFormNode()
288 $dom = new \DOMDocument();
289 $dom->loadHTML('<html><form><input type="submit" /></form></html>');
291 $form = new Form($dom->getElementsByTagName('input')->item(0), 'http://example.com');
293 $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
296 public function testGetFormNodeFromNamedForm()
298 $dom = new \DOMDocument();
299 $dom->loadHTML('<html><form name="my_form"><input type="submit" /></form></html>');
301 $form = new Form($dom->getElementsByTagName('form')->item(0), 'http://example.com');
303 $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
306 public function testGetMethod()
308 $form = $this->createForm('<form><input type="submit" /></form>');
309 $this->assertEquals('GET', $form->getMethod(), '->getMethod() returns get if no method is defined');
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');
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');
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');
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');
324 public function testGetSetValue()
326 $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
328 $this->assertEquals('foo', $form['foo']->getValue(), '->offsetGet() returns the value of a form field');
330 $form['foo'] = 'bar';
332 $this->assertEquals('bar', $form['foo']->getValue(), '->offsetSet() changes the value of a form field');
335 $form['foobar'] = 'bar';
336 $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
337 } catch (\InvalidArgumentException $e) {
338 $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
343 $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
344 } catch (\InvalidArgumentException $e) {
345 $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
349 public function testDisableValidation()
351 $form = $this->createForm('<form>
352 <select name="foo[bar]">
353 <option value="bar">bar</option>
355 <select name="foo[baz]">
356 <option value="foo">foo</option>
358 <input type="submit" />
361 $form->disableValidation();
363 $form['foo[bar]']->select('foo');
364 $form['foo[baz]']->select('bar');
365 $this->assertEquals('foo', $form['foo[bar]']->getValue(), '->disableValidation() disables validation of all ChoiceFormField.');
366 $this->assertEquals('bar', $form['foo[baz]']->getValue(), '->disableValidation() disables validation of all ChoiceFormField.');
369 public function testOffsetUnset()
371 $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
373 $this->assertFalse(isset($form['foo']), '->offsetUnset() removes a field');
376 public function testOffsetExists()
378 $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
380 $this->assertTrue(isset($form['foo']), '->offsetExists() return true if the field exists');
381 $this->assertFalse(isset($form['bar']), '->offsetExists() return false if the field does not exist');
384 public function testGetValues()
386 $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>');
387 $this->assertEquals(array('foo[bar]' => 'foo', 'bar' => 'bar', 'baz' => array()), $form->getValues(), '->getValues() returns all form field values');
389 $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
390 $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include not-checked checkboxes');
392 $form = $this->createForm('<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
393 $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include file input fields');
395 $form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
396 $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
399 public function testSetValues()
401 $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" checked="checked" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
402 $form->setValues(array('foo' => false, 'bar' => 'foo'));
403 $this->assertEquals(array('bar' => 'foo'), $form->getValues(), '->setValues() sets the values of fields');
406 public function testMultiselectSetValues()
408 $form = $this->createForm('<form><select multiple="multiple" name="multi"><option value="foo">foo</option><option value="bar">bar</option></select><input type="submit" /></form>');
409 $form->setValues(array('multi' => array('foo', 'bar')));
410 $this->assertEquals(array('multi' => array('foo', 'bar')), $form->getValues(), '->setValue() sets the values of select');
413 public function testGetPhpValues()
415 $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
416 $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');
418 $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>');
419 $this->assertEquals(array('fo.o' => array('ba.r' => 'foo'), 'ba r' => 'bar'), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names');
421 $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>');
422 $this->assertEquals(array('fo.o' => array('ba.r' => array('foo', 'ba.z' => 'bar'))), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names recursively');
424 $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>');
425 $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), "->getPhpValues() doesn't return empty values");
428 public function testGetFiles()
430 $form = $this->createForm('<form><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
431 $this->assertEquals(array(), $form->getFiles(), '->getFiles() returns an empty array if method is get');
433 $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
434 $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for POST');
436 $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'put');
437 $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PUT');
439 $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'delete');
440 $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for DELETE');
442 $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'patch');
443 $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PATCH');
445 $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
446 $this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
449 public function testGetPhpFiles()
451 $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
452 $this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');
454 $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>');
455 $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');
457 $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>');
458 $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');
462 * @dataProvider provideGetUriValues
464 public function testGetUri($message, $form, $values, $uri, $method = null)
466 $form = $this->createForm($form, $method);
467 $form->setValues($values);
469 $this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
472 public function testGetBaseUri()
474 $dom = new \DOMDocument();
475 $dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
477 $nodes = $dom->getElementsByTagName('input');
478 $form = new Form($nodes->item($nodes->length - 1), 'http://www.foo.com/');
479 $this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
482 public function testGetUriWithAnchor()
484 $form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com/id/123');
486 $this->assertEquals('http://example.com/id/123#foo', $form->getUri());
489 public function testGetUriActionAbsolute()
491 $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>';
493 $form = $this->createForm($formHtml);
494 $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
496 $form = $this->createForm($formHtml, null, 'https://login.foo.com');
497 $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
499 $form = $this->createForm($formHtml, null, 'https://login.foo.com/bar/');
500 $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
502 // The action URI haven't the same domain Host have an another domain as Host
503 $form = $this->createForm($formHtml, null, 'https://www.foo.com');
504 $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
506 $form = $this->createForm($formHtml, null, 'https://www.foo.com/bar/');
507 $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
510 public function testGetUriAbsolute()
512 $form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
513 $this->assertEquals('http://localhost/foo/foo', $form->getUri(), '->getUri() returns absolute URIs');
515 $form = $this->createForm('<form action="/foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
516 $this->assertEquals('http://localhost/foo', $form->getUri(), '->getUri() returns absolute URIs');
519 public function testGetUriWithOnlyQueryString()
521 $form = $this->createForm('<form action="?get=param"><input type="submit" /></form>', null, 'http://localhost/foo/bar');
522 $this->assertEquals('http://localhost/foo/bar?get=param', $form->getUri(), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
525 public function testGetUriWithoutAction()
527 $form = $this->createForm('<form><input type="submit" /></form>', null, 'http://localhost/foo/bar');
528 $this->assertEquals('http://localhost/foo/bar', $form->getUri(), '->getUri() returns path if no action defined');
531 public function provideGetUriValues()
535 'returns the URI of the form',
536 '<form action="/foo"><input type="submit" /></form>',
541 'appends the form values if the method is get',
542 '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
547 'appends the form values and merges the submitted values',
548 '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
549 array('foo' => 'bar'),
553 'does not append values if the method is post',
554 '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
559 'does not append values if the method is patch',
560 '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
566 'does not append values if the method is delete',
567 '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
573 'does not append values if the method is put',
574 '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
580 'appends the form values to an existing query string',
581 '<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
583 '/foo?bar=bar&foo=foo',
586 'replaces query values with the form values',
587 '<form action="/foo?bar=bar"><input type="text" name="bar" value="foo" /><input type="submit" /></form>',
592 'returns an empty URI if the action is empty',
593 '<form><input type="submit" /></form>',
598 'appends the form values even if the action is empty',
599 '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
604 'chooses the path if the action attribute value is a sharp (#)',
605 '<form action="#" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
612 public function testHas()
614 $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
616 $this->assertFalse($form->has('foo'), '->has() returns false if a field is not in the form');
617 $this->assertTrue($form->has('bar'), '->has() returns true if a field is in the form');
620 public function testRemove()
622 $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
623 $form->remove('bar');
624 $this->assertFalse($form->has('bar'), '->remove() removes a field');
627 public function testGet()
629 $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
631 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Field\\InputFormField', $form->get('bar'), '->get() returns the field object associated with the given name');
635 $this->fail('->get() throws an \InvalidArgumentException if the field does not exist');
636 } catch (\InvalidArgumentException $e) {
637 $this->assertTrue(true, '->get() throws an \InvalidArgumentException if the field does not exist');
641 public function testAll()
643 $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
645 $fields = $form->all();
646 $this->assertCount(1, $fields, '->all() return an array of form field objects');
647 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Field\\InputFormField', $fields['bar'], '->all() return an array of form field objects');
650 public function testSubmitWithoutAFormButton()
652 $dom = new \DOMDocument();
661 $nodes = $dom->getElementsByTagName('form');
662 $form = new Form($nodes->item(0), 'http://example.com');
663 $this->assertSame($nodes->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
666 public function testTypeAttributeIsCaseInsensitive()
668 $form = $this->createForm('<form method="post"><input type="IMAGE" name="example" /></form>');
669 $this->assertTrue($form->has('example.x'), '->has() returns true if the image input was correctly turned into an x and a y fields');
670 $this->assertTrue($form->has('example.y'), '->has() returns true if the image input was correctly turned into an x and a y fields');
673 public function testFormFieldRegistryAcceptAnyNames()
675 $field = $this->getFormFieldMock('[t:dbt%3adate;]data_daterange_enddate_value');
677 $registry = new FormFieldRegistry();
678 $registry->add($field);
679 $this->assertEquals($field, $registry->get('[t:dbt%3adate;]data_daterange_enddate_value'));
680 $registry->set('[t:dbt%3adate;]data_daterange_enddate_value', null);
682 $form = $this->createForm('<form><input type="text" name="[t:dbt%3adate;]data_daterange_enddate_value" value="bar" /><input type="submit" /></form>');
683 $form['[t:dbt%3adate;]data_daterange_enddate_value'] = 'bar';
685 $registry->remove('[t:dbt%3adate;]data_daterange_enddate_value');
689 * @expectedException \InvalidArgumentException
691 public function testFormFieldRegistryGetThrowAnExceptionWhenTheFieldDoesNotExist()
693 $registry = new FormFieldRegistry();
694 $registry->get('foo');
698 * @expectedException \InvalidArgumentException
700 public function testFormFieldRegistrySetThrowAnExceptionWhenTheFieldDoesNotExist()
702 $registry = new FormFieldRegistry();
703 $registry->set('foo', null);
706 public function testFormFieldRegistryHasReturnsTrueWhenTheFQNExists()
708 $registry = new FormFieldRegistry();
709 $registry->add($this->getFormFieldMock('foo[bar]'));
711 $this->assertTrue($registry->has('foo'));
712 $this->assertTrue($registry->has('foo[bar]'));
713 $this->assertFalse($registry->has('bar'));
714 $this->assertFalse($registry->has('foo[foo]'));
717 public function testFormRegistryFieldsCanBeRemoved()
719 $registry = new FormFieldRegistry();
720 $registry->add($this->getFormFieldMock('foo'));
721 $registry->remove('foo');
722 $this->assertFalse($registry->has('foo'));
725 public function testFormRegistrySupportsMultivaluedFields()
727 $registry = new FormFieldRegistry();
728 $registry->add($this->getFormFieldMock('foo[]'));
729 $registry->add($this->getFormFieldMock('foo[]'));
730 $registry->add($this->getFormFieldMock('bar[5]'));
731 $registry->add($this->getFormFieldMock('bar[]'));
732 $registry->add($this->getFormFieldMock('bar[baz]'));
735 array('foo[0]', 'foo[1]', 'bar[5]', 'bar[6]', 'bar[baz]'),
736 array_keys($registry->all())
740 public function testFormRegistrySetValues()
742 $registry = new FormFieldRegistry();
743 $registry->add($f2 = $this->getFormFieldMock('foo[2]'));
744 $registry->add($f3 = $this->getFormFieldMock('foo[3]'));
745 $registry->add($fbb = $this->getFormFieldMock('foo[bar][baz]'));
748 ->expects($this->exactly(2))
754 ->expects($this->exactly(2))
760 ->expects($this->exactly(2))
765 $registry->set('foo[2]', 2);
766 $registry->set('foo[3]', 3);
767 $registry->set('foo[bar][baz]', 'fbb');
769 $registry->set('foo', array(
779 * @expectedException \InvalidArgumentException
780 * @expectedExceptionMessage Cannot set value on a compound field "foo[bar]".
782 public function testFormRegistrySetValueOnCompoundField()
784 $registry = new FormFieldRegistry();
785 $registry->add($this->getFormFieldMock('foo[bar][baz]'));
787 $registry->set('foo[bar]', 'fbb');
791 * @expectedException \InvalidArgumentException
792 * @expectedExceptionMessage Unreachable field "0"
794 public function testFormRegistrySetArrayOnNotCompoundField()
796 $registry = new FormFieldRegistry();
797 $registry->add($this->getFormFieldMock('bar'));
799 $registry->set('bar', array('baz'));
802 public function testDifferentFieldTypesWithSameName()
804 $dom = new \DOMDocument();
809 <input type="hidden" name="option" value="default">
810 <input type="radio" name="option" value="A">
811 <input type="radio" name="option" value="B">
812 <input type="hidden" name="settings[1]" value="0">
813 <input type="checkbox" name="settings[1]" value="1" id="setting-1">
814 <button>klickme</button>
819 $form = new Form($dom->getElementsByTagName('form')->item(0), 'http://example.com');
821 $this->assertInstanceOf('Symfony\Component\DomCrawler\Field\ChoiceFormField', $form->get('option'));
824 protected function getFormFieldMock($name, $value = null)
827 ->getMockBuilder('Symfony\\Component\\DomCrawler\\Field\\FormField')
828 ->setMethods(array('getName', 'getValue', 'setValue', 'initialize'))
829 ->disableOriginalConstructor()
834 ->expects($this->any())
836 ->will($this->returnValue($name))
840 ->expects($this->any())
842 ->will($this->returnValue($value))
848 protected function createForm($form, $method = null, $currentUri = null)
850 $dom = new \DOMDocument();
851 $dom->loadHTML('<html>'.$form.'</html>');
853 $xPath = new \DOMXPath($dom);
854 $nodes = $xPath->query('//input | //button');
856 if (null === $currentUri) {
857 $currentUri = 'http://example.com/';
860 return new Form($nodes->item($nodes->length - 1), $currentUri, $method);
863 protected function createTestHtml5Form()
865 $dom = new \DOMDocument();
869 <form id="form-1" action="" method="POST">
870 <div><input type="checkbox" name="apples[]" value="1" checked /></div>
871 <input form="form_2" type="checkbox" name="oranges[]" value="1" checked />
872 <div><label></label><input form="form-1" type="hidden" name="form_name" value="form-1" /></div>
873 <input form="form-1" type="submit" name="button_1" value="Capture fields" />
874 <button form="form_2" type="submit" name="button_2">Submit form_2</button>
876 <input form="form-1" type="checkbox" name="apples[]" value="2" checked />
877 <form id="form_2" action="" method="POST">
878 <div><div><input type="checkbox" name="oranges[]" value="2" checked />
879 <input type="checkbox" name="oranges[]" value="3" checked /></div></div>
880 <input form="form_2" type="hidden" name="form_name" value="form_2" />
881 <input form="form-1" type="hidden" name="outer_field" value="success" />
882 <button form="form-1" type="submit" name="button_3">Submit from outside the form</button>
884 <label for="app_frontend_form_type_contact_form_type_contactType">Message subject</label>
886 <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>
890 <label for="app_frontend_form_type_contact_form_type_firstName">Firstname</label>
891 <input type="text" name="app_frontend_form_type_contact_form_type[firstName]" value="John" id="app_frontend_form_type_contact_form_type_firstName"/>
900 protected function createTestMultipleForm()
902 $dom = new \DOMDocument();
906 <form action="" method="POST">
907 <div><input type="checkbox" name="apples[]" value="1" checked /></div>
908 <input type="checkbox" name="oranges[]" value="1" checked />
909 <div><label></label><input type="hidden" name="form_name" value="form-1" /></div>
910 <input type="submit" name="button_1" value="Capture fields" />
911 <button type="submit" name="button_2">Submit form_2</button>
913 <form action="" method="POST">
914 <div><div><input type="checkbox" name="oranges[]" value="2" checked />
915 <input type="checkbox" name="oranges[]" value="3" checked /></div></div>
916 <input type="hidden" name="form_name" value="form_2" />
917 <input type="hidden" name="outer_field" value="success" />
918 <button type="submit" name="button_3">Submit from outside the form</button>
926 public function testgetPhpValuesWithEmptyTextarea()
928 $dom = new \DOMDocument();
932 <textarea name="example"></textarea>
937 $nodes = $dom->getElementsByTagName('form');
938 $form = new Form($nodes->item(0), 'http://example.com');
939 $this->assertEquals($form->getPhpValues(), array('example' => ''));