6 class ApiErrorFormatterTest
extends MediaWikiLangTestCase
{
9 * @covers ApiErrorFormatter
10 * @dataProvider provideErrorFormatter
12 public function testErrorFormatter( $format, $lang, $useDB,
13 $expect1, $expect2, $expect3
15 $result = new ApiResult( 8388608 );
16 $formatter = new ApiErrorFormatter( $result, Language
::factory( $lang ), $format, $useDB );
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' );
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' );
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' );
50 $expect3['errors']['status'],
51 $formatter->arrayFromStatus( $status, 'error' ),
52 'arrayFromStatus test for error'
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
;
70 array( 'wikitext', 'de', true,
74 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
80 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
87 'errWithData' => array(
88 array( 'code' => 'overriddenCode', 'text' => $mainpageText,
89 'overriddenData' => true, $C => 'text' ),
94 'messageWithData' => array(
95 array( 'code' => 'overriddenCode', 'text' => $mainpageText,
96 'overriddenData' => true, $C => 'text' ),
100 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
104 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
105 array( 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ),
113 array( 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ),
114 array( 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ),
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' ),
129 array( 'raw', 'fr', true,
134 'code' => 'mainpage',
135 'message' => 'mainpage',
136 'params' => array( $I => 'param' )
144 'code' => 'mainpage',
145 'message' => 'mainpage',
146 'params' => array( $I => 'param' )
154 'errWithData' => array(
156 'code' => 'overriddenCode',
157 'message' => 'mainpage',
158 'params' => array( $I => 'param' ),
159 'overriddenData' => true
165 'messageWithData' => array(
167 'code' => 'overriddenCode',
168 'message' => 'mainpage',
169 'params' => array( $I => 'param' ),
170 'overriddenData' => true
176 'code' => 'mainpage',
177 'message' => 'mainpage',
178 'params' => array( $I => 'param' )
184 'code' => 'mainpage',
185 'message' => 'mainpage',
186 'params' => array( $I => 'param' )
189 'code' => 'parentheses',
190 'message' => 'parentheses',
191 'params' => array( 'foobar', $I => 'param' )
201 'code' => 'mainpage',
202 'message' => 'mainpage',
203 'params' => array( $I => 'param' )
206 'code' => 'parentheses',
207 'message' => 'parentheses',
208 'params' => array( 'foobar', $I => 'param' )
216 'code' => 'mainpage',
217 'message' => 'mainpage',
218 'params' => array( $I => 'param' )
221 'code' => 'parentheses',
222 'message' => 'parentheses',
223 'params' => array( 'foobar', $I => 'param' )
226 'code' => 'overriddenCode',
227 'message' => 'mainpage',
228 'params' => array( $I => 'param' ),
229 'overriddenData' => true
236 array( 'none', 'fr', true,
240 array( 'code' => 'mainpage' ),
246 array( 'code' => 'mainpage' ),
253 'errWithData' => array(
254 array( 'code' => 'overriddenCode', 'overriddenData' => true ),
259 'messageWithData' => array(
260 array( 'code' => 'overriddenCode', 'overriddenData' => true ),
264 array( 'code' => 'mainpage' ),
268 array( 'code' => 'mainpage' ),
269 array( 'code' => 'parentheses' ),
277 array( 'code' => 'mainpage' ),
278 array( 'code' => 'parentheses' ),
284 array( 'code' => 'mainpage' ),
285 array( 'code' => 'parentheses' ),
286 array( 'code' => 'overriddenCode', 'overriddenData' => true ),
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(
309 'code' => 'mainpage',
310 'info' => $mainpagePlain,
314 'warnings' => $mainpagePlain,
315 ApiResult
::META_CONTENT
=> 'warnings',
318 ApiResult
::META_TYPE
=> 'assoc',
319 ), $result->getResultData(), 'Simple test' );
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(
332 'code' => 'overriddenCode',
333 'info' => $mainpagePlain,
334 'overriddenData' => true,
337 'messageWithData' => array(
338 'warnings' => $mainpagePlain,
339 ApiResult
::META_CONTENT
=> 'warnings',
342 'warnings' => $mainpagePlain,
343 ApiResult
::META_CONTENT
=> 'warnings',
346 'warnings' => "$mainpagePlain\n$parensPlain",
347 ApiResult
::META_CONTENT
=> 'warnings',
350 ApiResult
::META_TYPE
=> 'assoc',
351 ), $result->getResultData(), 'Complex test' );
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(
364 'code' => 'parentheses',
365 'info' => $parensPlain,
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
;
381 'message' => 'mainpage',
382 'params' => array( $I => 'param' )
386 'message' => 'parentheses',
387 'params' => array( 'foobar', $I => 'param' )
391 $formatter->arrayFromStatus( $status, 'error' ),
392 'arrayFromStatus test for error'
398 'message' => 'mainpage',
399 'params' => array( $I => 'param' )
403 'message' => 'parentheses',
404 'params' => array( 'foobar', $I => 'param' )
407 'message' => 'mainpage',
408 'params' => array( $I => 'param' ),
412 'message' => 'mainpage',
413 'params' => array( $I => 'param' ),
418 $formatter->arrayFromStatus( $status, 'warning' ),
419 'arrayFromStatus test for warning'