PageRenderTime 79ms CodeModel.GetById 38ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://bitbucket.org/ducnv66/bbb-admin
PHP | 695 lines | 426 code | 85 blank | 184 comment | 28 complexity | 0201c249c79f56cd290ec892b0778c97 MD5 | raw file
  1<?php
  2/* SVN FILE: $Id$ */
  3
  4/**
  5 * TimeHelperTest file
  6 *
  7 * Long description for file
  8 *
  9 * PHP versions 4 and 5
 10 *
 11 * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
 12 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 13 *
 14 *  Licensed under The Open Group Test Suite License
 15 *  Redistributions of files must retain the above copyright notice.
 16 *
 17 * @filesource
 18 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 19 * @link          https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
 20 * @package       cake
 21 * @subpackage    cake.tests.cases.libs.view.helpers
 22 * @since         CakePHP(tm) v 1.2.0.4206
 23 * @version       $Revision$
 24 * @modifiedby    $LastChangedBy$
 25 * @lastmodified  $Date$
 26 * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
 27 */
 28if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
 29	define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
 30}
 31App::import('Helper', 'Time');
 32
 33/**
 34 * TimeHelperTest class
 35 *
 36 * @package       cake
 37 * @subpackage    cake.tests.cases.libs.view.helpers
 38 */
 39class TimeHelperTest extends CakeTestCase {
 40
 41/**
 42 * setUp method
 43 *
 44 * @access public
 45 * @return void
 46 */
 47	function setUp() {
 48		$this->Time = new TimeHelper();
 49	}
 50
 51/**
 52 * tearDown method
 53 *
 54 * @access public
 55 * @return void
 56 */
 57	function tearDown() {
 58		unset($this->Time);
 59	}
 60
 61/**
 62 * testToQuarter method
 63 *
 64 * @access public
 65 * @return void
 66 */
 67	function testToQuarter() {
 68		$result = $this->Time->toQuarter('2007-12-25');
 69		$this->assertEqual($result, 4);
 70
 71		$result = $this->Time->toQuarter('2007-9-25');
 72		$this->assertEqual($result, 3);
 73
 74		$result = $this->Time->toQuarter('2007-3-25');
 75		$this->assertEqual($result, 1);
 76
 77		$result = $this->Time->toQuarter('2007-3-25', true);
 78		$this->assertEqual($result, array('2007-01-01', '2007-03-31'));
 79
 80		$result = $this->Time->toQuarter('2007-5-25', true);
 81		$this->assertEqual($result, array('2007-04-01', '2007-06-30'));
 82
 83		$result = $this->Time->toQuarter('2007-8-25', true);
 84		$this->assertEqual($result, array('2007-07-01', '2007-09-30'));
 85
 86		$result = $this->Time->toQuarter('2007-12-25', true);
 87		$this->assertEqual($result, array('2007-10-01', '2007-12-31'));
 88	}
 89
 90/**
 91 * testTimeAgoInWords method
 92 *
 93 * @access public
 94 * @return void
 95 */
 96	function testTimeAgoInWords() {
 97		$result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 3 days'), array('end' => '8 years'), true);
 98		$this->assertEqual($result, '4 months, 2 weeks, 3 days');
 99
100		$result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 2 days'), array('end' => '8 years'), true);
101		$this->assertEqual($result, '4 months, 2 weeks, 2 days');
102
103		$result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
104		$this->assertEqual($result, '4 months, 2 weeks, 1 day');
105
106		$result = $this->Time->timeAgoInWords(strtotime('3 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
107		$this->assertEqual($result, '3 months, 2 weeks, 1 day');
108
109		$result = $this->Time->timeAgoInWords(strtotime('3 months, 2 weeks'), array('end' => '8 years'), true);
110		$this->assertEqual($result, '3 months, 2 weeks');
111
112		$result = $this->Time->timeAgoInWords(strtotime('3 months, 1 week, 6 days'), array('end' => '8 years'), true);
113		$this->assertEqual($result, '3 months, 1 week, 6 days');
114
115		$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
116		$this->assertEqual($result, '2 months, 2 weeks, 1 day');
117
118		$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 weeks'), array('end' => '8 years'), true);
119		$this->assertEqual($result, '2 months, 2 weeks');
120
121		$result = $this->Time->timeAgoInWords(strtotime('2 months, 1 week, 6 days'), array('end' => '8 years'), true);
122		$this->assertEqual($result, '2 months, 1 week, 6 days');
123
124		$result = $this->Time->timeAgoInWords(strtotime('1 month, 1 week, 6 days'), array('end' => '8 years'), true);
125		$this->assertEqual($result, '1 month, 1 week, 6 days');
126
127		for($i = 0; $i < 200; $i ++) {
128			$years = mt_rand(0, 3);
129			$months = mt_rand(0, 11);
130			$weeks = mt_rand(0, 3);
131			$days = mt_rand(0, 6);
132			$hours = 0;
133			$minutes = 0;
134			$seconds = 0;
135			$relative_date = '';
136
137			if ($years > 0) {
138				// years and months and days
139				$relative_date .= ($relative_date ? ', -' : '-') . $years . ' year' . ($years > 1 ? 's' : '');
140				$relative_date .= $months > 0 ? ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '') : '';
141				$relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
142				$relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
143			} elseif (abs($months) > 0) {
144				// months, weeks and days
145				$relative_date .= ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '');
146				$relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
147				$relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
148			} elseif (abs($weeks) > 0) {
149				// weeks and days
150				$relative_date .= ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
151				$relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
152			} elseif (abs($days) > 0) {
153				// days and hours
154				$relative_date .= ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '');
155				$relative_date .= $hours > 0 ? ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
156			} elseif (abs($hours) > 0) {
157				// hours and minutes
158				$relative_date .= ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '');
159				$relative_date .= $minutes > 0 ? ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
160			} elseif (abs($minutes) > 0) {
161				// minutes only
162				$relative_date .= ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
163			} else {
164				// seconds only
165				$relative_date .= ($relative_date ? ', -' : '-') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
166			}
167
168			if (date('j/n/y', strtotime($relative_date)) != '1/1/70') {
169				$result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true);
170				if ($relative_date == '0 seconds') {
171					$relative_date = '0 seconds ago';
172				}
173
174				$relative_date = str_replace('-', '', $relative_date) . ' ago';
175				$this->assertEqual($result, $relative_date);
176
177			}
178		}
179
180		for ($i = 0; $i < 200; $i ++) {
181			$years = mt_rand(0, 3);
182			$months = mt_rand(0, 11);
183			$weeks = mt_rand(0, 3);
184			$days = mt_rand(0, 6);
185			$hours = 0;
186			$minutes = 0;
187			$seconds = 0;
188
189			$relative_date = '';
190
191			if ($years > 0) {
192				// years and months and days
193				$relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : '');
194				$relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : '';
195				$relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
196				$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
197			} elseif (abs($months) > 0) {
198				// months, weeks and days
199				$relative_date .= ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '');
200				$relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
201				$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
202			} elseif (abs($weeks) > 0) {
203				// weeks and days
204				$relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
205				$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
206			} elseif (abs($days) > 0) {
207				// days and hours
208				$relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
209				$relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
210			} elseif (abs($hours) > 0) {
211				// hours and minutes
212				$relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
213				$relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
214			} elseif (abs($minutes) > 0) {
215				// minutes only
216				$relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
217			} else {
218				// seconds only
219				$relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
220			}
221
222			if (date('j/n/y', strtotime($relative_date)) != '1/1/70') {
223				// echo $relative_date."<br />";
224				$result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true);
225				if ($relative_date == '0 seconds') {
226					$relative_date = '0 seconds ago';
227				}
228
229				$relative_date = str_replace('-', '', $relative_date) . '';
230				$this->assertEqual($result, $relative_date);
231			}
232		}
233
234		$result = $this->Time->timeAgoInWords(strtotime('-2 years, -5 months, -2 days'), array('end' => '3 years'), true);
235		$this->assertEqual($result, '2 years, 5 months, 2 days ago');
236
237		$result = $this->Time->timeAgoInWords('2007-9-25');
238		$this->assertEqual($result, 'on 25/9/07');
239
240		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
241		$this->assertEqual($result, 'on 2007-09-25');
242
243		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true);
244		$this->assertEqual($result, 'on 2007-09-25');
245
246		$result = $this->Time->timeAgoInWords(strtotime('-2 weeks, -2 days'), 'Y-m-d', false);
247		$this->assertEqual($result, '2 weeks, 2 days ago');
248
249		$result = $this->Time->timeAgoInWords(strtotime('2 weeks, 2 days'), 'Y-m-d', true);
250		$this->assertPattern('/^2 weeks, [1|2] day(s)?$/', $result);
251
252		$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '1 month'));
253		$this->assertEqual($result, 'on ' . date('j/n/y', strtotime('2 months, 2 days')));
254
255		$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '3 month'));
256		$this->assertPattern('/2 months/', $result);
257
258		$result = $this->Time->timeAgoInWords(strtotime('2 months, 12 days'), array('end' => '3 month'));
259		$this->assertPattern('/2 months, 1 week/', $result);
260
261		$result = $this->Time->timeAgoInWords(strtotime('3 months, 5 days'), array('end' => '4 month'));
262		$this->assertEqual($result, '3 months, 5 days');
263
264		$result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '3 month'));
265		$this->assertEqual($result, '2 months, 2 days ago');
266
267		$result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '3 month'));
268		$this->assertEqual($result, '2 months, 2 days ago');
269
270		$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '3 month'));
271		$this->assertPattern('/2 months/', $result);
272
273		$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
274		$this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('2 months, 2 days')));
275
276		$result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
277		$this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months, -2 days')));
278
279		$result = $this->Time->timeAgoInWords(strtotime('-13 months, -5 days'), array('end' => '2 years'));
280		$this->assertEqual($result, '1 year, 1 month, 5 days ago');
281
282		$fourHours = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => -4));
283		$result = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => 4));
284		$this->assertEqual($fourHours, $result);
285
286		$result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
287		$expected = '2 hours ago';
288		$this->assertEqual($expected, $result);
289
290		$result = $this->Time->timeAgoInWords(strtotime('-12 minutes'));
291		$expected = '12 minutes ago';
292		$this->assertEqual($expected, $result);
293
294		$result = $this->Time->timeAgoInWords(strtotime('-12 seconds'));
295		$expected = '12 seconds ago';
296		$this->assertEqual($expected, $result);
297
298		$time = strtotime('-3 years -12 months');
299		$result = $this->Time->timeAgoInWords($time);
300		$expected = 'on ' . date('j/n/y', $time);
301		$this->assertEqual($expected, $result);
302	}
303
304/**
305 * testRelative method
306 *
307 * @access public
308 * @return void
309 */
310	function testRelative() {
311		$result = $this->Time->relativeTime('-1 week');
312		$this->assertEqual($result, '1 week ago');
313		$result = $this->Time->relativeTime('+1 week');
314		$this->assertEqual($result, '1 week');
315	}
316
317/**
318 * testNice method
319 *
320 * @access public
321 * @return void
322 */
323	function testNice() {
324		$time = time() + 2 * DAY;
325		$this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
326
327		$time = time() - 2 * DAY;
328		$this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
329
330		$time = time();
331		$this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
332
333		$time = 0;
334		$this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
335
336		$time = null;
337		$this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
338	}
339
340/**
341 * testNiceShort method
342 *
343 * @access public
344 * @return void
345 */
346	function testNiceShort() {
347		$time = time() + 2 * DAY;
348		if (date('Y', $time) == date('Y')) {
349			$this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time));
350		} else {
351			$this->assertEqual(date('M jSY, H:i', $time), $this->Time->niceShort($time));
352		}
353
354		$time = time();
355		$this->assertEqual('Today, '.date('H:i', $time), $this->Time->niceShort($time));
356
357		$time = time() - DAY;
358		$this->assertEqual('Yesterday, '.date('H:i', $time), $this->Time->niceShort($time));
359	}
360
361/**
362 * testDaysAsSql method
363 *
364 * @access public
365 * @return void
366 */
367	function testDaysAsSql() {
368		$begin = time();
369		$end = time() + DAY;
370		$field = 'my_field';
371		$expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')';
372		$this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field));
373	}
374
375/**
376 * testDayAsSql method
377 *
378 * @access public
379 * @return void
380 */
381	function testDayAsSql() {
382		$time = time();
383		$field = 'my_field';
384		$expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')';
385		$this->assertEqual($expected, $this->Time->dayAsSql($time, $field));
386	}
387
388/**
389 * testToUnix method
390 *
391 * @access public
392 * @return void
393 */
394	function testToUnix() {
395		$this->assertEqual(time(), $this->Time->toUnix(time()));
396		$this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
397		$this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
398		$this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
399		$this->assertEqual(false, $this->Time->toUnix(''));
400		$this->assertEqual(false, $this->Time->toUnix(null));
401	}
402
403/**
404 * testToAtom method
405 *
406 * @access public
407 * @return void
408 */
409	function testToAtom() {
410		$this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
411	}
412
413/**
414 * testToRss method
415 *
416 * @access public
417 * @return void
418 */
419	function testToRss() {
420		$this->assertEqual(date('r'), $this->Time->toRss(time()));
421	}
422
423/**
424 * testFormat method
425 *
426 * @access public
427 * @return void
428 */
429	function testFormat() {
430		$format = 'D-M-Y';
431		$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
432		foreach ($arr as $val) {
433			$this->assertEqual(date($format, $val), $this->Time->format($format, $val));
434		}
435
436		$result = $this->Time->format('Y-m-d', null, 'never');
437		$this->assertEqual($result, 'never');
438	}
439
440/**
441 * testOfGmt method
442 *
443 * @access public
444 * @return void
445 */
446	function testGmt() {
447		$hour = 3;
448		$min = 4;
449		$sec = 2;
450		$month = 5;
451		$day = 14;
452		$year = 2007;
453		$time = mktime($hour, $min, $sec, $month, $day, $year);
454		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
455		$this->assertEqual($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
456
457		$hour = date('H');
458		$min = date('i');
459		$sec = date('s');
460		$month = date('m');
461		$day = date('d');
462		$year = date('Y');
463		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
464		$this->assertEqual($expected, $this->Time->gmt(null));
465	}
466
467/**
468 * testIsToday method
469 *
470 * @access public
471 * @return void
472 */
473	function testIsToday() {
474		$result = $this->Time->isToday('+1 day');
475		$this->assertFalse($result);
476		$result = $this->Time->isToday('+1 days');
477		$this->assertFalse($result);
478		$result = $this->Time->isToday('+0 day');
479		$this->assertTrue($result);
480		$result = $this->Time->isToday('-1 day');
481		$this->assertFalse($result);
482	}
483
484/**
485 * testIsThisWeek method
486 *
487 * @access public
488 * @return void
489 */
490	function testIsThisWeek() {
491		switch (date('D')) {
492			case 'Mon' :
493				for ($i = 0; $i < 6; $i++) {
494					$this->assertTrue($this->Time->isThisWeek("+$i days"));
495				}
496				$this->assertFalse($this->Time->isThisWeek("+7 days"));
497				$this->assertFalse($this->Time->isThisWeek("-1 days"));
498				break;
499			case 'Tue' :
500				for ($i = -1; $i < 5; $i++) {
501					$this->assertTrue($this->Time->isThisWeek("+$i days"));
502				}
503				$this->assertFalse($this->Time->isThisWeek("+6 days"));
504				$this->assertFalse($this->Time->isThisWeek("-2 days"));
505				break;
506			case 'Wed' :
507				for ($i = -2; $i < 5; $i++) {
508					$this->assertTrue($this->Time->isThisWeek("+$i days"));
509				}
510				$this->assertFalse($this->Time->isThisWeek("+5 days"));
511				$this->assertFalse($this->Time->isThisWeek("-3 days"));
512				break;
513			case 'Thu' :
514				for ($i = -3; $i < 4; $i++) {
515					$this->assertTrue($this->Time->isThisWeek("+$i days"));
516				}
517				$this->assertFalse($this->Time->isThisWeek("+4 days"));
518				$this->assertFalse($this->Time->isThisWeek("-4 days"));
519				break;
520			case 'Fri' :
521				for ($i = -4; $i < 3; $i++) {
522					$this->assertTrue($this->Time->isThisWeek("+$i days"));
523				}
524				$this->assertFalse($this->Time->isThisWeek("+3 days"));
525				$this->assertFalse($this->Time->isThisWeek("-5 days"));
526				break;
527			case 'Sat' :
528				for ($i = -5; $i < 2; $i++) {
529					$this->assertTrue($this->Time->isThisWeek("+$i days"));
530				}
531				$this->assertFalse($this->Time->isThisWeek("+2 days"));
532				$this->assertFalse($this->Time->isThisWeek("-6 days"));
533				break;
534			case 'Sun' :
535				for ($i = -6; $i < 1; $i++) {
536					$this->assertTrue($this->Time->isThisWeek("+$i days"));
537				}
538				$this->assertFalse($this->Time->isThisWeek("+1 days"));
539				$this->assertFalse($this->Time->isThisWeek("-7 days"));
540				break;
541		}
542	}
543
544/**
545 * testIsThisMonth method
546 *
547 * @access public
548 * @return void
549 */
550	function testIsThisMonth() {
551		$result = $this->Time->isThisMonth('+0 day');
552		$this->assertTrue($result);
553		$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
554		$this->assertTrue($result);
555		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
556		$this->assertFalse($result);
557		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
558		$this->assertFalse($result);
559
560	}
561
562/**
563 * testIsThisYear method
564 *
565 * @access public
566 * @return void
567 */
568	function testIsThisYear() {
569		$result = $this->Time->isThisYear('+0 day');
570		$this->assertTrue($result);
571		$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
572		$this->assertTrue($result);
573	}
574	/**
575 * testWasYesterday method
576 *
577 * @access public
578 * @return void
579 */
580	function testWasYesterday() {
581		$result = $this->Time->wasYesterday('+1 day');
582		$this->assertFalse($result);
583		$result = $this->Time->wasYesterday('+1 days');
584		$this->assertFalse($result);
585		$result = $this->Time->wasYesterday('+0 day');
586		$this->assertFalse($result);
587		$result = $this->Time->wasYesterday('-1 day');
588		$this->assertTrue($result);
589		$result = $this->Time->wasYesterday('-1 days');
590		$this->assertTrue($result);
591		$result = $this->Time->wasYesterday('-2 days');
592		$this->assertFalse($result);
593	}
594	/**
595 * testIsTomorrow method
596 *
597 * @access public
598 * @return void
599 */
600	function testIsTomorrow() {
601		$result = $this->Time->isTomorrow('+1 day');
602		$this->assertTrue($result);
603		$result = $this->Time->isTomorrow('+1 days');
604		$this->assertTrue($result);
605		$result = $this->Time->isTomorrow('+0 day');
606		$this->assertFalse($result);
607		$result = $this->Time->isTomorrow('-1 day');
608		$this->assertFalse($result);
609	}
610
611/**
612 * testWasWithinLast method
613 *
614 * @access public
615 * @return void
616 */
617	function testWasWithinLast() {
618		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
619		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
620		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
621		$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
622		$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
623		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
624		$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
625		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
626
627		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
628		$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
629		$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
630		$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
631		$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
632		$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
633		$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
634		$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
635
636		$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
637		$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
638		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
639		$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
640		$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
641		$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
642
643		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
644		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
645		$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
646		$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
647		$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
648		$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
649		$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
650
651		$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
652		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
653		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
654		$this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
655	}
656	/**
657 * testUserOffset method
658 *
659 * @access public
660 * @return void
661 */
662	function testUserOffset() {
663		$timezoneServer = new DateTimeZone(date_default_timezone_get());
664		$timeServer = new DateTime('now', $timezoneServer);
665		$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
666
667		$expected = time();
668		$result = $this->Time->fromString(time(), $yourTimezone);
669		$this->assertEqual($result, $expected);
670	}
671
672/**
673 * test fromString()
674 *
675 * @access public
676 * @return void
677 */
678	function testFromString() {
679		$result = $this->Time->fromString('');
680		$this->assertFalse($result);
681
682		$result = $this->Time->fromString(0, 0);
683		$this->assertFalse($result);
684
685		$result = $this->Time->fromString('+1 hour');
686		$expected = strtotime('+1 hour');
687		$this->assertEqual($result, $expected);
688
689		$timezone = date('Z', time());
690		$result = $this->Time->fromString('+1 hour', $timezone);
691		$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
692		$this->assertEqual($result, $expected);
693	}
694}
695?>