FormTest.php 10 KB

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