PageRenderTime 57ms CodeModel.GetById 15ms app.highlight 35ms RepoModel.GetById 2ms app.codeStats 0ms

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

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