Support offsets in prefix searching
[mediawiki.git] / tests / phpunit / includes / MWTimestampTest.php
blob05c1a661dce7c289be3dd51143cac60cdf39156e
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 requesting an invalid output format.
86 * @expectedException TimestampException
87 * @covers MWTimestamp::getTimestamp
89 public function testInvalidOutput() {
90 $timestamp = new MWTimestamp( '1343761268' );
91 $timestamp->getTimestamp( 98 );
94 /**
95 * Returns a list of valid timestamps in the format:
96 * array( type, timestamp_of_type, timestamp_in_MW )
98 public static function provideValidTimestamps() {
99 return array(
100 // Various formats
101 array( TS_UNIX, '1343761268', '20120731190108' ),
102 array( TS_MW, '20120731190108', '20120731190108' ),
103 array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ),
104 array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ),
105 array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ),
106 array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ),
107 array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
108 array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ),
109 array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ),
110 // Some extremes and weird values
111 array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ),
112 array( TS_UNIX, '-62135596801', '00001231235959' )
117 * @dataProvider provideHumanTimestampTests
118 * @covers MWTimestamp::getHumanTimestamp
120 public function testHumanTimestamp(
121 $tsTime, // The timestamp to format
122 $currentTime, // The time to consider "now"
123 $timeCorrection, // The time offset to use
124 $dateFormat, // The date preference to use
125 $expectedOutput, // The expected output
126 $desc // Description
128 $user = $this->getMock( 'User' );
129 $user->expects( $this->any() )
130 ->method( 'getOption' )
131 ->with( 'timecorrection' )
132 ->will( $this->returnValue( $timeCorrection ) );
134 $user->expects( $this->any() )
135 ->method( 'getDatePreference' )
136 ->will( $this->returnValue( $dateFormat ) );
138 $tsTime = new MWTimestamp( $tsTime );
139 $currentTime = new MWTimestamp( $currentTime );
141 $this->assertEquals(
142 $expectedOutput,
143 $tsTime->getHumanTimestamp( $currentTime, $user ),
144 $desc
148 public static function provideHumanTimestampTests() {
149 return array(
150 array(
151 '20111231170000',
152 '20120101000000',
153 'Offset|0',
154 'mdy',
155 'Yesterday at 17:00',
156 '"Yesterday" across years',
158 array(
159 '20120717190900',
160 '20120717190929',
161 'Offset|0',
162 'mdy',
163 'just now',
164 '"Just now"',
166 array(
167 '20120717190900',
168 '20120717191530',
169 'Offset|0',
170 'mdy',
171 '6 minutes ago',
172 'X minutes ago',
174 array(
175 '20121006173100',
176 '20121006173200',
177 'Offset|0',
178 'mdy',
179 '1 minute ago',
180 '"1 minute ago"',
182 array(
183 '20120617190900',
184 '20120717190900',
185 'Offset|0',
186 'mdy',
187 'June 17',
188 'Another month'
190 array(
191 '19910130151500',
192 '20120716193700',
193 'Offset|0',
194 'mdy',
195 '15:15, January 30, 1991',
196 'Different year',
198 array(
199 '20120101050000',
200 '20120101080000',
201 'Offset|-360',
202 'mdy',
203 'Yesterday at 23:00',
204 '"Yesterday" across years with time correction',
206 array(
207 '20120714184300',
208 '20120716184300',
209 'Offset|-420',
210 'mdy',
211 'Saturday at 11:43',
212 'Recent weekday with time correction',
214 array(
215 '20120714184300',
216 '20120715040000',
217 'Offset|-420',
218 'mdy',
219 '11:43',
220 'Today at another time with time correction',
222 array(
223 '20120617190900',
224 '20120717190900',
225 'Offset|0',
226 'dmy',
227 '17 June',
228 'Another month with dmy'
230 array(
231 '20120617190900',
232 '20120717190900',
233 'Offset|0',
234 'ISO 8601',
235 '06-17',
236 'Another month with ISO-8601'
238 array(
239 '19910130151500',
240 '20120716193700',
241 'Offset|0',
242 'ISO 8601',
243 '1991-01-30T15:15:00',
244 'Different year with ISO-8601',
250 * @dataProvider provideRelativeTimestampTests
251 * @covers MWTimestamp::getRelativeTimestamp
253 public function testRelativeTimestamp(
254 $tsTime, // The timestamp to format
255 $currentTime, // The time to consider "now"
256 $timeCorrection, // The time offset to use
257 $dateFormat, // The date preference to use
258 $expectedOutput, // The expected output
259 $desc // Description
261 $user = $this->getMock( 'User' );
262 $user->expects( $this->any() )
263 ->method( 'getOption' )
264 ->with( 'timecorrection' )
265 ->will( $this->returnValue( $timeCorrection ) );
267 $tsTime = new MWTimestamp( $tsTime );
268 $currentTime = new MWTimestamp( $currentTime );
270 $this->assertEquals(
271 $expectedOutput,
272 $tsTime->getRelativeTimestamp( $currentTime, $user ),
273 $desc
277 public static function provideRelativeTimestampTests() {
278 return array(
279 array(
280 '20111231170000',
281 '20120101000000',
282 'Offset|0',
283 'mdy',
284 '7 hours ago',
285 '"Yesterday" across years',
287 array(
288 '20120717190900',
289 '20120717190929',
290 'Offset|0',
291 'mdy',
292 '29 seconds ago',
293 '"Just now"',
295 array(
296 '20120717190900',
297 '20120717191530',
298 'Offset|0',
299 'mdy',
300 '6 minutes and 30 seconds ago',
301 'Combination of multiple units',
303 array(
304 '20121006173100',
305 '20121006173200',
306 'Offset|0',
307 'mdy',
308 '1 minute ago',
309 '"1 minute ago"',
311 array(
312 '19910130151500',
313 '20120716193700',
314 'Offset|0',
315 'mdy',
316 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
317 'A long time ago',
319 array(
320 '20120101050000',
321 '20120101080000',
322 'Offset|-360',
323 'mdy',
324 '3 hours ago',
325 '"Yesterday" across years with time correction',
327 array(
328 '20120714184300',
329 '20120716184300',
330 'Offset|-420',
331 'mdy',
332 '2 days ago',
333 'Recent weekday with time correction',
335 array(
336 '20120714184300',
337 '20120715040000',
338 'Offset|-420',
339 'mdy',
340 '9 hours and 17 minutes ago',
341 'Today at another time with time correction',