FormTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Form;
  11. require_once __DIR__ . '/Fixtures/Author.php';
  12. require_once __DIR__ . '/Fixtures/TestField.php';
  13. use Symfony\Component\Form\Form;
  14. use Symfony\Component\Form\FormContext;
  15. use Symfony\Component\Form\Field;
  16. use Symfony\Component\Form\HiddenField;
  17. use Symfony\Component\Form\FieldGroup;
  18. use Symfony\Component\Form\PropertyPath;
  19. use Symfony\Component\Validator\ConstraintViolation;
  20. use Symfony\Component\Validator\ConstraintViolationList;
  21. use Symfony\Tests\Component\Form\Fixtures\Author;
  22. use Symfony\Tests\Component\Form\Fixtures\TestField;
  23. class FormTest_PreconfiguredForm extends Form
  24. {
  25. protected function configure()
  26. {
  27. $this->add(new Field('firstName'));
  28. parent::configure();
  29. }
  30. }
  31. class TestSetDataBeforeConfigureForm extends Form
  32. {
  33. protected $testCase;
  34. protected $object;
  35. public function __construct($testCase, $name, $object, $validator)
  36. {
  37. $this->testCase = $testCase;
  38. $this->object = $object;
  39. parent::__construct($name, $object, $validator);
  40. }
  41. protected function configure()
  42. {
  43. $this->testCase->assertEquals($this->object, $this->getData());
  44. parent::configure();
  45. }
  46. }
  47. class FormTest extends \PHPUnit_Framework_TestCase
  48. {
  49. protected $validator;
  50. protected $form;
  51. public static function setUpBeforeClass()
  52. {
  53. @session_start();
  54. }
  55. protected function setUp()
  56. {
  57. $this->validator = $this->createMockValidator();
  58. $this->form = new Form('author', new Author(), $this->validator, array(
  59. 'csrf_protection' => false,
  60. 'csrf_secrets' => array(),
  61. ));
  62. }
  63. public function testConstructInitializesObject()
  64. {
  65. $this->assertEquals(new Author(), $this->form->getData());
  66. }
  67. public function testSetDataBeforeConfigure()
  68. {
  69. new TestSetDataBeforeConfigureForm($this, 'author', new Author(), $this->validator);
  70. }
  71. public function testNoCsrfProtectionByDefault()
  72. {
  73. $form = new Form('author', new Author(), $this->validator);
  74. $this->assertFalse($form->isCsrfProtected());
  75. }
  76. public function testCsrfProtectionCanBeEnabled()
  77. {
  78. $form = new Form('author', new Author(), $this->validator, array(
  79. 'csrf_protection' => true,
  80. ));
  81. $this->assertTrue($form->isCsrfProtected());
  82. }
  83. public function testGeneratedCsrfSecretByDefault()
  84. {
  85. $form = new Form('author', new Author(), $this->validator, array(
  86. 'csrf_protection' => true,
  87. ));
  88. $secrets = $form->getCsrfSecrets();
  89. $this->assertEquals(1, count($secrets));
  90. $this->assertTrue(strlen($secrets[0]) >= 32);
  91. }
  92. public function testCsrfSecretsCanBeSet()
  93. {
  94. $form = new Form('author', new Author(), $this->validator, array(
  95. 'csrf_protection' => true,
  96. 'csrf_field_name' => '_token',
  97. 'csrf_secrets' => array('foobar', 'secret'),
  98. ));
  99. $this->assertEquals(md5(get_class($form).'foobarsecret'), $form['_token']->getData());
  100. }
  101. public function testCsrfSecretsCanBeSetAsClosures()
  102. {
  103. $closure = function () {
  104. return 'foobar';
  105. };
  106. $form = new Form('author', new Author(), $this->validator, array(
  107. 'csrf_protection' => true,
  108. 'csrf_field_name' => '_token',
  109. 'csrf_secrets' => array($closure, 'secret'),
  110. ));
  111. $this->assertEquals(md5(get_class($form).'foobarsecret'), $form['_token']->getData());
  112. }
  113. public function testCsrfFieldNameCanBeSet()
  114. {
  115. $form = new Form('author', new Author(), $this->validator, array(
  116. 'csrf_protection' => true,
  117. 'csrf_field_name' => 'foobar',
  118. ));
  119. $this->assertEquals('foobar', $form->getCsrfFieldName());
  120. }
  121. public function testCsrfProtectedFormsHaveExtraField()
  122. {
  123. $form = new Form('author', new Author(), $this->validator, array(
  124. 'csrf_protection' => true,
  125. ));
  126. $this->assertTrue($form->has($this->form->getCsrfFieldName()));
  127. $field = $form->get($form->getCsrfFieldName());
  128. $this->assertTrue($field instanceof HiddenField);
  129. $this->assertGreaterThanOrEqual(32, strlen($field->getDisplayedData()));
  130. }
  131. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  132. {
  133. $this->form->bind(array());
  134. $this->assertTrue($this->form->isCsrfTokenValid());
  135. }
  136. public function testIsCsrfTokenValidPasses()
  137. {
  138. $form = new Form('author', new Author(), $this->validator, array(
  139. 'csrf_protection' => true,
  140. ));
  141. $field = $form->getCsrfFieldName();
  142. $token = $form->get($field)->getDisplayedData();
  143. $form->bind(array($field => $token));
  144. $this->assertTrue($form->isCsrfTokenValid());
  145. }
  146. public function testIsCsrfTokenValidFails()
  147. {
  148. $form = new Form('author', new Author(), $this->validator, array(
  149. 'csrf_protection' => true,
  150. ));
  151. $field = $form->getCsrfFieldName();
  152. $form->bind(array($field => 'foobar'));
  153. $this->assertFalse($form->isCsrfTokenValid());
  154. }
  155. public function testValidationGroupNullByDefault()
  156. {
  157. $this->assertNull($this->form->getValidationGroups());
  158. }
  159. public function testValidationGroupsCanBeSetToString()
  160. {
  161. $form = new Form('author', new Author(), $this->validator, array(
  162. 'validation_groups' => 'group',
  163. ));
  164. $this->assertEquals(array('group'), $form->getValidationGroups());
  165. }
  166. public function testValidationGroupsCanBeSetToArray()
  167. {
  168. $form = new Form('author', new Author(), $this->validator, array(
  169. 'validation_groups' => array('group1', 'group2'),
  170. ));
  171. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  172. }
  173. public function testBindUsesValidationGroups()
  174. {
  175. $field = $this->createMockField('firstName');
  176. $form = new Form('author', new Author(), $this->validator, array(
  177. 'validation_groups' => 'group',
  178. ));
  179. $form->add($field);
  180. $this->validator->expects($this->once())
  181. ->method('validate')
  182. ->with($this->equalTo($form), $this->equalTo(array('group')));
  183. $form->bind(array()); // irrelevant
  184. }
  185. public function testBindThrowsExceptionIfNoValidatorIsSet()
  186. {
  187. $field = $this->createMockField('firstName');
  188. $form = new Form('author', new Author());
  189. $form->add($field);
  190. $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
  191. $form->bind(array()); // irrelevant
  192. }
  193. public function testMultipartFormsWithoutParentsRequireFiles()
  194. {
  195. $form = new Form('author', new Author(), $this->validator);
  196. $form->add($this->createMultipartMockField('file'));
  197. $this->setExpectedException('InvalidArgumentException');
  198. // should be given in second argument
  199. $form->bind(array('file' => 'test.txt'));
  200. }
  201. public function testMultipartFormsWithParentsRequireNoFiles()
  202. {
  203. $form = new Form('author', new Author(), $this->validator);
  204. $form->add($this->createMultipartMockField('file'));
  205. $form->setParent($this->createMockField('group'));
  206. // files are expected to be converted by the parent
  207. $form->bind(array('file' => 'test.txt'));
  208. }
  209. public function testUpdateFromPropertyIsIgnoredIfFormHasObject()
  210. {
  211. $author = new Author();
  212. $author->child = new Author();
  213. $standaloneChild = new Author();
  214. $form = new Form('child', $standaloneChild);
  215. $form->updateFromProperty($author);
  216. // should not be $author->child!!
  217. $this->assertSame($standaloneChild, $form->getData());
  218. }
  219. public function testUpdateFromPropertyIsNotIgnoredIfFormHasNoObject()
  220. {
  221. $author = new Author();
  222. $author->child = new Author();
  223. $form = new Form('child');
  224. $form->updateFromProperty($author);
  225. // should not be $author->child!!
  226. $this->assertSame($author->child, $form->getData());
  227. }
  228. public function testUpdatePropertyIsIgnoredIfFormHasObject()
  229. {
  230. $author = new Author();
  231. $author->child = $child = new Author();
  232. $standaloneChild = new Author();
  233. $form = new Form('child', $standaloneChild);
  234. $form->updateProperty($author);
  235. // $author->child was not modified
  236. $this->assertSame($child, $author->child);
  237. }
  238. public function testUpdatePropertyIsNotIgnoredIfFormHasNoObject()
  239. {
  240. $author = new Author();
  241. $child = new Author();
  242. $form = new Form('child');
  243. $form->setData($child);
  244. $form->updateProperty($author);
  245. // $author->child was set
  246. $this->assertSame($child, $author->child);
  247. }
  248. protected function createMockField($key)
  249. {
  250. $field = $this->getMock(
  251. 'Symfony\Component\Form\FieldInterface',
  252. array(),
  253. array(),
  254. '',
  255. false, // don't use constructor
  256. false // don't call parent::__clone
  257. );
  258. $field->expects($this->any())
  259. ->method('getKey')
  260. ->will($this->returnValue($key));
  261. return $field;
  262. }
  263. protected function createMockFieldGroup($key)
  264. {
  265. $field = $this->getMock(
  266. 'Symfony\Component\Form\FieldGroup',
  267. array(),
  268. array(),
  269. '',
  270. false, // don't use constructor
  271. false // don't call parent::__clone
  272. );
  273. $field->expects($this->any())
  274. ->method('getKey')
  275. ->will($this->returnValue($key));
  276. return $field;
  277. }
  278. protected function createMultipartMockField($key)
  279. {
  280. $field = $this->createMockField($key);
  281. $field->expects($this->any())
  282. ->method('isMultipart')
  283. ->will($this->returnValue(true));
  284. return $field;
  285. }
  286. protected function createMockValidator()
  287. {
  288. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  289. }
  290. }