DateTimeFormatterTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Joas Schilling nickvergessen@owncloud.com
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. class DateTimeFormatterTest extends TestCase {
  10. /** @var \OC\DateTimeFormatter */
  11. protected $formatter;
  12. static protected $oneMinute = 60;
  13. static protected $oneHour = 3600;
  14. static protected $oneDay;
  15. static protected $oneYear;
  16. static protected $defaultTimeZone;
  17. public static function setUpBeforeClass() {
  18. parent::setUpBeforeClass();
  19. self::$defaultTimeZone = date_default_timezone_get();
  20. date_default_timezone_set('UTC');
  21. self::$oneDay = self::$oneHour * 24;
  22. self::$oneYear = self::$oneDay * 365;
  23. }
  24. public static function tearDownAfterClass() {
  25. date_default_timezone_set(self::$defaultTimeZone);
  26. parent::tearDownAfterClass();
  27. }
  28. protected function setUp() {
  29. parent::setUp();
  30. $this->formatter = new \OC\DateTimeFormatter(new \DateTimeZone('UTC'), \OC::$server->getL10N('lib', 'en'));
  31. }
  32. protected function getTimestampAgo($time, $seconds = 0, $minutes = 0, $hours = 0, $days = 0, $years = 0) {
  33. return $time - $seconds - $minutes * 60 - $hours * 3600 - $days * 24*3600 - $years * 365*24*3600;
  34. }
  35. public function formatTimeSpanData() {
  36. $time = 1416916800; // Use a fixed timestamp so we don't switch days/years with the getTimestampAgo
  37. $deL10N = \OC::$server->getL10N('lib', 'de');
  38. return array(
  39. array('seconds ago', $time, $time),
  40. array('in a few seconds', $time + 5 , $time),
  41. array('1 minute ago', $this->getTimestampAgo($time, 30, 1), $time),
  42. array('15 minutes ago', $this->getTimestampAgo($time, 30, 15), $time),
  43. array('in 15 minutes', $time, $this->getTimestampAgo($time, 30, 15)),
  44. array('1 hour ago', $this->getTimestampAgo($time, 30, 15, 1), $time),
  45. array('3 hours ago', $this->getTimestampAgo($time, 30, 15, 3), $time),
  46. array('in 3 hours', $time, $this->getTimestampAgo($time, 30, 15, 3)),
  47. array('4 days ago', $this->getTimestampAgo($time, 30, 15, 3, 4), $time),
  48. array('seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  49. array('seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  50. array('1 minute ago', new \DateTime('Wed, 02 Oct 2013 23:58:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  51. array('3 minutes ago', new \DateTime('Wed, 02 Oct 2013 23:56:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  52. array('59 minutes ago', new \DateTime('Wed, 02 Oct 2013 23:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  53. array('1 hour ago', new \DateTime('Wed, 02 Oct 2013 22:59:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  54. array('3 hours ago', new \DateTime('Wed, 02 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  55. array('yesterday', new \DateTime('Tue, 01 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  56. array('2 days ago', new \DateTime('Mon, 30 Sep 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  57. array($deL10N->t('seconds ago'), new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
  58. array($deL10N->n('%n minute ago', '%n minutes ago', 1), new \DateTime('Wed, 02 Oct 2013 23:58:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
  59. array($deL10N->n('%n minute ago', '%n minutes ago', 3), new \DateTime('Wed, 02 Oct 2013 23:56:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
  60. array($deL10N->n('%n hour ago', '%n hours ago', 1), new \DateTime('Wed, 02 Oct 2013 22:59:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
  61. array($deL10N->n('%n hour ago', '%n hours ago', 3), new \DateTime('Wed, 02 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
  62. array($deL10N->n('%n day ago', '%n days ago', 2), new \DateTime('Mon, 30 Sep 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
  63. );
  64. }
  65. /**
  66. * @dataProvider formatTimeSpanData
  67. */
  68. public function testFormatTimeSpan($expected, $timestamp, $compare, $locale = null) {
  69. $this->assertEquals((string) $expected, (string) $this->formatter->formatTimeSpan($timestamp, $compare, $locale));
  70. }
  71. public function formatDateSpanData() {
  72. $time = 1416916800; // Use a fixed timestamp so we don't switch days/years with the getTimestampAgo
  73. $deL10N = \OC::$server->getL10N('lib', 'de');
  74. return array(
  75. // Normal testing
  76. array('today', $this->getTimestampAgo($time, 30, 15), $time),
  77. array('yesterday', $this->getTimestampAgo($time, 0, 0, 0, 1), $time),
  78. array('tomorrow', $time, $this->getTimestampAgo($time, 0, 0, 0, 1)),
  79. array('4 days ago', $this->getTimestampAgo($time, 0, 0, 0, 4), $time),
  80. array('in 4 days', $time, $this->getTimestampAgo($time, 0, 0, 0, 4)),
  81. array('5 months ago', $this->getTimestampAgo($time, 0, 0, 0, 155), $time),
  82. array('next month', $time, $this->getTimestampAgo($time, 0, 0, 0, 32)),
  83. array('in 5 months', $time, $this->getTimestampAgo($time, 0, 0, 0, 155)),
  84. array('2 years ago', $this->getTimestampAgo($time, 0, 0, 0, 0, 2), $time),
  85. array('next year', $time, $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
  86. array('in 2 years', $time, $this->getTimestampAgo($time, 0, 0, 0, 0, 2)),
  87. // Test with compare timestamp
  88. array('today', $this->getTimestampAgo($time, 0, 0, 0, 0, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
  89. array('yesterday', $this->getTimestampAgo($time, 30, 15, 3, 1, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
  90. array('4 days ago', $this->getTimestampAgo($time, 30, 15, 3, 4, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
  91. array('5 months ago', $this->getTimestampAgo($time, 30, 15, 3, 155, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
  92. array('2 years ago', $this->getTimestampAgo($time, 30, 15, 3, 35, 3), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
  93. // Test translations
  94. array($deL10N->t('today'), new \DateTime('Wed, 02 Oct 2013 12:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
  95. array($deL10N->t('yesterday'), new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000'), $deL10N),
  96. array($deL10N->n('%n day ago', '%n days ago', 2), new \DateTime('Mon, 30 Sep 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000'), $deL10N),
  97. array($deL10N->n('%n month ago', '%n months ago', 9), new \DateTime('Tue, 31 Dec 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000'), $deL10N),
  98. array($deL10N->n('%n year ago', '%n years ago', 2), new \DateTime('Sun, 01 Jan 2012 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000'), $deL10N),
  99. // Test time
  100. array('today', new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  101. array('today', new \DateTime('Wed, 02 Oct 2013 12:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  102. array('today', new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  103. // Test some special yesterdays
  104. array('yesterday', new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000')),
  105. array('yesterday', new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  106. array('yesterday', new \DateTime('Tue, 01 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000')),
  107. array('yesterday', new \DateTime('Tue, 01 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
  108. array('yesterday', new \DateTime('Mon, 30 Sep 2013 00:00:00 +0000'), new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000')),
  109. array('yesterday', new \DateTime('Mon, 31 Dec 2012 00:00:00 +0000'), new \DateTime('Tue, 01 Jan 2013 00:00:00 +0000')),
  110. // Test last month
  111. array('2 days ago', new \DateTime('Mon, 30 Sep 2013 00:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000')),
  112. array('last month', new \DateTime('Mon, 30 Sep 2013 00:00:00 +0000'), new \DateTime('Tue, 31 Oct 2013 00:00:00 +0000')),
  113. array('last month', new \DateTime('Sun, 01 Sep 2013 00:00:00 +0000'), new \DateTime('Tue, 01 Oct 2013 00:00:00 +0000')),
  114. array('last month', new \DateTime('Sun, 01 Sep 2013 00:00:00 +0000'), new \DateTime('Thu, 31 Oct 2013 00:00:00 +0000')),
  115. // Test last year
  116. array('9 months ago', new \DateTime('Tue, 31 Dec 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')),
  117. array('11 months ago', new \DateTime('Thu, 03 Oct 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')),
  118. array('last year', new \DateTime('Wed, 02 Oct 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')),
  119. array('last year', new \DateTime('Tue, 01 Jan 2013 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')),
  120. array('2 years ago', new \DateTime('Sun, 01 Jan 2012 00:00:00 +0000'), new \DateTime('Thu, 02 Oct 2014 00:00:00 +0000')),
  121. );
  122. }
  123. /**
  124. * @dataProvider formatDateSpanData
  125. */
  126. public function testFormatDateSpan($expected, $timestamp, $compare = null, $locale = null) {
  127. $this->assertEquals((string) $expected, (string) $this->formatter->formatDateSpan($timestamp, $compare, $locale));
  128. }
  129. public function formatDateData() {
  130. return array(
  131. array(1102831200, 'December 12, 2004'),
  132. );
  133. }
  134. /**
  135. * @dataProvider formatDateData
  136. */
  137. public function testFormatDate($timestamp, $expected) {
  138. $this->assertEquals($expected, (string) $this->formatter->formatDate($timestamp));
  139. }
  140. public function formatDateTimeData() {
  141. return array(
  142. array(1350129205, null, 'October 13, 2012 at 11:53:25 AM GMT+0'),
  143. array(1350129205, new \DateTimeZone('Europe/Berlin'), 'October 13, 2012 at 1:53:25 PM GMT+2'),
  144. );
  145. }
  146. /**
  147. * @dataProvider formatDateTimeData
  148. */
  149. public function testFormatDateTime($timestamp, $timeZone, $expected) {
  150. $this->assertEquals($expected, (string) $this->formatter->formatDateTime($timestamp, 'long', 'long', $timeZone));
  151. }
  152. /**
  153. * @expectedException \Exception
  154. */
  155. function testFormatDateWithInvalidTZ() {
  156. $this->formatter->formatDate(1350129205, 'long', new \DateTimeZone('Mordor/Barad-dûr'));
  157. }
  158. }