CommentStore: Hard-deprecate newKey()
[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 protected function setUp() {
8 parent::setUp();
9
10 // Avoid 'GetHumanTimestamp' hook and others
11 $this->setMwGlobals( 'wgHooks', [] );
12 }
13
14 /**
15 * @dataProvider provideHumanTimestampTests
16 * @covers MWTimestamp::getHumanTimestamp
17 */
18 public function testHumanTimestamp(
19 $tsTime, // The timestamp to format
20 $currentTime, // The time to consider "now"
21 $timeCorrection, // The time offset to use
22 $dateFormat, // The date preference to use
23 $expectedOutput, // The expected output
24 $desc // Description
25 ) {
26 $user = $this->createMock( User::class );
27 $user->expects( $this->any() )
28 ->method( 'getOption' )
29 ->with( 'timecorrection' )
30 ->will( $this->returnValue( $timeCorrection ) );
31
32 $user->expects( $this->any() )
33 ->method( 'getDatePreference' )
34 ->will( $this->returnValue( $dateFormat ) );
35
36 $tsTime = new MWTimestamp( $tsTime );
37 $currentTime = new MWTimestamp( $currentTime );
38
39 $this->assertEquals(
40 $expectedOutput,
41 $tsTime->getHumanTimestamp( $currentTime, $user ),
42 $desc
43 );
44 }
45
46 public static function provideHumanTimestampTests() {
47 return [
48 [
49 '20111231170000',
50 '20120101000000',
51 'Offset|0',
52 'mdy',
53 'Yesterday at 17:00',
54 '"Yesterday" across years',
55 ],
56 [
57 '20120717190900',
58 '20120717190929',
59 'Offset|0',
60 'mdy',
61 'just now',
62 '"Just now"',
63 ],
64 [
65 '20120717190900',
66 '20120717191530',
67 'Offset|0',
68 'mdy',
69 '6 minutes ago',
70 'X minutes ago',
71 ],
72 [
73 '20121006173100',
74 '20121006173200',
75 'Offset|0',
76 'mdy',
77 '1 minute ago',
78 '"1 minute ago"',
79 ],
80 [
81 '20120617190900',
82 '20120717190900',
83 'Offset|0',
84 'mdy',
85 'June 17',
86 'Another month'
87 ],
88 [
89 '19910130151500',
90 '20120716193700',
91 'Offset|0',
92 'mdy',
93 '15:15, January 30, 1991',
94 'Different year',
95 ],
96 [
97 '20120101050000',
98 '20120101080000',
99 'Offset|-360',
100 'mdy',
101 'Yesterday at 23:00',
102 '"Yesterday" across years with time correction',
103 ],
104 [
105 '20120714184300',
106 '20120716184300',
107 'Offset|-420',
108 'mdy',
109 'Saturday at 11:43',
110 'Recent weekday with time correction',
111 ],
112 [
113 '20120714184300',
114 '20120715040000',
115 'Offset|-420',
116 'mdy',
117 '11:43',
118 'Today at another time with time correction',
119 ],
120 [
121 '20120617190900',
122 '20120717190900',
123 'Offset|0',
124 'dmy',
125 '17 June',
126 'Another month with dmy'
127 ],
128 [
129 '20120617190900',
130 '20120717190900',
131 'Offset|0',
132 'ISO 8601',
133 '06-17',
134 'Another month with ISO-8601'
135 ],
136 [
137 '19910130151500',
138 '20120716193700',
139 'Offset|0',
140 'ISO 8601',
141 '1991-01-30T15:15:00',
142 'Different year with ISO-8601',
143 ],
144 ];
145 }
146
147 /**
148 * @dataProvider provideRelativeTimestampTests
149 * @covers MWTimestamp::getRelativeTimestamp
150 */
151 public function testRelativeTimestamp(
152 $tsTime, // The timestamp to format
153 $currentTime, // The time to consider "now"
154 $timeCorrection, // The time offset to use
155 $dateFormat, // The date preference to use
156 $expectedOutput, // The expected output
157 $desc // Description
158 ) {
159 $user = $this->createMock( User::class );
160 $user->expects( $this->any() )
161 ->method( 'getOption' )
162 ->with( 'timecorrection' )
163 ->will( $this->returnValue( $timeCorrection ) );
164
165 $tsTime = new MWTimestamp( $tsTime );
166 $currentTime = new MWTimestamp( $currentTime );
167
168 $this->assertEquals(
169 $expectedOutput,
170 $tsTime->getRelativeTimestamp( $currentTime, $user ),
171 $desc
172 );
173 }
174
175 public static function provideRelativeTimestampTests() {
176 return [
177 [
178 '20111231170000',
179 '20120101000000',
180 'Offset|0',
181 'mdy',
182 '7 hours ago',
183 '"Yesterday" across years',
184 ],
185 [
186 '20120717190900',
187 '20120717190929',
188 'Offset|0',
189 'mdy',
190 '29 seconds ago',
191 '"Just now"',
192 ],
193 [
194 '20120717190900',
195 '20120717191530',
196 'Offset|0',
197 'mdy',
198 '6 minutes and 30 seconds ago',
199 'Combination of multiple units',
200 ],
201 [
202 '20121006173100',
203 '20121006173200',
204 'Offset|0',
205 'mdy',
206 '1 minute ago',
207 '"1 minute ago"',
208 ],
209 [
210 '19910130151500',
211 '20120716193700',
212 'Offset|0',
213 'mdy',
214 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
215 'A long time ago',
216 ],
217 [
218 '20120101050000',
219 '20120101080000',
220 'Offset|-360',
221 'mdy',
222 '3 hours ago',
223 '"Yesterday" across years with time correction',
224 ],
225 [
226 '20120714184300',
227 '20120716184300',
228 'Offset|-420',
229 'mdy',
230 '2 days ago',
231 'Recent weekday with time correction',
232 ],
233 [
234 '20120714184300',
235 '20120715040000',
236 'Offset|-420',
237 'mdy',
238 '9 hours and 17 minutes ago',
239 'Today at another time with time correction',
240 ],
241 ];
242 }
243 }