5d50c0a811e8f0d18974ab2945eaec1f33bc687f
[lhc/web/wiklou.git] / tests / phpunit / includes / TimestampTest.php
1 <?php
2
3 /**
4 * Tests timestamp parsing and output.
5 */
6 class TimestampTest extends MediaWikiTestCase {
7
8 protected function setUp() {
9 parent::setUp();
10
11 $this->setMwGlobals( array(
12 'wgLanguageCode' => 'en',
13 'wgContLang' => Language::factory( 'en' ),
14 'wgLang' => Language::factory( 'en' ),
15 ) );
16 }
17
18 /**
19 * Test parsing of valid timestamps and outputing to MW format.
20 * @dataProvider provideValidTimestamps
21 */
22 function testValidParse( $format, $original, $expected ) {
23 $timestamp = new MWTimestamp( $original );
24 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
25 }
26
27 /**
28 * Test outputting valid timestamps to different formats.
29 * @dataProvider provideValidTimestamps
30 */
31 function testValidOutput( $format, $expected, $original ) {
32 $timestamp = new MWTimestamp( $original );
33 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
34 }
35
36 /**
37 * Test an invalid timestamp.
38 * @expectedException TimestampException
39 */
40 function testInvalidParse() {
41 new MWTimestamp( "This is not a timestamp." );
42 }
43
44 /**
45 * Test requesting an invalid output format.
46 * @expectedException TimestampException
47 */
48 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 * @test
77 * @dataProvider provideHumanTimestampTests
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 }