FormTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. use Symfony\Component\DomCrawler\Field;
  14. class FormTest extends \PHPUnit_Framework_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. * @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 testConstructorHandlesFormAttribute()
  75. {
  76. $dom = $this->createTestHtml5Form();
  77. $inputElements = $dom->getElementsByTagName('input');
  78. $buttonElements = $dom->getElementsByTagName('button');
  79. // Tests if submit buttons are correctly assigned to forms
  80. $form1 = new Form($buttonElements->item(1), 'http://example.com');
  81. $this->assertSame($dom->getElementsByTagName('form')->item(0), $form1->getFormNode(), 'HTML5-compliant form attribute handled incorrectly');
  82. $form1 = new Form($inputElements->item(3), 'http://example.com');
  83. $this->assertSame($dom->getElementsByTagName('form')->item(0), $form1->getFormNode(), 'HTML5-compliant form attribute handled incorrectly');
  84. $form2 = new Form($buttonElements->item(0), 'http://example.com');
  85. $this->assertSame($dom->getElementsByTagName('form')->item(1), $form2->getFormNode(), 'HTML5-compliant form attribute handled incorrectly');
  86. }
  87. public function testConstructorHandlesFormValues()
  88. {
  89. $dom = $this->createTestHtml5Form();
  90. $inputElements = $dom->getElementsByTagName('input');
  91. $buttonElements = $dom->getElementsByTagName('button');
  92. $form1 = new Form($inputElements->item(3), 'http://example.com');
  93. $form2 = new Form($buttonElements->item(0), 'http://example.com');
  94. // Tests if form values are correctly assigned to forms
  95. $values1 = array(
  96. 'apples' => array('1', '2'),
  97. 'form_name' => 'form-1',
  98. 'button_1' => 'Capture fields',
  99. 'outer_field' => 'success'
  100. );
  101. $values2 = array(
  102. 'oranges' => array('1', '2', '3'),
  103. 'form_name' => 'form_2',
  104. 'button_2' => '',
  105. 'app_frontend_form_type_contact_form_type' => array('contactType' => '', 'firstName' => 'John')
  106. );
  107. $this->assertEquals($values1, $form1->getPhpValues(), 'HTML5-compliant form attribute handled incorrectly');
  108. $this->assertEquals($values2, $form2->getPhpValues(), 'HTML5-compliant form attribute handled incorrectly');
  109. }
  110. public function testMultiValuedFields()
  111. {
  112. $form = $this->createForm('<form>
  113. <input type="text" name="foo[4]" value="foo" disabled="disabled" />
  114. <input type="text" name="foo" value="foo" disabled="disabled" />
  115. <input type="text" name="foo[2]" value="foo" disabled="disabled" />
  116. <input type="text" name="foo[]" value="foo" disabled="disabled" />
  117. <input type="text" name="bar[foo][]" value="foo" disabled="disabled" />
  118. <input type="text" name="bar[foo][foobar]" value="foo" disabled="disabled" />
  119. <input type="submit" />
  120. </form>
  121. ');
  122. $this->assertEquals(
  123. array_keys($form->all()),
  124. array('foo[2]', 'foo[3]', 'bar[foo][0]', 'bar[foo][foobar]')
  125. );
  126. $this->assertEquals($form->get('foo[2]')->getValue(), 'foo');
  127. $this->assertEquals($form->get('foo[3]')->getValue(), 'foo');
  128. $this->assertEquals($form->get('bar[foo][0]')->getValue(), 'foo');
  129. $this->assertEquals($form->get('bar[foo][foobar]')->getValue(), 'foo');
  130. $form['foo[2]'] = 'bar';
  131. $form['foo[3]'] = 'bar';
  132. $this->assertEquals($form->get('foo[2]')->getValue(), 'bar');
  133. $this->assertEquals($form->get('foo[3]')->getValue(), 'bar');
  134. $form['bar'] = array('foo' => array('0' => 'bar', 'foobar' => 'foobar'));
  135. $this->assertEquals($form->get('bar[foo][0]')->getValue(), 'bar');
  136. $this->assertEquals($form->get('bar[foo][foobar]')->getValue(), 'foobar');
  137. }
  138. /**
  139. * @dataProvider provideInitializeValues
  140. */
  141. public function testConstructor($message, $form, $values)
  142. {
  143. $form = $this->createForm('<form>'.$form.'</form>');
  144. $this->assertEquals(
  145. $values,
  146. array_map(function ($field) {
  147. $class = get_class($field);
  148. return array(substr($class, strrpos($class, '\\') + 1), $field->getValue());
  149. },
  150. $form->all()
  151. ),
  152. '->getDefaultValues() '.$message
  153. );
  154. }
  155. public function provideInitializeValues()
  156. {
  157. return array(
  158. array(
  159. 'does not take into account input fields without a name attribute',
  160. '<input type="text" value="foo" />
  161. <input type="submit" />',
  162. array(),
  163. ),
  164. array(
  165. 'does not take into account input fields with an empty name attribute value',
  166. '<input type="text" name="" value="foo" />
  167. <input type="submit" />',
  168. array(),
  169. ),
  170. array(
  171. 'takes into account disabled input fields',
  172. '<input type="text" name="foo" value="foo" disabled="disabled" />
  173. <input type="submit" />',
  174. array('foo' => array('InputFormField', 'foo')),
  175. ),
  176. array(
  177. 'appends the submitted button value',
  178. '<input type="submit" name="bar" value="bar" />',
  179. array('bar' => array('InputFormField', 'bar')),
  180. ),
  181. array(
  182. 'appends the submitted button value for Button element',
  183. '<button type="submit" name="bar" value="bar">Bar</button>',
  184. array('bar' => array('InputFormField', 'bar')),
  185. ),
  186. array(
  187. 'appends the submitted button value but not other submit buttons',
  188. '<input type="submit" name="bar" value="bar" />
  189. <input type="submit" name="foobar" value="foobar" />',
  190. array('foobar' => array('InputFormField', 'foobar')),
  191. ),
  192. array(
  193. 'returns textareas',
  194. '<textarea name="foo">foo</textarea>
  195. <input type="submit" />',
  196. array('foo' => array('TextareaFormField', 'foo')),
  197. ),
  198. array(
  199. 'returns inputs',
  200. '<input type="text" name="foo" value="foo" />
  201. <input type="submit" />',
  202. array('foo' => array('InputFormField', 'foo')),
  203. ),
  204. array(
  205. 'returns checkboxes',
  206. '<input type="checkbox" name="foo" value="foo" checked="checked" />
  207. <input type="submit" />',
  208. array('foo' => array('ChoiceFormField', 'foo')),
  209. ),
  210. array(
  211. 'returns not-checked checkboxes',
  212. '<input type="checkbox" name="foo" value="foo" />
  213. <input type="submit" />',
  214. array('foo' => array('ChoiceFormField', false)),
  215. ),
  216. array(
  217. 'returns radio buttons',
  218. '<input type="radio" name="foo" value="foo" />
  219. <input type="radio" name="foo" value="bar" checked="bar" />
  220. <input type="submit" />',
  221. array('foo' => array('ChoiceFormField', 'bar')),
  222. ),
  223. array(
  224. 'returns file inputs',
  225. '<input type="file" name="foo" />
  226. <input type="submit" />',
  227. array('foo' => array('FileFormField', array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))),
  228. ),
  229. );
  230. }
  231. public function testGetFormNode()
  232. {
  233. $dom = new \DOMDocument();
  234. $dom->loadHTML('<html><form><input type="submit" /></form></html>');
  235. $form = new Form($dom->getElementsByTagName('input')->item(0), 'http://example.com');
  236. $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
  237. }
  238. public function testGetMethod()
  239. {
  240. $form = $this->createForm('<form><input type="submit" /></form>');
  241. $this->assertEquals('GET', $form->getMethod(), '->getMethod() returns get if no method is defined');
  242. $form = $this->createForm('<form method="post"><input type="submit" /></form>');
  243. $this->assertEquals('POST', $form->getMethod(), '->getMethod() returns the method attribute value of the form');
  244. $form = $this->createForm('<form method="post"><input type="submit" /></form>', 'put');
  245. $this->assertEquals('PUT', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
  246. $form = $this->createForm('<form method="post"><input type="submit" /></form>', 'delete');
  247. $this->assertEquals('DELETE', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
  248. $form = $this->createForm('<form method="post"><input type="submit" /></form>', 'patch');
  249. $this->assertEquals('PATCH', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
  250. }
  251. public function testGetSetValue()
  252. {
  253. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  254. $this->assertEquals('foo', $form['foo']->getValue(), '->offsetGet() returns the value of a form field');
  255. $form['foo'] = 'bar';
  256. $this->assertEquals('bar', $form['foo']->getValue(), '->offsetSet() changes the value of a form field');
  257. try {
  258. $form['foobar'] = 'bar';
  259. $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  260. } catch (\InvalidArgumentException $e) {
  261. $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  262. }
  263. try {
  264. $form['foobar'];
  265. $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  266. } catch (\InvalidArgumentException $e) {
  267. $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  268. }
  269. }
  270. public function testSetValueOnMultiValuedFieldsWithMalformedName()
  271. {
  272. $form = $this->createForm('<form><input type="text" name="foo[bar]" value="bar" /><input type="text" name="foo[baz]" value="baz" /><input type="submit" /></form>');
  273. try {
  274. $form['foo[bar'] = 'bar';
  275. $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the name is malformed.');
  276. } catch (\InvalidArgumentException $e) {
  277. $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the name is malformed.');
  278. }
  279. }
  280. public function testOffsetUnset()
  281. {
  282. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  283. unset($form['foo']);
  284. $this->assertFalse(isset($form['foo']), '->offsetUnset() removes a field');
  285. }
  286. public function testOffsetExists()
  287. {
  288. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  289. $this->assertTrue(isset($form['foo']), '->offsetExists() return true if the field exists');
  290. $this->assertFalse(isset($form['bar']), '->offsetExists() return false if the field does not exist');
  291. }
  292. public function testGetValues()
  293. {
  294. $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  295. $this->assertEquals(array('foo[bar]' => 'foo', 'bar' => 'bar'), $form->getValues(), '->getValues() returns all form field values');
  296. $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  297. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include not-checked checkboxes');
  298. $form = $this->createForm('<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  299. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include file input fields');
  300. $form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  301. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
  302. }
  303. public function testSetValues()
  304. {
  305. $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" checked="checked" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  306. $form->setValues(array('foo' => false, 'bar' => 'foo'));
  307. $this->assertEquals(array('bar' => 'foo'), $form->getValues(), '->setValues() sets the values of fields');
  308. }
  309. public function testMultiselectSetValues()
  310. {
  311. $form = $this->createForm('<form><select multiple="multiple" name="multi"><option value="foo">foo</option><option value="bar">bar</option></select><input type="submit" /></form>');
  312. $form->setValues(array('multi' => array("foo", "bar")));
  313. $this->assertEquals(array('multi' => array('foo', 'bar')), $form->getValues(), '->setValue() sets the values of select');
  314. }
  315. public function testGetPhpValues()
  316. {
  317. $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  318. $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');
  319. }
  320. public function testGetFiles()
  321. {
  322. $form = $this->createForm('<form><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  323. $this->assertEquals(array(), $form->getFiles(), '->getFiles() returns an empty array if method is get');
  324. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  325. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for POST');
  326. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'put');
  327. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PUT');
  328. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'delete');
  329. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for DELETE');
  330. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'patch');
  331. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PATCH');
  332. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
  333. $this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
  334. }
  335. public function testGetPhpFiles()
  336. {
  337. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  338. $this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');
  339. }
  340. /**
  341. * @dataProvider provideGetUriValues
  342. */
  343. public function testGetUri($message, $form, $values, $uri, $method = null)
  344. {
  345. $form = $this->createForm($form, $method);
  346. $form->setValues($values);
  347. $this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
  348. }
  349. public function testGetBaseUri()
  350. {
  351. $dom = new \DOMDocument();
  352. $dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  353. $nodes = $dom->getElementsByTagName('input');
  354. $form = new Form($nodes->item($nodes->length - 1), 'http://www.foo.com/');
  355. $this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
  356. }
  357. public function testGetUriWithAnchor()
  358. {
  359. $form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com/id/123');
  360. $this->assertEquals('http://example.com/id/123#foo', $form->getUri());
  361. }
  362. public function testGetUriActionAbsolute()
  363. {
  364. $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>';
  365. $form = $this->createForm($formHtml);
  366. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  367. $form = $this->createForm($formHtml, null, 'https://login.foo.com');
  368. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  369. $form = $this->createForm($formHtml, null, 'https://login.foo.com/bar/');
  370. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  371. // The action URI haven't the same domain Host have an another domain as Host
  372. $form = $this->createForm($formHtml, null, 'https://www.foo.com');
  373. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  374. $form = $this->createForm($formHtml, null, 'https://www.foo.com/bar/');
  375. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  376. }
  377. public function testGetUriAbsolute()
  378. {
  379. $form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
  380. $this->assertEquals('http://localhost/foo/foo', $form->getUri(), '->getUri() returns absolute URIs');
  381. $form = $this->createForm('<form action="/foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
  382. $this->assertEquals('http://localhost/foo', $form->getUri(), '->getUri() returns absolute URIs');
  383. }
  384. public function testGetUriWithOnlyQueryString()
  385. {
  386. $form = $this->createForm('<form action="?get=param"><input type="submit" /></form>', null, 'http://localhost/foo/bar');
  387. $this->assertEquals('http://localhost/foo/bar?get=param', $form->getUri(), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
  388. }
  389. public function testGetUriWithoutAction()
  390. {
  391. $form = $this->createForm('<form><input type="submit" /></form>', null, 'http://localhost/foo/bar');
  392. $this->assertEquals('http://localhost/foo/bar', $form->getUri(), '->getUri() returns path if no action defined');
  393. }
  394. public function provideGetUriValues()
  395. {
  396. return array(
  397. array(
  398. 'returns the URI of the form',
  399. '<form action="/foo"><input type="submit" /></form>',
  400. array(),
  401. '/foo'
  402. ),
  403. array(
  404. 'appends the form values if the method is get',
  405. '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  406. array(),
  407. '/foo?foo=foo'
  408. ),
  409. array(
  410. 'appends the form values and merges the submitted values',
  411. '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  412. array('foo' => 'bar'),
  413. '/foo?foo=bar'
  414. ),
  415. array(
  416. 'does not append values if the method is post',
  417. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  418. array(),
  419. '/foo'
  420. ),
  421. array(
  422. 'does not append values if the method is patch',
  423. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  424. array(),
  425. '/foo',
  426. 'PUT'
  427. ),
  428. array(
  429. 'does not append values if the method is delete',
  430. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  431. array(),
  432. '/foo',
  433. 'DELETE'
  434. ),
  435. array(
  436. 'does not append values if the method is put',
  437. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  438. array(),
  439. '/foo',
  440. 'PATCH'
  441. ),
  442. array(
  443. 'appends the form values to an existing query string',
  444. '<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  445. array(),
  446. '/foo?bar=bar&foo=foo'
  447. ),
  448. array(
  449. 'returns an empty URI if the action is empty',
  450. '<form><input type="submit" /></form>',
  451. array(),
  452. '/',
  453. ),
  454. array(
  455. 'appends the form values even if the action is empty',
  456. '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  457. array(),
  458. '/?foo=foo',
  459. ),
  460. array(
  461. 'chooses the path if the action attribute value is a sharp (#)',
  462. '<form action="#" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  463. array(),
  464. '/#',
  465. ),
  466. );
  467. }
  468. public function testHas()
  469. {
  470. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  471. $this->assertFalse($form->has('foo'), '->has() returns false if a field is not in the form');
  472. $this->assertTrue($form->has('bar'), '->has() returns true if a field is in the form');
  473. }
  474. public function testRemove()
  475. {
  476. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  477. $form->remove('bar');
  478. $this->assertFalse($form->has('bar'), '->remove() removes a field');
  479. }
  480. public function testGet()
  481. {
  482. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  483. $this->assertEquals('Symfony\\Component\\DomCrawler\\Field\\InputFormField', get_class($form->get('bar')), '->get() returns the field object associated with the given name');
  484. try {
  485. $form->get('foo');
  486. $this->fail('->get() throws an \InvalidArgumentException if the field does not exist');
  487. } catch (\InvalidArgumentException $e) {
  488. $this->assertTrue(true, '->get() throws an \InvalidArgumentException if the field does not exist');
  489. }
  490. }
  491. public function testAll()
  492. {
  493. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  494. $fields = $form->all();
  495. $this->assertEquals(1, count($fields), '->all() return an array of form field objects');
  496. $this->assertEquals('Symfony\\Component\\DomCrawler\\Field\\InputFormField', get_class($fields['bar']), '->all() return an array of form field objects');
  497. }
  498. public function testSubmitWithoutAFormButton()
  499. {
  500. $dom = new \DOMDocument();
  501. $dom->loadHTML('
  502. <html>
  503. <form>
  504. <input type="foo" />
  505. </form>
  506. </html>
  507. ');
  508. $nodes = $dom->getElementsByTagName('form');
  509. $form = new Form($nodes->item(0), 'http://example.com');
  510. $this->assertSame($nodes->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
  511. }
  512. /**
  513. * @expectedException \InvalidArgumentException
  514. */
  515. public function testFormFieldRegistryAddThrowAnExceptionWhenTheNameIsMalformed()
  516. {
  517. $registry = new FormFieldRegistry();
  518. $registry->add($this->getFormFieldMock('[foo]'));
  519. }
  520. /**
  521. * @expectedException \InvalidArgumentException
  522. */
  523. public function testFormFieldRegistryRemoveThrowAnExceptionWhenTheNameIsMalformed()
  524. {
  525. $registry = new FormFieldRegistry();
  526. $registry->remove('[foo]');
  527. }
  528. /**
  529. * @expectedException \InvalidArgumentException
  530. */
  531. public function testFormFieldRegistryGetThrowAnExceptionWhenTheNameIsMalformed()
  532. {
  533. $registry = new FormFieldRegistry();
  534. $registry->get('[foo]');
  535. }
  536. /**
  537. * @expectedException \InvalidArgumentException
  538. */
  539. public function testFormFieldRegistryGetThrowAnExceptionWhenTheFieldDoesNotExist()
  540. {
  541. $registry = new FormFieldRegistry();
  542. $registry->get('foo');
  543. }
  544. /**
  545. * @expectedException \InvalidArgumentException
  546. */
  547. public function testFormFieldRegistrySetThrowAnExceptionWhenTheNameIsMalformed()
  548. {
  549. $registry = new FormFieldRegistry();
  550. $registry->set('[foo]', null);
  551. }
  552. /**
  553. * @expectedException \InvalidArgumentException
  554. */
  555. public function testFormFieldRegistrySetThrowAnExceptionWhenTheFieldDoesNotExist()
  556. {
  557. $registry = new FormFieldRegistry();
  558. $registry->set('foo', null);
  559. }
  560. public function testFormFieldRegistryHasReturnsTrueWhenTheFQNExists()
  561. {
  562. $registry = new FormFieldRegistry();
  563. $registry->add($this->getFormFieldMock('foo[bar]'));
  564. $this->assertTrue($registry->has('foo'));
  565. $this->assertTrue($registry->has('foo[bar]'));
  566. $this->assertFalse($registry->has('bar'));
  567. $this->assertFalse($registry->has('foo[foo]'));
  568. }
  569. public function testFormRegistryFieldsCanBeRemoved()
  570. {
  571. $registry = new FormFieldRegistry();
  572. $registry->add($this->getFormFieldMock('foo'));
  573. $registry->remove('foo');
  574. $this->assertFalse($registry->has('foo'));
  575. }
  576. public function testFormRegistrySupportsMultivaluedFields()
  577. {
  578. $registry = new FormFieldRegistry();
  579. $registry->add($this->getFormFieldMock('foo[]'));
  580. $registry->add($this->getFormFieldMock('foo[]'));
  581. $registry->add($this->getFormFieldMock('bar[5]'));
  582. $registry->add($this->getFormFieldMock('bar[]'));
  583. $registry->add($this->getFormFieldMock('bar[baz]'));
  584. $this->assertEquals(
  585. array('foo[0]', 'foo[1]', 'bar[5]', 'bar[6]', 'bar[baz]'),
  586. array_keys($registry->all())
  587. );
  588. }
  589. public function testFormRegistrySetValues()
  590. {
  591. $registry = new FormFieldRegistry();
  592. $registry->add($f2 = $this->getFormFieldMock('foo[2]'));
  593. $registry->add($f3 = $this->getFormFieldMock('foo[3]'));
  594. $registry->add($fbb = $this->getFormFieldMock('foo[bar][baz]'));
  595. $f2
  596. ->expects($this->exactly(2))
  597. ->method('setValue')
  598. ->with(2)
  599. ;
  600. $f3
  601. ->expects($this->exactly(2))
  602. ->method('setValue')
  603. ->with(3)
  604. ;
  605. $fbb
  606. ->expects($this->exactly(2))
  607. ->method('setValue')
  608. ->with('fbb')
  609. ;
  610. $registry->set('foo[2]', 2);
  611. $registry->set('foo[3]', 3);
  612. $registry->set('foo[bar][baz]', 'fbb');
  613. $registry->set('foo', array(
  614. 2 => 2,
  615. 3 => 3,
  616. 'bar' => array(
  617. 'baz' => 'fbb'
  618. )
  619. ));
  620. }
  621. protected function getFormFieldMock($name, $value = null)
  622. {
  623. $field = $this
  624. ->getMockBuilder('Symfony\\Component\\DomCrawler\\Field\\FormField')
  625. ->setMethods(array('getName', 'getValue', 'setValue', 'initialize'))
  626. ->disableOriginalConstructor()
  627. ->getMock()
  628. ;
  629. $field
  630. ->expects($this->any())
  631. ->method('getName')
  632. ->will($this->returnValue($name))
  633. ;
  634. $field
  635. ->expects($this->any())
  636. ->method('getValue')
  637. ->will($this->returnValue($value))
  638. ;
  639. return $field;
  640. }
  641. protected function createForm($form, $method = null, $currentUri = null)
  642. {
  643. $dom = new \DOMDocument();
  644. $dom->loadHTML('<html>'.$form.'</html>');
  645. $nodes = $dom->getElementsByTagName('input');
  646. $xPath = new \DOMXPath($dom);
  647. $nodes = $xPath->query('//input | //button');
  648. if (null === $currentUri) {
  649. $currentUri = 'http://example.com/';
  650. }
  651. return new Form($nodes->item($nodes->length - 1), $currentUri, $method);
  652. }
  653. protected function createTestHtml5Form() {
  654. $dom = new \DOMDocument();
  655. $dom->loadHTML('
  656. <html>
  657. <h1>Hello form</h1>
  658. <form id="form-1" action="" method="POST">
  659. <div><input type="checkbox" name="apples[]" value="1" checked /></div>
  660. <input form="form_2" type="checkbox" name="oranges[]" value="1" checked />
  661. <div><label></label><input form="form-1" type="hidden" name="form_name" value="form-1" /></div>
  662. <input form="form-1" type="submit" name="button_1" value="Capture fields" />
  663. <button form="form_2" type="submit" name="button_2">Submit form_2</button>
  664. </form>
  665. <input form="form-1" type="checkbox" name="apples[]" value="2" checked />
  666. <form id="form_2" action="" method="POST">
  667. <div><div><input type="checkbox" name="oranges[]" value="2" checked />
  668. <input type="checkbox" name="oranges[]" value="3" checked /></div></div>
  669. <input form="form_2" type="hidden" name="form_name" value="form_2" />
  670. <input form="form-1" type="hidden" name="outer_field" value="success" />
  671. <button form="form-1" type="submit" name="button_3">Submit from outside the form</button>
  672. <div>
  673. <label for="app_frontend_form_type_contact_form_type_contactType">Message subject</label>
  674. <div>
  675. <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>
  676. </div>
  677. </div>
  678. <div>
  679. <label for="app_frontend_form_type_contact_form_type_firstName">Firstname</label>
  680. <input type="text" name="app_frontend_form_type_contact_form_type[firstName]" value="John" id="app_frontend_form_type_contact_form_type_firstName"/>
  681. </div>
  682. </form>
  683. <button />
  684. </html>');
  685. return $dom;
  686. }
  687. }