/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTypeTest.php

https://github.com/Faianca/symfony · PHP · 638 lines · 477 code · 136 blank · 25 comment · 0 complexity · 8729dbf31f1cccc88b3f0236c77bcd90 MD5 · raw file

  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\Extension\Core\Type;
  11. require_once __DIR__ . '/LocalizedTestCase.php';
  12. use Symfony\Component\Form\DateField;
  13. use Symfony\Component\Form\FormView;
  14. class DateTypeTest extends LocalizedTestCase
  15. {
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. \Locale::setDefault('de_AT');
  20. }
  21. /**
  22. * @expectedException Symfony\Component\Form\Exception\FormException
  23. */
  24. public function testInvalidWidgetOption()
  25. {
  26. $form = $this->factory->create('date', null, array(
  27. 'widget' => 'fake_widget',
  28. ));
  29. }
  30. /**
  31. * @expectedException Symfony\Component\Form\Exception\FormException
  32. */
  33. public function testInvalidInputOption()
  34. {
  35. $form = $this->factory->create('date', null, array(
  36. 'input' => 'fake_input',
  37. ));
  38. }
  39. public function testSubmitFromSingleTextDateTime()
  40. {
  41. $form = $this->factory->create('date', null, array(
  42. 'data_timezone' => 'UTC',
  43. 'user_timezone' => 'UTC',
  44. 'widget' => 'single_text',
  45. 'input' => 'datetime',
  46. ));
  47. $form->bind('2.6.2010');
  48. $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $form->getData());
  49. $this->assertEquals('02.06.2010', $form->getClientData());
  50. }
  51. public function testSubmitFromSingleTextString()
  52. {
  53. $form = $this->factory->create('date', null, array(
  54. 'data_timezone' => 'UTC',
  55. 'user_timezone' => 'UTC',
  56. 'widget' => 'single_text',
  57. 'input' => 'string',
  58. ));
  59. $form->bind('2.6.2010');
  60. $this->assertEquals('2010-06-02', $form->getData());
  61. $this->assertEquals('02.06.2010', $form->getClientData());
  62. }
  63. public function testSubmitFromSingleTextTimestamp()
  64. {
  65. $form = $this->factory->create('date', null, array(
  66. 'data_timezone' => 'UTC',
  67. 'user_timezone' => 'UTC',
  68. 'widget' => 'single_text',
  69. 'input' => 'timestamp',
  70. ));
  71. $form->bind('2.6.2010');
  72. $dateTime = new \DateTime('2010-06-02 UTC');
  73. $this->assertEquals($dateTime->format('U'), $form->getData());
  74. $this->assertEquals('02.06.2010', $form->getClientData());
  75. }
  76. public function testSubmitFromSingleTextRaw()
  77. {
  78. $form = $this->factory->create('date', null, array(
  79. 'data_timezone' => 'UTC',
  80. 'user_timezone' => 'UTC',
  81. 'widget' => 'single_text',
  82. 'input' => 'array',
  83. ));
  84. $form->bind('2.6.2010');
  85. $output = array(
  86. 'day' => '2',
  87. 'month' => '6',
  88. 'year' => '2010',
  89. );
  90. $this->assertEquals($output, $form->getData());
  91. $this->assertEquals('02.06.2010', $form->getClientData());
  92. }
  93. public function testSubmitFromText()
  94. {
  95. $form = $this->factory->create('date', null, array(
  96. 'data_timezone' => 'UTC',
  97. 'user_timezone' => 'UTC',
  98. 'widget' => 'text',
  99. ));
  100. $text = array(
  101. 'day' => '2',
  102. 'month' => '6',
  103. 'year' => '2010',
  104. );
  105. $form->bind($text);
  106. $dateTime = new \DateTime('2010-06-02 UTC');
  107. $this->assertDateTimeEquals($dateTime, $form->getData());
  108. $this->assertEquals($text, $form->getClientData());
  109. }
  110. public function testSubmitFromChoice()
  111. {
  112. $form = $this->factory->create('date', null, array(
  113. 'data_timezone' => 'UTC',
  114. 'user_timezone' => 'UTC',
  115. 'widget' => 'choice',
  116. ));
  117. $text = array(
  118. 'day' => '2',
  119. 'month' => '6',
  120. 'year' => '2010',
  121. );
  122. $form->bind($text);
  123. $dateTime = new \DateTime('2010-06-02 UTC');
  124. $this->assertDateTimeEquals($dateTime, $form->getData());
  125. $this->assertEquals($text, $form->getClientData());
  126. }
  127. public function testSubmitFromChoiceEmpty()
  128. {
  129. $form = $this->factory->create('date', null, array(
  130. 'data_timezone' => 'UTC',
  131. 'user_timezone' => 'UTC',
  132. 'widget' => 'choice',
  133. 'required' => false,
  134. ));
  135. $text = array(
  136. 'day' => '',
  137. 'month' => '',
  138. 'year' => '',
  139. );
  140. $form->bind($text);
  141. $this->assertNull($form->getData());
  142. $this->assertEquals($text, $form->getClientData());
  143. }
  144. public function testSubmitFromInputDateTimeDifferentPattern()
  145. {
  146. $form = $this->factory->create('date', null, array(
  147. 'data_timezone' => 'UTC',
  148. 'user_timezone' => 'UTC',
  149. 'format' => 'MM*yyyy*dd',
  150. 'widget' => 'single_text',
  151. 'input' => 'datetime',
  152. ));
  153. $form->bind('06*2010*02');
  154. $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $form->getData());
  155. $this->assertEquals('06*2010*02', $form->getClientData());
  156. }
  157. public function testSubmitFromInputStringDifferentPattern()
  158. {
  159. $form = $this->factory->create('date', null, array(
  160. 'data_timezone' => 'UTC',
  161. 'user_timezone' => 'UTC',
  162. 'format' => 'MM*yyyy*dd',
  163. 'widget' => 'single_text',
  164. 'input' => 'string',
  165. ));
  166. $form->bind('06*2010*02');
  167. $this->assertEquals('2010-06-02', $form->getData());
  168. $this->assertEquals('06*2010*02', $form->getClientData());
  169. }
  170. public function testSubmitFromInputTimestampDifferentPattern()
  171. {
  172. $form = $this->factory->create('date', null, array(
  173. 'data_timezone' => 'UTC',
  174. 'user_timezone' => 'UTC',
  175. 'format' => 'MM*yyyy*dd',
  176. 'widget' => 'single_text',
  177. 'input' => 'timestamp',
  178. ));
  179. $form->bind('06*2010*02');
  180. $dateTime = new \DateTime('2010-06-02 UTC');
  181. $this->assertEquals($dateTime->format('U'), $form->getData());
  182. $this->assertEquals('06*2010*02', $form->getClientData());
  183. }
  184. public function testSubmitFromInputRawDifferentPattern()
  185. {
  186. $form = $this->factory->create('date', null, array(
  187. 'data_timezone' => 'UTC',
  188. 'user_timezone' => 'UTC',
  189. 'format' => 'MM*yyyy*dd',
  190. 'widget' => 'single_text',
  191. 'input' => 'array',
  192. ));
  193. $form->bind('06*2010*02');
  194. $output = array(
  195. 'day' => '2',
  196. 'month' => '6',
  197. 'year' => '2010',
  198. );
  199. $this->assertEquals($output, $form->getData());
  200. $this->assertEquals('06*2010*02', $form->getClientData());
  201. }
  202. /**
  203. * This test is to check that the strings '0', '1', '2', '3' are no accepted
  204. * as valid IntlDateFormatter constants for FULL, LONG, MEDIUM or SHORT respectively.
  205. */
  206. public function testFormatOptionCustomPatternCollapsingIntlDateFormatterConstant()
  207. {
  208. $form = $this->factory->create('date', null, array(
  209. 'format' => '0',
  210. 'widget' => 'single_text',
  211. 'input' => 'string',
  212. ));
  213. $form->setData('2010-06-02');
  214. // This would be what would be outputed if '0' was mistaken for \IntlDateFormatter::FULL
  215. $this->assertNotEquals('Mittwoch, 02. Juni 2010', $form->getClientData());
  216. }
  217. /**
  218. * @expectedException Symfony\Component\Form\Exception\CreationException
  219. */
  220. public function testValidateFormatOptionGivenWrongConstants()
  221. {
  222. $form = $this->factory->create('date', null, array(
  223. 'format' => 105,
  224. ));
  225. }
  226. /**
  227. * @expectedException Symfony\Component\Form\Exception\CreationException
  228. */
  229. public function testValidateFormatOptionGivenArrayValue()
  230. {
  231. $form = $this->factory->create('date', null, array(
  232. 'format' => array(),
  233. ));
  234. }
  235. public function testSetData_differentTimezones()
  236. {
  237. $form = $this->factory->create('date', null, array(
  238. 'data_timezone' => 'America/New_York',
  239. 'user_timezone' => 'Pacific/Tahiti',
  240. 'input' => 'string',
  241. 'widget' => 'single_text',
  242. ));
  243. $form->setData('2010-06-02');
  244. $this->assertEquals('01.06.2010', $form->getClientData());
  245. }
  246. public function testSetData_differentTimezonesDateTime()
  247. {
  248. $form = $this->factory->create('date', null, array(
  249. 'data_timezone' => 'America/New_York',
  250. 'user_timezone' => 'Pacific/Tahiti',
  251. 'input' => 'datetime',
  252. 'widget' => 'single_text',
  253. ));
  254. $dateTime = new \DateTime('2010-06-02 America/New_York');
  255. $form->setData($dateTime);
  256. $this->assertDateTimeEquals($dateTime, $form->getData());
  257. $this->assertEquals('01.06.2010', $form->getClientData());
  258. }
  259. public function testIsYearWithinRangeReturnsTrueIfWithin()
  260. {
  261. $this->markTestIncomplete('Needs to be reimplemented using validators');
  262. $form = $this->factory->create('date', null, array(
  263. 'data_timezone' => 'UTC',
  264. 'user_timezone' => 'UTC',
  265. 'widget' => 'single_text',
  266. 'years' => array(2010, 2011),
  267. ));
  268. $form->bind('2.6.2010');
  269. $this->assertTrue($form->isYearWithinRange());
  270. }
  271. public function testIsYearWithinRangeReturnsTrueIfEmpty()
  272. {
  273. $this->markTestIncomplete('Needs to be reimplemented using validators');
  274. $form = $this->factory->create('date', null, array(
  275. 'data_timezone' => 'UTC',
  276. 'user_timezone' => 'UTC',
  277. 'widget' => 'single_text',
  278. 'years' => array(2010, 2011),
  279. ));
  280. $form->bind('');
  281. $this->assertTrue($form->isYearWithinRange());
  282. }
  283. public function testIsYearWithinRangeReturnsTrueIfEmptyChoice()
  284. {
  285. $this->markTestIncomplete('Needs to be reimplemented using validators');
  286. $form = $this->factory->create('date', null, array(
  287. 'data_timezone' => 'UTC',
  288. 'user_timezone' => 'UTC',
  289. 'widget' => 'choice',
  290. 'years' => array(2010, 2011),
  291. ));
  292. $form->bind(array(
  293. 'day' => '1',
  294. 'month' => '2',
  295. 'year' => '',
  296. ));
  297. $this->assertTrue($form->isYearWithinRange());
  298. }
  299. public function testIsYearWithinRangeReturnsFalseIfNotContained()
  300. {
  301. $this->markTestIncomplete('Needs to be reimplemented using validators');
  302. $form = $this->factory->create('date', null, array(
  303. 'data_timezone' => 'UTC',
  304. 'user_timezone' => 'UTC',
  305. 'widget' => 'single_text',
  306. 'years' => array(2010, 2012),
  307. ));
  308. $form->bind('2.6.2011');
  309. $this->assertFalse($form->isYearWithinRange());
  310. }
  311. public function testIsMonthWithinRangeReturnsTrueIfWithin()
  312. {
  313. $this->markTestIncomplete('Needs to be reimplemented using validators');
  314. $form = $this->factory->create('date', null, array(
  315. 'data_timezone' => 'UTC',
  316. 'user_timezone' => 'UTC',
  317. 'widget' => 'single_text',
  318. 'months' => array(6, 7),
  319. ));
  320. $form->bind('2.6.2010');
  321. $this->assertTrue($form->isMonthWithinRange());
  322. }
  323. public function testIsMonthWithinRangeReturnsTrueIfEmpty()
  324. {
  325. $this->markTestIncomplete('Needs to be reimplemented using validators');
  326. $form = $this->factory->create('date', null, array(
  327. 'data_timezone' => 'UTC',
  328. 'user_timezone' => 'UTC',
  329. 'widget' => 'single_text',
  330. 'months' => array(6, 7),
  331. ));
  332. $form->bind('');
  333. $this->assertTrue($form->isMonthWithinRange());
  334. }
  335. public function testIsMonthWithinRangeReturnsTrueIfEmptyChoice()
  336. {
  337. $this->markTestIncomplete('Needs to be reimplemented using validators');
  338. $form = $this->factory->create('date', null, array(
  339. 'data_timezone' => 'UTC',
  340. 'user_timezone' => 'UTC',
  341. 'widget' => 'choice',
  342. 'months' => array(6, 7),
  343. ));
  344. $form->bind(array(
  345. 'day' => '1',
  346. 'month' => '',
  347. 'year' => '2011',
  348. ));
  349. $this->assertTrue($form->isMonthWithinRange());
  350. }
  351. public function testIsMonthWithinRangeReturnsFalseIfNotContained()
  352. {
  353. $this->markTestIncomplete('Needs to be reimplemented using validators');
  354. $form = $this->factory->create('date', null, array(
  355. 'data_timezone' => 'UTC',
  356. 'user_timezone' => 'UTC',
  357. 'widget' => 'single_text',
  358. 'months' => array(6, 8),
  359. ));
  360. $form->bind('2.7.2010');
  361. $this->assertFalse($form->isMonthWithinRange());
  362. }
  363. public function testIsDayWithinRangeReturnsTrueIfWithin()
  364. {
  365. $this->markTestIncomplete('Needs to be reimplemented using validators');
  366. $form = $this->factory->create('date', null, array(
  367. 'data_timezone' => 'UTC',
  368. 'user_timezone' => 'UTC',
  369. 'widget' => 'single_text',
  370. 'days' => array(6, 7),
  371. ));
  372. $form->bind('6.6.2010');
  373. $this->assertTrue($form->isDayWithinRange());
  374. }
  375. public function testIsDayWithinRangeReturnsTrueIfEmpty()
  376. {
  377. $this->markTestIncomplete('Needs to be reimplemented using validators');
  378. $form = $this->factory->create('date', null, array(
  379. 'data_timezone' => 'UTC',
  380. 'user_timezone' => 'UTC',
  381. 'widget' => 'single_text',
  382. 'days' => array(6, 7),
  383. ));
  384. $form->bind('');
  385. $this->assertTrue($form->isDayWithinRange());
  386. }
  387. public function testIsDayWithinRangeReturnsTrueIfEmptyChoice()
  388. {
  389. $this->markTestIncomplete('Needs to be reimplemented using validators');
  390. $form = $this->factory->create('date', null, array(
  391. 'data_timezone' => 'UTC',
  392. 'user_timezone' => 'UTC',
  393. 'widget' => 'choice',
  394. 'days' => array(6, 7),
  395. ));
  396. $form->bind(array(
  397. 'day' => '',
  398. 'month' => '1',
  399. 'year' => '2011',
  400. ));
  401. $this->assertTrue($form->isDayWithinRange());
  402. }
  403. public function testIsDayWithinRangeReturnsFalseIfNotContained()
  404. {
  405. $this->markTestIncomplete('Needs to be reimplemented using validators');
  406. $this->markTestIncomplete('Needs to be reimplemented using validators');
  407. $form = $this->factory->create('date', null, array(
  408. 'data_timezone' => 'UTC',
  409. 'user_timezone' => 'UTC',
  410. 'widget' => 'single_text',
  411. 'days' => array(6, 8),
  412. ));
  413. $form->bind('7.6.2010');
  414. $this->assertFalse($form->isDayWithinRange());
  415. }
  416. public function testIsPartiallyFilledReturnsFalseIfSingleText()
  417. {
  418. $this->markTestIncomplete('Needs to be reimplemented using validators');
  419. $form = $this->factory->create('date', null, array(
  420. 'data_timezone' => 'UTC',
  421. 'user_timezone' => 'UTC',
  422. 'widget' => 'single_text',
  423. ));
  424. $form->bind('7.6.2010');
  425. $this->assertFalse($form->isPartiallyFilled());
  426. }
  427. public function testIsPartiallyFilledReturnsFalseIfChoiceAndCompletelyEmpty()
  428. {
  429. $this->markTestIncomplete('Needs to be reimplemented using validators');
  430. $form = $this->factory->create('date', null, array(
  431. 'data_timezone' => 'UTC',
  432. 'user_timezone' => 'UTC',
  433. 'widget' => 'choice',
  434. ));
  435. $form->bind(array(
  436. 'day' => '',
  437. 'month' => '',
  438. 'year' => '',
  439. ));
  440. $this->assertFalse($form->isPartiallyFilled());
  441. }
  442. public function testIsPartiallyFilledReturnsFalseIfChoiceAndCompletelyFilled()
  443. {
  444. $this->markTestIncomplete('Needs to be reimplemented using validators');
  445. $form = $this->factory->create('date', null, array(
  446. 'data_timezone' => 'UTC',
  447. 'user_timezone' => 'UTC',
  448. 'widget' => 'choice',
  449. ));
  450. $form->bind(array(
  451. 'day' => '2',
  452. 'month' => '6',
  453. 'year' => '2010',
  454. ));
  455. $this->assertFalse($form->isPartiallyFilled());
  456. }
  457. public function testIsPartiallyFilledReturnsTrueIfChoiceAndDayEmpty()
  458. {
  459. $this->markTestIncomplete('Needs to be reimplemented using validators');
  460. $form = $this->factory->create('date', null, array(
  461. 'data_timezone' => 'UTC',
  462. 'user_timezone' => 'UTC',
  463. 'widget' => 'choice',
  464. ));
  465. $form->bind(array(
  466. 'day' => '',
  467. 'month' => '6',
  468. 'year' => '2010',
  469. ));
  470. $this->assertTrue($form->isPartiallyFilled());
  471. }
  472. public function testPassDatePatternToView()
  473. {
  474. $form = $this->factory->create('date');
  475. $view = $form->createView();
  476. $this->assertSame('{{ day }}.{{ month }}.{{ year }}', $view->get('date_pattern'));
  477. }
  478. public function testPassDatePatternToViewDifferentPattern()
  479. {
  480. $form = $this->factory->create('date', null, array(
  481. 'format' => 'MM*yyyy*dd'
  482. ));
  483. $view = $form->createView();
  484. $this->assertSame('{{ month }}*{{ year }}*{{ day }}', $view->get('date_pattern'));
  485. }
  486. public function testDontPassDatePatternIfText()
  487. {
  488. $form = $this->factory->create('date', null, array(
  489. 'widget' => 'single_text',
  490. ));
  491. $view = $form->createView();
  492. $this->assertNull($view->get('date_pattern'));
  493. }
  494. public function testPassWidgetToView()
  495. {
  496. $form = $this->factory->create('date', null, array(
  497. 'widget' => 'single_text',
  498. ));
  499. $view = $form->createView();
  500. $this->assertSame('single_text', $view->get('widget'));
  501. }
  502. }