FormTest.php 43 KB

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