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