Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / api / ApiErrorFormatterTest.php
bloba08a86ef3a33bb8b8675e858909defafe81b9c2d
1 <?php
3 /**
4 * @group API
5 */
6 class ApiErrorFormatterTest extends MediaWikiLangTestCase {
8 /**
9 * @covers ApiErrorFormatter
10 * @dataProvider provideErrorFormatter
12 public function testErrorFormatter( $format, $lang, $useDB,
13 $expect1, $expect2, $expect3
14 ) {
15 $result = new ApiResult( 8388608 );
16 $formatter = new ApiErrorFormatter( $result, Language::factory( $lang ), $format, $useDB );
18 // Add default type
19 $expect1[ApiResult::META_TYPE] = 'assoc';
20 $expect2[ApiResult::META_TYPE] = 'assoc';
21 $expect3[ApiResult::META_TYPE] = 'assoc';
23 $formatter->addWarning( 'string', 'mainpage' );
24 $formatter->addError( 'err', 'mainpage' );
25 $this->assertSame( $expect1, $result->getResultData(), 'Simple test' );
27 $result->reset();
28 $formatter->addWarning( 'foo', 'mainpage' );
29 $formatter->addWarning( 'foo', 'mainpage' );
30 $formatter->addWarning( 'foo', array( 'parentheses', 'foobar' ) );
31 $msg1 = wfMessage( 'mainpage' );
32 $formatter->addWarning( 'message', $msg1 );
33 $msg2 = new ApiMessage( 'mainpage', 'overriddenCode', array( 'overriddenData' => true ) );
34 $formatter->addWarning( 'messageWithData', $msg2 );
35 $formatter->addError( 'errWithData', $msg2 );
36 $this->assertSame( $expect2, $result->getResultData(), 'Complex test' );
38 $result->reset();
39 $status = Status::newGood();
40 $status->warning( 'mainpage' );
41 $status->warning( 'parentheses', 'foobar' );
42 $status->warning( $msg1 );
43 $status->warning( $msg2 );
44 $status->error( 'mainpage' );
45 $status->error( 'parentheses', 'foobar' );
46 $formatter->addMessagesFromStatus( 'status', $status );
47 $this->assertSame( $expect3, $result->getResultData(), 'Status test' );
49 $this->assertSame(
50 $expect3['errors']['status'],
51 $formatter->arrayFromStatus( $status, 'error' ),
52 'arrayFromStatus test for error'
54 $this->assertSame(
55 $expect3['warnings']['status'],
56 $formatter->arrayFromStatus( $status, 'warning' ),
57 'arrayFromStatus test for warning'
61 public static function provideErrorFormatter() {
62 $mainpagePlain = wfMessage( 'mainpage' )->useDatabase( false )->plain();
63 $parensPlain = wfMessage( 'parentheses', 'foobar' )->useDatabase( false )->plain();
64 $mainpageText = wfMessage( 'mainpage' )->inLanguage( 'de' )->text();
65 $parensText = wfMessage( 'parentheses', 'foobar' )->inLanguage( 'de' )->text();
66 $C = ApiResult::META_CONTENT;
67 $I = ApiResult::META_INDEXED_TAG_NAME;
69 return array(
70 array( 'wikitext', 'de', true,
71 array(
72 'errors' => array(
73 'err' => array(
74 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
75 $I => 'error',
78 'warnings' => array(
79 'string' => array(
80 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
81 $I => 'warning',
85 array(
86 'errors' => array(
87 'errWithData' => array(
88 array( 'code' => 'overriddenCode', 'text' => $mainpageText,
89 'overriddenData' => true, $C => 'text' ),
90 $I => 'error',
93 'warnings' => array(
94 'messageWithData' => array(
95 array( 'code' => 'overriddenCode', 'text' => $mainpageText,
96 'overriddenData' => true, $C => 'text' ),
97 $I => 'warning',
99 'message' => array(
100 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
101 $I => 'warning',
103 'foo' => array(
104 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
105 array( 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ),
106 $I => 'warning',
110 array(
111 'errors' => array(
112 'status' => array(
113 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
114 array( 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ),
115 $I => 'error',
118 'warnings' => array(
119 'status' => array(
120 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
121 array( 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ),
122 array( 'code' => 'overriddenCode', 'text' => $mainpageText,
123 'overriddenData' => true, $C => 'text' ),
124 $I => 'warning',
129 array( 'raw', 'fr', true,
130 array(
131 'errors' => array(
132 'err' => array(
133 array(
134 'code' => 'mainpage',
135 'message' => 'mainpage',
136 'params' => array( $I => 'param' )
138 $I => 'error',
141 'warnings' => array(
142 'string' => array(
143 array(
144 'code' => 'mainpage',
145 'message' => 'mainpage',
146 'params' => array( $I => 'param' )
148 $I => 'warning',
152 array(
153 'errors' => array(
154 'errWithData' => array(
155 array(
156 'code' => 'overriddenCode',
157 'message' => 'mainpage',
158 'params' => array( $I => 'param' ),
159 'overriddenData' => true
161 $I => 'error',
164 'warnings' => array(
165 'messageWithData' => array(
166 array(
167 'code' => 'overriddenCode',
168 'message' => 'mainpage',
169 'params' => array( $I => 'param' ),
170 'overriddenData' => true
172 $I => 'warning',
174 'message' => array(
175 array(
176 'code' => 'mainpage',
177 'message' => 'mainpage',
178 'params' => array( $I => 'param' )
180 $I => 'warning',
182 'foo' => array(
183 array(
184 'code' => 'mainpage',
185 'message' => 'mainpage',
186 'params' => array( $I => 'param' )
188 array(
189 'code' => 'parentheses',
190 'message' => 'parentheses',
191 'params' => array( 'foobar', $I => 'param' )
193 $I => 'warning',
197 array(
198 'errors' => array(
199 'status' => array(
200 array(
201 'code' => 'mainpage',
202 'message' => 'mainpage',
203 'params' => array( $I => 'param' )
205 array(
206 'code' => 'parentheses',
207 'message' => 'parentheses',
208 'params' => array( 'foobar', $I => 'param' )
210 $I => 'error',
213 'warnings' => array(
214 'status' => array(
215 array(
216 'code' => 'mainpage',
217 'message' => 'mainpage',
218 'params' => array( $I => 'param' )
220 array(
221 'code' => 'parentheses',
222 'message' => 'parentheses',
223 'params' => array( 'foobar', $I => 'param' )
225 array(
226 'code' => 'overriddenCode',
227 'message' => 'mainpage',
228 'params' => array( $I => 'param' ),
229 'overriddenData' => true
231 $I => 'warning',
236 array( 'none', 'fr', true,
237 array(
238 'errors' => array(
239 'err' => array(
240 array( 'code' => 'mainpage' ),
241 $I => 'error',
244 'warnings' => array(
245 'string' => array(
246 array( 'code' => 'mainpage' ),
247 $I => 'warning',
251 array(
252 'errors' => array(
253 'errWithData' => array(
254 array( 'code' => 'overriddenCode', 'overriddenData' => true ),
255 $I => 'error',
258 'warnings' => array(
259 'messageWithData' => array(
260 array( 'code' => 'overriddenCode', 'overriddenData' => true ),
261 $I => 'warning',
263 'message' => array(
264 array( 'code' => 'mainpage' ),
265 $I => 'warning',
267 'foo' => array(
268 array( 'code' => 'mainpage' ),
269 array( 'code' => 'parentheses' ),
270 $I => 'warning',
274 array(
275 'errors' => array(
276 'status' => array(
277 array( 'code' => 'mainpage' ),
278 array( 'code' => 'parentheses' ),
279 $I => 'error',
282 'warnings' => array(
283 'status' => array(
284 array( 'code' => 'mainpage' ),
285 array( 'code' => 'parentheses' ),
286 array( 'code' => 'overriddenCode', 'overriddenData' => true ),
287 $I => 'warning',
296 * @covers ApiErrorFormatter_BackCompat
298 public function testErrorFormatterBC() {
299 $mainpagePlain = wfMessage( 'mainpage' )->useDatabase( false )->plain();
300 $parensPlain = wfMessage( 'parentheses', 'foobar' )->useDatabase( false )->plain();
302 $result = new ApiResult( 8388608 );
303 $formatter = new ApiErrorFormatter_BackCompat( $result );
305 $formatter->addWarning( 'string', 'mainpage' );
306 $formatter->addError( 'err', 'mainpage' );
307 $this->assertSame( array(
308 'error' => array(
309 'code' => 'mainpage',
310 'info' => $mainpagePlain,
312 'warnings' => array(
313 'string' => array(
314 'warnings' => $mainpagePlain,
315 ApiResult::META_CONTENT => 'warnings',
318 ApiResult::META_TYPE => 'assoc',
319 ), $result->getResultData(), 'Simple test' );
321 $result->reset();
322 $formatter->addWarning( 'foo', 'mainpage' );
323 $formatter->addWarning( 'foo', 'mainpage' );
324 $formatter->addWarning( 'foo', array( 'parentheses', 'foobar' ) );
325 $msg1 = wfMessage( 'mainpage' );
326 $formatter->addWarning( 'message', $msg1 );
327 $msg2 = new ApiMessage( 'mainpage', 'overriddenCode', array( 'overriddenData' => true ) );
328 $formatter->addWarning( 'messageWithData', $msg2 );
329 $formatter->addError( 'errWithData', $msg2 );
330 $this->assertSame( array(
331 'error' => array(
332 'code' => 'overriddenCode',
333 'info' => $mainpagePlain,
334 'overriddenData' => true,
336 'warnings' => array(
337 'messageWithData' => array(
338 'warnings' => $mainpagePlain,
339 ApiResult::META_CONTENT => 'warnings',
341 'message' => array(
342 'warnings' => $mainpagePlain,
343 ApiResult::META_CONTENT => 'warnings',
345 'foo' => array(
346 'warnings' => "$mainpagePlain\n$parensPlain",
347 ApiResult::META_CONTENT => 'warnings',
350 ApiResult::META_TYPE => 'assoc',
351 ), $result->getResultData(), 'Complex test' );
353 $result->reset();
354 $status = Status::newGood();
355 $status->warning( 'mainpage' );
356 $status->warning( 'parentheses', 'foobar' );
357 $status->warning( $msg1 );
358 $status->warning( $msg2 );
359 $status->error( 'mainpage' );
360 $status->error( 'parentheses', 'foobar' );
361 $formatter->addMessagesFromStatus( 'status', $status );
362 $this->assertSame( array(
363 'error' => array(
364 'code' => 'parentheses',
365 'info' => $parensPlain,
367 'warnings' => array(
368 'status' => array(
369 'warnings' => "$mainpagePlain\n$parensPlain",
370 ApiResult::META_CONTENT => 'warnings',
373 ApiResult::META_TYPE => 'assoc',
374 ), $result->getResultData(), 'Status test' );
376 $I = ApiResult::META_INDEXED_TAG_NAME;
377 $this->assertSame(
378 array(
379 array(
380 'type' => 'error',
381 'message' => 'mainpage',
382 'params' => array( $I => 'param' )
384 array(
385 'type' => 'error',
386 'message' => 'parentheses',
387 'params' => array( 'foobar', $I => 'param' )
389 $I => 'error',
391 $formatter->arrayFromStatus( $status, 'error' ),
392 'arrayFromStatus test for error'
394 $this->assertSame(
395 array(
396 array(
397 'type' => 'warning',
398 'message' => 'mainpage',
399 'params' => array( $I => 'param' )
401 array(
402 'type' => 'warning',
403 'message' => 'parentheses',
404 'params' => array( 'foobar', $I => 'param' )
406 array(
407 'message' => 'mainpage',
408 'params' => array( $I => 'param' ),
409 'type' => 'warning'
411 array(
412 'message' => 'mainpage',
413 'params' => array( $I => 'param' ),
414 'type' => 'warning'
416 $I => 'warning',
418 $formatter->arrayFromStatus( $status, 'warning' ),
419 'arrayFromStatus test for warning'