PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

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