PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/View/Helper/TimeHelperTest.php

https://github.com/Bancha/cakephp
PHP | 801 lines | 510 code | 111 blank | 180 comment | 22 complexity | b34a1f396f75e46d6695c383137b422f MD5 | raw file
  1. <?php
  2. /**
  3. * TimeHelperTest file
  4. *
  5. * PHP 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 MIT 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.tests.cases.libs.view.helpers
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('TimeHelper', 'View/Helper');
  20. App::uses('View', 'View');
  21. /**
  22. * TimeHelperTest class
  23. *
  24. * @package cake.tests.cases.libs.view.helpers
  25. */
  26. class TimeHelperTest extends CakeTestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @access public
  31. * @return void
  32. */
  33. function setUp() {
  34. $controller = null;
  35. $View = new View($controller);
  36. $this->Time = new TimeHelper($View);
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @access public
  42. * @return void
  43. */
  44. function tearDown() {
  45. unset($this->Time);
  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('-1 week');
  77. $this->assertEqual($result, '1 week ago');
  78. $result = $this->Time->timeAgoInWords('+1 week');
  79. $this->assertEqual($result, '1 week');
  80. $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +3 days'), array('end' => '8 years'), true);
  81. $this->assertEqual($result, '4 months, 2 weeks, 3 days');
  82. $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +2 days'), array('end' => '8 years'), true);
  83. $this->assertEqual($result, '4 months, 2 weeks, 2 days');
  84. $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +1 day'), array('end' => '8 years'), true);
  85. $this->assertEqual($result, '4 months, 2 weeks, 1 day');
  86. $result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks +1 day'), array('end' => '8 years'), true);
  87. $this->assertEqual($result, '3 months, 2 weeks, 1 day');
  88. $result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks'), array('end' => '8 years'), true);
  89. $this->assertEqual($result, '3 months, 2 weeks');
  90. $result = $this->Time->timeAgoInWords(strtotime('+3 months +1 week +6 days'), array('end' => '8 years'), true);
  91. $this->assertEqual($result, '3 months, 1 week, 6 days');
  92. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks +1 day'), array('end' => '8 years'), true);
  93. $this->assertEqual($result, '2 months, 2 weeks, 1 day');
  94. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks'), array('end' => '8 years'), true);
  95. $this->assertEqual($result, '2 months, 2 weeks');
  96. $result = $this->Time->timeAgoInWords(strtotime('+2 months +1 week +6 days'), array('end' => '8 years'), true);
  97. $this->assertEqual($result, '2 months, 1 week, 6 days');
  98. $result = $this->Time->timeAgoInWords(strtotime('+1 month +1 week +6 days'), array('end' => '8 years'), true);
  99. $this->assertEqual($result, '1 month, 1 week, 6 days');
  100. for($i = 0; $i < 200; $i ++) {
  101. $years = mt_rand(0, 3);
  102. $months = mt_rand(0, 11);
  103. $weeks = mt_rand(0, 3);
  104. $days = mt_rand(0, 6);
  105. $hours = 0;
  106. $minutes = 0;
  107. $seconds = 0;
  108. $relative_date = '';
  109. if ($years > 0) {
  110. // years and months and days
  111. $relative_date .= ($relative_date ? ', -' : '-') . $years . ' year' . ($years > 1 ? 's' : '');
  112. $relative_date .= $months > 0 ? ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '') : '';
  113. $relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  114. $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  115. } elseif (abs($months) > 0) {
  116. // months, weeks and days
  117. $relative_date .= ($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($weeks) > 0) {
  121. // weeks and days
  122. $relative_date .= ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
  123. $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  124. } elseif (abs($days) > 0) {
  125. // days and hours
  126. $relative_date .= ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '');
  127. $relative_date .= $hours > 0 ? ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
  128. } elseif (abs($hours) > 0) {
  129. // hours and minutes
  130. $relative_date .= ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '');
  131. $relative_date .= $minutes > 0 ? ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
  132. } elseif (abs($minutes) > 0) {
  133. // minutes only
  134. $relative_date .= ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
  135. } else {
  136. // seconds only
  137. $relative_date .= ($relative_date ? ', -' : '-') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
  138. }
  139. if (date('j/n/y', strtotime(str_replace(',','',$relative_date))) != '1/1/70') {
  140. $result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true);
  141. if ($relative_date == '0 seconds') {
  142. $relative_date = '0 seconds ago';
  143. }
  144. $relative_date = str_replace('-', '', $relative_date) . ' ago';
  145. $this->assertEqual($result, $relative_date);
  146. }
  147. }
  148. for ($i = 0; $i < 200; $i ++) {
  149. $years = mt_rand(0, 3);
  150. $months = mt_rand(0, 11);
  151. $weeks = mt_rand(0, 3);
  152. $days = mt_rand(0, 6);
  153. $hours = 0;
  154. $minutes = 0;
  155. $seconds = 0;
  156. $relative_date = '';
  157. if ($years > 0) {
  158. // years and months and days
  159. $relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : '');
  160. $relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : '';
  161. $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  162. $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  163. } elseif (abs($months) > 0) {
  164. // months, weeks and days
  165. $relative_date .= ($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($weeks) > 0) {
  169. // weeks and days
  170. $relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
  171. $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  172. } elseif (abs($days) > 0) {
  173. // days and hours
  174. $relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
  175. $relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
  176. } elseif (abs($hours) > 0) {
  177. // hours and minutes
  178. $relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
  179. $relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
  180. } elseif (abs($minutes) > 0) {
  181. // minutes only
  182. $relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
  183. } else {
  184. // seconds only
  185. $relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
  186. }
  187. if (date('j/n/y', strtotime(str_replace(',','',$relative_date))) != '1/1/70') {
  188. $result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true);
  189. if ($relative_date == '0 seconds') {
  190. $relative_date = '0 seconds ago';
  191. }
  192. $relative_date = str_replace('-', '', $relative_date) . '';
  193. $this->assertEqual($result, $relative_date);
  194. }
  195. }
  196. $result = $this->Time->timeAgoInWords(strtotime('-2 years -5 months -2 days'), array('end' => '3 years'), true);
  197. $this->assertEqual($result, '2 years, 5 months, 2 days ago');
  198. $result = $this->Time->timeAgoInWords('2007-9-25');
  199. $this->assertEqual($result, 'on 25/9/07');
  200. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
  201. $this->assertEqual($result, 'on 2007-09-25');
  202. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true);
  203. $this->assertEqual($result, 'on 2007-09-25');
  204. $result = $this->Time->timeAgoInWords(strtotime('-2 weeks -2 days'), 'Y-m-d', false);
  205. $this->assertEqual($result, '2 weeks, 2 days ago');
  206. $result = $this->Time->timeAgoInWords(strtotime('+2 weeks +2 days'), 'Y-m-d', true);
  207. $this->assertPattern('/^2 weeks, [1|2] day(s)?$/', $result);
  208. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month'));
  209. $this->assertEqual($result, 'on ' . date('j/n/y', strtotime('+2 months +2 days')));
  210. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
  211. $this->assertPattern('/2 months/', $result);
  212. $result = $this->Time->timeAgoInWords(strtotime('+2 months +12 days'), array('end' => '3 month'));
  213. $this->assertPattern('/2 months, 1 week/', $result);
  214. $result = $this->Time->timeAgoInWords(strtotime('+3 months +5 days'), array('end' => '4 month'));
  215. $this->assertEqual($result, '3 months, 5 days');
  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->assertEqual($result, '2 months, 2 days ago');
  220. $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
  221. $this->assertPattern('/2 months/', $result);
  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('-2 months -2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
  225. $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months -2 days')));
  226. $result = $this->Time->timeAgoInWords(strtotime('-13 months -5 days'), array('end' => '2 years'));
  227. $this->assertEqual($result, '1 year, 1 month, 5 days ago');
  228. $fourHours = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => -4));
  229. $result = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => 4));
  230. $this->assertEqual($fourHours, $result);
  231. $result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
  232. $expected = '2 hours ago';
  233. $this->assertEqual($expected, $result);
  234. $result = $this->Time->timeAgoInWords(strtotime('-12 minutes'));
  235. $expected = '12 minutes ago';
  236. $this->assertEqual($expected, $result);
  237. $result = $this->Time->timeAgoInWords(strtotime('-12 seconds'));
  238. $expected = '12 seconds ago';
  239. $this->assertEqual($expected, $result);
  240. $time = strtotime('-3 years -12 months');
  241. $result = $this->Time->timeAgoInWords($time);
  242. $expected = 'on ' . date('j/n/y', $time);
  243. $this->assertEqual($expected, $result);
  244. }
  245. /**
  246. * testNice method
  247. *
  248. * @access public
  249. * @return void
  250. */
  251. function testNice() {
  252. $time = time() + 2 * DAY;
  253. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  254. $time = time() - 2 * DAY;
  255. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  256. $time = time();
  257. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  258. $time = 0;
  259. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  260. $time = null;
  261. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  262. $time = time();
  263. $this->assertEqual(date('D', $time), $this->Time->nice($time, null, '%a'));
  264. $this->assertEqual(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y'));
  265. $this->Time->niceFormat = '%Y-%d-%m';
  266. $this->assertEqual(date('Y-d-m', $time), $this->Time->nice($time));
  267. }
  268. /**
  269. * testNiceShort method
  270. *
  271. * @access public
  272. * @return void
  273. */
  274. function testNiceShort() {
  275. $time = time() + 2 * DAY;
  276. if (date('Y', $time) == date('Y')) {
  277. $this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time));
  278. } else {
  279. $this->assertEqual(date('M jS Y, H:i', $time), $this->Time->niceShort($time));
  280. }
  281. $time = time();
  282. $this->assertEqual('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
  283. $time = time() - DAY;
  284. $this->assertEqual('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
  285. }
  286. /**
  287. * testDaysAsSql method
  288. *
  289. * @access public
  290. * @return void
  291. */
  292. function testDaysAsSql() {
  293. $begin = time();
  294. $end = time() + DAY;
  295. $field = 'my_field';
  296. $expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')';
  297. $this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field));
  298. }
  299. /**
  300. * testDayAsSql method
  301. *
  302. * @access public
  303. * @return void
  304. */
  305. function testDayAsSql() {
  306. $time = time();
  307. $field = 'my_field';
  308. $expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')';
  309. $this->assertEqual($expected, $this->Time->dayAsSql($time, $field));
  310. }
  311. /**
  312. * testToUnix method
  313. *
  314. * @access public
  315. * @return void
  316. */
  317. function testToUnix() {
  318. $this->assertEqual(time(), $this->Time->toUnix(time()));
  319. $this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
  320. $this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
  321. $this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
  322. $this->assertEqual(false, $this->Time->toUnix(''));
  323. $this->assertEqual(false, $this->Time->toUnix(null));
  324. }
  325. /**
  326. * testToAtom method
  327. *
  328. * @access public
  329. * @return void
  330. */
  331. function testToAtom() {
  332. $this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
  333. }
  334. /**
  335. * testToRss method
  336. *
  337. * @access public
  338. * @return void
  339. */
  340. function testToRss() {
  341. $this->assertEqual(date('r'), $this->Time->toRss(time()));
  342. }
  343. /**
  344. * testFormat method
  345. *
  346. * @access public
  347. * @return void
  348. */
  349. function testFormat() {
  350. $format = 'D-M-Y';
  351. $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
  352. foreach ($arr as $val) {
  353. $this->assertEqual(date($format, $val), $this->Time->format($format, $val));
  354. }
  355. $result = $this->Time->format('Y-m-d', null, 'never');
  356. $this->assertEqual($result, 'never');
  357. }
  358. /**
  359. * testOfGmt method
  360. *
  361. * @access public
  362. * @return void
  363. */
  364. function testGmt() {
  365. $hour = 3;
  366. $min = 4;
  367. $sec = 2;
  368. $month = 5;
  369. $day = 14;
  370. $year = 2007;
  371. $time = mktime($hour, $min, $sec, $month, $day, $year);
  372. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  373. $this->assertEqual($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
  374. $hour = date('H');
  375. $min = date('i');
  376. $sec = date('s');
  377. $month = date('m');
  378. $day = date('d');
  379. $year = date('Y');
  380. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  381. $this->assertEqual($expected, $this->Time->gmt(null));
  382. }
  383. /**
  384. * testIsToday method
  385. *
  386. * @access public
  387. * @return void
  388. */
  389. function testIsToday() {
  390. $result = $this->Time->isToday('+1 day');
  391. $this->assertFalse($result);
  392. $result = $this->Time->isToday('+1 days');
  393. $this->assertFalse($result);
  394. $result = $this->Time->isToday('+0 day');
  395. $this->assertTrue($result);
  396. $result = $this->Time->isToday('-1 day');
  397. $this->assertFalse($result);
  398. }
  399. /**
  400. * testIsThisWeek method
  401. *
  402. * @access public
  403. * @return void
  404. */
  405. function testIsThisWeek() {
  406. // A map of days which goes from -1 day of week to +1 day of week
  407. $map = array(
  408. 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
  409. 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
  410. 'Sun' => array(-7, 1)
  411. );
  412. $days = $map[date('D')];
  413. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  414. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  415. }
  416. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  417. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  418. }
  419. /**
  420. * testIsThisMonth method
  421. *
  422. * @access public
  423. * @return void
  424. */
  425. function testIsThisMonth() {
  426. $result = $this->Time->isThisMonth('+0 day');
  427. $this->assertTrue($result);
  428. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  429. $this->assertTrue($result);
  430. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  431. $this->assertFalse($result);
  432. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  433. $this->assertFalse($result);
  434. }
  435. /**
  436. * testIsThisYear method
  437. *
  438. * @access public
  439. * @return void
  440. */
  441. function testIsThisYear() {
  442. $result = $this->Time->isThisYear('+0 day');
  443. $this->assertTrue($result);
  444. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  445. $this->assertTrue($result);
  446. }
  447. /**
  448. * testWasYesterday method
  449. *
  450. * @access public
  451. * @return void
  452. */
  453. function testWasYesterday() {
  454. $result = $this->Time->wasYesterday('+1 day');
  455. $this->assertFalse($result);
  456. $result = $this->Time->wasYesterday('+1 days');
  457. $this->assertFalse($result);
  458. $result = $this->Time->wasYesterday('+0 day');
  459. $this->assertFalse($result);
  460. $result = $this->Time->wasYesterday('-1 day');
  461. $this->assertTrue($result);
  462. $result = $this->Time->wasYesterday('-1 days');
  463. $this->assertTrue($result);
  464. $result = $this->Time->wasYesterday('-2 days');
  465. $this->assertFalse($result);
  466. }
  467. /**
  468. * testIsTomorrow method
  469. *
  470. * @access public
  471. * @return void
  472. */
  473. function testIsTomorrow() {
  474. $result = $this->Time->isTomorrow('+1 day');
  475. $this->assertTrue($result);
  476. $result = $this->Time->isTomorrow('+1 days');
  477. $this->assertTrue($result);
  478. $result = $this->Time->isTomorrow('+0 day');
  479. $this->assertFalse($result);
  480. $result = $this->Time->isTomorrow('-1 day');
  481. $this->assertFalse($result);
  482. }
  483. /**
  484. * testWasWithinLast method
  485. *
  486. * @access public
  487. * @return void
  488. */
  489. function testWasWithinLast() {
  490. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  491. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  492. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  493. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  494. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  495. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  496. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  497. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  498. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  499. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  500. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  501. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  502. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  503. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  504. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  505. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  506. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  507. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  508. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  509. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  510. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  511. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  512. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  513. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  514. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  515. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  516. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  517. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  518. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  519. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  520. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  521. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  522. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  523. }
  524. /**
  525. * testUserOffset method
  526. *
  527. * @access public
  528. * @return void
  529. */
  530. function testUserOffset() {
  531. if ($this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
  532. return;
  533. }
  534. $timezoneServer = new DateTimeZone(date_default_timezone_get());
  535. $timeServer = new DateTime('now', $timezoneServer);
  536. $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
  537. $expected = time();
  538. $result = $this->Time->fromString(time(), $yourTimezone);
  539. $this->assertEqual($expected, $result);
  540. }
  541. /**
  542. * test fromString()
  543. *
  544. * @access public
  545. * @return void
  546. */
  547. function testFromString() {
  548. $result = $this->Time->fromString('');
  549. $this->assertFalse($result);
  550. $result = $this->Time->fromString(0, 0);
  551. $this->assertFalse($result);
  552. $result = $this->Time->fromString('+1 hour');
  553. $expected = strtotime('+1 hour');
  554. $this->assertEqual($expected, $result);
  555. $timezone = date('Z', time());
  556. $result = $this->Time->fromString('+1 hour', $timezone);
  557. $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
  558. $this->assertEqual($expected, $result);
  559. }
  560. /**
  561. * test converting time specifiers using a time definition localfe file
  562. *
  563. * @access public
  564. * @return void
  565. */
  566. function testConvertSpecifiers() {
  567. App::build(array(
  568. 'locales' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
  569. ), true);
  570. Configure::write('Config.language', 'time_test');
  571. $time = strtotime('Thu Jan 14 11:43:39 2010');
  572. $result = $this->Time->convertSpecifiers('%a', $time);
  573. $expected = 'jue';
  574. $this->assertEqual($expected, $result);
  575. $result = $this->Time->convertSpecifiers('%A', $time);
  576. $expected = 'jueves';
  577. $this->assertEqual($expected, $result);
  578. $result = $this->Time->convertSpecifiers('%c', $time);
  579. $expected = 'jue %d ene %Y %H:%M:%S %Z';
  580. $this->assertEqual($expected, $result);
  581. $result = $this->Time->convertSpecifiers('%C', $time);
  582. $expected = '20';
  583. $this->assertEqual($expected, $result);
  584. $result = $this->Time->convertSpecifiers('%D', $time);
  585. $expected = '%m/%d/%y';
  586. $this->assertEqual($expected, $result);
  587. $result = $this->Time->convertSpecifiers('%b', $time);
  588. $expected = 'ene';
  589. $this->assertEqual($expected, $result);
  590. $result = $this->Time->convertSpecifiers('%h', $time);
  591. $expected = 'ene';
  592. $this->assertEqual($expected, $result);
  593. $result = $this->Time->convertSpecifiers('%B', $time);
  594. $expected = 'enero';
  595. $this->assertEqual($expected, $result);
  596. $result = $this->Time->convertSpecifiers('%n', $time);
  597. $expected = "\n";
  598. $this->assertEqual($expected, $result);
  599. $result = $this->Time->convertSpecifiers('%n', $time);
  600. $expected = "\n";
  601. $this->assertEqual($expected, $result);
  602. $result = $this->Time->convertSpecifiers('%p', $time);
  603. $expected = 'AM';
  604. $this->assertEqual($expected, $result);
  605. $result = $this->Time->convertSpecifiers('%P', $time);
  606. $expected = 'am';
  607. $this->assertEqual($expected, $result);
  608. $result = $this->Time->convertSpecifiers('%r', $time);
  609. $expected = '%I:%M:%S AM';
  610. $this->assertEqual($expected, $result);
  611. $result = $this->Time->convertSpecifiers('%R', $time);
  612. $expected = '11:43';
  613. $this->assertEqual($expected, $result);
  614. $result = $this->Time->convertSpecifiers('%t', $time);
  615. $expected = "\t";
  616. $this->assertEqual($expected, $result);
  617. $result = $this->Time->convertSpecifiers('%T', $time);
  618. $expected = '%H:%M:%S';
  619. $this->assertEqual($expected, $result);
  620. $result = $this->Time->convertSpecifiers('%u', $time);
  621. $expected = 4;
  622. $this->assertEqual($expected, $result);
  623. $result = $this->Time->convertSpecifiers('%x', $time);
  624. $expected = '%d/%m/%y';
  625. $this->assertEqual($expected, $result);
  626. $result = $this->Time->convertSpecifiers('%X', $time);
  627. $expected = '%H:%M:%S';
  628. $this->assertEqual($expected, $result);
  629. }
  630. /**
  631. * test convert %e on windows.
  632. *
  633. * @return void
  634. */
  635. function testConvertPercentE() {
  636. if ($this->skipIf(DS !== '\\', 'Cannot run windows tests on non-windows OS')) {
  637. return;
  638. }
  639. $time = strtotime('Thu Jan 14 11:43:39 2010');
  640. $result = $this->Time->convertSpecifiers('%e', $time);
  641. $expected = '14';
  642. $this->assertEqual($expected, $result);
  643. $result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
  644. $expected = ' 1';
  645. $this->assertEqual($expected, $result);
  646. }
  647. /**
  648. * test formatting dates taking in account preferred i18n locale file
  649. *
  650. * @access public
  651. * @return void
  652. */
  653. function testI18nFormat() {
  654. App::build(array(
  655. 'locales' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
  656. ), true);
  657. Configure::write('Config.language', 'time_test');
  658. $time = strtotime('Thu Jan 14 13:59:28 2010');
  659. $result = $this->Time->i18nFormat($time);
  660. $expected = '14/01/10';
  661. $this->assertEqual($expected, $result);
  662. $result = $this->Time->i18nFormat($time, '%c');
  663. $expected = 'jue 14 ene 2010 13:59:28 ' . strftime('%Z', $time);
  664. $this->assertEqual($expected, $result);
  665. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  666. $expected = 'Time is 01:59:28 PM, and date is 14/01/10';
  667. $this->assertEqual($expected, $result);
  668. $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
  669. $expected = 'Date invalid';
  670. $this->assertEqual($expected, $result);
  671. }
  672. /**
  673. * test new format() syntax which inverts first and secod parameters
  674. *
  675. * @access public
  676. * @return void
  677. */
  678. function testFormatNewSyntax() {
  679. $time = time();
  680. $this->assertEqual($this->Time->format($time), $this->Time->i18nFormat($time));
  681. $this->assertEqual($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
  682. }
  683. }