TimeHelperTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. public $Time = null;
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $this->View = new View();
  37. $this->Time = new TimeHelper($this->View);
  38. Time::$defaultLocale = 'en_US';
  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('n/j/y', $timestamp),
  58. '/span'
  59. );
  60. $this->assertHtml($expected, $result);
  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('n/j/y', $timestamp),
  75. '/span'
  76. );
  77. $this->assertHtml($expected, $result);
  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->assertHtml($expected, $result);
  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->assertTimeFormat('Apr 20, 2014, 8:00 PM', $this->Time->nice($time));
  110. $result = $this->Time->nice($time, 'America/New_York');
  111. $this->assertTimeFormat('Apr 20, 2014, 4:00 PM', $result);
  112. }
  113. /**
  114. * testToUnix method
  115. *
  116. * @return void
  117. */
  118. public function testToUnix() {
  119. $this->assertEquals(1397980800, $this->Time->toUnix('2014-04-20 08:00:00'));
  120. }
  121. /**
  122. * testToAtom method
  123. *
  124. * @return void
  125. */
  126. public function testToAtom() {
  127. $dateTime = new \DateTime;
  128. $this->assertEquals($dateTime->format($dateTime::ATOM), $this->Time->toAtom($dateTime->getTimestamp()));
  129. }
  130. /**
  131. * testToRss method
  132. *
  133. * @return void
  134. */
  135. public function testToRss() {
  136. $date = '2012-08-12 12:12:45';
  137. $time = strtotime($date);
  138. $this->assertEquals(date('r', $time), $this->Time->toRss($time));
  139. $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
  140. foreach ($timezones as $timezone) {
  141. $yourTimezone = new \DateTimeZone($timezone);
  142. $yourTime = new \DateTime($date, $yourTimezone);
  143. $time = $yourTime->format('U');
  144. $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
  145. }
  146. }
  147. /**
  148. * testOfGmt method
  149. *
  150. * @return void
  151. */
  152. public function testGmt() {
  153. $this->assertEquals(1397980800, $this->Time->gmt('2014-04-20 08:00:00'));
  154. }
  155. /**
  156. * testIsToday method
  157. *
  158. * @return void
  159. */
  160. public function testIsToday() {
  161. $result = $this->Time->isToday('+1 day');
  162. $this->assertFalse($result);
  163. $result = $this->Time->isToday('+1 days');
  164. $this->assertFalse($result);
  165. $result = $this->Time->isToday('+0 day');
  166. $this->assertTrue($result);
  167. $result = $this->Time->isToday('-1 day');
  168. $this->assertFalse($result);
  169. }
  170. /**
  171. * testIsFuture method
  172. *
  173. * @return void
  174. */
  175. public function testIsFuture() {
  176. $this->assertTrue($this->Time->isFuture('+1 month'));
  177. $this->assertTrue($this->Time->isFuture('+1 days'));
  178. $this->assertTrue($this->Time->isFuture('+1 minute'));
  179. $this->assertTrue($this->Time->isFuture('+1 second'));
  180. $this->assertFalse($this->Time->isFuture('-1 second'));
  181. $this->assertFalse($this->Time->isFuture('-1 day'));
  182. $this->assertFalse($this->Time->isFuture('-1 week'));
  183. $this->assertFalse($this->Time->isFuture('-1 month'));
  184. }
  185. /**
  186. * testIsPast method
  187. *
  188. * @return void
  189. */
  190. public function testIsPast() {
  191. $this->assertFalse($this->Time->isPast('+1 month'));
  192. $this->assertFalse($this->Time->isPast('+1 days'));
  193. $this->assertFalse($this->Time->isPast('+1 minute'));
  194. $this->assertFalse($this->Time->isPast('+1 second'));
  195. $this->assertTrue($this->Time->isPast('-1 second'));
  196. $this->assertTrue($this->Time->isPast('-1 day'));
  197. $this->assertTrue($this->Time->isPast('-1 week'));
  198. $this->assertTrue($this->Time->isPast('-1 month'));
  199. }
  200. /**
  201. * testIsThisWeek method
  202. *
  203. * @return void
  204. */
  205. public function testIsThisWeek() {
  206. // A map of days which goes from -1 day of week to +1 day of week
  207. $map = array(
  208. 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
  209. 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
  210. 'Sun' => array(-7, 1)
  211. );
  212. $days = $map[date('D')];
  213. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  214. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  215. }
  216. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  217. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  218. }
  219. /**
  220. * testIsThisMonth method
  221. *
  222. * @return void
  223. */
  224. public function testIsThisMonth() {
  225. $result = $this->Time->isThisMonth('+0 day');
  226. $this->assertTrue($result);
  227. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  228. $this->assertTrue($result);
  229. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  230. $this->assertFalse($result);
  231. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  232. $this->assertFalse($result);
  233. }
  234. /**
  235. * testIsThisYear method
  236. *
  237. * @return void
  238. */
  239. public function testIsThisYear() {
  240. $result = $this->Time->isThisYear('+0 day');
  241. $this->assertTrue($result);
  242. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  243. $this->assertTrue($result);
  244. }
  245. /**
  246. * testWasYesterday method
  247. *
  248. * @return void
  249. */
  250. public function testWasYesterday() {
  251. $result = $this->Time->wasYesterday('+1 day');
  252. $this->assertFalse($result);
  253. $result = $this->Time->wasYesterday('+1 days');
  254. $this->assertFalse($result);
  255. $result = $this->Time->wasYesterday('+0 day');
  256. $this->assertFalse($result);
  257. $result = $this->Time->wasYesterday('-1 day');
  258. $this->assertTrue($result);
  259. $result = $this->Time->wasYesterday('-1 days');
  260. $this->assertTrue($result);
  261. $result = $this->Time->wasYesterday('-2 days');
  262. $this->assertFalse($result);
  263. }
  264. /**
  265. * testIsTomorrow method
  266. *
  267. * @return void
  268. */
  269. public function testIsTomorrow() {
  270. $result = $this->Time->isTomorrow('+1 day');
  271. $this->assertTrue($result);
  272. $result = $this->Time->isTomorrow('+1 days');
  273. $this->assertTrue($result);
  274. $result = $this->Time->isTomorrow('+0 day');
  275. $this->assertFalse($result);
  276. $result = $this->Time->isTomorrow('-1 day');
  277. $this->assertFalse($result);
  278. }
  279. /**
  280. * testWasWithinLast method
  281. *
  282. * @return void
  283. */
  284. public function testWasWithinLast() {
  285. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  286. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  287. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  288. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  289. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  290. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  291. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  292. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  293. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  294. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  295. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  296. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  297. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  298. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  299. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  300. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  301. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  302. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  303. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  304. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  305. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  306. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  307. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  308. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  309. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  310. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  311. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  312. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  313. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  314. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  315. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  316. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  317. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  318. }
  319. /**
  320. * testWasWithinLast method
  321. *
  322. * @return void
  323. */
  324. public function testIsWithinNext() {
  325. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  326. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
  327. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  328. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
  329. $this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
  330. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  331. $this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
  332. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  333. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
  334. $this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
  335. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
  336. $this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
  337. $this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
  338. $this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
  339. $this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
  340. $this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
  341. $this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
  342. $this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
  343. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  344. $this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
  345. $this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
  346. $this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
  347. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
  348. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  349. $this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
  350. $this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
  351. $this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
  352. $this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
  353. $this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
  354. $this->assertTrue($this->Time->isWithinNext('1 day', '+1 day'));
  355. $this->assertTrue($this->Time->isWithinNext('7 day', '+1 week'));
  356. $this->assertTrue($this->Time->isWithinNext('1 minute', '+1 second'));
  357. $this->assertTrue($this->Time->isWithinNext('1 month', '+1 month'));
  358. }
  359. /**
  360. * test formatting dates taking in account preferred i18n locale file
  361. *
  362. * @return void
  363. */
  364. public function testFormat() {
  365. $time = strtotime('Thu Jan 14 13:59:28 2010');
  366. $result = $this->Time->format($time);
  367. $expected = '1/14/10, 1:59 PM';
  368. $this->assertTimeFormat($expected, $result);
  369. $result = $this->Time->format($time, \IntlDateFormatter::FULL);
  370. $expected = 'Thursday, January 14, 2010 at 1:59:28 PM GMT';
  371. $this->assertTimeFormat($expected, $result);
  372. $result = $this->Time->format('invalid date', null, 'Date invalid');
  373. $expected = 'Date invalid';
  374. $this->assertEquals($expected, $result);
  375. }
  376. /**
  377. * Cusotm assert to allow for variation in the version of the intl library, where
  378. * some translations contain a few extra commas.
  379. *
  380. * @param string $expected
  381. * @param string $result
  382. * @return void
  383. */
  384. public function assertTimeFormat($expected, $result) {
  385. return $this->assertEquals(
  386. str_replace([',', '(', ')', ' at'], '', $expected),
  387. str_replace([',', '(', ')', ' at'], '', $result)
  388. );
  389. }
  390. }