TimeHelperTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * TimeHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since 1.2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  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. public $Time = null;
  30. /**
  31. * setUp method
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  36. parent::setUp();
  37. $this->View = new View();
  38. $this->Time = new TimeHelper($this->View);
  39. }
  40. /**
  41. * Test element wrapping in timeAgoInWords
  42. *
  43. * @return void
  44. */
  45. public function testTimeAgoInWords() {
  46. $Time = new TimeHelper($this->View);
  47. $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
  48. $result = $Time->timeAgoInWords($timestamp, array(
  49. 'end' => '1 years',
  50. 'element' => 'span'
  51. ));
  52. $expected = array(
  53. 'span' => array(
  54. 'title' => $timestamp,
  55. 'class' => 'time-ago-in-words'
  56. ),
  57. 'on ' . date('j/n/y', $timestamp),
  58. '/span'
  59. );
  60. $this->assertTags($result, $expected);
  61. $result = $Time->timeAgoInWords($timestamp, array(
  62. 'end' => '1 years',
  63. 'element' => array(
  64. 'title' => 'testing',
  65. 'rel' => 'test'
  66. )
  67. ));
  68. $expected = array(
  69. 'span' => array(
  70. 'title' => 'testing',
  71. 'class' => 'time-ago-in-words',
  72. 'rel' => 'test'
  73. ),
  74. 'on ' . date('j/n/y', $timestamp),
  75. '/span'
  76. );
  77. $this->assertTags($result, $expected);
  78. $timestamp = strtotime('+2 weeks');
  79. $result = $Time->timeAgoInWords(
  80. $timestamp,
  81. array('end' => '1 years', 'element' => 'div')
  82. );
  83. $expected = array(
  84. 'div' => array(
  85. 'title' => $timestamp,
  86. 'class' => 'time-ago-in-words'
  87. ),
  88. '2 weeks',
  89. '/div'
  90. );
  91. $this->assertTags($result, $expected);
  92. }
  93. /**
  94. * testToQuarter method
  95. *
  96. * @return void
  97. */
  98. public function testToQuarter() {
  99. $this->assertEquals(4, $this->Time->toQuarter('2007-12-25'));
  100. $this->assertEquals(['2007-10-01', '2007-12-31'], $this->Time->toQuarter('2007-12-25', true));
  101. }
  102. /**
  103. * testNice method
  104. *
  105. * @return void
  106. */
  107. public function testNice() {
  108. $time = '2014-04-20 20:00';
  109. $this->assertEquals('Apr 20, 2014, 8:00 PM', $this->Time->nice($time));
  110. $result = $this->Time->nice($time, 'America/New_York');
  111. $this->assertEquals('Apr 20, 2014, 4:00 PM', $result);
  112. }
  113. /**
  114. * testToUnix method
  115. *
  116. * @return void
  117. */
  118. public function testToUnix() {
  119. $this->assertEquals(time(), $this->Time->toUnix(time()));
  120. $this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
  121. $this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
  122. $this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
  123. }
  124. /**
  125. * testToAtom method
  126. *
  127. * @return void
  128. */
  129. public function testToAtom() {
  130. $dateTime = new \DateTime;
  131. $this->assertEquals($dateTime->format($dateTime::ATOM), $this->Time->toAtom($dateTime->getTimestamp()));
  132. }
  133. /**
  134. * testToRss method
  135. *
  136. * @return void
  137. */
  138. public function testToRss() {
  139. $date = '2012-08-12 12:12:45';
  140. $time = strtotime($date);
  141. $this->assertEquals(date('r', $time), $this->Time->toRss($time));
  142. $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
  143. foreach ($timezones as $timezone) {
  144. $yourTimezone = new \DateTimeZone($timezone);
  145. $yourTime = new \DateTime($date, $yourTimezone);
  146. $time = $yourTime->format('U');
  147. $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
  148. }
  149. }
  150. /**
  151. * testOfGmt method
  152. *
  153. * @return void
  154. */
  155. public function testGmt() {
  156. $hour = 3;
  157. $min = 4;
  158. $sec = 2;
  159. $month = 5;
  160. $day = 14;
  161. $year = 2007;
  162. $time = mktime($hour, $min, $sec, $month, $day, $year);
  163. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  164. $this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
  165. $hour = date('H');
  166. $min = date('i');
  167. $sec = date('s');
  168. $month = date('m');
  169. $day = date('d');
  170. $year = date('Y');
  171. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  172. $this->assertEquals($expected, $this->Time->gmt(null));
  173. }
  174. /**
  175. * testIsToday method
  176. *
  177. * @return void
  178. */
  179. public function testIsToday() {
  180. $result = $this->Time->isToday('+1 day');
  181. $this->assertFalse($result);
  182. $result = $this->Time->isToday('+1 days');
  183. $this->assertFalse($result);
  184. $result = $this->Time->isToday('+0 day');
  185. $this->assertTrue($result);
  186. $result = $this->Time->isToday('-1 day');
  187. $this->assertFalse($result);
  188. }
  189. /**
  190. * testIsFuture method
  191. *
  192. * @return void
  193. */
  194. public function testIsFuture() {
  195. $this->assertTrue($this->Time->isFuture('+1 month'));
  196. $this->assertTrue($this->Time->isFuture('+1 days'));
  197. $this->assertTrue($this->Time->isFuture('+1 minute'));
  198. $this->assertTrue($this->Time->isFuture('+1 second'));
  199. $this->assertFalse($this->Time->isFuture('-1 second'));
  200. $this->assertFalse($this->Time->isFuture('-1 day'));
  201. $this->assertFalse($this->Time->isFuture('-1 week'));
  202. $this->assertFalse($this->Time->isFuture('-1 month'));
  203. }
  204. /**
  205. * testIsPast method
  206. *
  207. * @return void
  208. */
  209. public function testIsPast() {
  210. $this->assertFalse($this->Time->isPast('+1 month'));
  211. $this->assertFalse($this->Time->isPast('+1 days'));
  212. $this->assertFalse($this->Time->isPast('+1 minute'));
  213. $this->assertFalse($this->Time->isPast('+1 second'));
  214. $this->assertTrue($this->Time->isPast('-1 second'));
  215. $this->assertTrue($this->Time->isPast('-1 day'));
  216. $this->assertTrue($this->Time->isPast('-1 week'));
  217. $this->assertTrue($this->Time->isPast('-1 month'));
  218. }
  219. /**
  220. * testIsThisWeek method
  221. *
  222. * @return void
  223. */
  224. public function testIsThisWeek() {
  225. // A map of days which goes from -1 day of week to +1 day of week
  226. $map = array(
  227. 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
  228. 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
  229. 'Sun' => array(-7, 1)
  230. );
  231. $days = $map[date('D')];
  232. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  233. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  234. }
  235. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  236. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  237. }
  238. /**
  239. * testIsThisMonth method
  240. *
  241. * @return void
  242. */
  243. public function testIsThisMonth() {
  244. $result = $this->Time->isThisMonth('+0 day');
  245. $this->assertTrue($result);
  246. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  247. $this->assertTrue($result);
  248. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  249. $this->assertFalse($result);
  250. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  251. $this->assertFalse($result);
  252. }
  253. /**
  254. * testIsThisYear method
  255. *
  256. * @return void
  257. */
  258. public function testIsThisYear() {
  259. $result = $this->Time->isThisYear('+0 day');
  260. $this->assertTrue($result);
  261. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  262. $this->assertTrue($result);
  263. }
  264. /**
  265. * testWasYesterday method
  266. *
  267. * @return void
  268. */
  269. public function testWasYesterday() {
  270. $result = $this->Time->wasYesterday('+1 day');
  271. $this->assertFalse($result);
  272. $result = $this->Time->wasYesterday('+1 days');
  273. $this->assertFalse($result);
  274. $result = $this->Time->wasYesterday('+0 day');
  275. $this->assertFalse($result);
  276. $result = $this->Time->wasYesterday('-1 day');
  277. $this->assertTrue($result);
  278. $result = $this->Time->wasYesterday('-1 days');
  279. $this->assertTrue($result);
  280. $result = $this->Time->wasYesterday('-2 days');
  281. $this->assertFalse($result);
  282. }
  283. /**
  284. * testIsTomorrow method
  285. *
  286. * @return void
  287. */
  288. public function testIsTomorrow() {
  289. $result = $this->Time->isTomorrow('+1 day');
  290. $this->assertTrue($result);
  291. $result = $this->Time->isTomorrow('+1 days');
  292. $this->assertTrue($result);
  293. $result = $this->Time->isTomorrow('+0 day');
  294. $this->assertFalse($result);
  295. $result = $this->Time->isTomorrow('-1 day');
  296. $this->assertFalse($result);
  297. }
  298. /**
  299. * testWasWithinLast method
  300. *
  301. * @return void
  302. */
  303. public function testWasWithinLast() {
  304. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  305. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  306. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  307. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  308. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  309. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  310. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  311. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  312. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  313. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  314. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  315. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  316. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  317. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  318. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  319. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  320. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  321. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  322. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  323. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  324. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  325. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  326. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  327. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  328. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  329. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  330. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  331. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  332. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  333. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  334. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  335. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  336. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  337. }
  338. /**
  339. * testWasWithinLast method
  340. *
  341. * @return void
  342. */
  343. public function testIsWithinNext() {
  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->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
  374. $this->assertFalse($this->Time->isWithinNext('1 ', '-1 hour'));
  375. $this->assertFalse($this->Time->isWithinNext('1 ', '-1 minute'));
  376. $this->assertFalse($this->Time->isWithinNext('1 ', '-23 hours -59 minutes -59 seconds'));
  377. $this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
  378. $this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
  379. }
  380. /**
  381. * test formatting dates taking in account preferred i18n locale file
  382. *
  383. * @return void
  384. */
  385. public function testFormat() {
  386. $time = strtotime('Thu Jan 14 13:59:28 2010');
  387. $result = $this->Time->format($time);
  388. $expected = '1/14/10, 1:59 PM';
  389. $this->assertTimeFormat($expected, $result);
  390. $result = $this->Time->format($time, \IntlDateFormatter::FULL);
  391. $expected = 'Thursday, January 14, 2010 at 1:59:28 PM GMT';
  392. $this->assertTimeFormat($expected, $result);
  393. $result = $this->Time->format('invalid date', null, 'Date invalid');
  394. $expected = 'Date invalid';
  395. $this->assertEquals($expected, $result);
  396. }
  397. /**
  398. * Cusotm assert to allow for variation in the version of the intl library, where
  399. * some translations contain a few extra commas.
  400. *
  401. * @param string $expected
  402. * @param string $result
  403. * @return void
  404. */
  405. public function assertTimeFormat($expected, $result) {
  406. return $this->assertEquals(str_replace(',', '', $expected), str_replace(',', '',$result));
  407. }
  408. }