DateFieldTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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(
  23. 'data_timezone' => 'UTC',
  24. 'user_timezone' => 'UTC',
  25. 'widget' => 'input',
  26. 'type' => DateField::DATETIME,
  27. ));
  28. $field->submit('2.6.2010');
  29. $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $field->getData());
  30. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  31. }
  32. public function testSubmit_fromInput_string()
  33. {
  34. $field = new DateField('name', array(
  35. 'data_timezone' => 'UTC',
  36. 'user_timezone' => 'UTC',
  37. 'widget' => 'input',
  38. 'type' => DateField::STRING,
  39. ));
  40. $field->submit('2.6.2010');
  41. $this->assertEquals('2010-06-02', $field->getData());
  42. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  43. }
  44. public function testSubmit_fromInput_timestamp()
  45. {
  46. $field = new DateField('name', array(
  47. 'data_timezone' => 'UTC',
  48. 'user_timezone' => 'UTC',
  49. 'widget' => 'input',
  50. 'type' => DateField::TIMESTAMP,
  51. ));
  52. $field->submit('2.6.2010');
  53. $dateTime = new \DateTime('2010-06-02 UTC');
  54. $this->assertEquals($dateTime->format('U'), $field->getData());
  55. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  56. }
  57. public function testSubmit_fromInput_raw()
  58. {
  59. $field = new DateField('name', array(
  60. 'data_timezone' => 'UTC',
  61. 'user_timezone' => 'UTC',
  62. 'widget' => 'input',
  63. 'type' => DateField::RAW,
  64. ));
  65. $field->submit('2.6.2010');
  66. $output = array(
  67. 'day' => '2',
  68. 'month' => '6',
  69. 'year' => '2010',
  70. );
  71. $this->assertEquals($output, $field->getData());
  72. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  73. }
  74. public function testSubmit_fromChoice()
  75. {
  76. $field = new DateField('name', array(
  77. 'data_timezone' => 'UTC',
  78. 'user_timezone' => 'UTC',
  79. 'widget' => DateField::CHOICE,
  80. ));
  81. $input = array(
  82. 'day' => '2',
  83. 'month' => '6',
  84. 'year' => '2010',
  85. );
  86. $field->submit($input);
  87. $dateTime = new \DateTime('2010-06-02 UTC');
  88. $this->assertDateTimeEquals($dateTime, $field->getData());
  89. $this->assertEquals($input, $field->getDisplayedData());
  90. }
  91. public function testSubmit_fromChoice_empty()
  92. {
  93. $field = new DateField('name', array(
  94. 'data_timezone' => 'UTC',
  95. 'user_timezone' => 'UTC',
  96. 'widget' => DateField::CHOICE,
  97. 'required' => false,
  98. ));
  99. $input = array(
  100. 'day' => '',
  101. 'month' => '',
  102. 'year' => '',
  103. );
  104. $field->submit($input);
  105. $this->assertSame(null, $field->getData());
  106. $this->assertEquals($input, $field->getDisplayedData());
  107. }
  108. public function testSetData_differentTimezones()
  109. {
  110. $field = new DateField('name', array(
  111. 'data_timezone' => 'America/New_York',
  112. 'user_timezone' => 'Pacific/Tahiti',
  113. // don't do this test with DateTime, because it leads to wrong results!
  114. 'type' => DateField::STRING,
  115. 'widget' => 'input',
  116. ));
  117. $field->setData('2010-06-02');
  118. $this->assertEquals('01.06.2010', $field->getDisplayedData());
  119. }
  120. public function testIsYearWithinRange_returnsTrueIfWithin()
  121. {
  122. $field = new DateField('name', array(
  123. 'data_timezone' => 'UTC',
  124. 'user_timezone' => 'UTC',
  125. 'widget' => 'input',
  126. 'years' => array(2010, 2011),
  127. ));
  128. $field->submit('2.6.2010');
  129. $this->assertTrue($field->isYearWithinRange());
  130. }
  131. public function testIsYearWithinRange_returnsTrueIfEmpty()
  132. {
  133. $field = new DateField('name', array(
  134. 'data_timezone' => 'UTC',
  135. 'user_timezone' => 'UTC',
  136. 'widget' => 'input',
  137. 'years' => array(2010, 2011),
  138. ));
  139. $field->submit('');
  140. $this->assertTrue($field->isYearWithinRange());
  141. }
  142. public function testIsYearWithinRange_returnsTrueIfEmpty_choice()
  143. {
  144. $field = new DateField('name', array(
  145. 'data_timezone' => 'UTC',
  146. 'user_timezone' => 'UTC',
  147. 'widget' => 'choice',
  148. 'years' => array(2010, 2011),
  149. ));
  150. $field->submit(array(
  151. 'day' => '1',
  152. 'month' => '2',
  153. 'year' => '',
  154. ));
  155. $this->assertTrue($field->isYearWithinRange());
  156. }
  157. public function testIsYearWithinRange_returnsFalseIfNotContained()
  158. {
  159. $field = new DateField('name', array(
  160. 'data_timezone' => 'UTC',
  161. 'user_timezone' => 'UTC',
  162. 'widget' => 'input',
  163. 'years' => array(2010, 2012),
  164. ));
  165. $field->submit('2.6.2011');
  166. $this->assertFalse($field->isYearWithinRange());
  167. }
  168. public function testIsMonthWithinRange_returnsTrueIfWithin()
  169. {
  170. $field = new DateField('name', array(
  171. 'data_timezone' => 'UTC',
  172. 'user_timezone' => 'UTC',
  173. 'widget' => 'input',
  174. 'months' => array(6, 7),
  175. ));
  176. $field->submit('2.6.2010');
  177. $this->assertTrue($field->isMonthWithinRange());
  178. }
  179. public function testIsMonthWithinRange_returnsTrueIfEmpty()
  180. {
  181. $field = new DateField('name', array(
  182. 'data_timezone' => 'UTC',
  183. 'user_timezone' => 'UTC',
  184. 'widget' => 'input',
  185. 'months' => array(6, 7),
  186. ));
  187. $field->submit('');
  188. $this->assertTrue($field->isMonthWithinRange());
  189. }
  190. public function testIsMonthWithinRange_returnsTrueIfEmpty_choice()
  191. {
  192. $field = new DateField('name', array(
  193. 'data_timezone' => 'UTC',
  194. 'user_timezone' => 'UTC',
  195. 'widget' => 'choice',
  196. 'months' => array(6, 7),
  197. ));
  198. $field->submit(array(
  199. 'day' => '1',
  200. 'month' => '',
  201. 'year' => '2011',
  202. ));
  203. $this->assertTrue($field->isMonthWithinRange());
  204. }
  205. public function testIsMonthWithinRange_returnsFalseIfNotContained()
  206. {
  207. $field = new DateField('name', array(
  208. 'data_timezone' => 'UTC',
  209. 'user_timezone' => 'UTC',
  210. 'widget' => 'input',
  211. 'months' => array(6, 8),
  212. ));
  213. $field->submit('2.7.2010');
  214. $this->assertFalse($field->isMonthWithinRange());
  215. }
  216. public function testIsDayWithinRange_returnsTrueIfWithin()
  217. {
  218. $field = new DateField('name', array(
  219. 'data_timezone' => 'UTC',
  220. 'user_timezone' => 'UTC',
  221. 'widget' => 'input',
  222. 'days' => array(6, 7),
  223. ));
  224. $field->submit('6.6.2010');
  225. $this->assertTrue($field->isDayWithinRange());
  226. }
  227. public function testIsDayWithinRange_returnsTrueIfEmpty()
  228. {
  229. $field = new DateField('name', array(
  230. 'data_timezone' => 'UTC',
  231. 'user_timezone' => 'UTC',
  232. 'widget' => 'input',
  233. 'days' => array(6, 7),
  234. ));
  235. $field->submit('');
  236. $this->assertTrue($field->isDayWithinRange());
  237. }
  238. public function testIsDayWithinRange_returnsTrueIfEmpty_choice()
  239. {
  240. $field = new DateField('name', array(
  241. 'data_timezone' => 'UTC',
  242. 'user_timezone' => 'UTC',
  243. 'widget' => 'choice',
  244. 'days' => array(6, 7),
  245. ));
  246. $field->submit(array(
  247. 'day' => '',
  248. 'month' => '1',
  249. 'year' => '2011',
  250. ));
  251. $this->assertTrue($field->isDayWithinRange());
  252. }
  253. public function testIsDayWithinRange_returnsFalseIfNotContained()
  254. {
  255. $field = new DateField('name', array(
  256. 'data_timezone' => 'UTC',
  257. 'user_timezone' => 'UTC',
  258. 'widget' => 'input',
  259. 'days' => array(6, 8),
  260. ));
  261. $field->submit('7.6.2010');
  262. $this->assertFalse($field->isDayWithinRange());
  263. }
  264. public function testIsPartiallyFilled_returnsFalseIfInput()
  265. {
  266. $field = new DateField('name', array(
  267. 'data_timezone' => 'UTC',
  268. 'user_timezone' => 'UTC',
  269. 'widget' => 'input',
  270. ));
  271. $field->submit('7.6.2010');
  272. $this->assertFalse($field->isPartiallyFilled());
  273. }
  274. public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyEmpty()
  275. {
  276. $field = new DateField('name', array(
  277. 'data_timezone' => 'UTC',
  278. 'user_timezone' => 'UTC',
  279. 'widget' => 'choice',
  280. ));
  281. $field->submit(array(
  282. 'day' => '',
  283. 'month' => '',
  284. 'year' => '',
  285. ));
  286. $this->assertFalse($field->isPartiallyFilled());
  287. }
  288. public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyFilled()
  289. {
  290. $field = new DateField('name', array(
  291. 'data_timezone' => 'UTC',
  292. 'user_timezone' => 'UTC',
  293. 'widget' => 'choice',
  294. ));
  295. $field->submit(array(
  296. 'day' => '2',
  297. 'month' => '6',
  298. 'year' => '2010',
  299. ));
  300. $this->assertFalse($field->isPartiallyFilled());
  301. }
  302. public function testIsPartiallyFilled_returnsTrueIfChoiceAndDayEmpty()
  303. {
  304. $field = new DateField('name', array(
  305. 'data_timezone' => 'UTC',
  306. 'user_timezone' => 'UTC',
  307. 'widget' => 'choice',
  308. ));
  309. $field->submit(array(
  310. 'day' => '',
  311. 'month' => '6',
  312. 'year' => '2010',
  313. ));
  314. $this->assertTrue($field->isPartiallyFilled());
  315. }
  316. }