PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/MrRio/wildflower
PHP | 658 lines | 413 code | 61 blank | 184 comment | 28 complexity | dfa07c976c6750e2495c24a91b734688 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Short description for 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.tests
  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. * Time Test Case
  33. *
  34. * @package cake.tests
  35. * @subpackage cake.tests.cases.libs.view.helpers
  36. */
  37. class TimeTest 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. * 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($relative_date)) != '1/1/70') {
  136. $result = $this->Time->timeAgoInWords(strtotime($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($relative_date)) != '1/1/70') {
  184. // echo $relative_date."<br />";
  185. $result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true);
  186. if ($relative_date == '0 seconds') {
  187. $relative_date = '0 seconds ago';
  188. }
  189. $relative_date = str_replace('-', '', $relative_date) . '';
  190. $this->assertEqual($result, $relative_date);
  191. }
  192. }
  193. $result = $this->Time->timeAgoInWords(strtotime('-2 years, -5 months, -2 days'), array('end' => '3 years'), true);
  194. $this->assertEqual($result, '2 years, 5 months, 2 days ago');
  195. $result = $this->Time->timeAgoInWords('2007-9-25');
  196. $this->assertEqual($result, 'on 25/9/07');
  197. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
  198. $this->assertEqual($result, 'on 2007-09-25');
  199. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true);
  200. $this->assertEqual($result, 'on 2007-09-25');
  201. $result = $this->Time->timeAgoInWords(strtotime('-2 weeks, -2 days'), 'Y-m-d', false);
  202. $this->assertEqual($result, '2 weeks, 2 days ago');
  203. $result = $this->Time->timeAgoInWords(strtotime('2 weeks, 2 days'), 'Y-m-d', true);
  204. $this->assertPattern('/^2 weeks, [1|2] day(s)?$/', $result);
  205. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '1 month'));
  206. $this->assertEqual($result, 'on ' . date('j/n/y', strtotime('2 months, 2 days')));
  207. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '3 month'));
  208. $this->assertPattern('/2 months/', $result);
  209. $result = $this->Time->timeAgoInWords(strtotime('2 months, 12 days'), array('end' => '3 month'));
  210. $this->assertPattern('/2 months, 1 week/', $result);
  211. $result = $this->Time->timeAgoInWords(strtotime('3 months, 5 days'), array('end' => '4 month'));
  212. $this->assertEqual($result, '3 months, 5 days');
  213. $result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '3 month'));
  214. $this->assertEqual($result, '2 months, 2 days ago');
  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->assertPattern('/2 months/', $result);
  219. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
  220. $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('2 months, 2 days')));
  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('-13 months, -5 days'), array('end' => '2 years'));
  224. $this->assertEqual($result, '1 year, 1 month, 5 days ago');
  225. $fourHours = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => -4));
  226. $result = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => 4));
  227. $this->assertEqual($fourHours, $result);
  228. }
  229. /**
  230. * testRelative method
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. function testRelative() {
  236. $result = $this->Time->relativeTime('-1 week');
  237. $this->assertEqual($result, '1 week ago');
  238. $result = $this->Time->relativeTime('+1 week');
  239. $this->assertEqual($result, '1 week');
  240. }
  241. /**
  242. * testNice method
  243. *
  244. * @access public
  245. * @return void
  246. */
  247. function testNice() {
  248. $time = time() + 2 * DAY;
  249. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  250. $time = time() - 2 * DAY;
  251. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  252. $time = time();
  253. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  254. $time = 0;
  255. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  256. $time = null;
  257. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  258. }
  259. /**
  260. * testNiceShort method
  261. *
  262. * @access public
  263. * @return void
  264. */
  265. function testNiceShort() {
  266. $time = time() + 2 * DAY;
  267. if (date('Y', $time) == date('Y')) {
  268. $this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time));
  269. } else {
  270. $this->assertEqual(date('M jSY, H:i', $time), $this->Time->niceShort($time));
  271. }
  272. $time = time();
  273. $this->assertEqual('Today, '.date('H:i', $time), $this->Time->niceShort($time));
  274. $time = time() - DAY;
  275. $this->assertEqual('Yesterday, '.date('H:i', $time), $this->Time->niceShort($time));
  276. }
  277. /**
  278. * testDaysAsSql method
  279. *
  280. * @access public
  281. * @return void
  282. */
  283. function testDaysAsSql() {
  284. $begin = time();
  285. $end = time() + DAY;
  286. $field = 'my_field';
  287. $expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')';
  288. $this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field));
  289. }
  290. /**
  291. * testDayAsSql method
  292. *
  293. * @access public
  294. * @return void
  295. */
  296. function testDayAsSql() {
  297. $time = time();
  298. $field = 'my_field';
  299. $expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')';
  300. $this->assertEqual($expected, $this->Time->dayAsSql($time, $field));
  301. }
  302. /**
  303. * testToUnix method
  304. *
  305. * @access public
  306. * @return void
  307. */
  308. function testToUnix() {
  309. $this->assertEqual(time(), $this->Time->toUnix(time()));
  310. $this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
  311. $this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
  312. $this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
  313. $this->assertEqual(false, $this->Time->toUnix(''));
  314. $this->assertEqual(false, $this->Time->toUnix(null));
  315. }
  316. /**
  317. * testToAtom method
  318. *
  319. * @access public
  320. * @return void
  321. */
  322. function testToAtom() {
  323. $this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
  324. }
  325. /**
  326. * testToRss method
  327. *
  328. * @access public
  329. * @return void
  330. */
  331. function testToRss() {
  332. $this->assertEqual(date('r'), $this->Time->toRss(time()));
  333. }
  334. /**
  335. * testFormat method
  336. *
  337. * @access public
  338. * @return void
  339. */
  340. function testFormat() {
  341. $format = 'D-M-Y';
  342. $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
  343. foreach ($arr as $val) {
  344. $this->assertEqual(date($format, $val), $this->Time->format($format, $val));
  345. }
  346. $result = $this->Time->format('Y-m-d', null, 'never');
  347. $this->assertEqual($result, 'never');
  348. }
  349. /**
  350. * testOfGmt method
  351. *
  352. * @access public
  353. * @return void
  354. */
  355. function testGmt() {
  356. $hour = 3;
  357. $min = 4;
  358. $sec = 2;
  359. $month = 5;
  360. $day = 14;
  361. $year = 2007;
  362. $time = mktime($hour, $min, $sec, $month, $day, $year);
  363. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  364. $this->assertEqual($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
  365. $hour = date('H');
  366. $min = date('i');
  367. $sec = date('s');
  368. $month = date('m');
  369. $day = date('d');
  370. $year = date('Y');
  371. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  372. $this->assertEqual($expected, $this->Time->gmt(null));
  373. }
  374. /**
  375. * testIsToday method
  376. *
  377. * @access public
  378. * @return void
  379. */
  380. function testIsToday() {
  381. $result = $this->Time->isToday('+1 day');
  382. $this->assertFalse($result);
  383. $result = $this->Time->isToday('+1 days');
  384. $this->assertFalse($result);
  385. $result = $this->Time->isToday('+0 day');
  386. $this->assertTrue($result);
  387. $result = $this->Time->isToday('-1 day');
  388. $this->assertFalse($result);
  389. }
  390. /**
  391. * testIsThisWeek method
  392. *
  393. * @access public
  394. * @return void
  395. */
  396. function testIsThisWeek() {
  397. switch (date('D')) {
  398. case 'Mon' :
  399. for ($i = 0; $i < 6; $i++) {
  400. $this->assertTrue($this->Time->isThisWeek("+$i days"));
  401. }
  402. $this->assertFalse($this->Time->isThisWeek("+7 days"));
  403. $this->assertFalse($this->Time->isThisWeek("-1 days"));
  404. break;
  405. case 'Tue' :
  406. for ($i = -1; $i < 5; $i++) {
  407. $this->assertTrue($this->Time->isThisWeek("+$i days"));
  408. }
  409. $this->assertFalse($this->Time->isThisWeek("+6 days"));
  410. $this->assertFalse($this->Time->isThisWeek("-2 days"));
  411. break;
  412. case 'Wed' :
  413. for ($i = -2; $i < 5; $i++) {
  414. $this->assertTrue($this->Time->isThisWeek("+$i days"));
  415. }
  416. $this->assertFalse($this->Time->isThisWeek("+5 days"));
  417. $this->assertFalse($this->Time->isThisWeek("-3 days"));
  418. break;
  419. case 'Thu' :
  420. for ($i = -3; $i < 4; $i++) {
  421. $this->assertTrue($this->Time->isThisWeek("+$i days"));
  422. }
  423. $this->assertFalse($this->Time->isThisWeek("+4 days"));
  424. $this->assertFalse($this->Time->isThisWeek("-4 days"));
  425. break;
  426. case 'Fri' :
  427. for ($i = -4; $i < 3; $i++) {
  428. $this->assertTrue($this->Time->isThisWeek("+$i days"));
  429. }
  430. $this->assertFalse($this->Time->isThisWeek("+3 days"));
  431. $this->assertFalse($this->Time->isThisWeek("-5 days"));
  432. break;
  433. case 'Sat' :
  434. for ($i = -5; $i < 2; $i++) {
  435. $this->assertTrue($this->Time->isThisWeek("+$i days"));
  436. }
  437. $this->assertFalse($this->Time->isThisWeek("+2 days"));
  438. $this->assertFalse($this->Time->isThisWeek("-6 days"));
  439. break;
  440. case 'Sun' :
  441. for ($i = -6; $i < 1; $i++) {
  442. $this->assertTrue($this->Time->isThisWeek("+$i days"));
  443. }
  444. $this->assertFalse($this->Time->isThisWeek("+1 days"));
  445. $this->assertFalse($this->Time->isThisWeek("-7 days"));
  446. break;
  447. }
  448. }
  449. /**
  450. * testIsThisMonth method
  451. *
  452. * @access public
  453. * @return void
  454. */
  455. function testIsThisMonth() {
  456. $result = $this->Time->isThisMonth('+0 day');
  457. $this->assertTrue($result);
  458. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  459. $this->assertTrue($result);
  460. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  461. $this->assertFalse($result);
  462. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  463. $this->assertFalse($result);
  464. }
  465. /**
  466. * testIsThisYear method
  467. *
  468. * @access public
  469. * @return void
  470. */
  471. function testIsThisYear() {
  472. $result = $this->Time->isThisYear('+0 day');
  473. $this->assertTrue($result);
  474. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  475. $this->assertTrue($result);
  476. }
  477. /**
  478. * testWasYesterday method
  479. *
  480. * @access public
  481. * @return void
  482. */
  483. function testWasYesterday() {
  484. $result = $this->Time->wasYesterday('+1 day');
  485. $this->assertFalse($result);
  486. $result = $this->Time->wasYesterday('+1 days');
  487. $this->assertFalse($result);
  488. $result = $this->Time->wasYesterday('+0 day');
  489. $this->assertFalse($result);
  490. $result = $this->Time->wasYesterday('-1 day');
  491. $this->assertTrue($result);
  492. $result = $this->Time->wasYesterday('-1 days');
  493. $this->assertTrue($result);
  494. $result = $this->Time->wasYesterday('-2 days');
  495. $this->assertFalse($result);
  496. }
  497. /**
  498. * testIsTomorrow method
  499. *
  500. * @access public
  501. * @return void
  502. */
  503. function testIsTomorrow() {
  504. $result = $this->Time->isTomorrow('+1 day');
  505. $this->assertTrue($result);
  506. $result = $this->Time->isTomorrow('+1 days');
  507. $this->assertTrue($result);
  508. $result = $this->Time->isTomorrow('+0 day');
  509. $this->assertFalse($result);
  510. $result = $this->Time->isTomorrow('-1 day');
  511. $this->assertFalse($result);
  512. }
  513. /**
  514. * testWasWithinLast method
  515. *
  516. * @access public
  517. * @return void
  518. */
  519. function testWasWithinLast() {
  520. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  521. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  522. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  523. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  524. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  525. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  526. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  527. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  528. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  529. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  530. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  531. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  532. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  533. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  534. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  535. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  536. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  537. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  538. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  539. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  540. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  541. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  542. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  543. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  544. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  545. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  546. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  547. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  548. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  549. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  550. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  551. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  552. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  553. }
  554. /**
  555. * testUserOffset method
  556. *
  557. * @access public
  558. * @return void
  559. */
  560. function testUserOffset() {
  561. $timezoneServer = new DateTimeZone(date_default_timezone_get());
  562. $timeServer = new DateTime('now', $timezoneServer);
  563. $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
  564. $expected = time();
  565. $result = $this->Time->fromString(time(), $yourTimezone);
  566. $this->assertEqual($result, $expected);
  567. }
  568. /**
  569. * test fromString()
  570. *
  571. * @access public
  572. * @return void
  573. */
  574. function testFromString() {
  575. $result = $this->Time->fromString('');
  576. $this->assertFalse($result);
  577. $result = $this->Time->fromString(0, 0);
  578. $this->assertFalse($result);
  579. $result = $this->Time->fromString('+1 hour');
  580. $expected = strtotime('+1 hour');
  581. $this->assertEqual($result, $expected);
  582. $timezone = date('Z', time());
  583. $result = $this->Time->fromString('+1 hour', $timezone);
  584. $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
  585. $this->assertEqual($result, $expected);
  586. }
  587. /**
  588. * tearDown method
  589. *
  590. * @access public
  591. * @return void
  592. */
  593. function tearDown() {
  594. unset($this->Time);
  595. }
  596. }
  597. ?>