TimeHelperTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use Cake\I18n\DateTime;
  18. use Cake\I18n\I18n;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\View\Helper\TimeHelper;
  21. use Cake\View\View;
  22. use DateTime as NativeDateTime;
  23. use DateTimeZone;
  24. use IntlDateFormatter;
  25. /**
  26. * TimeHelperTest class
  27. */
  28. class TimeHelperTest extends TestCase
  29. {
  30. /**
  31. * @var \Cake\View\Helper\TimeHelper
  32. */
  33. protected $Time;
  34. /**
  35. * @var \Cake\View\View
  36. */
  37. protected $View;
  38. /**
  39. * setUp method
  40. */
  41. public function setUp(): void
  42. {
  43. parent::setUp();
  44. $this->View = new View();
  45. $this->Time = new TimeHelper($this->View);
  46. }
  47. /**
  48. * tearDown method
  49. */
  50. public function tearDown(): void
  51. {
  52. parent::tearDown();
  53. DateTime::setDefaultLocale(null);
  54. I18n::setLocale(I18n::getDefaultLocale());
  55. }
  56. /**
  57. * Test element wrapping in timeAgoInWords
  58. */
  59. public function testTimeAgoInWords(): void
  60. {
  61. $Time = new TimeHelper($this->View);
  62. $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
  63. $result = $Time->timeAgoInWords($timestamp, [
  64. 'end' => '1 years',
  65. 'element' => 'span',
  66. ]);
  67. $expected = [
  68. 'span' => [
  69. 'title' => $timestamp,
  70. 'class' => 'time-ago-in-words',
  71. ],
  72. 'on ' . date('n/j/y', $timestamp),
  73. '/span',
  74. ];
  75. $this->assertHtml($expected, $result);
  76. $result = $Time->timeAgoInWords($timestamp, [
  77. 'end' => '1 years',
  78. 'element' => [
  79. 'title' => 'testing',
  80. 'rel' => 'test',
  81. ],
  82. ]);
  83. $expected = [
  84. 'span' => [
  85. 'title' => 'testing',
  86. 'class' => 'time-ago-in-words',
  87. 'rel' => 'test',
  88. ],
  89. 'on ' . date('n/j/y', $timestamp),
  90. '/span',
  91. ];
  92. $this->assertHtml($expected, $result);
  93. $timestamp = strtotime('+2 weeks');
  94. $result = $Time->timeAgoInWords(
  95. $timestamp,
  96. ['end' => '1 years', 'element' => 'div']
  97. );
  98. $expected = [
  99. 'div' => [
  100. 'title' => $timestamp,
  101. 'class' => 'time-ago-in-words',
  102. ],
  103. '2 weeks',
  104. '/div',
  105. ];
  106. $this->assertHtml($expected, $result);
  107. }
  108. /**
  109. * Test output timezone with timeAgoInWords
  110. */
  111. public function testTimeAgoInWordsOutputTimezone(): void
  112. {
  113. $Time = new TimeHelper($this->View, ['outputTimezone' => 'America/Vancouver']);
  114. $timestamp = new DateTime('+8 years, +4 months +2 weeks +3 days');
  115. $result = $Time->timeAgoInWords($timestamp, [
  116. 'end' => '1 years',
  117. 'element' => 'span',
  118. ]);
  119. $vancouver = clone $timestamp;
  120. $vancouver = $vancouver->setTimezone('America/Vancouver');
  121. $expected = [
  122. 'span' => [
  123. 'title' => $vancouver->__toString(),
  124. 'class' => 'time-ago-in-words',
  125. ],
  126. 'on ' . $vancouver->format('n/j/y'),
  127. '/span',
  128. ];
  129. $this->assertHtml($expected, $result);
  130. }
  131. /**
  132. * testToQuarter method
  133. */
  134. public function testToQuarter(): void
  135. {
  136. $this->assertSame(4, $this->Time->toQuarter('2007-12-25'));
  137. $this->assertEquals(['2007-10-01', '2007-12-31'], $this->Time->toQuarter('2007-12-25', true));
  138. }
  139. /**
  140. * testNice method
  141. */
  142. public function testNice(): void
  143. {
  144. $time = '2014-04-20 20:00';
  145. $this->assertTimeFormat('Apr 20, 2014, 8:00 PM', $this->Time->nice($time));
  146. $result = $this->Time->nice($time, 'America/New_York');
  147. $this->assertTimeFormat('Apr 20, 2014, 4:00 PM', $result);
  148. }
  149. /**
  150. * test nice with outputTimezone
  151. */
  152. public function testNiceOutputTimezone(): void
  153. {
  154. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  155. $time = '2014-04-20 20:00';
  156. $this->assertTimeFormat('Apr 20, 2014, 1:00 PM', $this->Time->nice($time));
  157. }
  158. /**
  159. * testToUnix method
  160. */
  161. public function testToUnix(): void
  162. {
  163. $this->assertSame('1397980800', $this->Time->toUnix('2014-04-20 08:00:00'));
  164. }
  165. /**
  166. * testToAtom method
  167. */
  168. public function testToAtom(): void
  169. {
  170. $dateTime = new NativeDateTime();
  171. $this->assertSame($dateTime->format($dateTime::ATOM), $this->Time->toAtom($dateTime->getTimestamp()));
  172. }
  173. /**
  174. * testToAtom method
  175. */
  176. public function testToAtomOutputTimezone(): void
  177. {
  178. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  179. $dateTime = new DateTime();
  180. $vancouver = clone $dateTime;
  181. $vancouver = $vancouver->setTimezone('America/Vancouver');
  182. $this->assertSame($vancouver->format(DATE_ATOM), $this->Time->toAtom($vancouver));
  183. }
  184. /**
  185. * testToRss method
  186. */
  187. public function testToRss(): void
  188. {
  189. $date = '2012-08-12 12:12:45';
  190. $time = strtotime($date);
  191. $this->assertSame(date('r', $time), $this->Time->toRss($time));
  192. $timezones = ['Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu'];
  193. foreach ($timezones as $timezone) {
  194. $yourTimezone = new DateTimeZone($timezone);
  195. $yourTime = new NativeDateTime($date, $yourTimezone);
  196. $time = $yourTime->format('U');
  197. $this->assertSame($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
  198. }
  199. }
  200. /**
  201. * test toRss with outputTimezone
  202. */
  203. public function testToRssOutputTimezone(): void
  204. {
  205. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  206. $dateTime = new DateTime();
  207. $vancouver = clone $dateTime;
  208. $vancouver = $vancouver->setTimezone('America/Vancouver');
  209. $this->assertSame($vancouver->format('r'), $this->Time->toRss($vancouver));
  210. }
  211. /**
  212. * testOfGmt method
  213. */
  214. public function testGmt(): void
  215. {
  216. $this->assertSame('1397980800', $this->Time->gmt('2014-04-20 08:00:00'));
  217. }
  218. /**
  219. * testIsToday method
  220. */
  221. public function testIsToday(): void
  222. {
  223. $result = $this->Time->isToday('+1 day');
  224. $this->assertFalse($result);
  225. $result = $this->Time->isToday('+1 days');
  226. $this->assertFalse($result);
  227. $result = $this->Time->isToday('+0 day');
  228. $this->assertTrue($result);
  229. $result = $this->Time->isToday('-1 day');
  230. $this->assertFalse($result);
  231. }
  232. /**
  233. * testIsFuture method
  234. */
  235. public function testIsFuture(): void
  236. {
  237. $this->assertTrue($this->Time->isFuture('+1 month'));
  238. $this->assertTrue($this->Time->isFuture('+1 days'));
  239. $this->assertTrue($this->Time->isFuture('+1 minute'));
  240. $this->assertTrue($this->Time->isFuture('+1 second'));
  241. $this->assertFalse($this->Time->isFuture('-1 second'));
  242. $this->assertFalse($this->Time->isFuture('-1 day'));
  243. $this->assertFalse($this->Time->isFuture('-1 week'));
  244. $this->assertFalse($this->Time->isFuture('-1 month'));
  245. }
  246. /**
  247. * testIsPast method
  248. */
  249. public function testIsPast(): void
  250. {
  251. $this->assertFalse($this->Time->isPast('+1 month'));
  252. $this->assertFalse($this->Time->isPast('+1 days'));
  253. $this->assertFalse($this->Time->isPast('+1 minute'));
  254. $this->assertFalse($this->Time->isPast('+1 second'));
  255. $this->assertTrue($this->Time->isPast('-1 second'));
  256. $this->assertTrue($this->Time->isPast('-1 day'));
  257. $this->assertTrue($this->Time->isPast('-1 week'));
  258. $this->assertTrue($this->Time->isPast('-1 month'));
  259. }
  260. /**
  261. * testIsThisWeek method
  262. */
  263. public function testIsThisWeek(): void
  264. {
  265. // A map of days which goes from -1 day of week to +1 day of week
  266. $map = [
  267. 'Mon' => [-1, 7], 'Tue' => [-2, 6], 'Wed' => [-3, 5],
  268. 'Thu' => [-4, 4], 'Fri' => [-5, 3], 'Sat' => [-6, 2],
  269. 'Sun' => [-7, 1],
  270. ];
  271. $days = $map[date('D')];
  272. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  273. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  274. }
  275. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  276. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  277. }
  278. /**
  279. * testIsThisMonth method
  280. */
  281. public function testIsThisMonth(): void
  282. {
  283. $result = $this->Time->isThisMonth('+0 day');
  284. $this->assertTrue($result);
  285. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, (int)date('m'), mt_rand(1, 28), (int)date('Y')));
  286. $this->assertTrue($result);
  287. $result = $this->Time->isThisMonth(mktime(0, 0, 0, (int)date('m'), mt_rand(1, 28), (int)date('Y') - mt_rand(1, 12)));
  288. $this->assertFalse($result);
  289. $result = $this->Time->isThisMonth(mktime(0, 0, 0, (int)date('m'), mt_rand(1, 28), (int)date('Y') + mt_rand(1, 12)));
  290. $this->assertFalse($result);
  291. }
  292. /**
  293. * testIsThisYear method
  294. */
  295. public function testIsThisYear(): void
  296. {
  297. $result = $this->Time->isThisYear('+0 day');
  298. $this->assertTrue($result);
  299. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), (int)date('Y')));
  300. $this->assertTrue($result);
  301. }
  302. /**
  303. * testWasYesterday method
  304. */
  305. public function testWasYesterday(): void
  306. {
  307. $result = $this->Time->wasYesterday('+1 day');
  308. $this->assertFalse($result);
  309. $result = $this->Time->wasYesterday('+1 days');
  310. $this->assertFalse($result);
  311. $result = $this->Time->wasYesterday('+0 day');
  312. $this->assertFalse($result);
  313. $result = $this->Time->wasYesterday('-1 day');
  314. $this->assertTrue($result);
  315. $result = $this->Time->wasYesterday('-1 days');
  316. $this->assertTrue($result);
  317. $result = $this->Time->wasYesterday('-2 days');
  318. $this->assertFalse($result);
  319. }
  320. /**
  321. * testIsTomorrow method
  322. */
  323. public function testIsTomorrow(): void
  324. {
  325. $result = $this->Time->isTomorrow('+1 day');
  326. $this->assertTrue($result);
  327. $result = $this->Time->isTomorrow('+1 days');
  328. $this->assertTrue($result);
  329. $result = $this->Time->isTomorrow('+0 day');
  330. $this->assertFalse($result);
  331. $result = $this->Time->isTomorrow('-1 day');
  332. $this->assertFalse($result);
  333. }
  334. /**
  335. * testWasWithinLast method
  336. */
  337. public function testWasWithinLast(): void
  338. {
  339. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  340. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  341. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  342. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  343. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  344. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-1 hour'));
  345. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  346. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  347. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  348. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  349. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  350. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  351. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  352. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  353. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  354. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  355. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  356. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  357. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  358. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  359. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  360. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  361. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  362. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  363. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  364. $this->assertTrue($this->Time->wasWithinLast('1 day', '-23 hours -59 minutes -59 seconds'));
  365. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  366. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  367. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  368. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  369. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  370. }
  371. /**
  372. * testisWithinNext method
  373. */
  374. public function testIsWithinNext(): void
  375. {
  376. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  377. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
  378. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  379. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
  380. $this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
  381. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  382. $this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
  383. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  384. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
  385. $this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
  386. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
  387. $this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
  388. $this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
  389. $this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
  390. $this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
  391. $this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
  392. $this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
  393. $this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
  394. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  395. $this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
  396. $this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
  397. $this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
  398. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
  399. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  400. $this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
  401. $this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
  402. $this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
  403. $this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
  404. $this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
  405. $this->assertTrue($this->Time->isWithinNext('1 day', '+1 day'));
  406. $this->assertTrue($this->Time->isWithinNext('7 day', '+1 week'));
  407. $this->assertTrue($this->Time->isWithinNext('1 minute', '+1 second'));
  408. $this->assertTrue($this->Time->isWithinNext('1 month', '+1 month'));
  409. }
  410. /**
  411. * test formatting dates taking in account preferred i18n locale file
  412. */
  413. public function testFormat(): void
  414. {
  415. $time = strtotime('Thu Jan 14 13:59:28 2010');
  416. $result = $this->Time->format($time);
  417. $expected = '1/14/10, 1:59 PM';
  418. $this->assertTimeFormat($expected, $result);
  419. $result = $this->Time->format($time, IntlDateFormatter::FULL);
  420. $expected = 'Thursday, January 14, 2010 at 1:59:28 PM';
  421. $this->assertStringStartsWith($expected, $result);
  422. $result = $this->Time->format('invalid date', null, 'Date invalid');
  423. $expected = 'Date invalid';
  424. $this->assertSame($expected, $result);
  425. I18n::setLocale('fr_FR');
  426. DateTime::setDefaultLocale('fr_FR');
  427. $time = new DateTime('Thu Jan 14 13:59:28 2010');
  428. $result = $this->Time->format($time, IntlDateFormatter::FULL);
  429. $this->assertStringContainsString('jeudi 14 janvier 2010', $result);
  430. $this->assertStringContainsString('13:59:28', $result);
  431. }
  432. /**
  433. * test format with outputTimezone
  434. */
  435. public function testFormatOutputTimezone(): void
  436. {
  437. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  438. $time = strtotime('Thu Jan 14 8:59:28 2010 UTC');
  439. $result = $this->Time->format($time);
  440. $expected = '1/14/10, 12:59 AM';
  441. $this->assertTimeFormat($expected, $result);
  442. $time = new DateTime('Thu Jan 14 8:59:28 2010', 'UTC');
  443. $result = $this->Time->format($time);
  444. $expected = '1/14/10, 12:59 AM';
  445. $this->assertTimeFormat($expected, $result);
  446. }
  447. /**
  448. * test i18nFormat with outputTimezone
  449. */
  450. public function testI18nFormatOutputTimezone(): void
  451. {
  452. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  453. $time = strtotime('Thu Jan 14 8:59:28 2010 UTC');
  454. $result = $this->Time->i18nFormat($time, [IntlDateFormatter::SHORT, IntlDateFormatter::FULL]);
  455. $expected = '1/14/10, 12:59:28 AM';
  456. $this->assertStringStartsWith($expected, $result);
  457. }
  458. /**
  459. * Test format() with a string.
  460. */
  461. public function testFormatString(): void
  462. {
  463. $time = '2010-01-14 13:59:28';
  464. $result = $this->Time->format($time);
  465. $this->assertTimeFormat('1/14/10 1:59 PM', $result);
  466. $result = $this->Time->format($time, 'HH:mm', false, 'America/New_York');
  467. $this->assertTimeFormat('08:59', $result);
  468. }
  469. /**
  470. * Test format() with a Time instance.
  471. */
  472. public function testFormatTimeInstance(): void
  473. {
  474. $time = new DateTime('2010-01-14 13:59:28', 'America/New_York');
  475. $result = $this->Time->format($time, 'HH:mm', false, 'America/New_York');
  476. $this->assertTimeFormat('13:59', $result);
  477. $time = new DateTime('2010-01-14 13:59:28', 'UTC');
  478. $result = $this->Time->format($time, 'HH:mm', false, 'America/New_York');
  479. $this->assertTimeFormat('08:59', $result);
  480. }
  481. /**
  482. * Custom assert to allow for variation in the version of the intl library, where
  483. * some translations contain a few extra commas.
  484. *
  485. * @param string $expected
  486. * @param string $result
  487. */
  488. public function assertTimeFormat($expected, $result): void
  489. {
  490. $this->assertSame(
  491. str_replace([',', '(', ')', ' at', ' à'], '', $expected),
  492. str_replace([',', '(', ')', ' at', ' à'], '', $result)
  493. );
  494. }
  495. /**
  496. * Test formatting in case the $time parameter is not set
  497. */
  498. public function testNullDateFormat(): void
  499. {
  500. $result = $this->Time->format(null);
  501. $this->assertFalse($result);
  502. $fallback = 'Date invalid or not set';
  503. $result = $this->Time->format(null, IntlDateFormatter::FULL, $fallback);
  504. $this->assertSame($fallback, $result);
  505. }
  506. }