DateTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.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::$defaultLocale;
  40. }
  41. /**
  42. * Teardown
  43. *
  44. * @return void
  45. */
  46. public function tearDown()
  47. {
  48. parent::tearDown();
  49. Date::$defaultLocale = $this->locale;
  50. FrozenDate::$defaultLocale = $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 void
  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::$defaultLocale = 'fr-FR';
  98. $result = $time->i18nFormat(\IntlDateFormatter::FULL);
  99. $result = str_replace(' à', '', $result);
  100. $expected = 'jeudi 14 janvier 2010 00:00:00 UTC';
  101. $this->assertEquals($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. }
  105. /**
  106. * test __toString
  107. *
  108. * @dataProvider classNameProvider
  109. * @return void
  110. */
  111. public function testToString($class)
  112. {
  113. $date = new $class('2015-11-06 11:32:45');
  114. $this->assertEquals('11/6/15', (string)$date);
  115. }
  116. /**
  117. * test nice()
  118. *
  119. * @dataProvider classNameProvider
  120. * @return void
  121. */
  122. public function testNice($class)
  123. {
  124. $date = new $class('2015-11-06 11:32:45');
  125. $this->assertEquals('Nov 6, 2015', $date->nice());
  126. $this->assertEquals('Nov 6, 2015', $date->nice(new DateTimeZone('America/New_York')));
  127. $this->assertEquals('6 nov. 2015', $date->nice(null, 'fr-FR'));
  128. }
  129. /**
  130. * test jsonSerialize()
  131. *
  132. * @dataProvider classNameProvider
  133. * @return void
  134. */
  135. public function testJsonSerialize($class)
  136. {
  137. $date = new $class('2015-11-06 11:32:45');
  138. $this->assertEquals('"2015-11-06T00:00:00+0000"', json_encode($date));
  139. }
  140. /**
  141. * test parseDate()
  142. *
  143. * @dataProvider classNameProvider
  144. * @return void
  145. */
  146. public function testParseDate($class)
  147. {
  148. $date = $class::parseDate('11/6/15');
  149. $this->assertEquals('2015-11-06 00:00:00', $date->format('Y-m-d H:i:s'));
  150. $class::$defaultLocale = 'fr-FR';
  151. $date = $class::parseDate('13 10, 2015');
  152. $this->assertEquals('2015-10-13 00:00:00', $date->format('Y-m-d H:i:s'));
  153. }
  154. /**
  155. * test parseDateTime()
  156. *
  157. * @dataProvider classNameProvider
  158. * @return void
  159. */
  160. public function testParseDateTime($class)
  161. {
  162. $date = $class::parseDate('11/6/15 12:33:12');
  163. $this->assertEquals('2015-11-06 00:00:00', $date->format('Y-m-d H:i:s'));
  164. $class::$defaultLocale = 'fr-FR';
  165. $date = $class::parseDate('13 10, 2015 12:54:12');
  166. $this->assertEquals('2015-10-13 00:00:00', $date->format('Y-m-d H:i:s'));
  167. }
  168. /**
  169. * provider for timeAgoInWords() tests
  170. *
  171. * @return array
  172. */
  173. public static function timeAgoProvider()
  174. {
  175. return [
  176. ['-12 seconds', 'today'],
  177. ['-12 minutes', 'today'],
  178. ['-2 hours', 'today'],
  179. ['-1 day', '1 day ago'],
  180. ['-2 days', '2 days ago'],
  181. ['-1 week', '1 week ago'],
  182. ['-2 weeks -2 days', '2 weeks, 2 days ago'],
  183. ['+1 second', 'today'],
  184. ['+1 minute, +10 seconds', 'today'],
  185. ['+1 week', '1 week'],
  186. ['+1 week 1 day', '1 week, 1 day'],
  187. ['+2 weeks 2 day', '2 weeks, 2 days'],
  188. ['2007-9-24', 'on 9/24/07'],
  189. ['now', 'today'],
  190. ];
  191. }
  192. /**
  193. * testTimeAgoInWords method
  194. *
  195. * @dataProvider timeAgoProvider
  196. * @return void
  197. */
  198. public function testTimeAgoInWords($input, $expected)
  199. {
  200. $date = new Date($input);
  201. $result = $date->timeAgoInWords();
  202. $this->assertEquals($expected, $result);
  203. }
  204. /**
  205. * testTimeAgoInWords with Frozen Date
  206. *
  207. * @dataProvider timeAgoProvider
  208. * @return void
  209. */
  210. public function testTimeAgoInWordsFrozenDate($input, $expected)
  211. {
  212. $date = new FrozenDate($input);
  213. $result = $date->timeAgoInWords();
  214. $this->assertEquals($expected, $result);
  215. }
  216. /**
  217. * test the timezone option for timeAgoInWords
  218. *
  219. * @dataProvider classNameProvider
  220. * @return void
  221. */
  222. public function testTimeAgoInWordsTimezone($class)
  223. {
  224. $date = new $class('1990-07-31 20:33:00 UTC');
  225. $result = $date->timeAgoInWords(
  226. [
  227. 'timezone' => 'America/Vancouver',
  228. 'end' => '+1month',
  229. 'format' => 'dd-MM-YYYY'
  230. ]
  231. );
  232. $this->assertEquals('on 31-07-1990', $result);
  233. }
  234. /**
  235. * provider for timeAgo with an end date.
  236. *
  237. * @return void
  238. */
  239. public function timeAgoEndProvider()
  240. {
  241. return [
  242. [
  243. '+4 months +2 weeks +3 days',
  244. '4 months, 2 weeks, 3 days',
  245. '8 years'
  246. ],
  247. [
  248. '+4 months +2 weeks +1 day',
  249. '4 months, 2 weeks, 1 day',
  250. '8 years'
  251. ],
  252. [
  253. '+3 months +2 weeks',
  254. '3 months, 2 weeks',
  255. '8 years'
  256. ],
  257. [
  258. '+3 months +2 weeks +1 day',
  259. '3 months, 2 weeks, 1 day',
  260. '8 years'
  261. ],
  262. [
  263. '+1 months +1 week +1 day',
  264. '1 month, 1 week, 1 day',
  265. '8 years'
  266. ],
  267. [
  268. '+2 months +2 days',
  269. '2 months, 2 days',
  270. '+2 months +2 days'
  271. ],
  272. [
  273. '+2 months +12 days',
  274. '2 months, 1 week, 5 days',
  275. '3 months'
  276. ],
  277. ];
  278. }
  279. /**
  280. * test the end option for timeAgoInWords
  281. *
  282. * @dataProvider timeAgoEndProvider
  283. * @return void
  284. */
  285. public function testTimeAgoInWordsEnd($input, $expected, $end)
  286. {
  287. $time = new Date($input);
  288. $result = $time->timeAgoInWords(['end' => $end]);
  289. $this->assertEquals($expected, $result);
  290. }
  291. /**
  292. * test the end option for timeAgoInWords
  293. *
  294. * @dataProvider timeAgoEndProvider
  295. * @return void
  296. */
  297. public function testTimeAgoInWordsEndFrozenDate($input, $expected, $end)
  298. {
  299. $time = new FrozenDate($input);
  300. $result = $time->timeAgoInWords(['end' => $end]);
  301. $this->assertEquals($expected, $result);
  302. }
  303. /**
  304. * test the custom string options for timeAgoInWords
  305. *
  306. * @dataProvider classNameProvider
  307. * @return void
  308. */
  309. public function testTimeAgoInWordsCustomStrings($class)
  310. {
  311. $date = new $class('-8 years -4 months -2 weeks -3 days');
  312. $result = $date->timeAgoInWords([
  313. 'relativeString' => 'at least %s ago',
  314. 'accuracy' => ['year' => 'year'],
  315. 'end' => '+10 years'
  316. ]);
  317. $expected = 'at least 8 years ago';
  318. $this->assertEquals($expected, $result);
  319. $date = new $class('+4 months +2 weeks +3 days');
  320. $result = $date->timeAgoInWords([
  321. 'absoluteString' => 'exactly on %s',
  322. 'accuracy' => ['year' => 'year'],
  323. 'end' => '+2 months'
  324. ]);
  325. $expected = 'exactly on ' . date('n/j/y', strtotime('+4 months +2 weeks +3 days'));
  326. $this->assertEquals($expected, $result);
  327. }
  328. /**
  329. * Test the accuracy option for timeAgoInWords()
  330. *
  331. * @dataProvider classNameProvider
  332. * @return void
  333. */
  334. public function testDateAgoInWordsAccuracy($class)
  335. {
  336. $date = new $class('+8 years +4 months +2 weeks +3 days');
  337. $result = $date->timeAgoInWords([
  338. 'accuracy' => ['year' => 'year'],
  339. 'end' => '+10 years'
  340. ]);
  341. $expected = '8 years';
  342. $this->assertEquals($expected, $result);
  343. $date = new $class('+8 years +4 months +2 weeks +3 days');
  344. $result = $date->timeAgoInWords([
  345. 'accuracy' => ['year' => 'month'],
  346. 'end' => '+10 years'
  347. ]);
  348. $expected = '8 years, 4 months';
  349. $this->assertEquals($expected, $result);
  350. $date = new $class('+8 years +4 months +2 weeks +3 days');
  351. $result = $date->timeAgoInWords([
  352. 'accuracy' => ['year' => 'week'],
  353. 'end' => '+10 years'
  354. ]);
  355. $expected = '8 years, 4 months, 2 weeks';
  356. $this->assertEquals($expected, $result);
  357. $date = new $class('+8 years +4 months +2 weeks +3 days');
  358. $result = $date->timeAgoInWords([
  359. 'accuracy' => ['year' => 'day'],
  360. 'end' => '+10 years'
  361. ]);
  362. $expected = '8 years, 4 months, 2 weeks, 3 days';
  363. $this->assertEquals($expected, $result);
  364. $date = new $class('+1 years +5 weeks');
  365. $result = $date->timeAgoInWords([
  366. 'accuracy' => ['year' => 'year'],
  367. 'end' => '+10 years'
  368. ]);
  369. $expected = '1 year';
  370. $this->assertEquals($expected, $result);
  371. $date = new $class('+23 hours');
  372. $result = $date->timeAgoInWords([
  373. 'accuracy' => 'day'
  374. ]);
  375. $expected = 'today';
  376. $this->assertEquals($expected, $result);
  377. }
  378. /**
  379. * Test the format option of timeAgoInWords()
  380. *
  381. * @dataProvider classNameProvider
  382. * @return void
  383. */
  384. public function testDateAgoInWordsWithFormat($class)
  385. {
  386. $date = new $class('2007-9-25');
  387. $result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
  388. $this->assertEquals('on 2007-09-25', $result);
  389. $date = new $class('2007-9-25');
  390. $result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
  391. $this->assertEquals('on 2007-09-25', $result);
  392. $date = new $class('+2 weeks +2 days');
  393. $result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
  394. $this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
  395. $date = new $class('+2 months +2 days');
  396. $result = $date->timeAgoInWords(['end' => '1 month', 'format' => 'yyyy-MM-dd']);
  397. $this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
  398. }
  399. /**
  400. * test timeAgoInWords() with negative values.
  401. *
  402. * @dataProvider classNameProvider
  403. * @return void
  404. */
  405. public function testDateAgoInWordsNegativeValues($class)
  406. {
  407. $date = new $class('-2 months -2 days');
  408. $result = $date->timeAgoInWords(['end' => '3 month']);
  409. $this->assertEquals('2 months, 2 days ago', $result);
  410. $date = new $class('-2 months -2 days');
  411. $result = $date->timeAgoInWords(['end' => '3 month']);
  412. $this->assertEquals('2 months, 2 days ago', $result);
  413. $date = new $class('-2 months -2 days');
  414. $result = $date->timeAgoInWords(['end' => '1 month', 'format' => 'yyyy-MM-dd']);
  415. $this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
  416. $date = new $class('-2 years -5 months -2 days');
  417. $result = $date->timeAgoInWords(['end' => '3 years']);
  418. $this->assertEquals('2 years, 5 months, 2 days ago', $result);
  419. $date = new $class('-2 weeks -2 days');
  420. $result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
  421. $this->assertEquals('2 weeks, 2 days ago', $result);
  422. $date = new $class('-3 years -12 months');
  423. $result = $date->timeAgoInWords();
  424. $expected = 'on ' . $date->format('n/j/y');
  425. $this->assertEquals($expected, $result);
  426. $date = new $class('-1 month -1 week -6 days');
  427. $result = $date->timeAgoInWords(
  428. ['end' => '1 year', 'accuracy' => ['month' => 'month']]
  429. );
  430. $this->assertEquals('1 month ago', $result);
  431. $date = new $class('-1 years -2 weeks -3 days');
  432. $result = $date->timeAgoInWords(
  433. ['accuracy' => ['year' => 'year']]
  434. );
  435. $expected = 'on ' . $date->format('n/j/y');
  436. $this->assertEquals($expected, $result);
  437. $date = new $class('-13 months -5 days');
  438. $result = $date->timeAgoInWords(['end' => '2 years']);
  439. $this->assertEquals('1 year, 1 month, 5 days ago', $result);
  440. $date = new $class('-23 hours');
  441. $result = $date->timeAgoInWords(['accuracy' => 'day']);
  442. $this->assertEquals('today', $result);
  443. }
  444. /**
  445. * Tests that parsing a date in a timezone other than UTC
  446. * will not alter the date
  447. *
  448. * @dataProvider classNameProvider
  449. * @return void
  450. */
  451. public function testParseDateDifferentTimezone($class)
  452. {
  453. date_default_timezone_set('Europe/Paris');
  454. $result = $class::parseDate('25-02-2016', 'd-M-y');
  455. $this->assertEquals('25-02-2016', $result->format('d-m-Y'));
  456. }
  457. }