FormTest.php 42 KB

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