| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- App::uses('DatetimeHelper', 'Tools.View/Helper');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- App::uses('View', 'View');
- /**
- * Datetime Test Case
- *
- */
- class DatetimeHelperTest extends MyCakeTestCase {
- public function setUp() {
- parent::setUp();
- $this->Datetime = new DatetimeHelper(new View(null));
- }
- /**
- * test user age
- *
- * 2012-04-10 ms
- */
- public function testUserAge() {
- $res = $this->Datetime->userAge('2010-01-01');
- $this->assertTrue($res >= 2);
- }
- /**
- * test cweek
- *
- * 2009-03-11 ms
- */
- public function testLengthOfTime() {
- $this->assertEquals('6 '.__('Minutes').', 40 '.__('Seconds'), $this->Datetime->lengthOfTime(400));
- $res = $this->Datetime->lengthOfTime(400, 'i');
- //pr($res);
- $this->assertEquals('6 '.__('Minutes'), $res);
- $res = $this->Datetime->lengthOfTime(6*DAY);
- //pr($res);
- $this->assertEquals('6 '.__('Days').', 0 '.__('Hours'), $res);
- #TODO: more
- }
- public function testRelLengthOfTime() {
- //echo $this->_header(__FUNCTION__);
- $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()-3600));
- //pr($res);
- $this->assertTrue(!empty($res));
- $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()-4*DAY-5*HOUR), null, array('plural'=>'n'));
- //pr($res);
- //$this->assertEquals($res, 'Vor 4 Tagen, 5 '.__('Hours'));
- $this->assertEquals(__('%s ago', '4 '.__('Days').', '.'5 '.__('Hours')), $res);
- $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()+4*DAY+5*HOUR), null, array('plural'=>'n'));
- //pr($res);
- $this->assertEquals(__('In %s', '4 '.__('Days').', '.'5 '.__('Hours')), $res);
- $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()), null, array('plural'=>'n'));
- //pr($res);
- $this->assertEquals($res, __('justNow'));
- }
- // Cake internal function...
- public function testTimeAgoInWords() {
- //echo $this->_header(__FUNCTION__);
- $res = $this->Datetime->timeAgoInWords(date(FORMAT_DB_DATETIME, time()-4*DAY-5*HOUR));
- //pr($res);
- }
- public function testIsInRange() {
- //echo $this->_header(__FUNCTION__);
- $day = date(FORMAT_DB_DATETIME, time()+10*DAY);
- $this->assertTrue($this->Datetime->isInRange($day, 11*DAY));
- $this->assertTrue($this->Datetime->isInRange($day, 10*DAY));
- $this->assertFalse($this->Datetime->isInRange($day, 9*DAY));
- $day = date(FORMAT_DB_DATETIME, time()-78*DAY);
- $this->assertTrue($this->Datetime->isInRange($day, 79*DAY));
- $this->assertTrue($this->Datetime->isInRange($day, 78*DAY));
- $this->assertFalse($this->Datetime->isInRange($day, 77*DAY));
- #TODO: more
- }
- /**
- * test cweek
- *
- * @access public
- * @return void
- * 2009-03-11 ms
- */
- public function testCweek() {
- $year = 2008;
- $month = 12;
- $day = 29;
- $date = mktime(0,0,0, $month, $day, $year);
- $this->assertEquals('01/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
- $year = 2009;
- $month = 1;
- $day = 1;
- $date = mktime(0,0,0, $month, $day, $year);
- $this->assertEquals('01/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
- $year = 2009;
- $month = 1;
- $day = 9;
- $date = mktime(0,0,0, $month, $day, $year);
- $this->assertEquals('02/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day.' 00:00:00'));
- $year = 2009;
- $month = 12;
- $day = 26;
- $date = mktime(0,0,0, $month, $day, $year);
- $this->assertEquals('52/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
- }
- /**
- * test age
- *
- * @access public
- * @return void
- * 2009-03-11 ms
- */
- public function testAge() {
- list($year, $month, $day) = explode('-',date('Y-m-d'));
- $this->assertEquals('0', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
- list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years')));
- $this->assertEquals('10', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
- list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years +1 day')));
- $this->assertEquals('9', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
- list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years -1 day')));
- $this->assertEquals('10', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
- # jahresübertritt
- list($year, $month, $day) = explode('-','2005-12-01');
- list($yearE, $monthE, $dayE) = explode('-','2008-02-29');
- $this->assertEquals('2', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
- list($year, $month, $day) = explode('-','2002-01-29');
- list($yearE, $monthE, $dayE) = explode('-','2008-12-02');
- $this->assertEquals('6', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
- # schaltjahr
- list($year, $month, $day) = explode('-','2005-02-29');
- list($yearE, $monthE, $dayE) = explode('-','2008-03-01');
- $this->assertEquals('3', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
- list($year, $month, $day) = explode('-','2005-03-01');
- list($yearE, $monthE, $dayE) = explode('-','2008-02-29');
- $this->assertEquals('2', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
- #zukunft
- list($yearE, $monthE, $dayE) = explode('-',date('Y-m-d',strtotime('+10 years -1 day')));
- $this->assertEquals('9', $this->Datetime->age(null, $yearE.'-'.$monthE.'-'.$dayE));
- list($yearE, $monthE, $dayE) = explode('-',date('Y-m-d',strtotime('+10 years +1 day')));
- $this->assertEquals('10', $this->Datetime->age(null, $yearE.'-'.$monthE.'-'.$dayE));
- $birthday = '1985-04-08';
- $relativeDate = '2010-04-07';
- $this->assertEquals('24', $this->Datetime->age($birthday, $relativeDate));
- $relativeDate = '2010-04-08';
- $this->assertEquals('25', $this->Datetime->age($birthday, $relativeDate));
- $relativeDate = '2010-04-09';
- $this->assertEquals('25', $this->Datetime->age($birthday, $relativeDate));
- }
- /**
- * test IsInTheFuture
- *
- * @access public
- * @return void
- * 2010-02-18 ms
- */
- public function testIsInTheFuture() {
- $testDate = date(FORMAT_DB_DATE, time()+2*DAY);
- $is = $this->Datetime->isInTheFuture($testDate);
- $this->assertTrue($is);
- $testDate = date(FORMAT_DB_DATETIME, time()-1*MINUTE);
- $is = $this->Datetime->isInTheFuture($testDate);
- $this->assertFalse($is);
- }
- /**
- * test IsNotTodayAndInTheFuture
- *
- * @access public
- * @return void
- * 2010-02-18 ms
- */
- public function testIsNotTodayAndInTheFuture() {
- $testDate = date(FORMAT_DB_DATE, time());
- $is = $this->Datetime->isNotTodayAndInTheFuture($testDate);
- $this->assertFalse($is);
- $testDate = date(FORMAT_DB_DATETIME, time()+1*DAY);
- $is = $this->Datetime->isNotTodayAndInTheFuture($testDate);
- $this->assertTrue($is);
- }
- /**
- * test IsDayAfterTomorrow
- *
- * @access public
- * @return void
- * 2010-02-18 ms
- */
- public function testIsDayAfterTomorrow() {
- $testDate = date(FORMAT_DB_DATE, time()+2*DAY);
- $is = $this->Datetime->isDayAfterTomorrow($testDate);
- $this->assertTrue($is);
- $testDate = date(FORMAT_DB_DATETIME, time()-1*MINUTE);
- $is = $this->Datetime->isDayAfterTomorrow($testDate);
- $this->assertFalse($is);
- }
- /**
- * tearDown method
- *
- * @access public
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- unset($this->Datetime);
- }
- }
|