FormTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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\Field;
  7. use Symfony\Component\Form\HiddenField;
  8. use Symfony\Component\Form\FieldGroup;
  9. use Symfony\Component\Form\PropertyPath;
  10. use Symfony\Component\Validator\ConstraintViolation;
  11. use Symfony\Component\Validator\ConstraintViolationList;
  12. use Symfony\Tests\Component\Form\Fixtures\Author;
  13. use Symfony\Tests\Component\Form\Fixtures\TestField;
  14. class FormTest_PreconfiguredForm extends Form
  15. {
  16. protected function configure()
  17. {
  18. $this->add(new Field('firstName'));
  19. parent::configure();
  20. }
  21. }
  22. class TestSetDataBeforeConfigureForm extends Form
  23. {
  24. protected $testCase;
  25. protected $object;
  26. public function __construct($testCase, $name, $object, $validator)
  27. {
  28. $this->testCase = $testCase;
  29. $this->object = $object;
  30. parent::__construct($name, $object, $validator);
  31. }
  32. protected function configure()
  33. {
  34. $this->testCase->assertEquals($this->object, $this->getData());
  35. parent::configure();
  36. }
  37. }
  38. class FormTest extends \PHPUnit_Framework_TestCase
  39. {
  40. protected $validator;
  41. protected $form;
  42. public static function setUpBeforeClass()
  43. {
  44. @session_start();
  45. }
  46. protected function setUp()
  47. {
  48. Form::disableDefaultCsrfProtection();
  49. Form::setDefaultCsrfSecret(null);
  50. $this->validator = $this->createMockValidator();
  51. $this->form = new Form('author', new Author(), $this->validator);
  52. }
  53. public function testConstructInitializesObject()
  54. {
  55. $this->assertEquals(new Author(), $this->form->getData());
  56. }
  57. public function testSetDataBeforeConfigure()
  58. {
  59. new TestSetDataBeforeConfigureForm($this, 'author', new Author(), $this->validator);
  60. }
  61. public function testIsCsrfProtected()
  62. {
  63. $this->assertFalse($this->form->isCsrfProtected());
  64. $this->form->enableCsrfProtection();
  65. $this->assertTrue($this->form->isCsrfProtected());
  66. $this->form->disableCsrfProtection();
  67. $this->assertFalse($this->form->isCsrfProtected());
  68. }
  69. public function testNoCsrfProtectionByDefault()
  70. {
  71. $form = new Form('author', new Author(), $this->validator);
  72. $this->assertFalse($form->isCsrfProtected());
  73. }
  74. public function testDefaultCsrfProtectionCanBeEnabled()
  75. {
  76. Form::enableDefaultCsrfProtection();
  77. $form = new Form('author', new Author(), $this->validator);
  78. $this->assertTrue($form->isCsrfProtected());
  79. }
  80. public function testGeneratedCsrfSecretByDefault()
  81. {
  82. $form = new Form('author', new Author(), $this->validator);
  83. $form->enableCsrfProtection();
  84. $this->assertTrue(strlen($form->getCsrfSecret()) >= 32);
  85. }
  86. public function testDefaultCsrfSecretCanBeSet()
  87. {
  88. Form::setDefaultCsrfSecret('foobar');
  89. $form = new Form('author', new Author(), $this->validator);
  90. $form->enableCsrfProtection();
  91. $this->assertEquals('foobar', $form->getCsrfSecret());
  92. }
  93. public function testDefaultCsrfFieldNameCanBeSet()
  94. {
  95. Form::setDefaultCsrfFieldName('foobar');
  96. $form = new Form('author', new Author(), $this->validator);
  97. $form->enableCsrfProtection();
  98. $this->assertEquals('foobar', $form->getCsrfFieldName());
  99. }
  100. public function testCsrfProtectedFormsHaveExtraField()
  101. {
  102. $this->form->enableCsrfProtection();
  103. $this->assertTrue($this->form->has($this->form->getCsrfFieldName()));
  104. $field = $this->form->get($this->form->getCsrfFieldName());
  105. $this->assertTrue($field instanceof HiddenField);
  106. $this->assertGreaterThanOrEqual(32, strlen($field->getDisplayedData()));
  107. }
  108. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  109. {
  110. $this->form->bind(array());
  111. $this->assertTrue($this->form->isCsrfTokenValid());
  112. }
  113. public function testIsCsrfTokenValidPasses()
  114. {
  115. $this->form->enableCsrfProtection();
  116. $field = $this->form->getCsrfFieldName();
  117. $token = $this->form->get($field)->getDisplayedData();
  118. $this->form->bind(array($field => $token));
  119. $this->assertTrue($this->form->isCsrfTokenValid());
  120. }
  121. public function testIsCsrfTokenValidFails()
  122. {
  123. $this->form->enableCsrfProtection();
  124. $field = $this->form->getCsrfFieldName();
  125. $this->form->bind(array($field => 'foobar'));
  126. $this->assertFalse($this->form->isCsrfTokenValid());
  127. }
  128. public function testDefaultLocaleCanBeSet()
  129. {
  130. Form::setDefaultLocale('de-DE-1996');
  131. $form = new Form('author', new Author(), $this->validator);
  132. $field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
  133. $field->expects($this->any())
  134. ->method('getKey')
  135. ->will($this->returnValue('firstName'));
  136. $field->expects($this->once())
  137. ->method('setLocale')
  138. ->with($this->equalTo('de-DE-1996'));
  139. $form->add($field);
  140. }
  141. public function testValidationGroupsCanBeSet()
  142. {
  143. $form = new Form('author', new Author(), $this->validator);
  144. $this->assertNull($form->getValidationGroups());
  145. $form->setValidationGroups('group');
  146. $this->assertEquals(array('group'), $form->getValidationGroups());
  147. $form->setValidationGroups(array('group1', 'group2'));
  148. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  149. $form->setValidationGroups(null);
  150. $this->assertNull($form->getValidationGroups());
  151. }
  152. public function testBindUsesValidationGroups()
  153. {
  154. $field = $this->createMockField('firstName');
  155. $form = new Form('author', new Author(), $this->validator);
  156. $form->add($field);
  157. $form->setValidationGroups('group');
  158. $this->validator->expects($this->once())
  159. ->method('validate')
  160. ->with($this->equalTo($form), $this->equalTo(array('group')));
  161. $form->bind(array()); // irrelevant
  162. }
  163. public function testMultipartFormsWithoutParentsRequireFiles()
  164. {
  165. $form = new Form('author', new Author(), $this->validator);
  166. $form->add($this->createMultipartMockField('file'));
  167. $this->setExpectedException('InvalidArgumentException');
  168. // should be given in second argument
  169. $form->bind(array('file' => 'test.txt'));
  170. }
  171. public function testMultipartFormsWithParentsRequireNoFiles()
  172. {
  173. $form = new Form('author', new Author(), $this->validator);
  174. $form->add($this->createMultipartMockField('file'));
  175. $form->setParent($this->createMockField('group'));
  176. // files are expected to be converted by the parent
  177. $form->bind(array('file' => 'test.txt'));
  178. }
  179. protected function createMockField($key)
  180. {
  181. $field = $this->getMock(
  182. 'Symfony\Component\Form\FieldInterface',
  183. array(),
  184. array(),
  185. '',
  186. false, // don't use constructor
  187. false // don't call parent::__clone
  188. );
  189. $field->expects($this->any())
  190. ->method('getKey')
  191. ->will($this->returnValue($key));
  192. return $field;
  193. }
  194. protected function createMockFieldGroup($key)
  195. {
  196. $field = $this->getMock(
  197. 'Symfony\Component\Form\FieldGroup',
  198. array(),
  199. array(),
  200. '',
  201. false, // don't use constructor
  202. false // don't call parent::__clone
  203. );
  204. $field->expects($this->any())
  205. ->method('getKey')
  206. ->will($this->returnValue($key));
  207. return $field;
  208. }
  209. protected function createMultipartMockField($key)
  210. {
  211. $field = $this->createMockField($key);
  212. $field->expects($this->any())
  213. ->method('isMultipart')
  214. ->will($this->returnValue(true));
  215. return $field;
  216. }
  217. protected function createMockValidator()
  218. {
  219. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  220. }
  221. }