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 testGetMethodWithOverride()
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');
330 public function testGetSetValue()
332 $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
334 $this->assertEquals('foo', $form['foo']->getValue(), '->offsetGet() returns the value of a form field');
336 $form['foo'] = 'bar';
338 $this->assertEquals('bar', $form['foo']->getValue(), '->offsetSet() changes the value of a form field');
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');
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');
355 public function testDisableValidation()
357 $form = $this->createForm('<form>
358 <select name="foo[bar]">
359 <option value="bar">bar</option>
361 <select name="foo[baz]">
362 <option value="foo">foo</option>
364 <input type="submit" />
367 $form->disableValidation();
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.');
375 public function testOffsetUnset()
377 $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
379 $this->assertArrayNotHasKey('foo', $form, '->offsetUnset() removes a field');
382 public function testOffsetExists()
384 $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
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');
390 public function testGetValues()
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');
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');
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');
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');
405 public function testSetValues()
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');
412 public function testMultiselectSetValues()
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');
419 public function testGetPhpValues()
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');
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');
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');
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");
434 public function testGetFiles()
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');
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');
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');
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');
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');
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');
455 public function testGetPhpFiles()
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');
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');
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');
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();
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');
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');
477 * @dataProvider provideGetUriValues
479 public function testGetUri($message, $form, $values, $uri, $method = null)
481 $form = $this->createForm($form, $method);
482 $form->setValues($values);
484 $this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
487 public function testGetBaseUri()
489 $dom = new \DOMDocument();
490 $dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
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());
497 public function testGetUriWithAnchor()
499 $form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com/id/123');
501 $this->assertEquals('http://example.com/id/123#foo', $form->getUri());
504 public function testGetUriActionAbsolute()
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>';
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');
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');
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');
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');
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');
525 public function testGetUriAbsolute()
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');
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');
534 public function testGetUriWithOnlyQueryString()
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');
540 public function testGetUriWithoutAction()
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');
546 public function testGetUriWithActionOverride()
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');
552 public function provideGetUriValues()
556 'returns the URI of the form',
557 '<form action="/foo"><input type="submit" /></form>',
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>',
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'),
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>',
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>',
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>',
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>',
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>',
604 '/foo?bar=bar&foo=foo',
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>',
613 'returns an empty URI if the action is empty',
614 '<form><input type="submit" /></form>',
619 'appends the form values even if the action is empty',
620 '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
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>',
633 public function testHas()
635 $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
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');
641 public function testRemove()
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');
648 public function testGet()
650 $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
652 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Field\\InputFormField', $form->get('bar'), '->get() returns the field object associated with the given name');
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');
662 public function testAll()
664 $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
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');
671 public function testSubmitWithoutAFormButton()
673 $dom = new \DOMDocument();
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');
687 public function testTypeAttributeIsCaseInsensitive()
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');
694 public function testFormFieldRegistryAcceptAnyNames()
696 $field = $this->getFormFieldMock('[t:dbt%3adate;]data_daterange_enddate_value');
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);
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';
706 $registry->remove('[t:dbt%3adate;]data_daterange_enddate_value');
710 * @expectedException \InvalidArgumentException
712 public function testFormFieldRegistryGetThrowAnExceptionWhenTheFieldDoesNotExist()
714 $registry = new FormFieldRegistry();
715 $registry->get('foo');
719 * @expectedException \InvalidArgumentException
721 public function testFormFieldRegistrySetThrowAnExceptionWhenTheFieldDoesNotExist()
723 $registry = new FormFieldRegistry();
724 $registry->set('foo', null);
727 public function testFormFieldRegistryHasReturnsTrueWhenTheFQNExists()
729 $registry = new FormFieldRegistry();
730 $registry->add($this->getFormFieldMock('foo[bar]'));
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]'));
738 public function testFormRegistryFieldsCanBeRemoved()
740 $registry = new FormFieldRegistry();
741 $registry->add($this->getFormFieldMock('foo'));
742 $registry->remove('foo');
743 $this->assertFalse($registry->has('foo'));
746 public function testFormRegistrySupportsMultivaluedFields()
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]'));
756 array('foo[0]', 'foo[1]', 'bar[5]', 'bar[6]', 'bar[baz]'),
757 array_keys($registry->all())
761 public function testFormRegistrySetValues()
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]'));
769 ->expects($this->exactly(2))
775 ->expects($this->exactly(2))
781 ->expects($this->exactly(2))
786 $registry->set('foo[2]', 2);
787 $registry->set('foo[3]', 3);
788 $registry->set('foo[bar][baz]', 'fbb');
790 $registry->set('foo', array(
800 * @expectedException \InvalidArgumentException
801 * @expectedExceptionMessage Cannot set value on a compound field "foo[bar]".
803 public function testFormRegistrySetValueOnCompoundField()
805 $registry = new FormFieldRegistry();
806 $registry->add($this->getFormFieldMock('foo[bar][baz]'));
808 $registry->set('foo[bar]', 'fbb');
812 * @expectedException \InvalidArgumentException
813 * @expectedExceptionMessage Unreachable field "0"
815 public function testFormRegistrySetArrayOnNotCompoundField()
817 $registry = new FormFieldRegistry();
818 $registry->add($this->getFormFieldMock('bar'));
820 $registry->set('bar', array('baz'));
823 public function testDifferentFieldTypesWithSameName()
825 $dom = new \DOMDocument();
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>
840 $form = new Form($dom->getElementsByTagName('form')->item(0), 'http://example.com');
842 $this->assertInstanceOf('Symfony\Component\DomCrawler\Field\ChoiceFormField', $form->get('option'));
845 protected function getFormFieldMock($name, $value = null)
848 ->getMockBuilder('Symfony\\Component\\DomCrawler\\Field\\FormField')
849 ->setMethods(array('getName', 'getValue', 'setValue', 'initialize'))
850 ->disableOriginalConstructor()
855 ->expects($this->any())
857 ->will($this->returnValue($name))
861 ->expects($this->any())
863 ->will($this->returnValue($value))
869 protected function createForm($form, $method = null, $currentUri = null)
871 $dom = new \DOMDocument();
872 $dom->loadHTML('<html>'.$form.'</html>');
874 $xPath = new \DOMXPath($dom);
875 $nodes = $xPath->query('//input | //button');
877 if (null === $currentUri) {
878 $currentUri = 'http://example.com/';
881 return new Form($nodes->item($nodes->length - 1), $currentUri, $method);
884 protected function createTestHtml5Form()
886 $dom = new \DOMDocument();
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>
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>
905 <label for="app_frontend_form_type_contact_form_type_contactType">Message subject</label>
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>
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"/>
921 protected function createTestMultipleForm()
923 $dom = new \DOMDocument();
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>
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>
947 public function testgetPhpValuesWithEmptyTextarea()
949 $dom = new \DOMDocument();
953 <textarea name="example"></textarea>
958 $nodes = $dom->getElementsByTagName('form');
959 $form = new Form($nodes->item(0), 'http://example.com');
960 $this->assertEquals($form->getPhpValues(), array('example' => ''));