PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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