Merge "Replace extract() with explicit variable definitions in DjVuImage"
[lhc/web/wiklou.git] / tests / phpunit / includes / MWTimestampTest.php
1 <?php
2
3 /**
4 * Tests timestamp parsing and output.
5 */
6 class MWTimestampTest extends MediaWikiLangTestCase {
7
8 protected function setUp() {
9 parent::setUp();
10
11 // Avoid 'GetHumanTimestamp' hook and others
12 $this->setMwGlobals( 'wgHooks', [] );
13 }
14
15 /**
16 * @covers MWTimestamp::__construct
17 */
18 public function testConstructWithNoTimestamp() {
19 $timestamp = new MWTimestamp();
20 $this->assertInternalType( 'string', $timestamp->getTimestamp() );
21 $this->assertNotEmpty( $timestamp->getTimestamp() );
22 $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW ) ) );
23 }
24
25 /**
26 * @covers MWTimestamp::__toString
27 */
28 public function testToString() {
29 $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108
30 $this->assertEquals( '1406833268', $timestamp->__toString() );
31 }
32
33 public static function provideValidTimestampDifferences() {
34 return [
35 [ '1406833268', '1406833269', '00 00 00 01' ],
36 [ '1406833268', '1406833329', '00 00 01 01' ],
37 [ '1406833268', '1406836929', '00 01 01 01' ],
38 [ '1406833268', '1406923329', '01 01 01 01' ],
39 ];
40 }
41
42 /**
43 * @dataProvider provideValidTimestampDifferences
44 * @covers MWTimestamp::diff
45 */
46 public function testDiff( $timestamp1, $timestamp2, $expected ) {
47 $timestamp1 = new MWTimestamp( $timestamp1 );
48 $timestamp2 = new MWTimestamp( $timestamp2 );
49 $diff = $timestamp1->diff( $timestamp2 );
50 $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) );
51 }
52
53 /**
54 * Test parsing of valid timestamps and outputing to MW format.
55 * @dataProvider provideValidTimestamps
56 * @covers MWTimestamp::getTimestamp
57 */
58 public function testValidParse( $format, $original, $expected ) {
59 $timestamp = new MWTimestamp( $original );
60 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
61 }
62
63 /**
64 * Test outputting valid timestamps to different formats.
65 * @dataProvider provideValidTimestamps
66 * @covers MWTimestamp::getTimestamp
67 */
68 public function testValidOutput( $format, $expected, $original ) {
69 $timestamp = new MWTimestamp( $original );
70 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
71 }
72
73 /**
74 * Test an invalid timestamp.
75 * @expectedException TimestampException
76 * @covers MWTimestamp
77 */
78 public function testInvalidParse() {
79 new MWTimestamp( "This is not a timestamp." );
80 }
81
82 /**
83 * Test an out of range timestamp
84 * @dataProvider provideOutOfRangeTimestamps
85 * @expectedException TimestampException
86 * @covers MWTimestamp
87 */
88 public function testOutOfRangeTimestamps( $format, $input ) {
89 $timestamp = new MWTimestamp( $input );
90 $timestamp->getTimestamp( $format );
91 }
92
93 /**
94 * Test requesting an invalid output format.
95 * @expectedException TimestampException
96 * @covers MWTimestamp::getTimestamp
97 */
98 public function testInvalidOutput() {
99 $timestamp = new MWTimestamp( '1343761268' );
100 $timestamp->getTimestamp( 98 );
101 }
102
103 /**
104 * Returns a list of valid timestamps in the format:
105 * [ type, timestamp_of_type, timestamp_in_MW ]
106 */
107 public static function provideValidTimestamps() {
108 return [
109 // Various formats
110 [ TS_UNIX, '1343761268', '20120731190108' ],
111 [ TS_MW, '20120731190108', '20120731190108' ],
112 [ TS_DB, '2012-07-31 19:01:08', '20120731190108' ],
113 [ TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ],
114 [ TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ],
115 [ TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ],
116 [ TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ],
117 [ TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ],
118 [ TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ],
119 // Some extremes and weird values
120 [ TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ],
121 [ TS_UNIX, '-62135596801', '00001231235959' ]
122 ];
123 }
124
125 /**
126 * Returns a list of out of range timestamps in the format:
127 * [ type, timestamp_of_type ]
128 */
129 public static function provideOutOfRangeTimestamps() {
130 return [
131 // Various formats
132 [ TS_MW, '-62167219201' ], // -0001-12-31T23:59:59Z
133 [ TS_MW, '253402300800' ], // 10000-01-01T00:00:00Z
134 ];
135 }
136
137 /**
138 * @dataProvider provideHumanTimestampTests
139 * @covers MWTimestamp::getHumanTimestamp
140 */
141 public function testHumanTimestamp(
142 $tsTime, // The timestamp to format
143 $currentTime, // The time to consider "now"
144 $timeCorrection, // The time offset to use
145 $dateFormat, // The date preference to use
146 $expectedOutput, // The expected output
147 $desc // Description
148 ) {
149 $user = $this->getMock( 'User' );
150 $user->expects( $this->any() )
151 ->method( 'getOption' )
152 ->with( 'timecorrection' )
153 ->will( $this->returnValue( $timeCorrection ) );
154
155 $user->expects( $this->any() )
156 ->method( 'getDatePreference' )
157 ->will( $this->returnValue( $dateFormat ) );
158
159 $tsTime = new MWTimestamp( $tsTime );
160 $currentTime = new MWTimestamp( $currentTime );
161
162 $this->assertEquals(
163 $expectedOutput,
164 $tsTime->getHumanTimestamp( $currentTime, $user ),
165 $desc
166 );
167 }
168
169 public static function provideHumanTimestampTests() {
170 return [
171 [
172 '20111231170000',
173 '20120101000000',
174 'Offset|0',
175 'mdy',
176 'Yesterday at 17:00',
177 '"Yesterday" across years',
178 ],
179 [
180 '20120717190900',
181 '20120717190929',
182 'Offset|0',
183 'mdy',
184 'just now',
185 '"Just now"',
186 ],
187 [
188 '20120717190900',
189 '20120717191530',
190 'Offset|0',
191 'mdy',
192 '6 minutes ago',
193 'X minutes ago',
194 ],
195 [
196 '20121006173100',
197 '20121006173200',
198 'Offset|0',
199 'mdy',
200 '1 minute ago',
201 '"1 minute ago"',
202 ],
203 [
204 '20120617190900',
205 '20120717190900',
206 'Offset|0',
207 'mdy',
208 'June 17',
209 'Another month'
210 ],
211 [
212 '19910130151500',
213 '20120716193700',
214 'Offset|0',
215 'mdy',
216 '15:15, January 30, 1991',
217 'Different year',
218 ],
219 [
220 '20120101050000',
221 '20120101080000',
222 'Offset|-360',
223 'mdy',
224 'Yesterday at 23:00',
225 '"Yesterday" across years with time correction',
226 ],
227 [
228 '20120714184300',
229 '20120716184300',
230 'Offset|-420',
231 'mdy',
232 'Saturday at 11:43',
233 'Recent weekday with time correction',
234 ],
235 [
236 '20120714184300',
237 '20120715040000',
238 'Offset|-420',
239 'mdy',
240 '11:43',
241 'Today at another time with time correction',
242 ],
243 [
244 '20120617190900',
245 '20120717190900',
246 'Offset|0',
247 'dmy',
248 '17 June',
249 'Another month with dmy'
250 ],
251 [
252 '20120617190900',
253 '20120717190900',
254 'Offset|0',
255 'ISO 8601',
256 '06-17',
257 'Another month with ISO-8601'
258 ],
259 [
260 '19910130151500',
261 '20120716193700',
262 'Offset|0',
263 'ISO 8601',
264 '1991-01-30T15:15:00',
265 'Different year with ISO-8601',
266 ],
267 ];
268 }
269
270 /**
271 * @dataProvider provideRelativeTimestampTests
272 * @covers MWTimestamp::getRelativeTimestamp
273 */
274 public function testRelativeTimestamp(
275 $tsTime, // The timestamp to format
276 $currentTime, // The time to consider "now"
277 $timeCorrection, // The time offset to use
278 $dateFormat, // The date preference to use
279 $expectedOutput, // The expected output
280 $desc // Description
281 ) {
282 $user = $this->getMock( 'User' );
283 $user->expects( $this->any() )
284 ->method( 'getOption' )
285 ->with( 'timecorrection' )
286 ->will( $this->returnValue( $timeCorrection ) );
287
288 $tsTime = new MWTimestamp( $tsTime );
289 $currentTime = new MWTimestamp( $currentTime );
290
291 $this->assertEquals(
292 $expectedOutput,
293 $tsTime->getRelativeTimestamp( $currentTime, $user ),
294 $desc
295 );
296 }
297
298 public static function provideRelativeTimestampTests() {
299 return [
300 [
301 '20111231170000',
302 '20120101000000',
303 'Offset|0',
304 'mdy',
305 '7 hours ago',
306 '"Yesterday" across years',
307 ],
308 [
309 '20120717190900',
310 '20120717190929',
311 'Offset|0',
312 'mdy',
313 '29 seconds ago',
314 '"Just now"',
315 ],
316 [
317 '20120717190900',
318 '20120717191530',
319 'Offset|0',
320 'mdy',
321 '6 minutes and 30 seconds ago',
322 'Combination of multiple units',
323 ],
324 [
325 '20121006173100',
326 '20121006173200',
327 'Offset|0',
328 'mdy',
329 '1 minute ago',
330 '"1 minute ago"',
331 ],
332 [
333 '19910130151500',
334 '20120716193700',
335 'Offset|0',
336 'mdy',
337 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
338 'A long time ago',
339 ],
340 [
341 '20120101050000',
342 '20120101080000',
343 'Offset|-360',
344 'mdy',
345 '3 hours ago',
346 '"Yesterday" across years with time correction',
347 ],
348 [
349 '20120714184300',
350 '20120716184300',
351 'Offset|-420',
352 'mdy',
353 '2 days ago',
354 'Recent weekday with time correction',
355 ],
356 [
357 '20120714184300',
358 '20120715040000',
359 'Offset|-420',
360 'mdy',
361 '9 hours and 17 minutes ago',
362 'Today at another time with time correction',
363 ],
364 ];
365 }
366 }