TimeHelperTest.php 17 KB

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