Merge "Workaround image magick issue with greyscale xcf files"
[mediawiki.git] / tests / phpunit / includes / MWTimestampTest.php
blobc82dc4275b7efb7ee027fb31bfe42f40192f8679
1 <?php
3 /**
4 * Tests timestamp parsing and output.
5 */
6 class MWTimestampTest extends MediaWikiLangTestCase {
8 protected function setUp() {
9 parent::setUp();
11 RequestContext::getMain()->setLanguage( Language::factory( 'en' ) );
14 /**
15 * @covers MWTimestamp::__construct
17 public function testConstructWithNoTimestamp() {
18 $timestamp = new MWTimestamp();
19 $this->assertInternalType( 'string', $timestamp->getTimestamp() );
20 $this->assertNotEmpty( $timestamp->getTimestamp() );
21 $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW ) ) );
24 /**
25 * @covers MWTimestamp::__toString
27 public function testToString() {
28 $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108
29 $this->assertEquals( '1406833268', $timestamp->__toString() );
32 public function provideValidTimestampDifferences() {
33 return array(
34 array( '1406833268', '1406833269', '00 00 00 01' ),
35 array( '1406833268', '1406833329', '00 00 01 01' ),
36 array( '1406833268', '1406836929', '00 01 01 01' ),
37 array( '1406833268', '1406923329', '01 01 01 01' ),
41 /**
42 * @dataProvider provideValidTimestampDifferences
43 * @covers MWTimestamp::diff
45 public function testDiff( $timestamp1, $timestamp2, $expected ) {
46 $timestamp1 = new MWTimestamp( $timestamp1 );
47 $timestamp2 = new MWTimestamp( $timestamp2 );
48 $diff = $timestamp1->diff( $timestamp2 );
49 $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) );
52 /**
53 * Test parsing of valid timestamps and outputing to MW format.
54 * @dataProvider provideValidTimestamps
55 * @covers MWTimestamp::getTimestamp
57 public function testValidParse( $format, $original, $expected ) {
58 $timestamp = new MWTimestamp( $original );
59 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
62 /**
63 * Test outputting valid timestamps to different formats.
64 * @dataProvider provideValidTimestamps
65 * @covers MWTimestamp::getTimestamp
67 public function testValidOutput( $format, $expected, $original ) {
68 $timestamp = new MWTimestamp( $original );
69 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
72 /**
73 * Test an invalid timestamp.
74 * @expectedException TimestampException
75 * @covers MWTimestamp
77 public function testInvalidParse() {
78 new MWTimestamp( "This is not a timestamp." );
81 /**
82 * Test requesting an invalid output format.
83 * @expectedException TimestampException
84 * @covers MWTimestamp::getTimestamp
86 public function testInvalidOutput() {
87 $timestamp = new MWTimestamp( '1343761268' );
88 $timestamp->getTimestamp( 98 );
91 /**
92 * Returns a list of valid timestamps in the format:
93 * array( type, timestamp_of_type, timestamp_in_MW )
95 public static function provideValidTimestamps() {
96 return array(
97 // Various formats
98 array( TS_UNIX, '1343761268', '20120731190108' ),
99 array( TS_MW, '20120731190108', '20120731190108' ),
100 array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ),
101 array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ),
102 array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ),
103 array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ),
104 array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
105 array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ),
106 array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ),
107 // Some extremes and weird values
108 array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ),
109 array( TS_UNIX, '-62135596801', '00001231235959' )
114 * @dataProvider provideHumanTimestampTests
115 * @covers MWTimestamp::getHumanTimestamp
117 public function testHumanTimestamp(
118 $tsTime, // The timestamp to format
119 $currentTime, // The time to consider "now"
120 $timeCorrection, // The time offset to use
121 $dateFormat, // The date preference to use
122 $expectedOutput, // The expected output
123 $desc // Description
125 $user = $this->getMock( 'User' );
126 $user->expects( $this->any() )
127 ->method( 'getOption' )
128 ->with( 'timecorrection' )
129 ->will( $this->returnValue( $timeCorrection ) );
131 $user->expects( $this->any() )
132 ->method( 'getDatePreference' )
133 ->will( $this->returnValue( $dateFormat ) );
135 $tsTime = new MWTimestamp( $tsTime );
136 $currentTime = new MWTimestamp( $currentTime );
138 $this->assertEquals(
139 $expectedOutput,
140 $tsTime->getHumanTimestamp( $currentTime, $user ),
141 $desc
145 public static function provideHumanTimestampTests() {
146 return array(
147 array(
148 '20111231170000',
149 '20120101000000',
150 'Offset|0',
151 'mdy',
152 'Yesterday at 17:00',
153 '"Yesterday" across years',
155 array(
156 '20120717190900',
157 '20120717190929',
158 'Offset|0',
159 'mdy',
160 'just now',
161 '"Just now"',
163 array(
164 '20120717190900',
165 '20120717191530',
166 'Offset|0',
167 'mdy',
168 '6 minutes ago',
169 'X minutes ago',
171 array(
172 '20121006173100',
173 '20121006173200',
174 'Offset|0',
175 'mdy',
176 '1 minute ago',
177 '"1 minute ago"',
179 array(
180 '20120617190900',
181 '20120717190900',
182 'Offset|0',
183 'mdy',
184 'June 17',
185 'Another month'
187 array(
188 '19910130151500',
189 '20120716193700',
190 'Offset|0',
191 'mdy',
192 '15:15, January 30, 1991',
193 'Different year',
195 array(
196 '20120101050000',
197 '20120101080000',
198 'Offset|-360',
199 'mdy',
200 'Yesterday at 23:00',
201 '"Yesterday" across years with time correction',
203 array(
204 '20120714184300',
205 '20120716184300',
206 'Offset|-420',
207 'mdy',
208 'Saturday at 11:43',
209 'Recent weekday with time correction',
211 array(
212 '20120714184300',
213 '20120715040000',
214 'Offset|-420',
215 'mdy',
216 '11:43',
217 'Today at another time with time correction',
219 array(
220 '20120617190900',
221 '20120717190900',
222 'Offset|0',
223 'dmy',
224 '17 June',
225 'Another month with dmy'
227 array(
228 '20120617190900',
229 '20120717190900',
230 'Offset|0',
231 'ISO 8601',
232 '06-17',
233 'Another month with ISO-8601'
235 array(
236 '19910130151500',
237 '20120716193700',
238 'Offset|0',
239 'ISO 8601',
240 '1991-01-30T15:15:00',
241 'Different year with ISO-8601',
247 * @dataProvider provideRelativeTimestampTests
248 * @covers MWTimestamp::getRelativeTimestamp
250 public function testRelativeTimestamp(
251 $tsTime, // The timestamp to format
252 $currentTime, // The time to consider "now"
253 $timeCorrection, // The time offset to use
254 $dateFormat, // The date preference to use
255 $expectedOutput, // The expected output
256 $desc // Description
258 $user = $this->getMock( 'User' );
259 $user->expects( $this->any() )
260 ->method( 'getOption' )
261 ->with( 'timecorrection' )
262 ->will( $this->returnValue( $timeCorrection ) );
264 $tsTime = new MWTimestamp( $tsTime );
265 $currentTime = new MWTimestamp( $currentTime );
267 $this->assertEquals(
268 $expectedOutput,
269 $tsTime->getRelativeTimestamp( $currentTime, $user ),
270 $desc
274 public static function provideRelativeTimestampTests() {
275 return array(
276 array(
277 '20111231170000',
278 '20120101000000',
279 'Offset|0',
280 'mdy',
281 '7 hours ago',
282 '"Yesterday" across years',
284 array(
285 '20120717190900',
286 '20120717190929',
287 'Offset|0',
288 'mdy',
289 '29 seconds ago',
290 '"Just now"',
292 array(
293 '20120717190900',
294 '20120717191530',
295 'Offset|0',
296 'mdy',
297 '6 minutes and 30 seconds ago',
298 'Combination of multiple units',
300 array(
301 '20121006173100',
302 '20121006173200',
303 'Offset|0',
304 'mdy',
305 '1 minute ago',
306 '"1 minute ago"',
308 array(
309 '19910130151500',
310 '20120716193700',
311 'Offset|0',
312 'mdy',
313 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
314 'A long time ago',
316 array(
317 '20120101050000',
318 '20120101080000',
319 'Offset|-360',
320 'mdy',
321 '3 hours ago',
322 '"Yesterday" across years with time correction',
324 array(
325 '20120714184300',
326 '20120716184300',
327 'Offset|-420',
328 'mdy',
329 '2 days ago',
330 'Recent weekday with time correction',
332 array(
333 '20120714184300',
334 '20120715040000',
335 'Offset|-420',
336 'mdy',
337 '9 hours and 17 minutes ago',
338 'Today at another time with time correction',