PageRenderTime 30ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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