PageRenderTime 71ms CodeModel.GetById 18ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/view/helpers/time.test.php

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