MediaWiki  1.28.0
ApiErrorFormatterTest.php
Go to the documentation of this file.
1 <?php
2 
7 
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 );
17 
18  // Add default type
19  $expect1[ApiResult::META_TYPE] = 'assoc';
20  $expect2[ApiResult::META_TYPE] = 'assoc';
21  $expect3[ApiResult::META_TYPE] = 'assoc';
22 
23  $formatter->addWarning( 'string', 'mainpage' );
24  $formatter->addError( 'err', 'mainpage' );
25  $this->assertSame( $expect1, $result->getResultData(), 'Simple test' );
26 
27  $result->reset();
28  $formatter->addWarning( 'foo', 'mainpage' );
29  $formatter->addWarning( 'foo', 'mainpage' );
30  $formatter->addWarning( 'foo', [ 'parentheses', 'foobar' ] );
31  $msg1 = wfMessage( 'mainpage' );
32  $formatter->addWarning( 'message', $msg1 );
33  $msg2 = new ApiMessage( 'mainpage', 'overriddenCode', [ 'overriddenData' => true ] );
34  $formatter->addWarning( 'messageWithData', $msg2 );
35  $formatter->addError( 'errWithData', $msg2 );
36  $this->assertSame( $expect2, $result->getResultData(), 'Complex test' );
37 
38  $result->reset();
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' );
48 
49  $this->assertSame(
50  $expect3['errors']['status'],
51  $formatter->arrayFromStatus( $status, 'error' ),
52  'arrayFromStatus test for error'
53  );
54  $this->assertSame(
55  $expect3['warnings']['status'],
56  $formatter->arrayFromStatus( $status, 'warning' ),
57  'arrayFromStatus test for warning'
58  );
59  }
60 
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();
68 
69  return [
70  [ 'wikitext', 'de', true,
71  [
72  'errors' => [
73  'err' => [
74  [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ],
75  $I => 'error',
76  ],
77  ],
78  'warnings' => [
79  'string' => [
80  [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ],
81  $I => 'warning',
82  ],
83  ],
84  ],
85  [
86  'errors' => [
87  'errWithData' => [
88  [ 'code' => 'overriddenCode', 'text' => $mainpageText,
89  'overriddenData' => true, $C => 'text' ],
90  $I => 'error',
91  ],
92  ],
93  'warnings' => [
94  'messageWithData' => [
95  [ 'code' => 'overriddenCode', 'text' => $mainpageText,
96  'overriddenData' => true, $C => 'text' ],
97  $I => 'warning',
98  ],
99  'message' => [
100  [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ],
101  $I => 'warning',
102  ],
103  'foo' => [
104  [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ],
105  [ 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ],
106  $I => 'warning',
107  ],
108  ],
109  ],
110  [
111  'errors' => [
112  'status' => [
113  [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ],
114  [ 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ],
115  $I => 'error',
116  ],
117  ],
118  'warnings' => [
119  'status' => [
120  [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ],
121  [ 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ],
122  [ 'code' => 'overriddenCode', 'text' => $mainpageText,
123  'overriddenData' => true, $C => 'text' ],
124  $I => 'warning',
125  ],
126  ],
127  ],
128  ],
129  [ 'raw', 'fr', true,
130  [
131  'errors' => [
132  'err' => [
133  [
134  'code' => 'mainpage',
135  'key' => 'mainpage',
136  'params' => [ $I => 'param' ]
137  ],
138  $I => 'error',
139  ],
140  ],
141  'warnings' => [
142  'string' => [
143  [
144  'code' => 'mainpage',
145  'key' => 'mainpage',
146  'params' => [ $I => 'param' ]
147  ],
148  $I => 'warning',
149  ],
150  ],
151  ],
152  [
153  'errors' => [
154  'errWithData' => [
155  [
156  'code' => 'overriddenCode',
157  'key' => 'mainpage',
158  'params' => [ $I => 'param' ],
159  'overriddenData' => true
160  ],
161  $I => 'error',
162  ],
163  ],
164  'warnings' => [
165  'messageWithData' => [
166  [
167  'code' => 'overriddenCode',
168  'key' => 'mainpage',
169  'params' => [ $I => 'param' ],
170  'overriddenData' => true
171  ],
172  $I => 'warning',
173  ],
174  'message' => [
175  [
176  'code' => 'mainpage',
177  'key' => 'mainpage',
178  'params' => [ $I => 'param' ]
179  ],
180  $I => 'warning',
181  ],
182  'foo' => [
183  [
184  'code' => 'mainpage',
185  'key' => 'mainpage',
186  'params' => [ $I => 'param' ]
187  ],
188  [
189  'code' => 'parentheses',
190  'key' => 'parentheses',
191  'params' => [ 'foobar', $I => 'param' ]
192  ],
193  $I => 'warning',
194  ],
195  ],
196  ],
197  [
198  'errors' => [
199  'status' => [
200  [
201  'code' => 'mainpage',
202  'key' => 'mainpage',
203  'params' => [ $I => 'param' ]
204  ],
205  [
206  'code' => 'parentheses',
207  'key' => 'parentheses',
208  'params' => [ 'foobar', $I => 'param' ]
209  ],
210  $I => 'error',
211  ],
212  ],
213  'warnings' => [
214  'status' => [
215  [
216  'code' => 'mainpage',
217  'key' => 'mainpage',
218  'params' => [ $I => 'param' ]
219  ],
220  [
221  'code' => 'parentheses',
222  'key' => 'parentheses',
223  'params' => [ 'foobar', $I => 'param' ]
224  ],
225  [
226  'code' => 'overriddenCode',
227  'key' => 'mainpage',
228  'params' => [ $I => 'param' ],
229  'overriddenData' => true
230  ],
231  $I => 'warning',
232  ],
233  ],
234  ],
235  ],
236  [ 'none', 'fr', true,
237  [
238  'errors' => [
239  'err' => [
240  [ 'code' => 'mainpage' ],
241  $I => 'error',
242  ],
243  ],
244  'warnings' => [
245  'string' => [
246  [ 'code' => 'mainpage' ],
247  $I => 'warning',
248  ],
249  ],
250  ],
251  [
252  'errors' => [
253  'errWithData' => [
254  [ 'code' => 'overriddenCode', 'overriddenData' => true ],
255  $I => 'error',
256  ],
257  ],
258  'warnings' => [
259  'messageWithData' => [
260  [ 'code' => 'overriddenCode', 'overriddenData' => true ],
261  $I => 'warning',
262  ],
263  'message' => [
264  [ 'code' => 'mainpage' ],
265  $I => 'warning',
266  ],
267  'foo' => [
268  [ 'code' => 'mainpage' ],
269  [ 'code' => 'parentheses' ],
270  $I => 'warning',
271  ],
272  ],
273  ],
274  [
275  'errors' => [
276  'status' => [
277  [ 'code' => 'mainpage' ],
278  [ 'code' => 'parentheses' ],
279  $I => 'error',
280  ],
281  ],
282  'warnings' => [
283  'status' => [
284  [ 'code' => 'mainpage' ],
285  [ 'code' => 'parentheses' ],
286  [ 'code' => 'overriddenCode', 'overriddenData' => true ],
287  $I => 'warning',
288  ],
289  ],
290  ],
291  ],
292  ];
293  }
294 
298  public function testErrorFormatterBC() {
299  $mainpagePlain = wfMessage( 'mainpage' )->useDatabase( false )->plain();
300  $parensPlain = wfMessage( 'parentheses', 'foobar' )->useDatabase( false )->plain();
301 
302  $result = new ApiResult( 8388608 );
303  $formatter = new ApiErrorFormatter_BackCompat( $result );
304 
305  $formatter->addWarning( 'string', 'mainpage' );
306  $formatter->addError( 'err', 'mainpage' );
307  $this->assertSame( [
308  'error' => [
309  'code' => 'mainpage',
310  'info' => $mainpagePlain,
311  ],
312  'warnings' => [
313  'string' => [
314  'warnings' => $mainpagePlain,
315  ApiResult::META_CONTENT => 'warnings',
316  ],
317  ],
318  ApiResult::META_TYPE => 'assoc',
319  ], $result->getResultData(), 'Simple test' );
320 
321  $result->reset();
322  $formatter->addWarning( 'foo', 'mainpage' );
323  $formatter->addWarning( 'foo', 'mainpage' );
324  $formatter->addWarning( 'foo', [ 'parentheses', 'foobar' ] );
325  $msg1 = wfMessage( 'mainpage' );
326  $formatter->addWarning( 'message', $msg1 );
327  $msg2 = new ApiMessage( 'mainpage', 'overriddenCode', [ 'overriddenData' => true ] );
328  $formatter->addWarning( 'messageWithData', $msg2 );
329  $formatter->addError( 'errWithData', $msg2 );
330  $this->assertSame( [
331  'error' => [
332  'code' => 'overriddenCode',
333  'info' => $mainpagePlain,
334  'overriddenData' => true,
335  ],
336  'warnings' => [
337  'messageWithData' => [
338  'warnings' => $mainpagePlain,
339  ApiResult::META_CONTENT => 'warnings',
340  ],
341  'message' => [
342  'warnings' => $mainpagePlain,
343  ApiResult::META_CONTENT => 'warnings',
344  ],
345  'foo' => [
346  'warnings' => "$mainpagePlain\n$parensPlain",
347  ApiResult::META_CONTENT => 'warnings',
348  ],
349  ],
350  ApiResult::META_TYPE => 'assoc',
351  ], $result->getResultData(), 'Complex test' );
352 
353  $result->reset();
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( [
363  'error' => [
364  'code' => 'parentheses',
365  'info' => $parensPlain,
366  ],
367  'warnings' => [
368  'status' => [
369  'warnings' => "$mainpagePlain\n$parensPlain",
370  ApiResult::META_CONTENT => 'warnings',
371  ],
372  ],
373  ApiResult::META_TYPE => 'assoc',
374  ], $result->getResultData(), 'Status test' );
375 
377  $this->assertSame(
378  [
379  [
380  'type' => 'error',
381  'message' => 'mainpage',
382  'params' => [ $I => 'param' ]
383  ],
384  [
385  'type' => 'error',
386  'message' => 'parentheses',
387  'params' => [ 'foobar', $I => 'param' ]
388  ],
389  $I => 'error',
390  ],
391  $formatter->arrayFromStatus( $status, 'error' ),
392  'arrayFromStatus test for error'
393  );
394  $this->assertSame(
395  [
396  [
397  'type' => 'warning',
398  'message' => 'mainpage',
399  'params' => [ $I => 'param' ]
400  ],
401  [
402  'type' => 'warning',
403  'message' => 'parentheses',
404  'params' => [ 'foobar', $I => 'param' ]
405  ],
406  [
407  'message' => 'mainpage',
408  'params' => [ $I => 'param' ],
409  'type' => 'warning'
410  ],
411  [
412  'message' => 'mainpage',
413  'params' => [ $I => 'param' ],
414  'type' => 'warning'
415  ],
416  $I => 'warning',
417  ],
418  $formatter->arrayFromStatus( $status, 'warning' ),
419  'arrayFromStatus test for warning'
420  );
421  }
422 
423 }
Formats errors and warnings for the API, and add them to the associated ApiResult.
if(!isset($args[0])) $lang
const META_TYPE
Key for the 'type' metadata item.
Definition: ApiResult.php:108
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message.Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item.Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page.Return false to stop further processing of the tag $reader:XMLReader object &$pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision.Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag.Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload.Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports.&$fullInterwikiPrefix:Interwiki prefix, may contain colons.&$pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable.Can be used to lazy-load the import sources list.&$importSources:The value of $wgImportSources.Modify as necessary.See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page.$context:IContextSource object &$pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect.&$title:Title object for the current page &$request:WebRequest &$ignoreRedirect:boolean to skip redirect check &$target:Title/string of redirect target &$article:Article object 'InternalParseBeforeLinks':during Parser's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InternalParseBeforeSanitize':during Parser's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings.Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not.Return true without providing an interwiki to continue interwiki search.$prefix:interwiki prefix we are looking for.&$iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user's email has been invalidated successfully.$user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification.Callee may modify $url and $query, URL will be constructed as $url.$query &$url:URL to index.php &$query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) &$article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() &$ip:IP being check &$result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from &$allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn't match your organization.$addr:The e-mail address entered by the user &$result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user &$result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we're looking for a messages file for &$file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED!Use $magicWords in a file listed in $wgExtensionMessagesFiles instead.Use this to define synonyms of magic words depending of the language &$magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces.Do not use this hook to add namespaces.Use CanonicalNamespaces for that.&$namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED!Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead.Use to define aliases of special pages names depending of the language &$specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names.&$names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page's language links.This is called in various places to allow extensions to define the effective language links for a page.$title:The page's Title.&$links:Associative array mapping language codes to prefixed links of the form"language:title".&$linkFlags:Associative array mapping prefixed links to arrays of flags.Currently unused, but planned to provide support for marking individual language links in the UI, e.g.for featured articles. 'LanguageSelector':Hook to change the language selector available on a page.$out:The output page.$cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED!Use HtmlPageLinkRendererBegin instead.Used when generating internal and interwiki links in Linker::link(), before processing starts.Return false to skip default processing and return $ret.See documentation for Linker::link() for details on the expected meanings of parameters.$skin:the Skin object $target:the Title that the link is pointing to &$html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1934
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1936
testErrorFormatter($format, $lang, $useDB, $expect1, $expect2, $expect3)
ApiErrorFormatter provideErrorFormatter.
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
Base class that store and restore the Language objects.
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:115
Format errors and warnings in the old style, for backwards compatibility.
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
This class represents the result of the API operations.
Definition: ApiResult.php:33
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
const META_INDEXED_TAG_NAME
Key for the 'indexed tag name' metadata item.
Definition: ApiResult.php:70
testErrorFormatterBC()
ApiErrorFormatter_BackCompat.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
const META_CONTENT
Key for the 'content' metadata item.
Definition: ApiResult.php:88
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:181