MWTimestampTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Tests timestamp parsing and output.
  4. */
  5. class MWTimestampTest extends MediaWikiLangTestCase {
  6. protected function setUp() {
  7. parent::setUp();
  8. // Avoid 'GetHumanTimestamp' hook and others
  9. $this->setMwGlobals( 'wgHooks', [] );
  10. }
  11. /**
  12. * @dataProvider provideHumanTimestampTests
  13. * @covers MWTimestamp::getHumanTimestamp
  14. */
  15. public function testHumanTimestamp(
  16. $tsTime, // The timestamp to format
  17. $currentTime, // The time to consider "now"
  18. $timeCorrection, // The time offset to use
  19. $dateFormat, // The date preference to use
  20. $expectedOutput, // The expected output
  21. $desc // Description
  22. ) {
  23. $user = $this->createMock( User::class );
  24. $user->expects( $this->any() )
  25. ->method( 'getOption' )
  26. ->with( 'timecorrection' )
  27. ->will( $this->returnValue( $timeCorrection ) );
  28. $user->expects( $this->any() )
  29. ->method( 'getDatePreference' )
  30. ->will( $this->returnValue( $dateFormat ) );
  31. $tsTime = new MWTimestamp( $tsTime );
  32. $currentTime = new MWTimestamp( $currentTime );
  33. $this->assertEquals(
  34. $expectedOutput,
  35. $tsTime->getHumanTimestamp( $currentTime, $user ),
  36. $desc
  37. );
  38. }
  39. public static function provideHumanTimestampTests() {
  40. return [
  41. [
  42. '20111231170000',
  43. '20120101000000',
  44. 'Offset|0',
  45. 'mdy',
  46. 'Yesterday at 17:00',
  47. '"Yesterday" across years',
  48. ],
  49. [
  50. '20120717190900',
  51. '20120717190929',
  52. 'Offset|0',
  53. 'mdy',
  54. 'just now',
  55. '"Just now"',
  56. ],
  57. [
  58. '20120717190900',
  59. '20120717191530',
  60. 'Offset|0',
  61. 'mdy',
  62. '6 minutes ago',
  63. 'X minutes ago',
  64. ],
  65. [
  66. '20121006173100',
  67. '20121006173200',
  68. 'Offset|0',
  69. 'mdy',
  70. '1 minute ago',
  71. '"1 minute ago"',
  72. ],
  73. [
  74. '20120617190900',
  75. '20120717190900',
  76. 'Offset|0',
  77. 'mdy',
  78. 'June 17',
  79. 'Another month'
  80. ],
  81. [
  82. '19910130151500',
  83. '20120716193700',
  84. 'Offset|0',
  85. 'mdy',
  86. '15:15, January 30, 1991',
  87. 'Different year',
  88. ],
  89. [
  90. '20120101050000',
  91. '20120101080000',
  92. 'Offset|-360',
  93. 'mdy',
  94. 'Yesterday at 23:00',
  95. '"Yesterday" across years with time correction',
  96. ],
  97. [
  98. '20120714184300',
  99. '20120716184300',
  100. 'Offset|-420',
  101. 'mdy',
  102. 'Saturday at 11:43',
  103. 'Recent weekday with time correction',
  104. ],
  105. [
  106. '20120714184300',
  107. '20120715040000',
  108. 'Offset|-420',
  109. 'mdy',
  110. '11:43',
  111. 'Today at another time with time correction',
  112. ],
  113. [
  114. '20120617190900',
  115. '20120717190900',
  116. 'Offset|0',
  117. 'dmy',
  118. '17 June',
  119. 'Another month with dmy'
  120. ],
  121. [
  122. '20120617190900',
  123. '20120717190900',
  124. 'Offset|0',
  125. 'ISO 8601',
  126. '06-17',
  127. 'Another month with ISO-8601'
  128. ],
  129. [
  130. '19910130151500',
  131. '20120716193700',
  132. 'Offset|0',
  133. 'ISO 8601',
  134. '1991-01-30T15:15:00',
  135. 'Different year with ISO-8601',
  136. ],
  137. ];
  138. }
  139. /**
  140. * @dataProvider provideRelativeTimestampTests
  141. * @covers MWTimestamp::getRelativeTimestamp
  142. */
  143. public function testRelativeTimestamp(
  144. $tsTime, // The timestamp to format
  145. $currentTime, // The time to consider "now"
  146. $timeCorrection, // The time offset to use
  147. $dateFormat, // The date preference to use
  148. $expectedOutput, // The expected output
  149. $desc // Description
  150. ) {
  151. $user = $this->createMock( User::class );
  152. $user->expects( $this->any() )
  153. ->method( 'getOption' )
  154. ->with( 'timecorrection' )
  155. ->will( $this->returnValue( $timeCorrection ) );
  156. $tsTime = new MWTimestamp( $tsTime );
  157. $currentTime = new MWTimestamp( $currentTime );
  158. $this->assertEquals(
  159. $expectedOutput,
  160. $tsTime->getRelativeTimestamp( $currentTime, $user ),
  161. $desc
  162. );
  163. }
  164. public static function provideRelativeTimestampTests() {
  165. return [
  166. [
  167. '20111231170000',
  168. '20120101000000',
  169. 'Offset|0',
  170. 'mdy',
  171. '7 hours ago',
  172. '"Yesterday" across years',
  173. ],
  174. [
  175. '20120717190900',
  176. '20120717190929',
  177. 'Offset|0',
  178. 'mdy',
  179. '29 seconds ago',
  180. '"Just now"',
  181. ],
  182. [
  183. '20120717190900',
  184. '20120717191530',
  185. 'Offset|0',
  186. 'mdy',
  187. '6 minutes and 30 seconds ago',
  188. 'Combination of multiple units',
  189. ],
  190. [
  191. '20121006173100',
  192. '20121006173200',
  193. 'Offset|0',
  194. 'mdy',
  195. '1 minute ago',
  196. '"1 minute ago"',
  197. ],
  198. [
  199. '19910130151500',
  200. '20120716193700',
  201. 'Offset|0',
  202. 'mdy',
  203. '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
  204. 'A long time ago',
  205. ],
  206. [
  207. '20120101050000',
  208. '20120101080000',
  209. 'Offset|-360',
  210. 'mdy',
  211. '3 hours ago',
  212. '"Yesterday" across years with time correction',
  213. ],
  214. [
  215. '20120714184300',
  216. '20120716184300',
  217. 'Offset|-420',
  218. 'mdy',
  219. '2 days ago',
  220. 'Recent weekday with time correction',
  221. ],
  222. [
  223. '20120714184300',
  224. '20120715040000',
  225. 'Offset|-420',
  226. 'mdy',
  227. '9 hours and 17 minutes ago',
  228. 'Today at another time with time correction',
  229. ],
  230. ];
  231. }
  232. }