DatetimeHelperTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. App::uses('DatetimeHelper', 'Tools.View/Helper');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. App::uses('View', 'View');
  5. /**
  6. * Datetime Test Case
  7. *
  8. * @package cake.tests
  9. * @subpackage cake.tests.cases.libs.view.helpers
  10. */
  11. class DatetimeHelperTest extends MyCakeTestCase {
  12. public function setUp() {
  13. parent::setUp();
  14. $this->Datetime = new DatetimeHelper(new View(null));
  15. }
  16. /**
  17. * test user age
  18. *
  19. * 2012-04-10 ms
  20. */
  21. public function testUserAge() {
  22. $res = $this->Datetime->userAge('2010-01-01');
  23. $this->assertTrue($res >= 2);
  24. }
  25. /**
  26. * test cweek
  27. *
  28. * 2009-03-11 ms
  29. */
  30. public function testLengthOfTime() {
  31. $this->assertEquals('6 '.__('Minutes').', 40 '.__('Seconds'), $this->Datetime->lengthOfTime(400));
  32. $res = $this->Datetime->lengthOfTime(400, 'i');
  33. //pr($res);
  34. $this->assertEquals('6 '.__('Minutes'), $res);
  35. $res = $this->Datetime->lengthOfTime(6*DAY);
  36. //pr($res);
  37. $this->assertEquals('6 '.__('Days').', 0 '.__('Hours'), $res);
  38. #TODO: more
  39. }
  40. public function testRelLengthOfTime() {
  41. //echo $this->_header(__FUNCTION__);
  42. $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()-3600));
  43. //pr($res);
  44. $this->assertTrue(!empty($res));
  45. $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()-4*DAY-5*HOUR), null, array('plural'=>'n'));
  46. //pr($res);
  47. //$this->assertEquals($res, 'Vor 4 Tagen, 5 '.__('Hours'));
  48. $this->assertEquals(__('%s ago', '4 '.__('Days').', '.'5 '.__('Hours')), $res);
  49. $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()+4*DAY+5*HOUR), null, array('plural'=>'n'));
  50. //pr($res);
  51. $this->assertEquals(__('In %s', '4 '.__('Days').', '.'5 '.__('Hours')), $res);
  52. $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()), null, array('plural'=>'n'));
  53. //pr($res);
  54. $this->assertEquals($res, __('justNow'));
  55. }
  56. // Cake internal function...
  57. public function testTimeAgoInWords() {
  58. //echo $this->_header(__FUNCTION__);
  59. $res = $this->Datetime->timeAgoInWords(date(FORMAT_DB_DATETIME, time()-4*DAY-5*HOUR));
  60. //pr($res);
  61. }
  62. public function testIsInRange() {
  63. //echo $this->_header(__FUNCTION__);
  64. $day = date(FORMAT_DB_DATETIME, time()+10*DAY);
  65. $this->assertTrue($this->Datetime->isInRange($day, 11*DAY));
  66. $this->assertTrue($this->Datetime->isInRange($day, 10*DAY));
  67. $this->assertFalse($this->Datetime->isInRange($day, 9*DAY));
  68. $day = date(FORMAT_DB_DATETIME, time()-78*DAY);
  69. $this->assertTrue($this->Datetime->isInRange($day, 79*DAY));
  70. $this->assertTrue($this->Datetime->isInRange($day, 78*DAY));
  71. $this->assertFalse($this->Datetime->isInRange($day, 77*DAY));
  72. #TODO: more
  73. }
  74. /**
  75. * test cweek
  76. *
  77. * @access public
  78. * @return void
  79. * 2009-03-11 ms
  80. */
  81. public function testCweek() {
  82. $year = 2008;
  83. $month = 12;
  84. $day = 29;
  85. $date = mktime(0,0,0, $month, $day, $year);
  86. $this->assertEquals('01/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  87. $year = 2009;
  88. $month = 1;
  89. $day = 1;
  90. $date = mktime(0,0,0, $month, $day, $year);
  91. $this->assertEquals('01/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  92. $year = 2009;
  93. $month = 1;
  94. $day = 9;
  95. $date = mktime(0,0,0, $month, $day, $year);
  96. $this->assertEquals('02/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day.' 00:00:00'));
  97. $year = 2009;
  98. $month = 12;
  99. $day = 26;
  100. $date = mktime(0,0,0, $month, $day, $year);
  101. $this->assertEquals('52/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  102. }
  103. /**
  104. * test age
  105. *
  106. * @access public
  107. * @return void
  108. * 2009-03-11 ms
  109. */
  110. public function testAge() {
  111. list($year, $month, $day) = explode('-',date('Y-m-d'));
  112. $this->assertEquals('0', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  113. list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years')));
  114. $this->assertEquals('10', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  115. list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years +1 day')));
  116. $this->assertEquals('9', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  117. list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years -1 day')));
  118. $this->assertEquals('10', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  119. # jahresübertritt
  120. list($year, $month, $day) = explode('-','2005-12-01');
  121. list($yearE, $monthE, $dayE) = explode('-','2008-02-29');
  122. $this->assertEquals('2', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  123. list($year, $month, $day) = explode('-','2002-01-29');
  124. list($yearE, $monthE, $dayE) = explode('-','2008-12-02');
  125. $this->assertEquals('6', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  126. # schaltjahr
  127. list($year, $month, $day) = explode('-','2005-02-29');
  128. list($yearE, $monthE, $dayE) = explode('-','2008-03-01');
  129. $this->assertEquals('3', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  130. list($year, $month, $day) = explode('-','2005-03-01');
  131. list($yearE, $monthE, $dayE) = explode('-','2008-02-29');
  132. $this->assertEquals('2', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  133. #zukunft
  134. list($yearE, $monthE, $dayE) = explode('-',date('Y-m-d',strtotime('+10 years -1 day')));
  135. $this->assertEquals('9', $this->Datetime->age(null, $yearE.'-'.$monthE.'-'.$dayE));
  136. list($yearE, $monthE, $dayE) = explode('-',date('Y-m-d',strtotime('+10 years +1 day')));
  137. $this->assertEquals('10', $this->Datetime->age(null, $yearE.'-'.$monthE.'-'.$dayE));
  138. $birthday = '1985-04-08';
  139. $relativeDate = '2010-04-07';
  140. $this->assertEquals('24', $this->Datetime->age($birthday, $relativeDate));
  141. $relativeDate = '2010-04-08';
  142. $this->assertEquals('25', $this->Datetime->age($birthday, $relativeDate));
  143. $relativeDate = '2010-04-09';
  144. $this->assertEquals('25', $this->Datetime->age($birthday, $relativeDate));
  145. }
  146. /**
  147. * test IsInTheFuture
  148. *
  149. * @access public
  150. * @return void
  151. * 2010-02-18 ms
  152. */
  153. public function testIsInTheFuture() {
  154. $testDate = date(FORMAT_DB_DATE, time()+2*DAY);
  155. $is = $this->Datetime->isInTheFuture($testDate);
  156. $this->assertTrue($is);
  157. $testDate = date(FORMAT_DB_DATETIME, time()-1*MINUTE);
  158. $is = $this->Datetime->isInTheFuture($testDate);
  159. $this->assertFalse($is);
  160. }
  161. /**
  162. * test IsNotTodayAndInTheFuture
  163. *
  164. * @access public
  165. * @return void
  166. * 2010-02-18 ms
  167. */
  168. public function testIsNotTodayAndInTheFuture() {
  169. $testDate = date(FORMAT_DB_DATE, time());
  170. $is = $this->Datetime->isNotTodayAndInTheFuture($testDate);
  171. $this->assertFalse($is);
  172. $testDate = date(FORMAT_DB_DATETIME, time()+1*DAY);
  173. $is = $this->Datetime->isNotTodayAndInTheFuture($testDate);
  174. $this->assertTrue($is);
  175. }
  176. /**
  177. * test IsDayAfterTomorrow
  178. *
  179. * @access public
  180. * @return void
  181. * 2010-02-18 ms
  182. */
  183. public function testIsDayAfterTomorrow() {
  184. $testDate = date(FORMAT_DB_DATE, time()+2*DAY);
  185. $is = $this->Datetime->isDayAfterTomorrow($testDate);
  186. $this->assertTrue($is);
  187. $testDate = date(FORMAT_DB_DATETIME, time()-1*MINUTE);
  188. $is = $this->Datetime->isDayAfterTomorrow($testDate);
  189. $this->assertFalse($is);
  190. }
  191. /**
  192. * tearDown method
  193. *
  194. * @access public
  195. * @return void
  196. */
  197. public function tearDown() {
  198. parent::tearDown();
  199. unset($this->Datetime);
  200. }
  201. }