FormTest.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 testGetSetValue()
  276. {
  277. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  278. $this->assertEquals('foo', $form['foo']->getValue(), '->offsetGet() returns the value of a form field');
  279. $form['foo'] = 'bar';
  280. $this->assertEquals('bar', $form['foo']->getValue(), '->offsetSet() changes the value of a form field');
  281. try {
  282. $form['foobar'] = 'bar';
  283. $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  284. } catch (\InvalidArgumentException $e) {
  285. $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  286. }
  287. try {
  288. $form['foobar'];
  289. $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  290. } catch (\InvalidArgumentException $e) {
  291. $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  292. }
  293. }
  294. public function testDisableValidation()
  295. {
  296. $form = $this->createForm('<form>
  297. <select name="foo[bar]">
  298. <option value="bar">bar</option>
  299. </select>
  300. <select name="foo[baz]">
  301. <option value="foo">foo</option>
  302. </select>
  303. <input type="submit" />
  304. </form>');
  305. $form->disableValidation();
  306. $form['foo[bar]']->select('foo');
  307. $form['foo[baz]']->select('bar');
  308. $this->assertEquals('foo', $form['foo[bar]']->getValue(), '->disableValidation() disables validation of all ChoiceFormField.');
  309. $this->assertEquals('bar', $form['foo[baz]']->getValue(), '->disableValidation() disables validation of all ChoiceFormField.');
  310. }
  311. public function testOffsetUnset()
  312. {
  313. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  314. unset($form['foo']);
  315. $this->assertArrayNotHasKey('foo', $form, '->offsetUnset() removes a field');
  316. }
  317. public function testOffsetExists()
  318. {
  319. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  320. $this->assertArrayHasKey('foo', $form, '->offsetExists() return true if the field exists');
  321. $this->assertArrayNotHasKey('bar', $form, '->offsetExists() return false if the field does not exist');
  322. }
  323. public function testGetValues()
  324. {
  325. $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>');
  326. $this->assertEquals(array('foo[bar]' => 'foo', 'bar' => 'bar', 'baz' => array()), $form->getValues(), '->getValues() returns all form field values');
  327. $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  328. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include not-checked checkboxes');
  329. $form = $this->createForm('<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  330. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include file input fields');
  331. $form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  332. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
  333. $form = $this->createForm('<form><template><input type="text" name="foo" value="foo" /></template><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  334. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include template fields');
  335. $this->assertFalse($form->has('foo'));
  336. }
  337. public function testSetValues()
  338. {
  339. $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" checked="checked" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  340. $form->setValues(array('foo' => false, 'bar' => 'foo'));
  341. $this->assertEquals(array('bar' => 'foo'), $form->getValues(), '->setValues() sets the values of fields');
  342. }
  343. public function testMultiselectSetValues()
  344. {
  345. $form = $this->createForm('<form><select multiple="multiple" name="multi"><option value="foo">foo</option><option value="bar">bar</option></select><input type="submit" /></form>');
  346. $form->setValues(array('multi' => array('foo', 'bar')));
  347. $this->assertEquals(array('multi' => array('foo', 'bar')), $form->getValues(), '->setValue() sets the values of select');
  348. }
  349. public function testGetPhpValues()
  350. {
  351. $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  352. $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');
  353. $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>');
  354. $this->assertEquals(array('fo.o' => array('ba.r' => 'foo'), 'ba r' => 'bar'), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names');
  355. $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>');
  356. $this->assertEquals(array('fo.o' => array('ba.r' => array('foo', 'ba.z' => 'bar'))), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names recursively');
  357. $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>');
  358. $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), "->getPhpValues() doesn't return empty values");
  359. }
  360. public function testGetFiles()
  361. {
  362. $form = $this->createForm('<form><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  363. $this->assertEquals(array(), $form->getFiles(), '->getFiles() returns an empty array if method is get');
  364. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  365. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for POST');
  366. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'put');
  367. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PUT');
  368. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'delete');
  369. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for DELETE');
  370. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'patch');
  371. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PATCH');
  372. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
  373. $this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
  374. $form = $this->createForm('<form method="post"><template><input type="file" name="foo"/></template><input type="text" name="bar" value="bar"/><input type="submit"/></form>');
  375. $this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include template file fields');
  376. $this->assertFalse($form->has('foo'));
  377. }
  378. public function testGetPhpFiles()
  379. {
  380. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  381. $this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');
  382. $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>');
  383. $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');
  384. $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>');
  385. $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');
  386. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  387. $files = $form->getPhpFiles();
  388. $this->assertSame(0, $files['foo']['bar']['size'], '->getPhpFiles() converts size to int');
  389. $this->assertSame(4, $files['foo']['bar']['error'], '->getPhpFiles() converts error to int');
  390. $form = $this->createForm('<form method="post"><input type="file" name="size[error]" /><input type="text" name="error" value="error" /><input type="submit" /></form>');
  391. $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');
  392. }
  393. /**
  394. * @dataProvider provideGetUriValues
  395. */
  396. public function testGetUri($message, $form, $values, $uri, $method = null)
  397. {
  398. $form = $this->createForm($form, $method);
  399. $form->setValues($values);
  400. $this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
  401. }
  402. public function testGetBaseUri()
  403. {
  404. $dom = new \DOMDocument();
  405. $dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  406. $nodes = $dom->getElementsByTagName('input');
  407. $form = new Form($nodes->item($nodes->length - 1), 'http://www.foo.com/');
  408. $this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
  409. }
  410. public function testGetUriWithAnchor()
  411. {
  412. $form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com/id/123');
  413. $this->assertEquals('http://example.com/id/123#foo', $form->getUri());
  414. }
  415. public function testGetUriActionAbsolute()
  416. {
  417. $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>';
  418. $form = $this->createForm($formHtml);
  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://login.foo.com');
  421. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  422. $form = $this->createForm($formHtml, null, 'https://login.foo.com/bar/');
  423. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  424. // The action URI haven't the same domain Host have an another domain as Host
  425. $form = $this->createForm($formHtml, null, 'https://www.foo.com');
  426. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  427. $form = $this->createForm($formHtml, null, 'https://www.foo.com/bar/');
  428. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  429. }
  430. public function testGetUriAbsolute()
  431. {
  432. $form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
  433. $this->assertEquals('http://localhost/foo/foo', $form->getUri(), '->getUri() returns absolute URIs');
  434. $form = $this->createForm('<form action="/foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
  435. $this->assertEquals('http://localhost/foo', $form->getUri(), '->getUri() returns absolute URIs');
  436. }
  437. public function testGetUriWithOnlyQueryString()
  438. {
  439. $form = $this->createForm('<form action="?get=param"><input type="submit" /></form>', null, 'http://localhost/foo/bar');
  440. $this->assertEquals('http://localhost/foo/bar?get=param', $form->getUri(), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
  441. }
  442. public function testGetUriWithoutAction()
  443. {
  444. $form = $this->createForm('<form><input type="submit" /></form>', null, 'http://localhost/foo/bar');
  445. $this->assertEquals('http://localhost/foo/bar', $form->getUri(), '->getUri() returns path if no action defined');
  446. }
  447. public function provideGetUriValues()
  448. {
  449. return array(
  450. array(
  451. 'returns the URI of the form',
  452. '<form action="/foo"><input type="submit" /></form>',
  453. array(),
  454. '/foo',
  455. ),
  456. array(
  457. 'appends the form values if the method is get',
  458. '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  459. array(),
  460. '/foo?foo=foo',
  461. ),
  462. array(
  463. 'appends the form values and merges the submitted values',
  464. '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  465. array('foo' => 'bar'),
  466. '/foo?foo=bar',
  467. ),
  468. array(
  469. 'does not append values if the method is post',
  470. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  471. array(),
  472. '/foo',
  473. ),
  474. array(
  475. 'does not append values if the method is patch',
  476. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  477. array(),
  478. '/foo',
  479. 'PUT',
  480. ),
  481. array(
  482. 'does not append values if the method is delete',
  483. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  484. array(),
  485. '/foo',
  486. 'DELETE',
  487. ),
  488. array(
  489. 'does not append values if the method is put',
  490. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  491. array(),
  492. '/foo',
  493. 'PATCH',
  494. ),
  495. array(
  496. 'appends the form values to an existing query string',
  497. '<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  498. array(),
  499. '/foo?bar=bar&foo=foo',
  500. ),
  501. array(
  502. 'replaces query values with the form values',
  503. '<form action="/foo?bar=bar"><input type="text" name="bar" value="foo" /><input type="submit" /></form>',
  504. array(),
  505. '/foo?bar=foo',
  506. ),
  507. array(
  508. 'returns an empty URI if the action is empty',
  509. '<form><input type="submit" /></form>',
  510. array(),
  511. '/',
  512. ),
  513. array(
  514. 'appends the form values even if the action is empty',
  515. '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  516. array(),
  517. '/?foo=foo',
  518. ),
  519. array(
  520. 'chooses the path if the action attribute value is a sharp (#)',
  521. '<form action="#" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  522. array(),
  523. '/#',
  524. ),
  525. );
  526. }
  527. public function testHas()
  528. {
  529. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  530. $this->assertFalse($form->has('foo'), '->has() returns false if a field is not in the form');
  531. $this->assertTrue($form->has('bar'), '->has() returns true if a field is in the form');
  532. }
  533. public function testRemove()
  534. {
  535. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  536. $form->remove('bar');
  537. $this->assertFalse($form->has('bar'), '->remove() removes a field');
  538. }
  539. public function testGet()
  540. {
  541. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  542. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Field\\InputFormField', $form->get('bar'), '->get() returns the field object associated with the given name');
  543. try {
  544. $form->get('foo');
  545. $this->fail('->get() throws an \InvalidArgumentException if the field does not exist');
  546. } catch (\InvalidArgumentException $e) {
  547. $this->assertTrue(true, '->get() throws an \InvalidArgumentException if the field does not exist');
  548. }
  549. }
  550. public function testAll()
  551. {
  552. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  553. $fields = $form->all();
  554. $this->assertCount(1, $fields, '->all() return an array of form field objects');
  555. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Field\\InputFormField', $fields['bar'], '->all() return an array of form field objects');
  556. }
  557. public function testSubmitWithoutAFormButton()
  558. {
  559. $dom = new \DOMDocument();
  560. $dom->loadHTML('
  561. <html>
  562. <form>
  563. <input type="foo" />
  564. </form>
  565. </html>
  566. ');
  567. $nodes = $dom->getElementsByTagName('form');
  568. $form = new Form($nodes->item(0), 'http://example.com');
  569. $this->assertSame($nodes->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
  570. }
  571. public function testTypeAttributeIsCaseInsensitive()
  572. {
  573. $form = $this->createForm('<form method="post"><input type="IMAGE" name="example" /></form>');
  574. $this->assertTrue($form->has('example.x'), '->has() returns true if the image input was correctly turned into an x and a y fields');
  575. $this->assertTrue($form->has('example.y'), '->has() returns true if the image input was correctly turned into an x and a y fields');
  576. }
  577. public function testFormFieldRegistryAcceptAnyNames()
  578. {
  579. $field = $this->getFormFieldMock('[t:dbt%3adate;]data_daterange_enddate_value');
  580. $registry = new FormFieldRegistry();
  581. $registry->add($field);
  582. $this->assertEquals($field, $registry->get('[t:dbt%3adate;]data_daterange_enddate_value'));
  583. $registry->set('[t:dbt%3adate;]data_daterange_enddate_value', null);
  584. $form = $this->createForm('<form><input type="text" name="[t:dbt%3adate;]data_daterange_enddate_value" value="bar" /><input type="submit" /></form>');
  585. $form['[t:dbt%3adate;]data_daterange_enddate_value'] = 'bar';
  586. $registry->remove('[t:dbt%3adate;]data_daterange_enddate_value');
  587. }
  588. /**
  589. * @expectedException \InvalidArgumentException
  590. */
  591. public function testFormFieldRegistryGetThrowAnExceptionWhenTheFieldDoesNotExist()
  592. {
  593. $registry = new FormFieldRegistry();
  594. $registry->get('foo');
  595. }
  596. /**
  597. * @expectedException \InvalidArgumentException
  598. */
  599. public function testFormFieldRegistrySetThrowAnExceptionWhenTheFieldDoesNotExist()
  600. {
  601. $registry = new FormFieldRegistry();
  602. $registry->set('foo', null);
  603. }
  604. public function testFormFieldRegistryHasReturnsTrueWhenTheFQNExists()
  605. {
  606. $registry = new FormFieldRegistry();
  607. $registry->add($this->getFormFieldMock('foo[bar]'));
  608. $this->assertTrue($registry->has('foo'));
  609. $this->assertTrue($registry->has('foo[bar]'));
  610. $this->assertFalse($registry->has('bar'));
  611. $this->assertFalse($registry->has('foo[foo]'));
  612. }
  613. public function testFormRegistryFieldsCanBeRemoved()
  614. {
  615. $registry = new FormFieldRegistry();
  616. $registry->add($this->getFormFieldMock('foo'));
  617. $registry->remove('foo');
  618. $this->assertFalse($registry->has('foo'));
  619. }
  620. public function testFormRegistrySupportsMultivaluedFields()
  621. {
  622. $registry = new FormFieldRegistry();
  623. $registry->add($this->getFormFieldMock('foo[]'));
  624. $registry->add($this->getFormFieldMock('foo[]'));
  625. $registry->add($this->getFormFieldMock('bar[5]'));
  626. $registry->add($this->getFormFieldMock('bar[]'));
  627. $registry->add($this->getFormFieldMock('bar[baz]'));
  628. $this->assertEquals(
  629. array('foo[0]', 'foo[1]', 'bar[5]', 'bar[6]', 'bar[baz]'),
  630. array_keys($registry->all())
  631. );
  632. }
  633. public function testFormRegistrySetValues()
  634. {
  635. $registry = new FormFieldRegistry();
  636. $registry->add($f2 = $this->getFormFieldMock('foo[2]'));
  637. $registry->add($f3 = $this->getFormFieldMock('foo[3]'));
  638. $registry->add($fbb = $this->getFormFieldMock('foo[bar][baz]'));
  639. $f2
  640. ->expects($this->exactly(2))
  641. ->method('setValue')
  642. ->with(2)
  643. ;
  644. $f3
  645. ->expects($this->exactly(2))
  646. ->method('setValue')
  647. ->with(3)
  648. ;
  649. $fbb
  650. ->expects($this->exactly(2))
  651. ->method('setValue')
  652. ->with('fbb')
  653. ;
  654. $registry->set('foo[2]', 2);
  655. $registry->set('foo[3]', 3);
  656. $registry->set('foo[bar][baz]', 'fbb');
  657. $registry->set('foo', array(
  658. 2 => 2,
  659. 3 => 3,
  660. 'bar' => array(
  661. 'baz' => 'fbb',
  662. ),
  663. ));
  664. }
  665. /**
  666. * @expectedException \InvalidArgumentException
  667. * @expectedExceptionMessage Cannot set value on a compound field "foo[bar]".
  668. */
  669. public function testFormRegistrySetValueOnCompoundField()
  670. {
  671. $registry = new FormFieldRegistry();
  672. $registry->add($this->getFormFieldMock('foo[bar][baz]'));
  673. $registry->set('foo[bar]', 'fbb');
  674. }
  675. /**
  676. * @expectedException \InvalidArgumentException
  677. * @expectedExceptionMessage Unreachable field "0"
  678. */
  679. public function testFormRegistrySetArrayOnNotCompoundField()
  680. {
  681. $registry = new FormFieldRegistry();
  682. $registry->add($this->getFormFieldMock('bar'));
  683. $registry->set('bar', array('baz'));
  684. }
  685. public function testDifferentFieldTypesWithSameName()
  686. {
  687. $dom = new \DOMDocument();
  688. $dom->loadHTML('
  689. <html>
  690. <body>
  691. <form action="/">
  692. <input type="hidden" name="option" value="default">
  693. <input type="radio" name="option" value="A">
  694. <input type="radio" name="option" value="B">
  695. <input type="hidden" name="settings[1]" value="0">
  696. <input type="checkbox" name="settings[1]" value="1" id="setting-1">
  697. <button>klickme</button>
  698. </form>
  699. </body>
  700. </html>
  701. ');
  702. $form = new Form($dom->getElementsByTagName('form')->item(0), 'http://example.com');
  703. $this->assertInstanceOf('Symfony\Component\DomCrawler\Field\ChoiceFormField', $form->get('option'));
  704. }
  705. protected function getFormFieldMock($name, $value = null)
  706. {
  707. $field = $this
  708. ->getMockBuilder('Symfony\\Component\\DomCrawler\\Field\\FormField')
  709. ->setMethods(array('getName', 'getValue', 'setValue', 'initialize'))
  710. ->disableOriginalConstructor()
  711. ->getMock()
  712. ;
  713. $field
  714. ->expects($this->any())
  715. ->method('getName')
  716. ->will($this->returnValue($name))
  717. ;
  718. $field
  719. ->expects($this->any())
  720. ->method('getValue')
  721. ->will($this->returnValue($value))
  722. ;
  723. return $field;
  724. }
  725. protected function createForm($form, $method = null, $currentUri = null)
  726. {
  727. $dom = new \DOMDocument();
  728. @$dom->loadHTML('<html>'.$form.'</html>');
  729. $xPath = new \DOMXPath($dom);
  730. $nodes = $xPath->query('//input | //button');
  731. if (null === $currentUri) {
  732. $currentUri = 'http://example.com/';
  733. }
  734. return new Form($nodes->item($nodes->length - 1), $currentUri, $method);
  735. }
  736. protected function createTestHtml5Form()
  737. {
  738. $dom = new \DOMDocument();
  739. $dom->loadHTML('
  740. <html>
  741. <h1>Hello form</h1>
  742. <form id="form-1" action="" method="POST">
  743. <div><input type="checkbox" name="apples[]" value="1" checked /></div>
  744. <input form="form_2" type="checkbox" name="oranges[]" value="1" checked />
  745. <div><label></label><input form="form-1" type="hidden" name="form_name" value="form-1" /></div>
  746. <input form="form-1" type="submit" name="button_1" value="Capture fields" />
  747. <button form="form_2" type="submit" name="button_2">Submit form_2</button>
  748. </form>
  749. <input form="form-1" type="checkbox" name="apples[]" value="2" checked />
  750. <form id="form_2" action="" method="POST">
  751. <div><div><input type="checkbox" name="oranges[]" value="2" checked />
  752. <input type="checkbox" name="oranges[]" value="3" checked /></div></div>
  753. <input form="form_2" type="hidden" name="form_name" value="form_2" />
  754. <input form="form-1" type="hidden" name="outer_field" value="success" />
  755. <button form="form-1" type="submit" name="button_3">Submit from outside the form</button>
  756. <div>
  757. <label for="app_frontend_form_type_contact_form_type_contactType">Message subject</label>
  758. <div>
  759. <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>
  760. </div>
  761. </div>
  762. <div>
  763. <label for="app_frontend_form_type_contact_form_type_firstName">Firstname</label>
  764. <input type="text" name="app_frontend_form_type_contact_form_type[firstName]" value="John" id="app_frontend_form_type_contact_form_type_firstName"/>
  765. </div>
  766. </form>
  767. <button />
  768. </html>');
  769. return $dom;
  770. }
  771. protected function createTestMultipleForm()
  772. {
  773. $dom = new \DOMDocument();
  774. $dom->loadHTML('
  775. <html>
  776. <h1>Hello form</h1>
  777. <form action="" method="POST">
  778. <div><input type="checkbox" name="apples[]" value="1" checked /></div>
  779. <input type="checkbox" name="oranges[]" value="1" checked />
  780. <div><label></label><input type="hidden" name="form_name" value="form-1" /></div>
  781. <input type="submit" name="button_1" value="Capture fields" />
  782. <button type="submit" name="button_2">Submit form_2</button>
  783. </form>
  784. <form action="" method="POST">
  785. <div><div><input type="checkbox" name="oranges[]" value="2" checked />
  786. <input type="checkbox" name="oranges[]" value="3" checked /></div></div>
  787. <input type="hidden" name="form_name" value="form_2" />
  788. <input type="hidden" name="outer_field" value="success" />
  789. <button type="submit" name="button_3">Submit from outside the form</button>
  790. </form>
  791. <button />
  792. </html>');
  793. return $dom;
  794. }
  795. public function testgetPhpValuesWithEmptyTextarea()
  796. {
  797. $dom = new \DOMDocument();
  798. $dom->loadHTML('
  799. <html>
  800. <form>
  801. <textarea name="example"></textarea>
  802. </form>
  803. </html>'
  804. );
  805. $nodes = $dom->getElementsByTagName('form');
  806. $form = new Form($nodes->item(0), 'http://example.com');
  807. $this->assertEquals($form->getPhpValues(), array('example' => ''));
  808. }
  809. }