PageRenderTime 78ms CodeModel.GetById 31ms app.highlight 39ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://bitbucket.org/praveen_excell/opshop
PHP | 1067 lines | 720 code | 140 blank | 207 comment | 3 complexity | 78bc06d5bd7d925565682e25554cab17 MD5 | raw file
   1<?php
   2/**
   3 * CakeTimeTest file
   4 *
   5 * PHP 5
   6 *
   7 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
   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/2.0/en/development/testing.html 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 * Default system timezone identifier
  30 *
  31 * @var string
  32 */
  33	protected $_systemTimezoneIdentifier = null;
  34
  35/**
  36 * setUp method
  37 *
  38 * @return void
  39 */
  40	public function setUp() {
  41		$this->Time = new CakeTime();
  42		$this->_systemTimezoneIdentifier = date_default_timezone_get();
  43	}
  44
  45/**
  46 * tearDown method
  47 *
  48 * @return void
  49 */
  50	public function tearDown() {
  51		unset($this->Time);
  52		$this->_restoreSystemTimezone();
  53	}
  54
  55/**
  56 * Restored the original system timezone
  57 *
  58 * @param string $timezoneIdentifier Timezone string
  59 * @return void
  60 */
  61	protected function _restoreSystemTimezone() {
  62		date_default_timezone_set($this->_systemTimezoneIdentifier);
  63	}
  64
  65/**
  66 * testToQuarter method
  67 *
  68 * @return void
  69 */
  70	public function testToQuarter() {
  71		$result = $this->Time->toQuarter('2007-12-25');
  72		$this->assertEquals(4, $result);
  73
  74		$result = $this->Time->toQuarter('2007-9-25');
  75		$this->assertEquals(3, $result);
  76
  77		$result = $this->Time->toQuarter('2007-3-25');
  78		$this->assertEquals(1, $result);
  79
  80		$result = $this->Time->toQuarter('2007-3-25', true);
  81		$this->assertEquals(array('2007-01-01', '2007-03-31'), $result);
  82
  83		$result = $this->Time->toQuarter('2007-5-25', true);
  84		$this->assertEquals(array('2007-04-01', '2007-06-30'), $result);
  85
  86		$result = $this->Time->toQuarter('2007-8-25', true);
  87		$this->assertEquals(array('2007-07-01', '2007-09-30'), $result);
  88
  89		$result = $this->Time->toQuarter('2007-12-25', true);
  90		$this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
  91	}
  92
  93/**
  94 * provider for timeAgoInWords() tests
  95 *
  96 * @return array
  97 */
  98	public static function timeAgoProvider() {
  99		return array(
 100			array('-12 seconds', '12 seconds ago'),
 101			array('-12 minutes', '12 minutes ago'),
 102			array('-2 hours', '2 hours ago'),
 103			array('-1 day', '1 day ago'),
 104			array('-2 days', '2 days ago'),
 105			array('-2 days -3 hours', '2 days, 3 hours ago'),
 106			array('-1 week', '1 week ago'),
 107			array('-2 weeks -2 days', '2 weeks, 2 days ago'),
 108			array('+1 week', '1 week'),
 109			array('+1 week 1 day', '1 week, 1 day'),
 110			array('+2 weeks 2 day', '2 weeks, 2 days'),
 111			array('2007-9-24', 'on 24/9/07'),
 112			array('now', 'just now'),
 113		);
 114	}
 115
 116/**
 117 * testTimeAgoInWords method
 118 *
 119 * @dataProvider timeAgoProvider
 120 * @return void
 121 */
 122	public function testTimeAgoInWords($input, $expected) {
 123		$result = $this->Time->timeAgoInWords($input);
 124		$this->assertEquals($expected, $result);
 125	}
 126
 127/**
 128 * provider for timeAgo with an end date.
 129 *
 130 * @return void
 131 */
 132	public function timeAgoEndProvider() {
 133		return array(
 134			array(
 135				'+4 months +2 weeks +3 days',
 136				'4 months, 2 weeks, 3 days',
 137				'8 years'
 138			),
 139			array(
 140				'+4 months +2 weeks +1 day',
 141				'4 months, 2 weeks, 1 day',
 142				'8 years'
 143			),
 144			array(
 145				'+3 months +2 weeks',
 146				'3 months, 2 weeks',
 147				'8 years'
 148			),
 149			array(
 150				'+3 months +2 weeks +1 day',
 151				'3 months, 2 weeks, 1 day',
 152				'8 years'
 153			),
 154			array(
 155				'+1 months +1 week +1 day',
 156				'1 month, 1 week, 1 day',
 157				'8 years'
 158			),
 159			array(
 160				'+2 months +2 days',
 161				'2 months, 2 days',
 162				'on ' . date('j/n/y', strtotime('+2 months +2 days'))
 163			),
 164			array(
 165				'+2 months +12 days',
 166				'2 months, 1 week, 5 days',
 167				'3 months'
 168			),
 169		);
 170	}
 171
 172/**
 173 * test the end option for timeAgoInWords
 174 *
 175 * @dataProvider timeAgoEndProvider
 176 * @return void
 177 */
 178	public function testTimeAgoInWordsEnd($input, $expected, $end) {
 179		$result = $this->Time->timeAgoInWords(
 180			$input, array('end' => $end)
 181		);
 182		$this->assertEquals($expected, $result);
 183	}
 184
 185/**
 186 * Test the accuracy option for timeAgoInWords()
 187 *
 188 * @return void
 189 */
 190	public function testTimeAgoInWordsAccuracy() {
 191		$result = $this->Time->timeAgoInWords(
 192			strtotime('+8 years +4 months +2 weeks +3 days'),
 193			array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
 194		);
 195		$expected = '8 years';
 196		$this->assertEquals($expected, $result);
 197
 198		$result = $this->Time->timeAgoInWords(
 199			strtotime('+8 years +4 months +2 weeks +3 days'),
 200			array('accuracy' => array('year' => 'month'), 'end' => '+10 years')
 201		);
 202		$expected = '8 years, 4 months';
 203		$this->assertEquals($expected, $result);
 204
 205		$result = $this->Time->timeAgoInWords(
 206			strtotime('+8 years +4 months +2 weeks +3 days'),
 207			array('accuracy' => array('year' => 'week'), 'end' => '+10 years')
 208		);
 209		$expected = '8 years, 4 months, 2 weeks';
 210		$this->assertEquals($expected, $result);
 211
 212		$result = $this->Time->timeAgoInWords(
 213			strtotime('+8 years +4 months +2 weeks +3 days'),
 214			array('accuracy' => array('year' => 'day'), 'end' => '+10 years')
 215		);
 216		$expected = '8 years, 4 months, 2 weeks, 3 days';
 217		$this->assertEquals($expected, $result);
 218
 219		$result = $this->Time->timeAgoInWords(
 220			strtotime('+1 years +5 weeks'),
 221			array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
 222		);
 223		$expected = '1 year';
 224		$this->assertEquals($expected, $result);
 225	}
 226
 227/**
 228 * Test the format option of timeAgoInWords()
 229 *
 230 * @return void
 231 */
 232	public function testTimeAgoInWordsWithFormat() {
 233		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
 234		$this->assertEquals('on 2007-09-25', $result);
 235
 236		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
 237		$this->assertEquals('on 2007-09-25', $result);
 238
 239		$result = $this->Time->timeAgoInWords(
 240			strtotime('+2 weeks +2 days'),
 241			'Y-m-d'
 242		);
 243		$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
 244
 245		$result = $this->Time->timeAgoInWords(
 246			strtotime('+2 months +2 days'),
 247			array('end' => '1 month', 'format' => 'Y-m-d')
 248		);
 249		$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
 250	}
 251
 252/**
 253 * test timeAgoInWords() with negative values.
 254 *
 255 * @return void
 256 */
 257	public function testTimeAgoInWordsNegativeValues() {
 258		$result = $this->Time->timeAgoInWords(
 259			strtotime('-2 months -2 days'),
 260			array('end' => '3 month')
 261		);
 262		$this->assertEquals('2 months, 2 days ago', $result);
 263
 264		$result = $this->Time->timeAgoInWords(
 265			strtotime('-2 months -2 days'),
 266			array('end' => '3 month')
 267		);
 268		$this->assertEquals('2 months, 2 days ago', $result);
 269
 270		$result = $this->Time->timeAgoInWords(
 271			strtotime('-2 months -2 days'),
 272			array('end' => '1 month', 'format' => 'Y-m-d')
 273		);
 274		$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
 275
 276		$result = $this->Time->timeAgoInWords(
 277			strtotime('-2 years -5 months -2 days'),
 278			array('end' => '3 years')
 279		);
 280		$this->assertEquals('2 years, 5 months, 2 days ago', $result);
 281
 282		$result = $this->Time->timeAgoInWords(
 283			strtotime('-2 weeks -2 days'),
 284			'Y-m-d'
 285		);
 286		$this->assertEquals('2 weeks, 2 days ago', $result);
 287
 288		$time = strtotime('-3 years -12 months');
 289		$result = $this->Time->timeAgoInWords($time);
 290		$expected = 'on ' . date('j/n/y', $time);
 291		$this->assertEquals($expected, $result);
 292
 293		$result = $this->Time->timeAgoInWords(
 294			strtotime('-1 month -1 week -6 days'),
 295			array('end' => '1 year', 'accuracy' => array('month' => 'month'))
 296		);
 297		$this->assertEquals('1 month ago', $result);
 298
 299		$timestamp = strtotime('-1 years -2 weeks -3 days');
 300		$result = $this->Time->timeAgoInWords(
 301			$timestamp,
 302			array('accuracy' => array('year' => 'year'))
 303		);
 304		$expected = 'on ' . date('j/n/y', $timestamp);
 305		$this->assertEquals($expected, $result);
 306
 307		$result = $this->Time->timeAgoInWords(
 308			strtotime('-13 months -5 days'),
 309			array('end' => '2 years')
 310		);
 311		$this->assertEquals('1 year, 1 month, 5 days ago', $result);
 312	}
 313
 314/**
 315 * testNice method
 316 *
 317 * @return void
 318 */
 319	public function testNice() {
 320		$time = time() + 2 * DAY;
 321		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
 322
 323		$time = time() - 2 * DAY;
 324		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
 325
 326		$time = time();
 327		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
 328
 329		$time = 0;
 330		$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
 331
 332		$time = null;
 333		$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
 334
 335		$time = time();
 336		$this->assertEquals(date('D', $time), $this->Time->nice($time, null, '%a'));
 337		$this->assertEquals(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y'));
 338
 339		$this->Time->niceFormat = '%Y-%d-%m';
 340		$this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
 341		$this->assertEquals('%Y-%d-%m', $this->Time->niceFormat);
 342
 343		CakeTime::$niceFormat = '%Y-%d-%m %H:%M:%S';
 344		$this->assertEquals(date('Y-d-m H:i:s', $time), $this->Time->nice($time));
 345		$this->assertEquals('%Y-%d-%m %H:%M:%S', $this->Time->niceFormat);
 346
 347		date_default_timezone_set('UTC');
 348		$result = $this->Time->nice(null, 'America/New_York');
 349		$expected = $this->Time->nice(time(), 'America/New_York');
 350		$this->assertEquals($expected, $result);
 351
 352		$this->_restoreSystemTimezone();
 353	}
 354
 355/**
 356 * testNiceShort method
 357 *
 358 * @return void
 359 */
 360	public function testNiceShort() {
 361		$time = time();
 362		$this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
 363
 364		$time = time() - DAY;
 365		$this->assertEquals('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
 366
 367		$time = time() + DAY;
 368		$this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
 369
 370		date_default_timezone_set('Europe/London');
 371		$result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
 372		$this->assertEquals('Jan 15th 2005, 11:00', $result);
 373
 374		date_default_timezone_set('UTC');
 375		$result = $this->Time->niceShort(null, 'America/New_York');
 376		$expected = $this->Time->niceShort(time(), 'America/New_York');
 377		$this->assertEquals($expected, $result);
 378
 379		$this->_restoreSystemTimezone();
 380	}
 381
 382/**
 383 * testDaysAsSql method
 384 *
 385 * @return void
 386 */
 387	public function testDaysAsSql() {
 388		$begin = time();
 389		$end = time() + DAY;
 390		$field = 'my_field';
 391		$expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
 392		$this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
 393	}
 394
 395/**
 396 * testDayAsSql method
 397 *
 398 * @return void
 399 */
 400	public function testDayAsSql() {
 401		$time = time();
 402		$field = 'my_field';
 403		$expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
 404		$this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
 405	}
 406
 407/**
 408 * testToUnix method
 409 *
 410 * @return void
 411 */
 412	public function testToUnix() {
 413		$this->assertEquals(time(), $this->Time->toUnix(time()));
 414		$this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
 415		$this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
 416		$this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
 417		$this->assertEquals(false, $this->Time->toUnix(''));
 418		$this->assertEquals(false, $this->Time->toUnix(null));
 419	}
 420
 421/**
 422 * testToServer method
 423 *
 424 * @return void
 425 */
 426	public function testToServer() {
 427		date_default_timezone_set('Europe/Paris');
 428
 429		$time = time();
 430		$this->assertEquals(date('Y-m-d H:i:s', $time), $this->Time->toServer($time));
 431
 432		date_default_timezone_set('America/New_York');
 433		$time = time();
 434		date_default_timezone_set('Europe/Paris');
 435		$result = $this->Time->toServer($time, 'America/New_York');
 436		$this->assertEquals(date('Y-m-d H:i:s', $time), $result);
 437
 438		date_default_timezone_set('Europe/Paris');
 439		$time = '2005-10-25 10:00:00';
 440		$result = $this->Time->toServer($time);
 441		$date = new DateTime($time, new DateTimeZone('UTC'));
 442		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
 443		$expected = $date->format('Y-m-d H:i:s');
 444		$this->assertEquals($expected, $result);
 445
 446		$time = '2002-01-01 05:15:30';
 447		$result = $this->Time->toServer($time, 'America/New_York');
 448		$date = new DateTime($time, new DateTimeZone('America/New_York'));
 449		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
 450		$expected = $date->format('Y-m-d H:i:s');
 451		$this->assertEquals($expected, $result);
 452
 453		$time = '2010-01-28T15:00:00+10:00';
 454		$result = $this->Time->toServer($time, 'America/New_York');
 455		$date = new DateTime($time);
 456		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
 457		$expected = $date->format('Y-m-d H:i:s');
 458		$this->assertEquals($expected, $result);
 459
 460		$date = new DateTime(null, new DateTimeZone('America/New_York'));
 461		$result = $this->Time->toServer($date, 'Pacific/Tahiti');
 462		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
 463		$expected = $date->format('Y-m-d H:i:s');
 464		$this->assertEquals($expected, $result);
 465
 466		$this->_restoreSystemTimezone();
 467
 468		$time = time();
 469		$result = $this->Time->toServer($time, null, 'l jS \of F Y h:i:s A');
 470		$expected = date('l jS \of F Y h:i:s A', $time);
 471		$this->assertEquals($expected, $result);
 472
 473		$this->assertFalse($this->Time->toServer(time(), new Object()));
 474
 475		date_default_timezone_set('UTC');
 476
 477		$serverTime = new DateTime('now');
 478
 479		$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
 480		foreach ($timezones as $timezone) {
 481			$result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
 482			$tz = new DateTimeZone($timezone);
 483			$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
 484		}
 485
 486		date_default_timezone_set('UTC');
 487		$date = new DateTime('now', new DateTimeZone('America/New_York'));
 488
 489		$result = $this->Time->toServer($date, null, 'Y-m-d H:i:s');
 490		$date->setTimezone($this->Time->timezone());
 491		$expected = $date->format('Y-m-d H:i:s');
 492		$this->assertEquals($expected, $result);
 493
 494		$this->_restoreSystemTimezone();
 495	}
 496
 497/**
 498 * testToAtom method
 499 *
 500 * @return void
 501 */
 502	public function testToAtom() {
 503		$this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
 504	}
 505
 506/**
 507 * testToRss method
 508 *
 509 * @return void
 510 */
 511	public function testToRss() {
 512		$this->assertEquals(date('r'), $this->Time->toRss(time()));
 513
 514		if (!$this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
 515			$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
 516			foreach ($timezones as $timezone) {
 517				$yourTimezone = new DateTimeZone($timezone);
 518				$yourTime = new DateTime('now', $yourTimezone);
 519				$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
 520				$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $userOffset));
 521				$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $timezone));
 522			}
 523		}
 524	}
 525
 526/**
 527 * testFormat method
 528 *
 529 * @return void
 530 */
 531	public function testFormat() {
 532		$format = 'D-M-Y';
 533		$tz = date_default_timezone_get();
 534		$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
 535		foreach ($arr as $val) {
 536			$this->assertEquals(date($format, $val), $this->Time->format($format, $val));
 537			$this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
 538		}
 539
 540		$result = $this->Time->format('Y-m-d', null, 'never');
 541		$this->assertEquals('never', $result);
 542
 543		$result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
 544		$this->assertEquals('13-01-2012', $result);
 545
 546		$result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
 547		$this->assertEquals('invalid', $result);
 548	}
 549
 550/**
 551 * testOfGmt method
 552 *
 553 * @return void
 554 */
 555	public function testGmt() {
 556		$hour = 3;
 557		$min = 4;
 558		$sec = 2;
 559		$month = 5;
 560		$day = 14;
 561		$year = 2007;
 562		$time = mktime($hour, $min, $sec, $month, $day, $year);
 563		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
 564		$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
 565
 566		$hour = date('H');
 567		$min = date('i');
 568		$sec = date('s');
 569		$month = date('m');
 570		$day = date('d');
 571		$year = date('Y');
 572		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
 573		$this->assertEquals($expected, $this->Time->gmt(null));
 574	}
 575
 576/**
 577 * testIsToday method
 578 *
 579 * @return void
 580 */
 581	public function testIsToday() {
 582		$result = $this->Time->isToday('+1 day');
 583		$this->assertFalse($result);
 584		$result = $this->Time->isToday('+1 days');
 585		$this->assertFalse($result);
 586		$result = $this->Time->isToday('+0 day');
 587		$this->assertTrue($result);
 588		$result = $this->Time->isToday('-1 day');
 589		$this->assertFalse($result);
 590	}
 591
 592/**
 593 * testIsThisWeek method
 594 *
 595 * @return void
 596 */
 597	public function testIsThisWeek() {
 598		// A map of days which goes from -1 day of week to +1 day of week
 599		$map = array(
 600			'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
 601			'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
 602			'Sun' => array(-7, 1)
 603		);
 604		$days = $map[date('D')];
 605
 606		for ($day = $days[0] + 1; $day < $days[1]; $day++) {
 607			$this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
 608		}
 609		$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
 610		$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
 611	}
 612
 613/**
 614 * testIsThisMonth method
 615 *
 616 * @return void
 617 */
 618	public function testIsThisMonth() {
 619		$result = $this->Time->isThisMonth('+0 day');
 620		$this->assertTrue($result);
 621		$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
 622		$this->assertTrue($result);
 623		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
 624		$this->assertFalse($result);
 625		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
 626		$this->assertFalse($result);
 627	}
 628
 629/**
 630 * testIsThisYear method
 631 *
 632 * @return void
 633 */
 634	public function testIsThisYear() {
 635		$result = $this->Time->isThisYear('+0 day');
 636		$this->assertTrue($result);
 637		$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
 638		$this->assertTrue($result);
 639	}
 640
 641/**
 642 * testWasYesterday method
 643 *
 644 * @return void
 645 */
 646	public function testWasYesterday() {
 647		$result = $this->Time->wasYesterday('+1 day');
 648		$this->assertFalse($result);
 649		$result = $this->Time->wasYesterday('+1 days');
 650		$this->assertFalse($result);
 651		$result = $this->Time->wasYesterday('+0 day');
 652		$this->assertFalse($result);
 653		$result = $this->Time->wasYesterday('-1 day');
 654		$this->assertTrue($result);
 655		$result = $this->Time->wasYesterday('-1 days');
 656		$this->assertTrue($result);
 657		$result = $this->Time->wasYesterday('-2 days');
 658		$this->assertFalse($result);
 659	}
 660
 661/**
 662 * testIsTomorrow method
 663 *
 664 * @return void
 665 */
 666	public function testIsTomorrow() {
 667		$result = $this->Time->isTomorrow('+1 day');
 668		$this->assertTrue($result);
 669		$result = $this->Time->isTomorrow('+1 days');
 670		$this->assertTrue($result);
 671		$result = $this->Time->isTomorrow('+0 day');
 672		$this->assertFalse($result);
 673		$result = $this->Time->isTomorrow('-1 day');
 674		$this->assertFalse($result);
 675	}
 676
 677/**
 678 * testWasWithinLast method
 679 *
 680 * @return void
 681 */
 682	public function testWasWithinLast() {
 683		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
 684		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
 685		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
 686		$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
 687		$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
 688		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
 689		$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
 690		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
 691
 692		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
 693		$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
 694		$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
 695		$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
 696		$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
 697		$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
 698		$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
 699		$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
 700
 701		$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
 702		$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
 703		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
 704		$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
 705		$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
 706		$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
 707
 708		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
 709		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
 710		$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
 711		$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
 712		$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
 713		$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
 714		$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
 715
 716		$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
 717		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
 718		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
 719		$this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
 720	}
 721
 722/**
 723 * testWasWithinLast method
 724 *
 725 * @return void
 726 */
 727	public function testIsWithinNext() {
 728		$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
 729		$this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
 730		$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
 731		$this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
 732		$this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
 733		$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
 734		$this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
 735		$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
 736
 737		$this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
 738		$this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
 739		$this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
 740		$this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
 741		$this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
 742		$this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
 743		$this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
 744		$this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
 745
 746		$this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
 747		$this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
 748		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
 749		$this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
 750		$this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
 751		$this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
 752
 753		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
 754		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
 755		$this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
 756		$this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
 757		$this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
 758		$this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
 759		$this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
 760
 761		$this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
 762		$this->assertFalse($this->Time->isWithinNext('1   ', '-1 hour'));
 763		$this->assertFalse($this->Time->isWithinNext('1   ', '-1 minute'));
 764		$this->assertFalse($this->Time->isWithinNext('1   ', '-23 hours -59 minutes -59 seconds'));
 765
 766		$this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
 767		$this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
 768	}
 769
 770/**
 771 * testUserOffset method
 772 *
 773 * @return void
 774 */
 775	public function testUserOffset() {
 776		$timezoneServer = new DateTimeZone(date_default_timezone_get());
 777		$timeServer = new DateTime('now', $timezoneServer);
 778		$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
 779
 780		$expected = time();
 781		$result = $this->Time->fromString(time(), $yourTimezone);
 782		$this->assertEquals($expected, $result);
 783
 784		$result = $this->Time->fromString(time(), $timezoneServer->getName());
 785		$this->assertEquals($expected, $result);
 786
 787		$result = $this->Time->fromString(time(), $timezoneServer);
 788		$this->assertEquals($expected, $result);
 789
 790		Configure::write('Config.timezone', $timezoneServer->getName());
 791		$result = $this->Time->fromString(time());
 792		$this->assertEquals($expected, $result);
 793		Configure::delete('Config.timezone');
 794	}
 795
 796/**
 797 * test fromString()
 798 *
 799 * @return void
 800 */
 801	public function testFromString() {
 802		$result = $this->Time->fromString('');
 803		$this->assertFalse($result);
 804
 805		$result = $this->Time->fromString(0, 0);
 806		$this->assertFalse($result);
 807
 808		$result = $this->Time->fromString('+1 hour');
 809		$expected = strtotime('+1 hour');
 810		$this->assertEquals($expected, $result);
 811
 812		$timezone = date('Z', time());
 813		$result = $this->Time->fromString('+1 hour', $timezone);
 814		$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
 815		$this->assertEquals($expected, $result);
 816
 817		$timezone = date_default_timezone_get();
 818		$result = $this->Time->fromString('+1 hour', $timezone);
 819		$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
 820		$this->assertEquals($expected, $result);
 821
 822		date_default_timezone_set('UTC');
 823		$date = new DateTime('now', new DateTimeZone('Europe/London'));
 824		$this->Time->fromString($date);
 825		$this->assertEquals('Europe/London', $date->getTimeZone()->getName());
 826
 827		$this->_restoreSystemTimezone();
 828	}
 829
 830/**
 831 * test fromString() with a DateTime object as the dateString
 832 *
 833 * @return void
 834 */
 835	public function testFromStringWithDateTime() {
 836		date_default_timezone_set('UTC');
 837
 838		$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
 839		$result = $this->Time->fromString($date, 'UTC');
 840		$date->setTimezone(new DateTimeZone('UTC'));
 841		$expected = $date->format('U') + $date->getOffset();
 842
 843		$this->assertEquals($expected, $result);
 844
 845		date_default_timezone_set('Australia/Melbourne');
 846
 847		$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
 848		$result = $this->Time->fromString($date, 'Asia/Kuwait');
 849		$date->setTimezone(new DateTimeZone('Asia/Kuwait'));
 850		$expected = $date->format('U') + $date->getOffset();
 851		$this->assertEquals($expected, $result);
 852
 853		$this->_restoreSystemTimezone();
 854	}
 855
 856/**
 857 * test converting time specifiers using a time definition localfe file
 858 *
 859 * @return void
 860 */
 861	public function testConvertSpecifiers() {
 862		App::build(array(
 863			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
 864		), App::RESET);
 865		Configure::write('Config.language', 'time_test');
 866		$time = strtotime('Thu Jan 14 11:43:39 2010');
 867
 868		$result = $this->Time->convertSpecifiers('%a', $time);
 869		$expected = 'jue';
 870		$this->assertEquals($expected, $result);
 871
 872		$result = $this->Time->convertSpecifiers('%A', $time);
 873		$expected = 'jueves';
 874		$this->assertEquals($expected, $result);
 875
 876		$result = $this->Time->convertSpecifiers('%c', $time);
 877		$expected = 'jue %d ene %Y %H:%M:%S %Z';
 878		$this->assertEquals($expected, $result);
 879
 880		$result = $this->Time->convertSpecifiers('%C', $time);
 881		$expected = '20';
 882		$this->assertEquals($expected, $result);
 883
 884		$result = $this->Time->convertSpecifiers('%D', $time);
 885		$expected = '%m/%d/%y';
 886		$this->assertEquals($expected, $result);
 887
 888		$result = $this->Time->convertSpecifiers('%b', $time);
 889		$expected = 'ene';
 890		$this->assertEquals($expected, $result);
 891
 892		$result = $this->Time->convertSpecifiers('%h', $time);
 893		$expected = 'ene';
 894		$this->assertEquals($expected, $result);
 895
 896		$result = $this->Time->convertSpecifiers('%B', $time);
 897		$expected = 'enero';
 898		$this->assertEquals($expected, $result);
 899
 900		$result = $this->Time->convertSpecifiers('%n', $time);
 901		$expected = "\n";
 902		$this->assertEquals($expected, $result);
 903
 904		$result = $this->Time->convertSpecifiers('%n', $time);
 905		$expected = "\n";
 906		$this->assertEquals($expected, $result);
 907
 908		$result = $this->Time->convertSpecifiers('%p', $time);
 909		$expected = 'AM';
 910		$this->assertEquals($expected, $result);
 911
 912		$result = $this->Time->convertSpecifiers('%P', $time);
 913		$expected = 'am';
 914		$this->assertEquals($expected, $result);
 915
 916		$result = $this->Time->convertSpecifiers('%r', $time);
 917		$expected = '%I:%M:%S AM';
 918		$this->assertEquals($expected, $result);
 919
 920		$result = $this->Time->convertSpecifiers('%R', $time);
 921		$expected = '11:43';
 922		$this->assertEquals($expected, $result);
 923
 924		$result = $this->Time->convertSpecifiers('%t', $time);
 925		$expected = "\t";
 926		$this->assertEquals($expected, $result);
 927
 928		$result = $this->Time->convertSpecifiers('%T', $time);
 929		$expected = '%H:%M:%S';
 930		$this->assertEquals($expected, $result);
 931
 932		$result = $this->Time->convertSpecifiers('%u', $time);
 933		$expected = 4;
 934		$this->assertEquals($expected, $result);
 935
 936		$result = $this->Time->convertSpecifiers('%x', $time);
 937		$expected = '%d/%m/%y';
 938		$this->assertEquals($expected, $result);
 939
 940		$result = $this->Time->convertSpecifiers('%X', $time);
 941		$expected = '%H:%M:%S';
 942		$this->assertEquals($expected, $result);
 943	}
 944
 945/**
 946 * test convert %e on windows.
 947 *
 948 * @return void
 949 */
 950	public function testConvertPercentE() {
 951		$this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run windows tests on non-windows OS.');
 952
 953		$time = strtotime('Thu Jan 14 11:43:39 2010');
 954		$result = $this->Time->convertSpecifiers('%e', $time);
 955		$expected = '14';
 956		$this->assertEquals($expected, $result);
 957
 958		$result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
 959		$expected = ' 1';
 960		$this->assertEquals($expected, $result);
 961	}
 962
 963/**
 964 * test formatting dates taking in account preferred i18n locale file
 965 *
 966 * @return void
 967 */
 968	public function testI18nFormat() {
 969		App::build(array(
 970			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
 971		), App::RESET);
 972		Configure::write('Config.language', 'time_test');
 973
 974		$time = strtotime('Thu Jan 14 13:59:28 2010');
 975
 976		$result = $this->Time->i18nFormat($time);
 977		$expected = '14/01/10';
 978		$this->assertEquals($expected, $result);
 979
 980		$result = $this->Time->i18nFormat($time, '%c');
 981		$expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
 982		$this->assertEquals($expected, $result);
 983
 984		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
 985		$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
 986		$this->assertEquals($expected, $result);
 987
 988		$time = strtotime('Wed Jan 13 13:59:28 2010');
 989
 990		$result = $this->Time->i18nFormat($time);
 991		$expected = '13/01/10';
 992		$this->assertEquals($expected, $result);
 993
 994		$result = $this->Time->i18nFormat($time, '%c');
 995		$expected = 'miĂŠ 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
 996		$this->assertEquals($expected, $result);
 997
 998		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
 999		$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
1000		$this->assertEquals($expected, $result);
1001
1002		$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
1003		$expected = 'Date invalid';
1004		$this->assertEquals($expected, $result);
1005	}
1006
1007/**
1008 * test new format() syntax which inverts first and second parameters
1009 *
1010 * @return void
1011 */
1012	public function testFormatNewSyntax() {
1013		$time = time();
1014		$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
1015		$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
1016	}
1017
1018/**
1019 * testListTimezones
1020 *
1021 * @return void
1022 */
1023	public function testListTimezones() {
1024		$return = CakeTime::listTimezones();
1025		$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1026		$this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
1027		$this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
1028		$this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
1029		$this->assertTrue(isset($return['UTC']['UTC']));
1030		$this->assertFalse(isset($return['Cuba']));
1031		$this->assertFalse(isset($return['US']));
1032
1033		$return = CakeTime::listTimezones('#^Asia/#');
1034		$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1035		$this->assertFalse(isset($return['Pacific']));
1036
1037		$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
1038		$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
1039		$this->assertTrue(isset($return['Pacific/Tahiti']));
1040
1041		if (!$this->skipIf(version_compare(PHP_VERSION, '5.3.0', '<'))) {
1042			$return = CakeTime::listTimezones(DateTimeZone::ASIA);
1043			$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1044			$this->assertFalse(isset($return['Pacific']));
1045
1046			$return = CakeTime::listTimezones(DateTimeZone::PER_COUNTRY, 'US', false);
1047			$this->assertTrue(isset($return['Pacific/Honolulu']));
1048			$this->assertFalse(isset($return['Asia/Bangkok']));
1049		}
1050	}
1051
1052/**
1053 * Tests that using CakeTime::format() with the correct sytax actually converts
1054 * from one timezone to the other correctly
1055 *
1056 * @return void
1057 **/
1058	public function testCorrectTimezoneConversion() {
1059		date_default_timezone_set('UTC');
1060		$date = '2012-01-01 10:00:00';
1061		$converted = CakeTime::format($date, '%Y-%m-%d %H:%M:%S', '', 'Europe/Copenhagen');
1062		$expected = new DateTime($date);
1063		$expected->setTimezone(new DateTimeZone('Europe/Copenhagen'));
1064		$this->assertEquals($expected->format('Y-m-d H:i:s'), $converted);
1065	}
1066
1067}