FormTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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\Tests\Component\DomCrawler;
  11. use Symfony\Component\DomCrawler\Form;
  12. class FormTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructorThrowsExceptionIfTheNodeHasNoFormAncestor()
  15. {
  16. $dom = new \DOMDocument();
  17. $dom->loadHTML('
  18. <html>
  19. <input type="submit" />
  20. <form>
  21. <input type="foo" />
  22. </form>
  23. <button />
  24. </html>
  25. ');
  26. $nodes = $dom->getElementsByTagName('input');
  27. try {
  28. $form = new Form($nodes->item(0), 'http://example.com');
  29. $this->fail('__construct() throws a \\LogicException if the node has no form ancestor');
  30. } catch (\LogicException $e) {
  31. $this->assertTrue(true, '__construct() throws a \\LogicException if the node has no form ancestor');
  32. }
  33. try {
  34. $form = new Form($nodes->item(1), 'http://example.com');
  35. $this->fail('__construct() throws a \\LogicException if the input type is not submit, button, or image');
  36. } catch (\LogicException $e) {
  37. $this->assertTrue(true, '__construct() throws a \\LogicException if the input type is not submit, button, or image');
  38. }
  39. $nodes = $dom->getElementsByTagName('button');
  40. try {
  41. $form = new Form($nodes->item(0), '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. }
  47. /**
  48. * @dataProvider provideInitializeValues
  49. */
  50. public function testConstructor($message, $form, $values)
  51. {
  52. $form = $this->createForm('<form>'.$form.'</form>');
  53. $this->assertEquals($values, array_map(function ($field) { return array(get_class($field), $field->getValue()); }, $form->all()), '->getDefaultValues() '.$message);
  54. }
  55. public function provideInitializeValues()
  56. {
  57. return array(
  58. array(
  59. 'does not take into account input fields without a name attribute',
  60. '<input type="text" value="foo" />
  61. <input type="submit" />',
  62. array(),
  63. ),
  64. array(
  65. 'takes into account disabled input fields',
  66. '<input type="text" name="foo" value="foo" disabled="disabled" />
  67. <input type="submit" />',
  68. array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\InputFormField', 'foo')),
  69. ),
  70. array(
  71. 'appends the submitted button value',
  72. '<input type="submit" name="bar" value="bar" />',
  73. array('bar' => array('Symfony\\Component\\DomCrawler\\Field\\InputFormField', 'bar')),
  74. ),
  75. array(
  76. 'appends the submitted button value but not other submit buttons',
  77. '<input type="submit" name="bar" value="bar" />
  78. <input type="submit" name="foobar" value="foobar" />',
  79. array('foobar' => array('Symfony\\Component\\DomCrawler\\Field\\InputFormField', 'foobar')),
  80. ),
  81. array(
  82. 'returns textareas',
  83. '<textarea name="foo">foo</textarea>
  84. <input type="submit" />',
  85. array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\TextareaFormField', 'foo')),
  86. ),
  87. array(
  88. 'returns inputs',
  89. '<input type="text" name="foo" value="foo" />
  90. <input type="submit" />',
  91. array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\InputFormField', 'foo')),
  92. ),
  93. array(
  94. 'returns checkboxes',
  95. '<input type="checkbox" name="foo" value="foo" checked="checked" />
  96. <input type="submit" />',
  97. array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\ChoiceFormField', 'foo')),
  98. ),
  99. array(
  100. 'returns not-checked checkboxes',
  101. '<input type="checkbox" name="foo" value="foo" />
  102. <input type="submit" />',
  103. array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\ChoiceFormField', false)),
  104. ),
  105. array(
  106. 'returns radio buttons',
  107. '<input type="radio" name="foo" value="foo" />
  108. <input type="radio" name="foo" value="bar" checked="bar" />
  109. <input type="submit" />',
  110. array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\ChoiceFormField', 'bar')),
  111. ),
  112. array(
  113. 'returns file inputs',
  114. '<input type="file" name="foo" />
  115. <input type="submit" />',
  116. array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\FileFormField', array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))),
  117. ),
  118. );
  119. }
  120. public function testGetFormNode()
  121. {
  122. $dom = new \DOMDocument();
  123. $dom->loadHTML('<html><form><input type="submit" /></form></html>');
  124. $form = new Form($dom->getElementsByTagName('input')->item(0), 'http://example.com');
  125. $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
  126. }
  127. public function testGetMethod()
  128. {
  129. $form = $this->createForm('<form><input type="submit" /></form>');
  130. $this->assertEquals('GET', $form->getMethod(), '->getMethod() returns get if no method is defined');
  131. $form = $this->createForm('<form method="post"><input type="submit" /></form>');
  132. $this->assertEquals('POST', $form->getMethod(), '->getMethod() returns the method attribute value of the form');
  133. $form = $this->createForm('<form method="post"><input type="submit" /></form>', 'put');
  134. $this->assertEquals('PUT', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
  135. }
  136. public function testGetSetValue()
  137. {
  138. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  139. $this->assertEquals('foo', $form['foo']->getValue(), '->offsetGet() returns the value of a form field');
  140. $form['foo'] = 'bar';
  141. $this->assertEquals('bar', $form['foo']->getValue(), '->offsetSet() changes the value of a form field');
  142. try {
  143. $form['foobar'] = 'bar';
  144. $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  145. } catch (\InvalidArgumentException $e) {
  146. $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  147. }
  148. try {
  149. $form['foobar'];
  150. $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  151. } catch (\InvalidArgumentException $e) {
  152. $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
  153. }
  154. }
  155. public function testOffsetUnset()
  156. {
  157. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  158. unset($form['foo']);
  159. $this->assertFalse(isset($form['foo']), '->offsetUnset() removes a field');
  160. }
  161. public function testOffsetExists()
  162. {
  163. $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
  164. $this->assertTrue(isset($form['foo']), '->offsetExists() return true if the field exists');
  165. $this->assertFalse(isset($form['bar']), '->offsetExists() return false if the field does not exist');
  166. }
  167. public function testGetValues()
  168. {
  169. $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  170. $this->assertEquals(array('foo[bar]' => 'foo', 'bar' => 'bar'), $form->getValues(), '->getValues() returns all form field values');
  171. $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  172. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include not-checked checkboxes');
  173. $form = $this->createForm('<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  174. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include file input fields');
  175. $form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  176. $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
  177. }
  178. public function testSetValues()
  179. {
  180. $form = $this->createForm('<form><input type="checkbox" name="foo" value="foo" checked="checked" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  181. $form->setValues(array('foo' => false, 'bar' => 'foo'));
  182. $this->assertEquals(array('bar' => 'foo'), $form->getValues(), '->setValues() sets the values of fields');
  183. }
  184. public function testGetPhpValues()
  185. {
  186. $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  187. $this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');
  188. }
  189. public function testGetFiles()
  190. {
  191. $form = $this->createForm('<form><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  192. $this->assertEquals(array(), $form->getFiles(), '->getFiles() returns an empty array if method is get');
  193. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  194. $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields');
  195. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
  196. $this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
  197. }
  198. public function testGetPhpFiles()
  199. {
  200. $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  201. $this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');
  202. }
  203. /**
  204. * @dataProvider provideGetUriValues
  205. */
  206. public function testGetUri($message, $form, $values, $uri)
  207. {
  208. $form = $this->createForm($form);
  209. $form->setValues($values);
  210. $this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
  211. }
  212. public function testGetUriActionAbsolute()
  213. {
  214. $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>';
  215. $form = $this->createForm($formHtml);
  216. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  217. $form = $this->createForm($formHtml, null, 'https://login.foo.com');
  218. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  219. $form = $this->createForm($formHtml, null, 'https://login.foo.com/bar/');
  220. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  221. // The action URI haven't the same domain Host have an another domain as Host
  222. $form = $this->createForm($formHtml, null, 'https://www.foo.com');
  223. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  224. $form = $this->createForm($formHtml, null, 'https://www.foo.com/bar/');
  225. $this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
  226. }
  227. public function testGetUriAbsolute()
  228. {
  229. $form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
  230. $this->assertEquals('http://localhost/foo/foo', $form->getUri(), '->getUri() returns absolute URIs');
  231. $form = $this->createForm('<form action="/foo"><input type="submit" /></form>', null, 'http://localhost/foo/');
  232. $this->assertEquals('http://localhost/foo', $form->getUri(), '->getUri() returns absolute URIs');
  233. }
  234. public function testGetUriWithOnlyQueryString()
  235. {
  236. $form = $this->createForm('<form action="?get=param"><input type="submit" /></form>', null, 'http://localhost/foo/bar');
  237. $this->assertEquals('http://localhost/foo/bar?get=param', $form->getUri(), '->getUri() returns absolute URIs only if the host has been defined in the constructor');
  238. }
  239. public function testGetUriWithoutAction()
  240. {
  241. $form = $this->createForm('<form><input type="submit" /></form>', null, 'http://localhost/foo/bar');
  242. $this->assertEquals('http://localhost/foo/bar', $form->getUri(), '->getUri() returns path if no action defined');
  243. }
  244. public function provideGetUriValues()
  245. {
  246. return array(
  247. array(
  248. 'returns the URI of the form',
  249. '<form action="/foo"><input type="submit" /></form>',
  250. array(),
  251. '/foo'
  252. ),
  253. array(
  254. 'appends the form values if the method is get',
  255. '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  256. array(),
  257. '/foo?foo=foo'
  258. ),
  259. array(
  260. 'appends the form values and merges the submitted values',
  261. '<form action="/foo"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  262. array('foo' => 'bar'),
  263. '/foo?foo=bar'
  264. ),
  265. array(
  266. 'does not append values if the method is post',
  267. '<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  268. array(),
  269. '/foo'
  270. ),
  271. array(
  272. 'appends the form values to an existing query string',
  273. '<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  274. array(),
  275. '/foo?bar=bar&foo=foo'
  276. ),
  277. array(
  278. 'returns an empty URI if the action is empty',
  279. '<form><input type="submit" /></form>',
  280. array(),
  281. '/',
  282. ),
  283. array(
  284. 'appends the form values even if the action is empty',
  285. '<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  286. array(),
  287. '/?foo=foo',
  288. ),
  289. array(
  290. 'chooses the path if the action attribute value is a sharp (#)',
  291. '<form action="#" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
  292. array(),
  293. '/#',
  294. ),
  295. );
  296. }
  297. public function testHas()
  298. {
  299. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  300. $this->assertFalse($form->has('foo'), '->has() returns false if a field is not in the form');
  301. $this->assertTrue($form->has('bar'), '->has() returns true if a field is in the form');
  302. }
  303. public function testRemove()
  304. {
  305. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  306. $form->remove('bar');
  307. $this->assertFalse($form->has('bar'), '->remove() removes a field');
  308. }
  309. public function testGet()
  310. {
  311. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  312. $this->assertEquals('Symfony\\Component\\DomCrawler\\Field\\InputFormField', get_class($form->get('bar')), '->get() returns the field object associated with the given name');
  313. try {
  314. $form->get('foo');
  315. $this->fail('->get() throws an \InvalidArgumentException if the field does not exist');
  316. } catch (\InvalidArgumentException $e) {
  317. $this->assertTrue(true, '->get() throws an \InvalidArgumentException if the field does not exist');
  318. }
  319. }
  320. public function testAll()
  321. {
  322. $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  323. $fields = $form->all();
  324. $this->assertEquals(1, count($fields), '->all() return an array of form field objects');
  325. $this->assertEquals('Symfony\\Component\\DomCrawler\\Field\\InputFormField', get_class($fields['bar']), '->all() return an array of form field objects');
  326. }
  327. public function testBase()
  328. {
  329. $dom = new \DOMDocument();
  330. $dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
  331. $nodes = $dom->getElementsByTagName('input');
  332. $form = new Form($nodes->item($nodes->length - 1), 'http://www.foo.com/');
  333. $this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
  334. }
  335. public function testUriWithAnchor()
  336. {
  337. $form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com/id/123');
  338. $this->assertEquals('http://example.com/id/123#foo', $form->getUri());
  339. }
  340. protected function createForm($form, $method = null, $currentUri = null)
  341. {
  342. $dom = new \DOMDocument();
  343. $dom->loadHTML('<html>'.$form.'</html>');
  344. $nodes = $dom->getElementsByTagName('input');
  345. if (null === $currentUri) {
  346. $currentUri = 'http://example.com/';
  347. }
  348. return new Form($nodes->item($nodes->length - 1), $currentUri, $method);
  349. }
  350. }