TimeHelperTest.php 21 KB

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