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