FormTest.php 31 KB

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