Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / MWTimestampTest.php
blob365625459e04eacbb749a65ca5856d0fa220b41b
1 <?php
3 /**
4 * Tests timestamp parsing and output.
5 */
6 class MWTimestampTest extends MediaWikiLangTestCase {
8 protected function setUp() {
9 parent::setUp();
11 // Avoid 'GetHumanTimestamp' hook and others
12 $this->setMwGlobals( 'wgHooks', array() );
14 RequestContext::getMain()->setLanguage( Language::factory( 'en' ) );
17 /**
18 * @covers MWTimestamp::__construct
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 ) ) );
27 /**
28 * @covers MWTimestamp::__toString
30 public function testToString() {
31 $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108
32 $this->assertEquals( '1406833268', $timestamp->__toString() );
35 public static function provideValidTimestampDifferences() {
36 return array(
37 array( '1406833268', '1406833269', '00 00 00 01' ),
38 array( '1406833268', '1406833329', '00 00 01 01' ),
39 array( '1406833268', '1406836929', '00 01 01 01' ),
40 array( '1406833268', '1406923329', '01 01 01 01' ),
44 /**
45 * @dataProvider provideValidTimestampDifferences
46 * @covers MWTimestamp::diff
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' ) );
55 /**
56 * Test parsing of valid timestamps and outputing to MW format.
57 * @dataProvider provideValidTimestamps
58 * @covers MWTimestamp::getTimestamp
60 public function testValidParse( $format, $original, $expected ) {
61 $timestamp = new MWTimestamp( $original );
62 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
65 /**
66 * Test outputting valid timestamps to different formats.
67 * @dataProvider provideValidTimestamps
68 * @covers MWTimestamp::getTimestamp
70 public function testValidOutput( $format, $expected, $original ) {
71 $timestamp = new MWTimestamp( $original );
72 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
75 /**
76 * Test an invalid timestamp.
77 * @expectedException TimestampException
78 * @covers MWTimestamp
80 public function testInvalidParse() {
81 new MWTimestamp( "This is not a timestamp." );
84 /**
85 * Test an out of range timestamp
86 * @dataProvider provideOutOfRangeTimestamps
87 * @expectedException TimestampException
88 * @covers MWTimestamp
90 public function testOutOfRangeTimestamps( $format, $input ) {
91 $timestamp = new MWTimestamp( $input );
92 $timestamp->getTimestamp( $format );
95 /**
96 * Test requesting an invalid output format.
97 * @expectedException TimestampException
98 * @covers MWTimestamp::getTimestamp
100 public function testInvalidOutput() {
101 $timestamp = new MWTimestamp( '1343761268' );
102 $timestamp->getTimestamp( 98 );
106 * Returns a list of valid timestamps in the format:
107 * array( type, timestamp_of_type, timestamp_in_MW )
109 public static function provideValidTimestamps() {
110 return array(
111 // Various formats
112 array( TS_UNIX, '1343761268', '20120731190108' ),
113 array( TS_MW, '20120731190108', '20120731190108' ),
114 array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ),
115 array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ),
116 array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ),
117 array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ),
118 array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
119 array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ),
120 array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ),
121 // Some extremes and weird values
122 array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ),
123 array( TS_UNIX, '-62135596801', '00001231235959' )
128 * Returns a list of out of range timestamps in the format:
129 * array( type, timestamp_of_type )
131 public static function provideOutOfRangeTimestamps() {
132 return array(
133 // Various formats
134 array( TS_MW, '-62167219201' ), // -0001-12-31T23:59:59Z
135 array( TS_MW, '253402300800' ), // 10000-01-01T00:00:00Z
140 * @dataProvider provideHumanTimestampTests
141 * @covers MWTimestamp::getHumanTimestamp
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
151 $user = $this->getMock( 'User' );
152 $user->expects( $this->any() )
153 ->method( 'getOption' )
154 ->with( 'timecorrection' )
155 ->will( $this->returnValue( $timeCorrection ) );
157 $user->expects( $this->any() )
158 ->method( 'getDatePreference' )
159 ->will( $this->returnValue( $dateFormat ) );
161 $tsTime = new MWTimestamp( $tsTime );
162 $currentTime = new MWTimestamp( $currentTime );
164 $this->assertEquals(
165 $expectedOutput,
166 $tsTime->getHumanTimestamp( $currentTime, $user ),
167 $desc
171 public static function provideHumanTimestampTests() {
172 return array(
173 array(
174 '20111231170000',
175 '20120101000000',
176 'Offset|0',
177 'mdy',
178 'Yesterday at 17:00',
179 '"Yesterday" across years',
181 array(
182 '20120717190900',
183 '20120717190929',
184 'Offset|0',
185 'mdy',
186 'just now',
187 '"Just now"',
189 array(
190 '20120717190900',
191 '20120717191530',
192 'Offset|0',
193 'mdy',
194 '6 minutes ago',
195 'X minutes ago',
197 array(
198 '20121006173100',
199 '20121006173200',
200 'Offset|0',
201 'mdy',
202 '1 minute ago',
203 '"1 minute ago"',
205 array(
206 '20120617190900',
207 '20120717190900',
208 'Offset|0',
209 'mdy',
210 'June 17',
211 'Another month'
213 array(
214 '19910130151500',
215 '20120716193700',
216 'Offset|0',
217 'mdy',
218 '15:15, January 30, 1991',
219 'Different year',
221 array(
222 '20120101050000',
223 '20120101080000',
224 'Offset|-360',
225 'mdy',
226 'Yesterday at 23:00',
227 '"Yesterday" across years with time correction',
229 array(
230 '20120714184300',
231 '20120716184300',
232 'Offset|-420',
233 'mdy',
234 'Saturday at 11:43',
235 'Recent weekday with time correction',
237 array(
238 '20120714184300',
239 '20120715040000',
240 'Offset|-420',
241 'mdy',
242 '11:43',
243 'Today at another time with time correction',
245 array(
246 '20120617190900',
247 '20120717190900',
248 'Offset|0',
249 'dmy',
250 '17 June',
251 'Another month with dmy'
253 array(
254 '20120617190900',
255 '20120717190900',
256 'Offset|0',
257 'ISO 8601',
258 '06-17',
259 'Another month with ISO-8601'
261 array(
262 '19910130151500',
263 '20120716193700',
264 'Offset|0',
265 'ISO 8601',
266 '1991-01-30T15:15:00',
267 'Different year with ISO-8601',
273 * @dataProvider provideRelativeTimestampTests
274 * @covers MWTimestamp::getRelativeTimestamp
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
284 $user = $this->getMock( 'User' );
285 $user->expects( $this->any() )
286 ->method( 'getOption' )
287 ->with( 'timecorrection' )
288 ->will( $this->returnValue( $timeCorrection ) );
290 $tsTime = new MWTimestamp( $tsTime );
291 $currentTime = new MWTimestamp( $currentTime );
293 $this->assertEquals(
294 $expectedOutput,
295 $tsTime->getRelativeTimestamp( $currentTime, $user ),
296 $desc
300 public static function provideRelativeTimestampTests() {
301 return array(
302 array(
303 '20111231170000',
304 '20120101000000',
305 'Offset|0',
306 'mdy',
307 '7 hours ago',
308 '"Yesterday" across years',
310 array(
311 '20120717190900',
312 '20120717190929',
313 'Offset|0',
314 'mdy',
315 '29 seconds ago',
316 '"Just now"',
318 array(
319 '20120717190900',
320 '20120717191530',
321 'Offset|0',
322 'mdy',
323 '6 minutes and 30 seconds ago',
324 'Combination of multiple units',
326 array(
327 '20121006173100',
328 '20121006173200',
329 'Offset|0',
330 'mdy',
331 '1 minute ago',
332 '"1 minute ago"',
334 array(
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',
342 array(
343 '20120101050000',
344 '20120101080000',
345 'Offset|-360',
346 'mdy',
347 '3 hours ago',
348 '"Yesterday" across years with time correction',
350 array(
351 '20120714184300',
352 '20120716184300',
353 'Offset|-420',
354 'mdy',
355 '2 days ago',
356 'Recent weekday with time correction',
358 array(
359 '20120714184300',
360 '20120715040000',
361 'Offset|-420',
362 'mdy',
363 '9 hours and 17 minutes ago',
364 'Today at another time with time correction',