DateTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\I18n;
  16. use Cake\I18n\Date;
  17. use Cake\I18n\FrozenDate;
  18. use Cake\TestSuite\TestCase;
  19. use DateTimeZone;
  20. /**
  21. * DateTest class
  22. */
  23. class DateTest extends TestCase
  24. {
  25. /**
  26. * Backup the locale property
  27. *
  28. * @var string
  29. */
  30. protected $locale;
  31. /**
  32. * setup
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. parent::setUp();
  39. $this->locale = Date::getDefaultLocale();
  40. }
  41. /**
  42. * Teardown
  43. *
  44. * @return void
  45. */
  46. public function tearDown()
  47. {
  48. parent::tearDown();
  49. Date::setDefaultLocale($this->locale);
  50. FrozenDate::setDefaultLocale($this->locale);
  51. date_default_timezone_set('UTC');
  52. }
  53. /**
  54. * Provider for ensuring that Date and FrozenDate work the same way.
  55. *
  56. * @return array
  57. */
  58. public static function classNameProvider()
  59. {
  60. return ['mutable' => ['Cake\I18n\Date'], 'immutable' => ['Cake\I18n\FrozenDate']];
  61. }
  62. /**
  63. * Ensure that instances can be built from other objects.
  64. *
  65. * @dataProvider classNameProvider
  66. * @return void
  67. */
  68. public function testConstructFromAnotherInstance($class)
  69. {
  70. $time = '2015-01-22';
  71. $frozen = new FrozenDate($time, 'America/Chicago');
  72. $subject = new $class($frozen);
  73. $this->assertEquals($time, $subject->format('Y-m-d'), 'frozen date construction');
  74. $mut = new Date($time, 'America/Chicago');
  75. $subject = new $class($mut);
  76. $this->assertEquals($time, $subject->format('Y-m-d'), 'mutable date construction');
  77. }
  78. /**
  79. * test formatting dates taking in account preferred i18n locale file
  80. *
  81. * @dataProvider classNameProvider
  82. * @return void
  83. */
  84. public function testI18nFormat($class)
  85. {
  86. $time = new $class('Thu Jan 14 13:59:28 2010');
  87. $result = $time->i18nFormat();
  88. $expected = '1/14/10';
  89. $this->assertEquals($expected, $result);
  90. $format = [\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT];
  91. $result = $time->i18nFormat($format);
  92. $expected = '12:00 AM';
  93. $this->assertEquals($expected, $result);
  94. $result = $time->i18nFormat('HH:mm:ss', 'Australia/Sydney');
  95. $expected = '00:00:00';
  96. $this->assertEquals($expected, $result);
  97. $class::setDefaultLocale('fr-FR');
  98. $result = $time->i18nFormat(\IntlDateFormatter::FULL);
  99. $result = str_replace(' à', '', $result);
  100. $expected = 'jeudi 14 janvier 2010 00:00:00';
  101. $this->assertStringStartsWith($expected, $result);
  102. $result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
  103. $this->assertContains('14 de enero de 2010', $result, 'Default locale should not be used');
  104. $time = new $class('2014-01-01T00:00:00Z');
  105. $result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'en-US');
  106. $expected = 'Wednesday, January 1, 2014 at 12:00:00 AM';
  107. $this->assertStringStartsWith($expected, $result);
  108. }
  109. /**
  110. * test __toString
  111. *
  112. * @dataProvider classNameProvider
  113. * @return void
  114. */
  115. public function testToString($class)
  116. {
  117. $date = new $class('2015-11-06 11:32:45');
  118. $this->assertEquals('11/6/15', (string)$date);
  119. }
  120. /**
  121. * test nice()
  122. *
  123. * @dataProvider classNameProvider
  124. * @return void
  125. */
  126. public function testNice($class)
  127. {
  128. $date = new $class('2015-11-06 11:32:45');
  129. $this->assertEquals('Nov 6, 2015', $date->nice());
  130. $this->assertEquals('Nov 6, 2015', $date->nice(new DateTimeZone('America/New_York')));
  131. $this->assertEquals('6 nov. 2015', $date->nice(null, 'fr-FR'));
  132. }
  133. /**
  134. * test jsonSerialize()
  135. *
  136. * @dataProvider classNameProvider
  137. * @return void
  138. */
  139. public function testJsonSerialize($class)
  140. {
  141. if (version_compare(INTL_ICU_VERSION, '50.0', '<')) {
  142. $this->markTestSkipped('ICU 5x is needed');
  143. }
  144. $date = new $class('2015-11-06 11:32:45');
  145. $this->assertEquals('"2015-11-06T00:00:00+00:00"', json_encode($date));
  146. }
  147. /**
  148. * test parseDate()
  149. *
  150. * @dataProvider classNameProvider
  151. * @return void
  152. */
  153. public function testParseDate($class)
  154. {
  155. $date = $class::parseDate('11/6/15');
  156. $this->assertEquals('2015-11-06 00:00:00', $date->format('Y-m-d H:i:s'));
  157. $class::setDefaultLocale('fr-FR');
  158. $date = $class::parseDate('13 10, 2015');
  159. $this->assertEquals('2015-10-13 00:00:00', $date->format('Y-m-d H:i:s'));
  160. }
  161. /**
  162. * test parseDateTime()
  163. *
  164. * @dataProvider classNameProvider
  165. * @return void
  166. */
  167. public function testParseDateTime($class)
  168. {
  169. $date = $class::parseDate('11/6/15 12:33:12');
  170. $this->assertEquals('2015-11-06 00:00:00', $date->format('Y-m-d H:i:s'));
  171. $class::setDefaultLocale('fr-FR');
  172. $date = $class::parseDate('13 10, 2015 12:54:12');
  173. $this->assertEquals('2015-10-13 00:00:00', $date->format('Y-m-d H:i:s'));
  174. }
  175. /**
  176. * provider for timeAgoInWords() tests
  177. *
  178. * @return array
  179. */
  180. public static function timeAgoProvider()
  181. {
  182. return [
  183. ['-12 seconds', 'today'],
  184. ['-12 minutes', 'today'],
  185. ['-2 hours', 'today'],
  186. ['-1 day', '1 day ago'],
  187. ['-2 days', '2 days ago'],
  188. ['-1 week', '1 week ago'],
  189. ['-2 weeks -2 days', '2 weeks, 2 days ago'],
  190. ['+1 second', 'today'],
  191. ['+1 minute, +10 seconds', 'today'],
  192. ['+1 week', '1 week'],
  193. ['+1 week 1 day', '1 week, 1 day'],
  194. ['+2 weeks 2 day', '2 weeks, 2 days'],
  195. ['2007-9-24', 'on 9/24/07'],
  196. ['now', 'today'],
  197. ];
  198. }
  199. /**
  200. * testTimeAgoInWords method
  201. *
  202. * @dataProvider timeAgoProvider
  203. * @return void
  204. */
  205. public function testTimeAgoInWords($input, $expected)
  206. {
  207. $date = new Date($input);
  208. $result = $date->timeAgoInWords();
  209. $this->assertEquals($expected, $result);
  210. }
  211. /**
  212. * testTimeAgoInWords with Frozen Date
  213. *
  214. * @dataProvider timeAgoProvider
  215. * @return void
  216. */
  217. public function testTimeAgoInWordsFrozenDate($input, $expected)
  218. {
  219. $date = new FrozenDate($input);
  220. $result = $date->timeAgoInWords();
  221. $this->assertEquals($expected, $result);
  222. }
  223. /**
  224. * test the timezone option for timeAgoInWords
  225. *
  226. * @dataProvider classNameProvider
  227. * @return void
  228. */
  229. public function testTimeAgoInWordsTimezone($class)
  230. {
  231. $date = new $class('1990-07-31 20:33:00 UTC');
  232. $result = $date->timeAgoInWords(
  233. [
  234. 'timezone' => 'America/Vancouver',
  235. 'end' => '+1month',
  236. 'format' => 'dd-MM-YYYY',
  237. ]
  238. );
  239. $this->assertEquals('on 31-07-1990', $result);
  240. }
  241. /**
  242. * provider for timeAgo with an end date.
  243. *
  244. * @return array
  245. */
  246. public function timeAgoEndProvider()
  247. {
  248. return [
  249. [
  250. '+4 months +2 weeks +3 days',
  251. '4 months, 2 weeks, 3 days',
  252. '8 years',
  253. ],
  254. [
  255. '+4 months +2 weeks +1 day',
  256. '4 months, 2 weeks, 1 day',
  257. '8 years',
  258. ],
  259. [
  260. '+3 months +2 weeks',
  261. '3 months, 2 weeks',
  262. '8 years',
  263. ],
  264. [
  265. '+3 months +2 weeks +1 day',
  266. '3 months, 2 weeks, 1 day',
  267. '8 years',
  268. ],
  269. [
  270. '+1 months +1 week +1 day',
  271. '1 month, 1 week, 1 day',
  272. '8 years',
  273. ],
  274. [
  275. '+2 months +2 days',
  276. '2 months, 2 days',
  277. '+2 months +2 days',
  278. ],
  279. [
  280. '+2 months +12 days',
  281. '2 months, 1 week, 5 days',
  282. '3 months',
  283. ],
  284. ];
  285. }
  286. /**
  287. * test the end option for timeAgoInWords
  288. *
  289. * @dataProvider timeAgoEndProvider
  290. * @return void
  291. */
  292. public function testTimeAgoInWordsEnd($input, $expected, $end)
  293. {
  294. $time = new Date($input);
  295. $result = $time->timeAgoInWords(['end' => $end]);
  296. $this->assertEquals($expected, $result);
  297. }
  298. /**
  299. * test the end option for timeAgoInWords
  300. *
  301. * @dataProvider timeAgoEndProvider
  302. * @return void
  303. */
  304. public function testTimeAgoInWordsEndFrozenDate($input, $expected, $end)
  305. {
  306. $time = new FrozenDate($input);
  307. $result = $time->timeAgoInWords(['end' => $end]);
  308. $this->assertEquals($expected, $result);
  309. }
  310. /**
  311. * test the custom string options for timeAgoInWords
  312. *
  313. * @dataProvider classNameProvider
  314. * @return void
  315. */
  316. public function testTimeAgoInWordsCustomStrings($class)
  317. {
  318. $date = new $class('-8 years -4 months -2 weeks -3 days');
  319. $result = $date->timeAgoInWords([
  320. 'relativeString' => 'at least %s ago',
  321. 'accuracy' => ['year' => 'year'],
  322. 'end' => '+10 years',
  323. ]);
  324. $expected = 'at least 8 years ago';
  325. $this->assertEquals($expected, $result);
  326. $date = new $class('+4 months +2 weeks +3 days');
  327. $result = $date->timeAgoInWords([
  328. 'absoluteString' => 'exactly on %s',
  329. 'accuracy' => ['year' => 'year'],
  330. 'end' => '+2 months',
  331. ]);
  332. $expected = 'exactly on ' . date('n/j/y', strtotime('+4 months +2 weeks +3 days'));
  333. $this->assertEquals($expected, $result);
  334. }
  335. /**
  336. * Test the accuracy option for timeAgoInWords()
  337. *
  338. * @dataProvider classNameProvider
  339. * @return void
  340. */
  341. public function testDateAgoInWordsAccuracy($class)
  342. {
  343. $date = new $class('+8 years +4 months +2 weeks +3 days');
  344. $result = $date->timeAgoInWords([
  345. 'accuracy' => ['year' => 'year'],
  346. 'end' => '+10 years',
  347. ]);
  348. $expected = '8 years';
  349. $this->assertEquals($expected, $result);
  350. $date = new $class('+8 years +4 months +2 weeks +3 days');
  351. $result = $date->timeAgoInWords([
  352. 'accuracy' => ['year' => 'month'],
  353. 'end' => '+10 years',
  354. ]);
  355. $expected = '8 years, 4 months';
  356. $this->assertEquals($expected, $result);
  357. $date = new $class('+8 years +4 months +2 weeks +3 days');
  358. $result = $date->timeAgoInWords([
  359. 'accuracy' => ['year' => 'week'],
  360. 'end' => '+10 years',
  361. ]);
  362. $expected = '8 years, 4 months, 2 weeks';
  363. $this->assertEquals($expected, $result);
  364. $date = new $class('+8 years +4 months +2 weeks +3 days');
  365. $result = $date->timeAgoInWords([
  366. 'accuracy' => ['year' => 'day'],
  367. 'end' => '+10 years',
  368. ]);
  369. $expected = '8 years, 4 months, 2 weeks, 3 days';
  370. $this->assertEquals($expected, $result);
  371. $date = new $class('+1 years +5 weeks');
  372. $result = $date->timeAgoInWords([
  373. 'accuracy' => ['year' => 'year'],
  374. 'end' => '+10 years',
  375. ]);
  376. $expected = '1 year';
  377. $this->assertEquals($expected, $result);
  378. $date = new $class('+23 hours');
  379. $result = $date->timeAgoInWords([
  380. 'accuracy' => 'day',
  381. ]);
  382. $expected = 'today';
  383. $this->assertEquals($expected, $result);
  384. }
  385. /**
  386. * Test the format option of timeAgoInWords()
  387. *
  388. * @dataProvider classNameProvider
  389. * @return void
  390. */
  391. public function testDateAgoInWordsWithFormat($class)
  392. {
  393. $date = new $class('2007-9-25');
  394. $result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
  395. $this->assertEquals('on 2007-09-25', $result);
  396. $date = new $class('2007-9-25');
  397. $result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
  398. $this->assertEquals('on 2007-09-25', $result);
  399. $date = new $class('+2 weeks +2 days');
  400. $result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
  401. $this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
  402. $date = new $class('+2 months +2 days');
  403. $result = $date->timeAgoInWords(['end' => '1 month', 'format' => 'yyyy-MM-dd']);
  404. $this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
  405. }
  406. /**
  407. * test timeAgoInWords() with negative values.
  408. *
  409. * @dataProvider classNameProvider
  410. * @return void
  411. */
  412. public function testDateAgoInWordsNegativeValues($class)
  413. {
  414. $date = new $class('-2 months -2 days');
  415. $result = $date->timeAgoInWords(['end' => '3 month']);
  416. $this->assertEquals('2 months, 2 days ago', $result);
  417. $date = new $class('-2 months -2 days');
  418. $result = $date->timeAgoInWords(['end' => '3 month']);
  419. $this->assertEquals('2 months, 2 days ago', $result);
  420. $date = new $class('-2 months -2 days');
  421. $result = $date->timeAgoInWords(['end' => '1 month', 'format' => 'yyyy-MM-dd']);
  422. $this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
  423. $date = new $class('-2 years -5 months -2 days');
  424. $result = $date->timeAgoInWords(['end' => '3 years']);
  425. $this->assertEquals('2 years, 5 months, 2 days ago', $result);
  426. $date = new $class('-2 weeks -2 days');
  427. $result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
  428. $this->assertEquals('2 weeks, 2 days ago', $result);
  429. $date = new $class('-3 years -12 months');
  430. $result = $date->timeAgoInWords();
  431. $expected = 'on ' . $date->format('n/j/y');
  432. $this->assertEquals($expected, $result);
  433. $date = new $class('-1 month -1 week -6 days');
  434. $result = $date->timeAgoInWords(
  435. ['end' => '1 year', 'accuracy' => ['month' => 'month']]
  436. );
  437. $this->assertEquals('1 month ago', $result);
  438. $date = new $class('-1 years -2 weeks -3 days');
  439. $result = $date->timeAgoInWords(
  440. ['accuracy' => ['year' => 'year']]
  441. );
  442. $expected = 'on ' . $date->format('n/j/y');
  443. $this->assertEquals($expected, $result);
  444. $date = new $class('-13 months -5 days');
  445. $result = $date->timeAgoInWords(['end' => '2 years']);
  446. $this->assertEquals('1 year, 1 month, 5 days ago', $result);
  447. $date = new $class('-23 hours');
  448. $result = $date->timeAgoInWords(['accuracy' => 'day']);
  449. $this->assertEquals('today', $result);
  450. }
  451. /**
  452. * Tests that parsing a date in a timezone other than UTC
  453. * will not alter the date
  454. *
  455. * @dataProvider classNameProvider
  456. * @return void
  457. */
  458. public function testParseDateDifferentTimezone($class)
  459. {
  460. date_default_timezone_set('Europe/Paris');
  461. $result = $class::parseDate('25-02-2016', 'd-M-y');
  462. $this->assertEquals('25-02-2016', $result->format('d-m-Y'));
  463. }
  464. /**
  465. * Tests the default locale setter.
  466. *
  467. * @dataProvider classNameProvider
  468. * @return void
  469. */
  470. public function testGetSetDefaultLocale($class)
  471. {
  472. $class::setDefaultLocale('fr-FR');
  473. $this->assertSame('fr-FR', $class::getDefaultLocale());
  474. }
  475. /**
  476. * Tests the default locale setter.
  477. *
  478. * @dataProvider classNameProvider
  479. * @return void
  480. */
  481. public function testDefaultLocaleEffectsFormatting($class)
  482. {
  483. $result = $class::parseDate('12/03/2015');
  484. $this->assertEquals('Dec 3, 2015', $result->nice());
  485. $class::setDefaultLocale('fr-FR');
  486. $result = $class::parseDate('12/03/2015');
  487. $this->assertEquals('12 mars 2015', $result->nice());
  488. $expected = 'Y-m-d';
  489. $result = $class::parseDate('12/03/2015');
  490. $this->assertEquals('2015-03-12', $result->format($expected));
  491. }
  492. }