FormTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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\FormConfiguration;
  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. FormConfiguration::disableDefaultCsrfProtection();
  58. FormConfiguration::setDefaultCsrfSecrets(array());
  59. $this->validator = $this->createMockValidator();
  60. $this->form = new Form('author', new Author(), $this->validator);
  61. }
  62. public function testConstructInitializesObject()
  63. {
  64. $this->assertEquals(new Author(), $this->form->getData());
  65. }
  66. public function testSetDataBeforeConfigure()
  67. {
  68. new TestSetDataBeforeConfigureForm($this, 'author', new Author(), $this->validator);
  69. }
  70. public function testIsCsrfProtected()
  71. {
  72. $this->assertFalse($this->form->isCsrfProtected());
  73. $this->form->enableCsrfProtection();
  74. $this->assertTrue($this->form->isCsrfProtected());
  75. $this->form->disableCsrfProtection();
  76. $this->assertFalse($this->form->isCsrfProtected());
  77. }
  78. public function testNoCsrfProtectionByDefault()
  79. {
  80. $form = new Form('author', new Author(), $this->validator);
  81. $this->assertFalse($form->isCsrfProtected());
  82. }
  83. public function testDefaultCsrfProtectionCanBeEnabled()
  84. {
  85. FormConfiguration::enableDefaultCsrfProtection();
  86. $form = new Form('author', new Author(), $this->validator);
  87. $this->assertTrue($form->isCsrfProtected());
  88. }
  89. public function testGeneratedCsrfSecretByDefault()
  90. {
  91. $form = new Form('author', new Author(), $this->validator);
  92. $form->enableCsrfProtection();
  93. $this->assertTrue(strlen($form->getCsrfSecret()) >= 32);
  94. }
  95. public function testDefaultCsrfSecretsCanBeAdded()
  96. {
  97. FormConfiguration::addDefaultCsrfSecret('foobar');
  98. $form = new Form('author', new Author(), $this->validator);
  99. $form->enableCsrfProtection('_token', 'secret');
  100. $this->assertEquals(md5('secret'.get_class($form).'foobar'), $form['_token']->getData());
  101. }
  102. public function testDefaultCsrfSecretsCanBeAddedAsClosures()
  103. {
  104. FormConfiguration::addDefaultCsrfSecret(function () {
  105. return 'foobar';
  106. });
  107. $form = new Form('author', new Author(), $this->validator);
  108. $form->enableCsrfProtection('_token', 'secret');
  109. $this->assertEquals(md5('secret'.get_class($form).'foobar'), $form['_token']->getData());
  110. }
  111. public function testDefaultCsrfFieldNameCanBeSet()
  112. {
  113. FormConfiguration::setDefaultCsrfFieldName('foobar');
  114. $form = new Form('author', new Author(), $this->validator);
  115. $form->enableCsrfProtection();
  116. $this->assertEquals('foobar', $form->getCsrfFieldName());
  117. }
  118. public function testCsrfProtectedFormsHaveExtraField()
  119. {
  120. $this->form->enableCsrfProtection();
  121. $this->assertTrue($this->form->has($this->form->getCsrfFieldName()));
  122. $field = $this->form->get($this->form->getCsrfFieldName());
  123. $this->assertTrue($field instanceof HiddenField);
  124. $this->assertGreaterThanOrEqual(32, strlen($field->getDisplayedData()));
  125. }
  126. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  127. {
  128. $this->form->bind(array());
  129. $this->assertTrue($this->form->isCsrfTokenValid());
  130. }
  131. public function testIsCsrfTokenValidPasses()
  132. {
  133. $this->form->enableCsrfProtection();
  134. $field = $this->form->getCsrfFieldName();
  135. $token = $this->form->get($field)->getDisplayedData();
  136. $this->form->bind(array($field => $token));
  137. $this->assertTrue($this->form->isCsrfTokenValid());
  138. }
  139. public function testIsCsrfTokenValidFails()
  140. {
  141. $this->form->enableCsrfProtection();
  142. $field = $this->form->getCsrfFieldName();
  143. $this->form->bind(array($field => 'foobar'));
  144. $this->assertFalse($this->form->isCsrfTokenValid());
  145. }
  146. public function testValidationGroupsCanBeSet()
  147. {
  148. $form = new Form('author', new Author(), $this->validator);
  149. $this->assertNull($form->getValidationGroups());
  150. $form->setValidationGroups('group');
  151. $this->assertEquals(array('group'), $form->getValidationGroups());
  152. $form->setValidationGroups(array('group1', 'group2'));
  153. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  154. $form->setValidationGroups(null);
  155. $this->assertNull($form->getValidationGroups());
  156. }
  157. public function testBindUsesValidationGroups()
  158. {
  159. $field = $this->createMockField('firstName');
  160. $form = new Form('author', new Author(), $this->validator);
  161. $form->add($field);
  162. $form->setValidationGroups('group');
  163. $this->validator->expects($this->once())
  164. ->method('validate')
  165. ->with($this->equalTo($form), $this->equalTo(array('group')));
  166. $form->bind(array()); // irrelevant
  167. }
  168. public function testBindThrowsExceptionIfNoValidatorIsSet()
  169. {
  170. $field = $this->createMockField('firstName');
  171. $form = new Form('author', new Author());
  172. $form->add($field);
  173. $form->setValidationGroups('group');
  174. $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
  175. $form->bind(array()); // irrelevant
  176. }
  177. public function testMultipartFormsWithoutParentsRequireFiles()
  178. {
  179. $form = new Form('author', new Author(), $this->validator);
  180. $form->add($this->createMultipartMockField('file'));
  181. $this->setExpectedException('InvalidArgumentException');
  182. // should be given in second argument
  183. $form->bind(array('file' => 'test.txt'));
  184. }
  185. public function testMultipartFormsWithParentsRequireNoFiles()
  186. {
  187. $form = new Form('author', new Author(), $this->validator);
  188. $form->add($this->createMultipartMockField('file'));
  189. $form->setParent($this->createMockField('group'));
  190. // files are expected to be converted by the parent
  191. $form->bind(array('file' => 'test.txt'));
  192. }
  193. public function testUpdateFromPropertyIsIgnoredIfFormHasObject()
  194. {
  195. $author = new Author();
  196. $author->child = new Author();
  197. $standaloneChild = new Author();
  198. $form = new Form('child', $standaloneChild);
  199. $form->updateFromProperty($author);
  200. // should not be $author->child!!
  201. $this->assertSame($standaloneChild, $form->getData());
  202. }
  203. public function testUpdateFromPropertyIsNotIgnoredIfFormHasNoObject()
  204. {
  205. $author = new Author();
  206. $author->child = new Author();
  207. $form = new Form('child');
  208. $form->updateFromProperty($author);
  209. // should not be $author->child!!
  210. $this->assertSame($author->child, $form->getData());
  211. }
  212. public function testUpdatePropertyIsIgnoredIfFormHasObject()
  213. {
  214. $author = new Author();
  215. $author->child = $child = new Author();
  216. $standaloneChild = new Author();
  217. $form = new Form('child', $standaloneChild);
  218. $form->updateProperty($author);
  219. // $author->child was not modified
  220. $this->assertSame($child, $author->child);
  221. }
  222. public function testUpdatePropertyIsNotIgnoredIfFormHasNoObject()
  223. {
  224. $author = new Author();
  225. $child = new Author();
  226. $form = new Form('child');
  227. $form->setData($child);
  228. $form->updateProperty($author);
  229. // $author->child was set
  230. $this->assertSame($child, $author->child);
  231. }
  232. protected function createMockField($key)
  233. {
  234. $field = $this->getMock(
  235. 'Symfony\Component\Form\FieldInterface',
  236. array(),
  237. array(),
  238. '',
  239. false, // don't use constructor
  240. false // don't call parent::__clone
  241. );
  242. $field->expects($this->any())
  243. ->method('getKey')
  244. ->will($this->returnValue($key));
  245. return $field;
  246. }
  247. protected function createMockFieldGroup($key)
  248. {
  249. $field = $this->getMock(
  250. 'Symfony\Component\Form\FieldGroup',
  251. array(),
  252. array(),
  253. '',
  254. false, // don't use constructor
  255. false // don't call parent::__clone
  256. );
  257. $field->expects($this->any())
  258. ->method('getKey')
  259. ->will($this->returnValue($key));
  260. return $field;
  261. }
  262. protected function createMultipartMockField($key)
  263. {
  264. $field = $this->createMockField($key);
  265. $field->expects($this->any())
  266. ->method('isMultipart')
  267. ->will($this->returnValue(true));
  268. return $field;
  269. }
  270. protected function createMockValidator()
  271. {
  272. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  273. }
  274. }