PageRenderTime 70ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/CakeTimeTest.php

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