DatetimeHelperTest.php 7.1 KB

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