PageRenderTime 143ms CodeModel.GetById 105ms app.highlight 32ms RepoModel.GetById 0ms app.codeStats 1ms

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