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