FormTest.php 42 KB

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