DateFieldTest.php 12 KB

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