PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mfriesen/kaching-php
PHP | 784 lines | 476 code | 113 blank | 195 comment | 22 complexity | 30dfe1fd84974aee0483f74559c62669 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * TimeHelperTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake
  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(str_replace(',','',$relative_date))) != '1/1/70') {
  138. $result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$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(str_replace(',','',$relative_date))) != '1/1/70') {
  186. $result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true);
  187. if ($relative_date == '0 seconds') {
  188. $relative_date = '0 seconds ago';
  189. }
  190. $relative_date = str_replace('-', '', $relative_date) . '';
  191. $this->assertEqual($result, $relative_date);
  192. }
  193. }
  194. $result = $this->Time->timeAgoInWords(strtotime('-2 years -5 months -2 days'), array('end' => '3 years'), true);
  195. $this->assertEqual($result, '2 years, 5 months, 2 days ago');
  196. $result = $this->Time->timeAgoInWords('2007-9-25');
  197. $this->assertEqual($result, 'on 25/9/07');
  198. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
  199. $this->assertEqual($result, 'on 2007-09-25');
  200. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true);
  201. $this->assertEqual($result, 'on 2007-09-25');
  202. $result = $this->Time->timeAgoInWords(strtotime('-2 weeks -2 days'), 'Y-m-d', false);
  203. $this->assertEqual($result, '2 weeks, 2 days ago');
  204. $result = $this->Time->timeAgoInWords(strtotime('+2 weeks +2 days'), 'Y-m-d', true);
  205. $this->assertPattern('/^2 weeks, [1|2] day(s)?$/', $result);
  206. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month'));
  207. $this->assertEqual($result, 'on ' . date('j/n/y', strtotime('+2 months +2 days')));
  208. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
  209. $this->assertPattern('/2 months/', $result);
  210. $result = $this->Time->timeAgoInWords(strtotime('+2 months +12 days'), array('end' => '3 month'));
  211. $this->assertPattern('/2 months, 1 week/', $result);
  212. $result = $this->Time->timeAgoInWords(strtotime('+3 months +5 days'), array('end' => '4 month'));
  213. $this->assertEqual($result, '3 months, 5 days');
  214. $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month'));
  215. $this->assertEqual($result, '2 months, 2 days ago');
  216. $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month'));
  217. $this->assertEqual($result, '2 months, 2 days ago');
  218. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
  219. $this->assertPattern('/2 months/', $result);
  220. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
  221. $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('+2 months +2 days')));
  222. $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
  223. $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months -2 days')));
  224. $result = $this->Time->timeAgoInWords(strtotime('-13 months -5 days'), array('end' => '2 years'));
  225. $this->assertEqual($result, '1 year, 1 month, 5 days ago');
  226. $fourHours = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => -4));
  227. $result = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => 4));
  228. $this->assertEqual($fourHours, $result);
  229. $result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
  230. $expected = '2 hours ago';
  231. $this->assertEqual($expected, $result);
  232. $result = $this->Time->timeAgoInWords(strtotime('-12 minutes'));
  233. $expected = '12 minutes ago';
  234. $this->assertEqual($expected, $result);
  235. $result = $this->Time->timeAgoInWords(strtotime('-12 seconds'));
  236. $expected = '12 seconds ago';
  237. $this->assertEqual($expected, $result);
  238. $time = strtotime('-3 years -12 months');
  239. $result = $this->Time->timeAgoInWords($time);
  240. $expected = 'on ' . date('j/n/y', $time);
  241. $this->assertEqual($expected, $result);
  242. }
  243. /**
  244. * testRelative method
  245. *
  246. * @access public
  247. * @return void
  248. */
  249. function testRelative() {
  250. $result = $this->Time->relativeTime('-1 week');
  251. $this->assertEqual($result, '1 week ago');
  252. $result = $this->Time->relativeTime('+1 week');
  253. $this->assertEqual($result, '1 week');
  254. }
  255. /**
  256. * testNice method
  257. *
  258. * @access public
  259. * @return void
  260. */
  261. function testNice() {
  262. $time = time() + 2 * DAY;
  263. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  264. $time = time() - 2 * DAY;
  265. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  266. $time = time();
  267. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  268. $time = 0;
  269. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  270. $time = null;
  271. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  272. }
  273. /**
  274. * testNiceShort method
  275. *
  276. * @access public
  277. * @return void
  278. */
  279. function testNiceShort() {
  280. $time = time() + 2 * DAY;
  281. if (date('Y', $time) == date('Y')) {
  282. $this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time));
  283. } else {
  284. $this->assertEqual(date('M jSY, H:i', $time), $this->Time->niceShort($time));
  285. }
  286. $time = time();
  287. $this->assertEqual('Today, '.date('H:i', $time), $this->Time->niceShort($time));
  288. $time = time() - DAY;
  289. $this->assertEqual('Yesterday, '.date('H:i', $time), $this->Time->niceShort($time));
  290. }
  291. /**
  292. * testDaysAsSql method
  293. *
  294. * @access public
  295. * @return void
  296. */
  297. function testDaysAsSql() {
  298. $begin = time();
  299. $end = time() + DAY;
  300. $field = 'my_field';
  301. $expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')';
  302. $this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field));
  303. }
  304. /**
  305. * testDayAsSql method
  306. *
  307. * @access public
  308. * @return void
  309. */
  310. function testDayAsSql() {
  311. $time = time();
  312. $field = 'my_field';
  313. $expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')';
  314. $this->assertEqual($expected, $this->Time->dayAsSql($time, $field));
  315. }
  316. /**
  317. * testToUnix method
  318. *
  319. * @access public
  320. * @return void
  321. */
  322. function testToUnix() {
  323. $this->assertEqual(time(), $this->Time->toUnix(time()));
  324. $this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
  325. $this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
  326. $this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
  327. $this->assertEqual(false, $this->Time->toUnix(''));
  328. $this->assertEqual(false, $this->Time->toUnix(null));
  329. }
  330. /**
  331. * testToAtom method
  332. *
  333. * @access public
  334. * @return void
  335. */
  336. function testToAtom() {
  337. $this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
  338. }
  339. /**
  340. * testToRss method
  341. *
  342. * @access public
  343. * @return void
  344. */
  345. function testToRss() {
  346. $this->assertEqual(date('r'), $this->Time->toRss(time()));
  347. }
  348. /**
  349. * testFormat method
  350. *
  351. * @access public
  352. * @return void
  353. */
  354. function testFormat() {
  355. $format = 'D-M-Y';
  356. $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
  357. foreach ($arr as $val) {
  358. $this->assertEqual(date($format, $val), $this->Time->format($format, $val));
  359. }
  360. $result = $this->Time->format('Y-m-d', null, 'never');
  361. $this->assertEqual($result, 'never');
  362. }
  363. /**
  364. * testOfGmt method
  365. *
  366. * @access public
  367. * @return void
  368. */
  369. function testGmt() {
  370. $hour = 3;
  371. $min = 4;
  372. $sec = 2;
  373. $month = 5;
  374. $day = 14;
  375. $year = 2007;
  376. $time = mktime($hour, $min, $sec, $month, $day, $year);
  377. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  378. $this->assertEqual($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
  379. $hour = date('H');
  380. $min = date('i');
  381. $sec = date('s');
  382. $month = date('m');
  383. $day = date('d');
  384. $year = date('Y');
  385. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  386. $this->assertEqual($expected, $this->Time->gmt(null));
  387. }
  388. /**
  389. * testIsToday method
  390. *
  391. * @access public
  392. * @return void
  393. */
  394. function testIsToday() {
  395. $result = $this->Time->isToday('+1 day');
  396. $this->assertFalse($result);
  397. $result = $this->Time->isToday('+1 days');
  398. $this->assertFalse($result);
  399. $result = $this->Time->isToday('+0 day');
  400. $this->assertTrue($result);
  401. $result = $this->Time->isToday('-1 day');
  402. $this->assertFalse($result);
  403. }
  404. /**
  405. * testIsThisWeek method
  406. *
  407. * @access public
  408. * @return void
  409. */
  410. function testIsThisWeek() {
  411. // A map of days which goes from -1 day of week to +1 day of week
  412. $map = array(
  413. 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
  414. 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
  415. 'Sun' => array(-7, 1)
  416. );
  417. $days = $map[date('D')];
  418. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  419. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  420. }
  421. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  422. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  423. }
  424. /**
  425. * testIsThisMonth method
  426. *
  427. * @access public
  428. * @return void
  429. */
  430. function testIsThisMonth() {
  431. $result = $this->Time->isThisMonth('+0 day');
  432. $this->assertTrue($result);
  433. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  434. $this->assertTrue($result);
  435. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  436. $this->assertFalse($result);
  437. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  438. $this->assertFalse($result);
  439. }
  440. /**
  441. * testIsThisYear method
  442. *
  443. * @access public
  444. * @return void
  445. */
  446. function testIsThisYear() {
  447. $result = $this->Time->isThisYear('+0 day');
  448. $this->assertTrue($result);
  449. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  450. $this->assertTrue($result);
  451. }
  452. /**
  453. * testWasYesterday method
  454. *
  455. * @access public
  456. * @return void
  457. */
  458. function testWasYesterday() {
  459. $result = $this->Time->wasYesterday('+1 day');
  460. $this->assertFalse($result);
  461. $result = $this->Time->wasYesterday('+1 days');
  462. $this->assertFalse($result);
  463. $result = $this->Time->wasYesterday('+0 day');
  464. $this->assertFalse($result);
  465. $result = $this->Time->wasYesterday('-1 day');
  466. $this->assertTrue($result);
  467. $result = $this->Time->wasYesterday('-1 days');
  468. $this->assertTrue($result);
  469. $result = $this->Time->wasYesterday('-2 days');
  470. $this->assertFalse($result);
  471. }
  472. /**
  473. * testIsTomorrow method
  474. *
  475. * @access public
  476. * @return void
  477. */
  478. function testIsTomorrow() {
  479. $result = $this->Time->isTomorrow('+1 day');
  480. $this->assertTrue($result);
  481. $result = $this->Time->isTomorrow('+1 days');
  482. $this->assertTrue($result);
  483. $result = $this->Time->isTomorrow('+0 day');
  484. $this->assertFalse($result);
  485. $result = $this->Time->isTomorrow('-1 day');
  486. $this->assertFalse($result);
  487. }
  488. /**
  489. * testWasWithinLast method
  490. *
  491. * @access public
  492. * @return void
  493. */
  494. function testWasWithinLast() {
  495. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  496. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  497. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  498. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  499. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  500. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  501. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  502. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  503. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  504. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  505. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  506. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  507. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  508. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  509. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  510. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  511. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  512. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  513. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  514. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  515. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  516. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  517. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  518. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  519. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  520. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  521. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  522. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  523. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  524. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  525. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  526. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  527. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  528. }
  529. /**
  530. * testUserOffset method
  531. *
  532. * @access public
  533. * @return void
  534. */
  535. function testUserOffset() {
  536. if ($this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
  537. return;
  538. }
  539. $timezoneServer = new DateTimeZone(date_default_timezone_get());
  540. $timeServer = new DateTime('now', $timezoneServer);
  541. $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
  542. $expected = time();
  543. $result = $this->Time->fromString(time(), $yourTimezone);
  544. $this->assertEqual($result, $expected);
  545. }
  546. /**
  547. * test fromString()
  548. *
  549. * @access public
  550. * @return void
  551. */
  552. function testFromString() {
  553. $result = $this->Time->fromString('');
  554. $this->assertFalse($result);
  555. $result = $this->Time->fromString(0, 0);
  556. $this->assertFalse($result);
  557. $result = $this->Time->fromString('+1 hour');
  558. $expected = strtotime('+1 hour');
  559. $this->assertEqual($result, $expected);
  560. $timezone = date('Z', time());
  561. $result = $this->Time->fromString('+1 hour', $timezone);
  562. $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
  563. $this->assertEqual($result, $expected);
  564. }
  565. /**
  566. * test converting time specifiers using a time definition localfe file
  567. *
  568. * @access public
  569. * @return void
  570. */
  571. function testConvertSpecifiers() {
  572. App::build(array(
  573. 'locales' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale' . DS)
  574. ), true);
  575. Configure::write('Config.language', 'time_test');
  576. $time = strtotime('Thu Jan 14 11:43:39 2010');
  577. $result = $this->Time->convertSpecifiers('%a', $time);
  578. $expected = 'jue';
  579. $this->assertEqual($result, $expected);
  580. $result = $this->Time->convertSpecifiers('%A', $time);
  581. $expected = 'jueves';
  582. $this->assertEqual($result, $expected);
  583. $result = $this->Time->convertSpecifiers('%c', $time);
  584. $expected = 'jue %d ene %Y %H:%M:%S %Z';
  585. $this->assertEqual($result, $expected);
  586. $result = $this->Time->convertSpecifiers('%C', $time);
  587. $expected = '20';
  588. $this->assertEqual($result, $expected);
  589. $result = $this->Time->convertSpecifiers('%D', $time);
  590. $expected = '%m/%d/%y';
  591. $this->assertEqual($result, $expected);
  592. $result = $this->Time->convertSpecifiers('%b', $time);
  593. $expected = 'ene';
  594. $this->assertEqual($result, $expected);
  595. $result = $this->Time->convertSpecifiers('%h', $time);
  596. $expected = 'ene';
  597. $this->assertEqual($result, $expected);
  598. $result = $this->Time->convertSpecifiers('%B', $time);
  599. $expected = 'enero';
  600. $this->assertEqual($result, $expected);
  601. $result = $this->Time->convertSpecifiers('%n', $time);
  602. $expected = "\n";
  603. $this->assertEqual($result, $expected);
  604. $result = $this->Time->convertSpecifiers('%n', $time);
  605. $expected = "\n";
  606. $this->assertEqual($result, $expected);
  607. $result = $this->Time->convertSpecifiers('%p', $time);
  608. $expected = 'AM';
  609. $this->assertEqual($result, $expected);
  610. $result = $this->Time->convertSpecifiers('%P', $time);
  611. $expected = 'am';
  612. $this->assertEqual($result, $expected);
  613. $result = $this->Time->convertSpecifiers('%r', $time);
  614. $expected = '%I:%M:%S AM';
  615. $this->assertEqual($result, $expected);
  616. $result = $this->Time->convertSpecifiers('%R', $time);
  617. $expected = '11:43';
  618. $this->assertEqual($result, $expected);
  619. $result = $this->Time->convertSpecifiers('%t', $time);
  620. $expected = "\t";
  621. $this->assertEqual($result, $expected);
  622. $result = $this->Time->convertSpecifiers('%T', $time);
  623. $expected = '%H:%M:%S';
  624. $this->assertEqual($result, $expected);
  625. $result = $this->Time->convertSpecifiers('%u', $time);
  626. $expected = 4;
  627. $this->assertEqual($result, $expected);
  628. $result = $this->Time->convertSpecifiers('%x', $time);
  629. $expected = '%d/%m/%y';
  630. $this->assertEqual($result, $expected);
  631. $result = $this->Time->convertSpecifiers('%X', $time);
  632. $expected = '%H:%M:%S';
  633. $this->assertEqual($result, $expected);
  634. }
  635. /**
  636. * test formatting dates taking in account preferred i18n locale file
  637. *
  638. * @access public
  639. * @return void
  640. */
  641. function testI18nFormat() {
  642. App::build(array(
  643. 'locales' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale' . DS)
  644. ), true);
  645. Configure::write('Config.language', 'time_test');
  646. $time = strtotime('Thu Jan 14 13:59:28 2010');
  647. $result = $this->Time->i18nFormat($time);
  648. $expected = '14/01/10';
  649. $this->assertEqual($result, $expected);
  650. $result = $this->Time->i18nFormat($time, '%c');
  651. $expected = 'jue 14 ene 2010 13:59:28 ' . strftime('%Z', $time);
  652. $this->assertEqual($result, $expected);
  653. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  654. $expected = 'Time is 01:59:28 PM, and date is 14/01/10';
  655. $this->assertEqual($result, $expected);
  656. $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
  657. $expected = 'Date invalid';
  658. $this->assertEqual($result, $expected);
  659. }
  660. /**
  661. * test new format() syntax which inverts first and secod parameters
  662. *
  663. * @access public
  664. * @return void
  665. */
  666. function testFormatNewSyntax() {
  667. $time = time();
  668. $this->assertEqual($this->Time->format($time), $this->Time->i18nFormat($time));
  669. $this->assertEqual($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
  670. }
  671. }