TimeHelperTest.php 18 KB

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