PageRenderTime 98ms CodeModel.GetById 58ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

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

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