DatetimeHelperTest.php 7.1 KB

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