FormTest.php 42 KB

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