DatetimeHelperTest.php 7.2 KB

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