diff --git a/src/Symfony/Component/DomCrawler/Form.php b/src/Symfony/Component/DomCrawler/Form.php index 754e78a0a2..7f44a5be5e 100644 --- a/src/Symfony/Component/DomCrawler/Form.php +++ b/src/Symfony/Component/DomCrawler/Form.php @@ -395,7 +395,7 @@ class Form extends Link implements \ArrayAccess // corresponding elements are either descendants or have a matching HTML5 form attribute $formId = Crawler::xpathLiteral($this->node->getAttribute('id')); - $fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]/input[not(@form)] | //form[@id=%s]/button[not(@form)] | //form[@id=%s]/textarea[not(@form)] | //form[@id=%s]/select[not(@form)]', $formId, $formId, $formId, $formId, $formId, $formId, $formId, $formId), $root); + $fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]//input[not(@form)] | //form[@id=%s]//button[not(@form)] | //form[@id=%s]//textarea[not(@form)] | //form[@id=%s]//select[not(@form)]', $formId, $formId, $formId, $formId, $formId, $formId, $formId, $formId), $root); foreach ($fieldNodes as $node) { $this->addField($node); } diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index c771c7e6c6..394f1c04e0 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -466,8 +466,8 @@ EOF $this->assertEquals($crawler->form()->getFormNode()->getAttribute('id'), $crawler2->form()->getFormNode()->getAttribute('id'), '->form() works on elements with form attribute'); $this->assertEquals(array('FooName' => 'FooBar', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form(array('FooName' => 'FooBar'))->getValues(), '->form() takes an array of values to submit as its first argument'); - $this->assertEquals(array('FooName' => 'FooValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form()->getValues(), '->form() takes an array of values to submit as its first argument'); - $this->assertEquals(array('FooBarName' => 'FooBarValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler2->form()->getValues(), '->form() takes an array of values to submit as its first argument'); + $this->assertEquals(array('FooName' => 'FooValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form()->getValues(), '->getValues() returns correct form values'); + $this->assertEquals(array('FooBarName' => 'FooBarValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler2->form()->getValues(), '->getValues() returns correct form values'); try { $this->createTestCrawler()->filterXPath('//ol')->form(); diff --git a/src/Symfony/Component/DomCrawler/Tests/FormTest.php b/src/Symfony/Component/DomCrawler/Tests/FormTest.php index 640201dd27..587717841c 100644 --- a/src/Symfony/Component/DomCrawler/Tests/FormTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/FormTest.php @@ -87,27 +87,7 @@ class FormTest extends \PHPUnit_Framework_TestCase public function testConstructorHandlesFormAttribute() { - $dom = new \DOMDocument(); - $dom->loadHTML(' - -
- - - - - '); + $dom = $this->createTestHtml5Form(); $inputElements = $dom->getElementsByTagName('input'); $buttonElements = $dom->getElementsByTagName('button'); @@ -121,11 +101,22 @@ class FormTest extends \PHPUnit_Framework_TestCase $form2 = new Form($buttonElements->item(0), 'http://example.com'); $this->assertSame($dom->getElementsByTagName('form')->item(1), $form2->getFormNode(), 'HTML5-compliant form attribute handled incorrectly'); + } - // Tests if form elements are correctly assigned to forms + public function testConstructorHandlesFormValues() + { + $dom = $this->createTestHtml5Form(); + + $inputElements = $dom->getElementsByTagName('input'); + $buttonElements = $dom->getElementsByTagName('button'); + + $form1 = new Form($inputElements->item(3), 'http://example.com'); + $form2 = new Form($buttonElements->item(0), 'http://example.com'); + + // Tests if form values are correctly assigned to forms $values1 = array( 'apples' => array('1', '2'), - 'form_name' => 'form_1', + 'form_name' => 'form-1', 'button_1' => 'Capture fields', 'outer_field' => 'success' ); @@ -133,10 +124,11 @@ class FormTest extends \PHPUnit_Framework_TestCase 'oranges' => array('1', '2', '3'), 'form_name' => 'form_2', 'button_2' => '', + 'app_frontend_form_type_contact_form_type' => array('contactType' => '', 'firstName' => 'John') ); + $this->assertEquals($values1, $form1->getPhpValues(), 'HTML5-compliant form attribute handled incorrectly'); $this->assertEquals($values2, $form2->getPhpValues(), 'HTML5-compliant form attribute handled incorrectly'); - } public function testMultiValuedFields() @@ -777,4 +769,40 @@ class FormTest extends \PHPUnit_Framework_TestCase return new Form($nodes->item($nodes->length - 1), $currentUri, $method); } + + protected function createTestHtml5Form() { + $dom = new \DOMDocument(); + $dom->loadHTML(' + +