TimeHelperTest.php 18 KB

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