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