FormTest.php 42 KB

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