PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

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