FormTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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::setDefaultCsrfSecret(null);
  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 testDefaultCsrfSecretCanBeSet()
  88. {
  89. FormConfiguration::setDefaultCsrfSecret('foobar');
  90. $form = new Form('author', new Author(), $this->validator);
  91. $form->enableCsrfProtection();
  92. $this->assertEquals('foobar', $form->getCsrfSecret());
  93. }
  94. public function testDefaultCsrfFieldNameCanBeSet()
  95. {
  96. FormConfiguration::setDefaultCsrfFieldName('foobar');
  97. $form = new Form('author', new Author(), $this->validator);
  98. $form->enableCsrfProtection();
  99. $this->assertEquals('foobar', $form->getCsrfFieldName());
  100. }
  101. public function testCsrfProtectedFormsHaveExtraField()
  102. {
  103. $this->form->enableCsrfProtection();
  104. $this->assertTrue($this->form->has($this->form->getCsrfFieldName()));
  105. $field = $this->form->get($this->form->getCsrfFieldName());
  106. $this->assertTrue($field instanceof HiddenField);
  107. $this->assertGreaterThanOrEqual(32, strlen($field->getDisplayedData()));
  108. }
  109. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  110. {
  111. $this->form->bind(array());
  112. $this->assertTrue($this->form->isCsrfTokenValid());
  113. }
  114. public function testIsCsrfTokenValidPasses()
  115. {
  116. $this->form->enableCsrfProtection();
  117. $field = $this->form->getCsrfFieldName();
  118. $token = $this->form->get($field)->getDisplayedData();
  119. $this->form->bind(array($field => $token));
  120. $this->assertTrue($this->form->isCsrfTokenValid());
  121. }
  122. public function testIsCsrfTokenValidFails()
  123. {
  124. $this->form->enableCsrfProtection();
  125. $field = $this->form->getCsrfFieldName();
  126. $this->form->bind(array($field => 'foobar'));
  127. $this->assertFalse($this->form->isCsrfTokenValid());
  128. }
  129. public function testDefaultLocaleCanBeSet()
  130. {
  131. FormConfiguration::setDefaultLocale('de-DE-1996');
  132. $form = new Form('author', new Author(), $this->validator);
  133. $field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
  134. $field->expects($this->any())
  135. ->method('getKey')
  136. ->will($this->returnValue('firstName'));
  137. $field->expects($this->once())
  138. ->method('setLocale')
  139. ->with($this->equalTo('de-DE-1996'));
  140. $form->add($field);
  141. }
  142. public function testValidationGroupsCanBeSet()
  143. {
  144. $form = new Form('author', new Author(), $this->validator);
  145. $this->assertNull($form->getValidationGroups());
  146. $form->setValidationGroups('group');
  147. $this->assertEquals(array('group'), $form->getValidationGroups());
  148. $form->setValidationGroups(array('group1', 'group2'));
  149. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  150. $form->setValidationGroups(null);
  151. $this->assertNull($form->getValidationGroups());
  152. }
  153. public function testBindUsesValidationGroups()
  154. {
  155. $field = $this->createMockField('firstName');
  156. $form = new Form('author', new Author(), $this->validator);
  157. $form->add($field);
  158. $form->setValidationGroups('group');
  159. $this->validator->expects($this->once())
  160. ->method('validate')
  161. ->with($this->equalTo($form), $this->equalTo(array('group')));
  162. $form->bind(array()); // irrelevant
  163. }
  164. public function testMultipartFormsWithoutParentsRequireFiles()
  165. {
  166. $form = new Form('author', new Author(), $this->validator);
  167. $form->add($this->createMultipartMockField('file'));
  168. $this->setExpectedException('InvalidArgumentException');
  169. // should be given in second argument
  170. $form->bind(array('file' => 'test.txt'));
  171. }
  172. public function testMultipartFormsWithParentsRequireNoFiles()
  173. {
  174. $form = new Form('author', new Author(), $this->validator);
  175. $form->add($this->createMultipartMockField('file'));
  176. $form->setParent($this->createMockField('group'));
  177. // files are expected to be converted by the parent
  178. $form->bind(array('file' => 'test.txt'));
  179. }
  180. protected function createMockField($key)
  181. {
  182. $field = $this->getMock(
  183. 'Symfony\Component\Form\FieldInterface',
  184. array(),
  185. array(),
  186. '',
  187. false, // don't use constructor
  188. false // don't call parent::__clone
  189. );
  190. $field->expects($this->any())
  191. ->method('getKey')
  192. ->will($this->returnValue($key));
  193. return $field;
  194. }
  195. protected function createMockFieldGroup($key)
  196. {
  197. $field = $this->getMock(
  198. 'Symfony\Component\Form\FieldGroup',
  199. array(),
  200. array(),
  201. '',
  202. false, // don't use constructor
  203. false // don't call parent::__clone
  204. );
  205. $field->expects($this->any())
  206. ->method('getKey')
  207. ->will($this->returnValue($key));
  208. return $field;
  209. }
  210. protected function createMultipartMockField($key)
  211. {
  212. $field = $this->createMockField($key);
  213. $field->expects($this->any())
  214. ->method('isMultipart')
  215. ->will($this->returnValue(true));
  216. return $field;
  217. }
  218. protected function createMockValidator()
  219. {
  220. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  221. }
  222. }