FormTest.php 10 KB

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