PageRenderTime 46ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

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