PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/view/helpers/time.test.php

https://github.com/v2ninad/nm_cake
PHP | 782 lines | 476 code | 113 blank | 193 comment | 21 complexity | 90c0258b531effa4b9847bc973c3a7e1 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * TimeHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs.view.helpers
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::import('Helper', 'Time');
  20. App::import('Core', 'View');
  21. /**
  22. * TimeHelperTest class
  23. *
  24. * @package cake.tests.cases.libs.view.helpers
  25. */
  26. class TimeHelperTest extends CakeTestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @access public
  31. * @return void
  32. */
  33. function setUp() {
  34. $controller = null;
  35. $View = new View($controller);
  36. $this->Time = new TimeHelper($View);
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @access public
  42. * @return void
  43. */
  44. function tearDown() {
  45. unset($this->Time);
  46. }
  47. /**
  48. * testToQuarter method
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. function testToQuarter() {
  54. $result = $this->Time->toQuarter('2007-12-25');
  55. $this->assertEqual($result, 4);
  56. $result = $this->Time->toQuarter('2007-9-25');
  57. $this->assertEqual($result, 3);
  58. $result = $this->Time->toQuarter('2007-3-25');
  59. $this->assertEqual($result, 1);
  60. $result = $this->Time->toQuarter('2007-3-25', true);
  61. $this->assertEqual($result, array('2007-01-01', '2007-03-31'));
  62. $result = $this->Time->toQuarter('2007-5-25', true);
  63. $this->assertEqual($result, array('2007-04-01', '2007-06-30'));
  64. $result = $this->Time->toQuarter('2007-8-25', true);
  65. $this->assertEqual($result, array('2007-07-01', '2007-09-30'));
  66. $result = $this->Time->toQuarter('2007-12-25', true);
  67. $this->assertEqual($result, array('2007-10-01', '2007-12-31'));
  68. }
  69. /**
  70. * testTimeAgoInWords method
  71. *
  72. * @access public
  73. * @return void
  74. */
  75. function testTimeAgoInWords() {
  76. $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +3 days'), array('end' => '8 years'), true);
  77. $this->assertEqual($result, '4 months, 2 weeks, 3 days');
  78. $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +2 days'), array('end' => '8 years'), true);
  79. $this->assertEqual($result, '4 months, 2 weeks, 2 days');
  80. $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +1 day'), array('end' => '8 years'), true);
  81. $this->assertEqual($result, '4 months, 2 weeks, 1 day');
  82. $result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks +1 day'), array('end' => '8 years'), true);
  83. $this->assertEqual($result, '3 months, 2 weeks, 1 day');
  84. $result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks'), array('end' => '8 years'), true);
  85. $this->assertEqual($result, '3 months, 2 weeks');
  86. $result = $this->Time->timeAgoInWords(strtotime('+3 months +1 week +6 days'), array('end' => '8 years'), true);
  87. $this->assertEqual($result, '3 months, 1 week, 6 days');
  88. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks +1 day'), array('end' => '8 years'), true);
  89. $this->assertEqual($result, '2 months, 2 weeks, 1 day');
  90. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks'), array('end' => '8 years'), true);
  91. $this->assertEqual($result, '2 months, 2 weeks');
  92. $result = $this->Time->timeAgoInWords(strtotime('+2 months +1 week +6 days'), array('end' => '8 years'), true);
  93. $this->assertEqual($result, '2 months, 1 week, 6 days');
  94. $result = $this->Time->timeAgoInWords(strtotime('+1 month +1 week +6 days'), array('end' => '8 years'), true);
  95. $this->assertEqual($result, '1 month, 1 week, 6 days');
  96. for($i = 0; $i < 200; $i ++) {
  97. $years = mt_rand(0, 3);
  98. $months = mt_rand(0, 11);
  99. $weeks = mt_rand(0, 3);
  100. $days = mt_rand(0, 6);
  101. $hours = 0;
  102. $minutes = 0;
  103. $seconds = 0;
  104. $relative_date = '';
  105. if ($years > 0) {
  106. // years and months and days
  107. $relative_date .= ($relative_date ? ', -' : '-') . $years . ' year' . ($years > 1 ? 's' : '');
  108. $relative_date .= $months > 0 ? ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '') : '';
  109. $relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  110. $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  111. } elseif (abs($months) > 0) {
  112. // months, weeks and days
  113. $relative_date .= ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '');
  114. $relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  115. $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  116. } elseif (abs($weeks) > 0) {
  117. // weeks and days
  118. $relative_date .= ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
  119. $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  120. } elseif (abs($days) > 0) {
  121. // days and hours
  122. $relative_date .= ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '');
  123. $relative_date .= $hours > 0 ? ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
  124. } elseif (abs($hours) > 0) {
  125. // hours and minutes
  126. $relative_date .= ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '');
  127. $relative_date .= $minutes > 0 ? ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
  128. } elseif (abs($minutes) > 0) {
  129. // minutes only
  130. $relative_date .= ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
  131. } else {
  132. // seconds only
  133. $relative_date .= ($relative_date ? ', -' : '-') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
  134. }
  135. if (date('j/n/y', strtotime(str_replace(',','',$relative_date))) != '1/1/70') {
  136. $result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true);
  137. if ($relative_date == '0 seconds') {
  138. $relative_date = '0 seconds ago';
  139. }
  140. $relative_date = str_replace('-', '', $relative_date) . ' ago';
  141. $this->assertEqual($result, $relative_date);
  142. }
  143. }
  144. for ($i = 0; $i < 200; $i ++) {
  145. $years = mt_rand(0, 3);
  146. $months = mt_rand(0, 11);
  147. $weeks = mt_rand(0, 3);
  148. $days = mt_rand(0, 6);
  149. $hours = 0;
  150. $minutes = 0;
  151. $seconds = 0;
  152. $relative_date = '';
  153. if ($years > 0) {
  154. // years and months and days
  155. $relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : '');
  156. $relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : '';
  157. $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  158. $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  159. } elseif (abs($months) > 0) {
  160. // months, weeks and days
  161. $relative_date .= ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '');
  162. $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  163. $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  164. } elseif (abs($weeks) > 0) {
  165. // weeks and days
  166. $relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
  167. $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  168. } elseif (abs($days) > 0) {
  169. // days and hours
  170. $relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
  171. $relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
  172. } elseif (abs($hours) > 0) {
  173. // hours and minutes
  174. $relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
  175. $relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
  176. } elseif (abs($minutes) > 0) {
  177. // minutes only
  178. $relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
  179. } else {
  180. // seconds only
  181. $relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
  182. }
  183. if (date('j/n/y', strtotime(str_replace(',','',$relative_date))) != '1/1/70') {
  184. $result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true);
  185. if ($relative_date == '0 seconds') {
  186. $relative_date = '0 seconds ago';
  187. }
  188. $relative_date = str_replace('-', '', $relative_date) . '';
  189. $this->assertEqual($result, $relative_date);
  190. }
  191. }
  192. $result = $this->Time->timeAgoInWords(strtotime('-2 years -5 months -2 days'), array('end' => '3 years'), true);
  193. $this->assertEqual($result, '2 years, 5 months, 2 days ago');
  194. $result = $this->Time->timeAgoInWords('2007-9-25');
  195. $this->assertEqual($result, 'on 25/9/07');
  196. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
  197. $this->assertEqual($result, 'on 2007-09-25');
  198. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true);
  199. $this->assertEqual($result, 'on 2007-09-25');
  200. $result = $this->Time->timeAgoInWords(strtotime('-2 weeks -2 days'), 'Y-m-d', false);
  201. $this->assertEqual($result, '2 weeks, 2 days ago');
  202. $result = $this->Time->timeAgoInWords(strtotime('+2 weeks +2 days'), 'Y-m-d', true);
  203. $this->assertPattern('/^2 weeks, [1|2] day(s)?$/', $result);
  204. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month'));
  205. $this->assertEqual($result, 'on ' . date('j/n/y', strtotime('+2 months +2 days')));
  206. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
  207. $this->assertPattern('/2 months/', $result);
  208. $result = $this->Time->timeAgoInWords(strtotime('+2 months +12 days'), array('end' => '3 month'));
  209. $this->assertPattern('/2 months, 1 week/', $result);
  210. $result = $this->Time->timeAgoInWords(strtotime('+3 months +5 days'), array('end' => '4 month'));
  211. $this->assertEqual($result, '3 months, 5 days');
  212. $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month'));
  213. $this->assertEqual($result, '2 months, 2 days ago');
  214. $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month'));
  215. $this->assertEqual($result, '2 months, 2 days ago');
  216. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
  217. $this->assertPattern('/2 months/', $result);
  218. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
  219. $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('+2 months +2 days')));
  220. $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
  221. $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months -2 days')));
  222. $result = $this->Time->timeAgoInWords(strtotime('-13 months -5 days'), array('end' => '2 years'));
  223. $this->assertEqual($result, '1 year, 1 month, 5 days ago');
  224. $fourHours = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => -4));
  225. $result = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => 4));
  226. $this->assertEqual($fourHours, $result);
  227. $result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
  228. $expected = '2 hours ago';
  229. $this->assertEqual($expected, $result);
  230. $result = $this->Time->timeAgoInWords(strtotime('-12 minutes'));
  231. $expected = '12 minutes ago';
  232. $this->assertEqual($expected, $result);
  233. $result = $this->Time->timeAgoInWords(strtotime('-12 seconds'));
  234. $expected = '12 seconds ago';
  235. $this->assertEqual($expected, $result);
  236. $time = strtotime('-3 years -12 months');
  237. $result = $this->Time->timeAgoInWords($time);
  238. $expected = 'on ' . date('j/n/y', $time);
  239. $this->assertEqual($expected, $result);
  240. }
  241. /**
  242. * testRelative method
  243. *
  244. * @access public
  245. * @return void
  246. */
  247. function testRelative() {
  248. $result = $this->Time->relativeTime('-1 week');
  249. $this->assertEqual($result, '1 week ago');
  250. $result = $this->Time->relativeTime('+1 week');
  251. $this->assertEqual($result, '1 week');
  252. }
  253. /**
  254. * testNice method
  255. *
  256. * @access public
  257. * @return void
  258. */
  259. function testNice() {
  260. $time = time() + 2 * DAY;
  261. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  262. $time = time() - 2 * DAY;
  263. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  264. $time = time();
  265. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  266. $time = 0;
  267. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  268. $time = null;
  269. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  270. }
  271. /**
  272. * testNiceShort method
  273. *
  274. * @access public
  275. * @return void
  276. */
  277. function testNiceShort() {
  278. $time = time() + 2 * DAY;
  279. if (date('Y', $time) == date('Y')) {
  280. $this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time));
  281. } else {
  282. $this->assertEqual(date('M jSY, H:i', $time), $this->Time->niceShort($time));
  283. }
  284. $time = time();
  285. $this->assertEqual('Today, '.date('H:i', $time), $this->Time->niceShort($time));
  286. $time = time() - DAY;
  287. $this->assertEqual('Yesterday, '.date('H:i', $time), $this->Time->niceShort($time));
  288. }
  289. /**
  290. * testDaysAsSql method
  291. *
  292. * @access public
  293. * @return void
  294. */
  295. function testDaysAsSql() {
  296. $begin = time();
  297. $end = time() + DAY;
  298. $field = 'my_field';
  299. $expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')';
  300. $this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field));
  301. }
  302. /**
  303. * testDayAsSql method
  304. *
  305. * @access public
  306. * @return void
  307. */
  308. function testDayAsSql() {
  309. $time = time();
  310. $field = 'my_field';
  311. $expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')';
  312. $this->assertEqual($expected, $this->Time->dayAsSql($time, $field));
  313. }
  314. /**
  315. * testToUnix method
  316. *
  317. * @access public
  318. * @return void
  319. */
  320. function testToUnix() {
  321. $this->assertEqual(time(), $this->Time->toUnix(time()));
  322. $this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
  323. $this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
  324. $this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
  325. $this->assertEqual(false, $this->Time->toUnix(''));
  326. $this->assertEqual(false, $this->Time->toUnix(null));
  327. }
  328. /**
  329. * testToAtom method
  330. *
  331. * @access public
  332. * @return void
  333. */
  334. function testToAtom() {
  335. $this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
  336. }
  337. /**
  338. * testToRss method
  339. *
  340. * @access public
  341. * @return void
  342. */
  343. function testToRss() {
  344. $this->assertEqual(date('r'), $this->Time->toRss(time()));
  345. }
  346. /**
  347. * testFormat method
  348. *
  349. * @access public
  350. * @return void
  351. */
  352. function testFormat() {
  353. $format = 'D-M-Y';
  354. $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
  355. foreach ($arr as $val) {
  356. $this->assertEqual(date($format, $val), $this->Time->format($format, $val));
  357. }
  358. $result = $this->Time->format('Y-m-d', null, 'never');
  359. $this->assertEqual($result, 'never');
  360. }
  361. /**
  362. * testOfGmt method
  363. *
  364. * @access public
  365. * @return void
  366. */
  367. function testGmt() {
  368. $hour = 3;
  369. $min = 4;
  370. $sec = 2;
  371. $month = 5;
  372. $day = 14;
  373. $year = 2007;
  374. $time = mktime($hour, $min, $sec, $month, $day, $year);
  375. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  376. $this->assertEqual($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
  377. $hour = date('H');
  378. $min = date('i');
  379. $sec = date('s');
  380. $month = date('m');
  381. $day = date('d');
  382. $year = date('Y');
  383. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  384. $this->assertEqual($expected, $this->Time->gmt(null));
  385. }
  386. /**
  387. * testIsToday method
  388. *
  389. * @access public
  390. * @return void
  391. */
  392. function testIsToday() {
  393. $result = $this->Time->isToday('+1 day');
  394. $this->assertFalse($result);
  395. $result = $this->Time->isToday('+1 days');
  396. $this->assertFalse($result);
  397. $result = $this->Time->isToday('+0 day');
  398. $this->assertTrue($result);
  399. $result = $this->Time->isToday('-1 day');
  400. $this->assertFalse($result);
  401. }
  402. /**
  403. * testIsThisWeek method
  404. *
  405. * @access public
  406. * @return void
  407. */
  408. function testIsThisWeek() {
  409. // A map of days which goes from -1 day of week to +1 day of week
  410. $map = array(
  411. 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
  412. 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
  413. 'Sun' => array(-7, 1)
  414. );
  415. $days = $map[date('D')];
  416. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  417. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  418. }
  419. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  420. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  421. }
  422. /**
  423. * testIsThisMonth method
  424. *
  425. * @access public
  426. * @return void
  427. */
  428. function testIsThisMonth() {
  429. $result = $this->Time->isThisMonth('+0 day');
  430. $this->assertTrue($result);
  431. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  432. $this->assertTrue($result);
  433. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  434. $this->assertFalse($result);
  435. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  436. $this->assertFalse($result);
  437. }
  438. /**
  439. * testIsThisYear method
  440. *
  441. * @access public
  442. * @return void
  443. */
  444. function testIsThisYear() {
  445. $result = $this->Time->isThisYear('+0 day');
  446. $this->assertTrue($result);
  447. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  448. $this->assertTrue($result);
  449. }
  450. /**
  451. * testWasYesterday method
  452. *
  453. * @access public
  454. * @return void
  455. */
  456. function testWasYesterday() {
  457. $result = $this->Time->wasYesterday('+1 day');
  458. $this->assertFalse($result);
  459. $result = $this->Time->wasYesterday('+1 days');
  460. $this->assertFalse($result);
  461. $result = $this->Time->wasYesterday('+0 day');
  462. $this->assertFalse($result);
  463. $result = $this->Time->wasYesterday('-1 day');
  464. $this->assertTrue($result);
  465. $result = $this->Time->wasYesterday('-1 days');
  466. $this->assertTrue($result);
  467. $result = $this->Time->wasYesterday('-2 days');
  468. $this->assertFalse($result);
  469. }
  470. /**
  471. * testIsTomorrow method
  472. *
  473. * @access public
  474. * @return void
  475. */
  476. function testIsTomorrow() {
  477. $result = $this->Time->isTomorrow('+1 day');
  478. $this->assertTrue($result);
  479. $result = $this->Time->isTomorrow('+1 days');
  480. $this->assertTrue($result);
  481. $result = $this->Time->isTomorrow('+0 day');
  482. $this->assertFalse($result);
  483. $result = $this->Time->isTomorrow('-1 day');
  484. $this->assertFalse($result);
  485. }
  486. /**
  487. * testWasWithinLast method
  488. *
  489. * @access public
  490. * @return void
  491. */
  492. function testWasWithinLast() {
  493. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  494. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  495. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  496. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  497. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  498. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  499. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  500. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  501. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  502. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  503. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  504. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  505. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  506. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  507. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  508. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  509. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  510. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  511. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  512. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  513. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  514. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  515. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  516. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  517. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  518. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  519. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  520. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  521. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  522. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  523. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  524. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  525. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  526. }
  527. /**
  528. * testUserOffset method
  529. *
  530. * @access public
  531. * @return void
  532. */
  533. function testUserOffset() {
  534. if ($this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
  535. return;
  536. }
  537. $timezoneServer = new DateTimeZone(date_default_timezone_get());
  538. $timeServer = new DateTime('now', $timezoneServer);
  539. $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
  540. $expected = time();
  541. $result = $this->Time->fromString(time(), $yourTimezone);
  542. $this->assertEqual($result, $expected);
  543. }
  544. /**
  545. * test fromString()
  546. *
  547. * @access public
  548. * @return void
  549. */
  550. function testFromString() {
  551. $result = $this->Time->fromString('');
  552. $this->assertFalse($result);
  553. $result = $this->Time->fromString(0, 0);
  554. $this->assertFalse($result);
  555. $result = $this->Time->fromString('+1 hour');
  556. $expected = strtotime('+1 hour');
  557. $this->assertEqual($result, $expected);
  558. $timezone = date('Z', time());
  559. $result = $this->Time->fromString('+1 hour', $timezone);
  560. $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
  561. $this->assertEqual($result, $expected);
  562. }
  563. /**
  564. * test converting time specifiers using a time definition localfe file
  565. *
  566. * @access public
  567. * @return void
  568. */
  569. function testConvertSpecifiers() {
  570. App::build(array(
  571. 'locales' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale' . DS)
  572. ), true);
  573. Configure::write('Config.language', 'time_test');
  574. $time = strtotime('Thu Jan 14 11:43:39 2010');
  575. $result = $this->Time->convertSpecifiers('%a', $time);
  576. $expected = 'jue';
  577. $this->assertEqual($result, $expected);
  578. $result = $this->Time->convertSpecifiers('%A', $time);
  579. $expected = 'jueves';
  580. $this->assertEqual($result, $expected);
  581. $result = $this->Time->convertSpecifiers('%c', $time);
  582. $expected = 'jue %d ene %Y %H:%M:%S %Z';
  583. $this->assertEqual($result, $expected);
  584. $result = $this->Time->convertSpecifiers('%C', $time);
  585. $expected = '20';
  586. $this->assertEqual($result, $expected);
  587. $result = $this->Time->convertSpecifiers('%D', $time);
  588. $expected = '%m/%d/%y';
  589. $this->assertEqual($result, $expected);
  590. $result = $this->Time->convertSpecifiers('%b', $time);
  591. $expected = 'ene';
  592. $this->assertEqual($result, $expected);
  593. $result = $this->Time->convertSpecifiers('%h', $time);
  594. $expected = 'ene';
  595. $this->assertEqual($result, $expected);
  596. $result = $this->Time->convertSpecifiers('%B', $time);
  597. $expected = 'enero';
  598. $this->assertEqual($result, $expected);
  599. $result = $this->Time->convertSpecifiers('%n', $time);
  600. $expected = "\n";
  601. $this->assertEqual($result, $expected);
  602. $result = $this->Time->convertSpecifiers('%n', $time);
  603. $expected = "\n";
  604. $this->assertEqual($result, $expected);
  605. $result = $this->Time->convertSpecifiers('%p', $time);
  606. $expected = 'AM';
  607. $this->assertEqual($result, $expected);
  608. $result = $this->Time->convertSpecifiers('%P', $time);
  609. $expected = 'am';
  610. $this->assertEqual($result, $expected);
  611. $result = $this->Time->convertSpecifiers('%r', $time);
  612. $expected = '%I:%M:%S AM';
  613. $this->assertEqual($result, $expected);
  614. $result = $this->Time->convertSpecifiers('%R', $time);
  615. $expected = '11:43';
  616. $this->assertEqual($result, $expected);
  617. $result = $this->Time->convertSpecifiers('%t', $time);
  618. $expected = "\t";
  619. $this->assertEqual($result, $expected);
  620. $result = $this->Time->convertSpecifiers('%T', $time);
  621. $expected = '%H:%M:%S';
  622. $this->assertEqual($result, $expected);
  623. $result = $this->Time->convertSpecifiers('%u', $time);
  624. $expected = 4;
  625. $this->assertEqual($result, $expected);
  626. $result = $this->Time->convertSpecifiers('%x', $time);
  627. $expected = '%d/%m/%y';
  628. $this->assertEqual($result, $expected);
  629. $result = $this->Time->convertSpecifiers('%X', $time);
  630. $expected = '%H:%M:%S';
  631. $this->assertEqual($result, $expected);
  632. }
  633. /**
  634. * test formatting dates taking in account preferred i18n locale file
  635. *
  636. * @access public
  637. * @return void
  638. */
  639. function testI18nFormat() {
  640. App::build(array(
  641. 'locales' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale' . DS)
  642. ), true);
  643. Configure::write('Config.language', 'time_test');
  644. $time = strtotime('Thu Jan 14 13:59:28 2010');
  645. $result = $this->Time->i18nFormat($time);
  646. $expected = '14/01/10';
  647. $this->assertEqual($result, $expected);
  648. $result = $this->Time->i18nFormat($time, '%c');
  649. $expected = 'jue 14 ene 2010 13:59:28 ' . strftime('%Z', $time);
  650. $this->assertEqual($result, $expected);
  651. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  652. $expected = 'Time is 01:59:28 PM, and date is 14/01/10';
  653. $this->assertEqual($result, $expected);
  654. $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
  655. $expected = 'Date invalid';
  656. $this->assertEqual($result, $expected);
  657. }
  658. /**
  659. * test new format() syntax which inverts first and secod parameters
  660. *
  661. * @access public
  662. * @return void
  663. */
  664. function testFormatNewSyntax() {
  665. $time = time();
  666. $this->assertEqual($this->Time->format($time), $this->Time->i18nFormat($time));
  667. $this->assertEqual($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
  668. }
  669. }