DateFieldTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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__ . '/DateTimeTestCase.php';
  12. use Symfony\Component\Form\DateField;
  13. use Symfony\Component\Form\FormContext;
  14. class DateFieldTest extends DateTimeTestCase
  15. {
  16. protected function setUp()
  17. {
  18. \Locale::setDefault('de_AT');
  19. }
  20. public function testSubmit_fromInput_dateTime()
  21. {
  22. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::DATETIME));
  23. $field->submit('2.6.2010');
  24. $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $field->getData());
  25. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  26. }
  27. public function testSubmit_fromInput_string()
  28. {
  29. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::STRING));
  30. $field->submit('2.6.2010');
  31. $this->assertEquals('2010-06-02', $field->getData());
  32. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  33. }
  34. public function testSubmit_fromInput_timestamp()
  35. {
  36. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::TIMESTAMP));
  37. $field->submit('2.6.2010');
  38. $dateTime = new \DateTime('2010-06-02 UTC');
  39. $this->assertEquals($dateTime->format('U'), $field->getData());
  40. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  41. }
  42. public function testSubmit_fromInput_raw()
  43. {
  44. $field = new DateField('name', array(
  45. 'data_timezone' => 'UTC',
  46. 'user_timezone' => 'UTC',
  47. 'widget' => 'input',
  48. 'type' => DateField::RAW,
  49. ));
  50. $field->submit('2.6.2010');
  51. $output = array(
  52. 'day' => '2',
  53. 'month' => '6',
  54. 'year' => '2010',
  55. );
  56. $this->assertEquals($output, $field->getData());
  57. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  58. }
  59. public function testSubmit_fromChoice()
  60. {
  61. $field = new DateField('name', array('widget' => DateField::CHOICE));
  62. $input = array(
  63. 'day' => '2',
  64. 'month' => '6',
  65. 'year' => '2010',
  66. );
  67. $field->submit($input);
  68. $dateTime = new \DateTime('2010-06-02 UTC');
  69. $this->assertDateTimeEquals($dateTime, $field->getData());
  70. $this->assertEquals($input, $field->getDisplayedData());
  71. }
  72. public function testSubmit_fromChoice_empty()
  73. {
  74. $field = new DateField('name', array('widget' => DateField::CHOICE, 'required' => false));
  75. $input = array(
  76. 'day' => '',
  77. 'month' => '',
  78. 'year' => '',
  79. );
  80. $field->submit($input);
  81. $this->assertSame(null, $field->getData());
  82. $this->assertEquals($input, $field->getDisplayedData());
  83. }
  84. public function testSetData_differentTimezones()
  85. {
  86. $field = new DateField('name', array(
  87. 'data_timezone' => 'America/New_York',
  88. 'user_timezone' => 'Pacific/Tahiti',
  89. // don't do this test with DateTime, because it leads to wrong results!
  90. 'type' => DateField::STRING,
  91. 'widget' => 'input',
  92. ));
  93. $field->setData('2010-06-02');
  94. $this->assertEquals('01.06.2010', $field->getDisplayedData());
  95. }
  96. public function testIsYearWithinRange_returnsTrueIfWithin()
  97. {
  98. $field = new DateField('name', array(
  99. 'widget' => 'input',
  100. 'years' => array(2010, 2011),
  101. ));
  102. $field->submit('2.6.2010');
  103. $this->assertTrue($field->isYearWithinRange());
  104. }
  105. public function testIsYearWithinRange_returnsTrueIfEmpty()
  106. {
  107. $field = new DateField('name', array(
  108. 'widget' => 'input',
  109. 'years' => array(2010, 2011),
  110. ));
  111. $field->submit('');
  112. $this->assertTrue($field->isYearWithinRange());
  113. }
  114. public function testIsYearWithinRange_returnsTrueIfEmpty_choice()
  115. {
  116. $field = new DateField('name', array(
  117. 'widget' => 'choice',
  118. 'years' => array(2010, 2011),
  119. ));
  120. $field->submit(array(
  121. 'day' => '1',
  122. 'month' => '2',
  123. 'year' => '',
  124. ));
  125. $this->assertTrue($field->isYearWithinRange());
  126. }
  127. public function testIsYearWithinRange_returnsFalseIfNotContained()
  128. {
  129. $field = new DateField('name', array(
  130. 'widget' => 'input',
  131. 'years' => array(2010, 2012),
  132. ));
  133. $field->submit('2.6.2011');
  134. $this->assertFalse($field->isYearWithinRange());
  135. }
  136. public function testIsMonthWithinRange_returnsTrueIfWithin()
  137. {
  138. $field = new DateField('name', array(
  139. 'widget' => 'input',
  140. 'months' => array(6, 7),
  141. ));
  142. $field->submit('2.6.2010');
  143. $this->assertTrue($field->isMonthWithinRange());
  144. }
  145. public function testIsMonthWithinRange_returnsTrueIfEmpty()
  146. {
  147. $field = new DateField('name', array(
  148. 'widget' => 'input',
  149. 'months' => array(6, 7),
  150. ));
  151. $field->submit('');
  152. $this->assertTrue($field->isMonthWithinRange());
  153. }
  154. public function testIsMonthWithinRange_returnsTrueIfEmpty_choice()
  155. {
  156. $field = new DateField('name', array(
  157. 'widget' => 'choice',
  158. 'months' => array(6, 7),
  159. ));
  160. $field->submit(array(
  161. 'day' => '1',
  162. 'month' => '',
  163. 'year' => '2011',
  164. ));
  165. $this->assertTrue($field->isMonthWithinRange());
  166. }
  167. public function testIsMonthWithinRange_returnsFalseIfNotContained()
  168. {
  169. $field = new DateField('name', array(
  170. 'widget' => 'input',
  171. 'months' => array(6, 8),
  172. ));
  173. $field->submit('2.7.2010');
  174. $this->assertFalse($field->isMonthWithinRange());
  175. }
  176. public function testIsDayWithinRange_returnsTrueIfWithin()
  177. {
  178. $field = new DateField('name', array(
  179. 'widget' => 'input',
  180. 'days' => array(6, 7),
  181. ));
  182. $field->submit('6.6.2010');
  183. $this->assertTrue($field->isDayWithinRange());
  184. }
  185. public function testIsDayWithinRange_returnsTrueIfEmpty()
  186. {
  187. $field = new DateField('name', array(
  188. 'widget' => 'input',
  189. 'days' => array(6, 7),
  190. ));
  191. $field->submit('');
  192. $this->assertTrue($field->isDayWithinRange());
  193. }
  194. public function testIsDayWithinRange_returnsTrueIfEmpty_choice()
  195. {
  196. $field = new DateField('name', array(
  197. 'widget' => 'choice',
  198. 'days' => array(6, 7),
  199. ));
  200. $field->submit(array(
  201. 'day' => '',
  202. 'month' => '1',
  203. 'year' => '2011',
  204. ));
  205. $this->assertTrue($field->isDayWithinRange());
  206. }
  207. public function testIsDayWithinRange_returnsFalseIfNotContained()
  208. {
  209. $field = new DateField('name', array(
  210. 'widget' => 'input',
  211. 'days' => array(6, 8),
  212. ));
  213. $field->submit('7.6.2010');
  214. $this->assertFalse($field->isDayWithinRange());
  215. }
  216. public function testIsPartiallyFilled_returnsFalseIfInput()
  217. {
  218. $field = new DateField('name', array(
  219. 'widget' => 'input',
  220. ));
  221. $field->submit('7.6.2010');
  222. $this->assertFalse($field->isPartiallyFilled());
  223. }
  224. public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyEmpty()
  225. {
  226. $field = new DateField('name', array(
  227. 'widget' => 'choice',
  228. ));
  229. $field->submit(array(
  230. 'day' => '',
  231. 'month' => '',
  232. 'year' => '',
  233. ));
  234. $this->assertFalse($field->isPartiallyFilled());
  235. }
  236. public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyFilled()
  237. {
  238. $field = new DateField('name', array(
  239. 'widget' => 'choice',
  240. ));
  241. $field->submit(array(
  242. 'day' => '2',
  243. 'month' => '6',
  244. 'year' => '2010',
  245. ));
  246. $this->assertFalse($field->isPartiallyFilled());
  247. }
  248. public function testIsPartiallyFilled_returnsTrueIfChoiceAndDayEmpty()
  249. {
  250. $field = new DateField('name', array(
  251. 'widget' => 'choice',
  252. ));
  253. $field->submit(array(
  254. 'day' => '',
  255. 'month' => '6',
  256. 'year' => '2010',
  257. ));
  258. $this->assertTrue($field->isPartiallyFilled());
  259. }
  260. }