7 * @covers ApiQuerySiteinfo
9 class ApiQuerySiteinfoTest
extends ApiTestCase
{
10 // We don't try to test every single thing for every category, just a sample
12 protected function doQuery( $siprop = null, $extraParams = [] ) {
13 $params = [ 'action' => 'query', 'meta' => 'siteinfo' ];
14 if ( $siprop !== null ) {
15 $params['siprop'] = $siprop;
17 $params = array_merge( $params, $extraParams );
19 $res = $this->doApiRequest( $params );
21 $this->assertArrayNotHasKey( 'warnings', $res[0] );
22 $this->assertCount( 1, $res[0]['query'] );
24 return $res[0]['query'][$siprop === null ?
'general' : $siprop];
27 public function testGeneral() {
28 $this->setMwGlobals( [
29 'wgAllowExternalImagesFrom' => '//localhost/',
32 $data = $this->doQuery();
34 $this->assertSame( Title
::newMainPage()->getPrefixedText(), $data['mainpage'] );
35 $this->assertSame( PHP_VERSION
, $data['phpversion'] );
36 $this->assertSame( [ '//localhost/' ], $data['externalimages'] );
39 public function testLinkPrefixCharset() {
42 $this->setContentLang( 'ar' );
43 $this->assertTrue( $wgContLang->linkPrefixExtension(), 'Sanity check' );
45 $data = $this->doQuery();
47 $this->assertSame( $wgContLang->linkPrefixCharset(), $data['linkprefixcharset'] );
50 public function testVariants() {
53 $this->setContentLang( 'zh' );
54 $this->assertTrue( $wgContLang->hasVariants(), 'Sanity check' );
56 $data = $this->doQuery();
58 $expected = array_map(
59 function ( $code ) use ( $wgContLang ) {
60 return [ 'code' => $code, 'name' => $wgContLang->getVariantname( $code ) ];
62 $wgContLang->getVariants()
65 $this->assertSame( $expected, $data['variants'] );
68 public function testReadOnly() {
69 $svc = \MediaWiki\MediaWikiServices
::getInstance()->getReadOnlyMode();
70 $svc->setReason( 'Need more donations' );
72 $data = $this->doQuery();
74 $svc->setReason( false );
77 $this->assertTrue( $data['readonly'] );
78 $this->assertSame( 'Need more donations', $data['readonlyreason'] );
81 public function testNamespaces() {
84 $this->setMwGlobals( 'wgExtraNamespaces', [ '138' => 'Testing' ] );
86 $this->assertSame( array_keys( $wgContLang->getFormattedNamespaces() ),
87 array_keys( $this->doQuery( 'namespaces' ) ) );
90 public function testNamespaceAliases() {
91 global $wgNamespaceAliases, $wgContLang;
93 $expected = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
94 $expected = array_map(
95 function ( $key, $val ) {
96 return [ 'id' => $val, 'alias' => strtr( $key, '_', ' ' ) ];
98 array_keys( $expected ),
102 // Test that we don't list duplicates
103 $this->mergeMwGlobalArrayValue( 'wgNamespaceAliases', [ 'Talk' => NS_TALK
] );
105 $this->assertSame( $expected, $this->doQuery( 'namespacealiases' ) );
108 public function testSpecialPageAliases() {
110 count( SpecialPageFactory
::getNames() ),
111 $this->doQuery( 'specialpagealiases' )
115 public function testMagicWords() {
119 count( $wgContLang->getMagicWords() ),
120 $this->doQuery( 'magicwords' )
125 * @dataProvider interwikiMapProvider
127 public function testInterwikiMap( $filter ) {
128 global $wgServer, $wgScriptPath;
130 $dbw = wfGetDB( DB_MASTER
);
135 'iw_prefix' => 'self',
136 'iw_url' => "$wgServer$wgScriptPath/index.php?title=$1",
137 'iw_api' => "$wgServer$wgScriptPath/api.php",
138 'iw_wikiid' => 'somedbname',
143 'iw_prefix' => 'foreign',
144 'iw_url' => '//foreign.example/wiki/$1',
154 $this->tablesUsed
[] = 'interwiki';
156 $this->setMwGlobals( [
157 'wgLocalInterwikis' => [ 'self' ],
158 'wgExtraInterlanguageLinkPrefixes' => [ 'self' ],
159 'wgExtraLanguageNames' => [ 'self' => 'Recursion' ],
162 MessageCache
::singleton()->enable();
164 $this->editPage( 'MediaWiki:Interlanguage-link-self', 'Self!' );
165 $this->editPage( 'MediaWiki:Interlanguage-link-sitename-self', 'Circular logic' );
169 if ( $filter === null ||
$filter === '!local' ) {
171 'prefix' => 'foreign',
172 'url' => wfExpandUrl( '//foreign.example/wiki/$1', PROTO_CURRENT
),
176 if ( $filter === null ||
$filter === 'local' ) {
181 'language' => 'Recursion',
182 'localinterwiki' => true,
183 'extralanglink' => true,
184 'linktext' => 'Self!',
185 'sitename' => 'Circular logic',
186 'url' => "$wgServer$wgScriptPath/index.php?title=$1",
188 'wikiid' => 'somedbname',
189 'api' => "$wgServer$wgScriptPath/api.php",
193 $data = $this->doQuery( 'interwikimap',
194 $filter === null ?
[] : [ 'sifilteriw' => $filter ] );
196 $this->assertSame( $expected, $data );
199 public function interwikiMapProvider() {
200 return [ [ 'local' ], [ '!local' ], [ null ] ];
204 * @dataProvider dbReplLagProvider
206 public function testDbReplLagInfo( $showHostnames, $includeAll ) {
207 if ( !$showHostnames && $includeAll ) {
208 $this->setExpectedApiException( 'apierror-siteinfo-includealldenied' );
211 $mockLB = $this->getMockBuilder( LoadBalancer
::class )
212 ->disableOriginalConstructor()
213 ->setMethods( [ 'getMaxLag', 'getLagTimes', 'getServerName', '__destruct' ] )
215 $mockLB->method( 'getMaxLag' )->willReturn( [ null, 7, 1 ] );
216 $mockLB->method( 'getLagTimes' )->willReturn( [ 5, 7 ] );
217 $mockLB->method( 'getServerName' )->will( $this->returnValueMap( [
218 [ 0, 'apple' ], [ 1, 'carrot' ]
220 $this->setService( 'DBLoadBalancer', $mockLB );
222 $this->setMwGlobals( 'wgShowHostnames', $showHostnames );
226 $expected[] = [ 'host' => $showHostnames ?
'apple' : '', 'lag' => 5 ];
228 $expected[] = [ 'host' => $showHostnames ?
'carrot' : '', 'lag' => 7 ];
230 $data = $this->doQuery( 'dbrepllag', $includeAll ?
[ 'sishowalldb' => '' ] : [] );
232 $this->assertSame( $expected, $data );
235 public function dbReplLagProvider() {
237 'no hostnames, no showalldb' => [ false, false ],
238 'no hostnames, showalldb' => [ false, true ],
239 'hostnames, no showalldb' => [ true, false ],
240 'hostnames, showalldb' => [ true, true ]
244 public function testStatistics() {
245 $this->setTemporaryHook( 'APIQuerySiteInfoStatisticsInfo',
246 function ( &$data ) {
247 $data['addedstats'] = 42;
252 'pages' => intval( SiteStats
::pages() ),
253 'articles' => intval( SiteStats
::articles() ),
254 'edits' => intval( SiteStats
::edits() ),
255 'images' => intval( SiteStats
::images() ),
256 'users' => intval( SiteStats
::users() ),
257 'activeusers' => intval( SiteStats
::activeUsers() ),
258 'admins' => intval( SiteStats
::numberingroup( 'sysop' ) ),
259 'jobs' => intval( SiteStats
::jobs() ),
263 $this->assertSame( $expected, $this->doQuery( 'statistics' ) );
267 * @dataProvider groupsProvider
269 public function testUserGroups( $numInGroup ) {
270 global $wgGroupPermissions, $wgAutopromote;
272 $this->setGroupPermissions( 'viscount', 'perambulate', 'yes' );
273 $this->setGroupPermissions( 'viscount', 'legislate', '0' );
274 $this->setMwGlobals( [
275 'wgAddGroups' => [ 'viscount' => true, 'bot' => [] ],
276 'wgRemoveGroups' => [ 'viscount' => [ 'sysop' ], 'bot' => [ '*', 'earl' ] ],
277 'wgGroupsAddToSelf' => [ 'bot' => [ 'bureaucrat', 'sysop' ] ],
278 'wgGroupsRemoveFromSelf' => [ 'bot' => [ 'bot' ] ],
281 $data = $this->doQuery( 'usergroups', $numInGroup ?
[ 'sinumberingroup' => '' ] : [] );
290 $this->assertSame( array_keys( $wgGroupPermissions ), $names );
292 foreach ( $data as $val ) {
293 if ( !$numInGroup ) {
294 $expectedSize = null;
295 } elseif ( $val['name'] === 'user' ) {
296 $expectedSize = SiteStats
::users();
297 } elseif ( $val['name'] === '*' ||
isset( $wgAutopromote[$val['name']] ) ) {
298 $expectedSize = null;
300 $expectedSize = SiteStats
::numberingroup( $val['name'] );
303 if ( $expectedSize === null ) {
304 $this->assertArrayNotHasKey( 'number', $val );
306 $this->assertSame( $expectedSize, $val['number'] );
309 if ( $val['name'] === 'viscount' ) {
310 $viscountFound = true;
311 $this->assertSame( [ 'perambulate' ], $val['rights'] );
312 $this->assertSame( User
::getAllGroups(), $val['add'] );
313 } elseif ( $val['name'] === 'bot' ) {
314 $this->assertArrayNotHasKey( 'add', $val );
315 $this->assertArrayNotHasKey( 'remove', $val );
316 $this->assertSame( [ 'bureaucrat', 'sysop' ], $val['add-self'] );
317 $this->assertSame( [ 'bot' ], $val['remove-self'] );
322 public function testFileExtensions() {
323 global $wgFileExtensions;
325 $this->stashMwGlobals( 'wgFileExtensions' );
327 $wgFileExtensions[] = 'png';
329 $expected = array_map(
331 return [ 'ext' => $val ];
333 array_unique( $wgFileExtensions )
336 $this->assertSame( $expected, $this->doQuery( 'fileextensions' ) );
339 public function groupsProvider() {
341 'numingroup' => [ true ],
342 'nonumingroup' => [ false ],
346 public function testInstalledLibraries() {
347 // @todo Test no installed.json? Moving installed.json to a different name temporarily
348 // seems a bit scary, but I don't see any other way to do it.
350 // @todo Install extensions/skins somehow so that we can test they're filtered out
353 $path = "$IP/vendor/composer/installed.json";
354 if ( !file_exists( $path ) ) {
355 $this->markTestSkipped( 'No installed libraries' );
358 $expected = ( new ComposerInstalled( $path ) )->getInstalledDependencies();
360 $expected = array_filter( $expected,
362 return strpos( $info['type'], 'mediawiki-' ) !== 0;
366 $expected = array_map(
367 function ( $name, $info ) {
368 return [ 'name' => $name, 'version' => $info['version'] ];
370 array_keys( $expected ),
371 array_values( $expected )
374 $this->assertSame( $expected, $this->doQuery( 'libraries' ) );
377 public function testExtensions() {
378 $tmpdir = $this->getNewTempDirectory();
379 touch( "$tmpdir/ErsatzExtension.php" );
380 touch( "$tmpdir/LICENSE" );
381 touch( "$tmpdir/AUTHORS.txt" );
384 'path' => "$tmpdir/ErsatzExtension.php",
385 'name' => 'Ersatz Extension',
386 'namemsg' => 'ersatz-extension-name',
387 'author' => 'John Smith',
388 'version' => '0.0.2',
389 'url' => 'https://www.example.com/software/ersatz-extension',
390 'description' => 'An extension that is not what it seems.',
391 'descriptionmsg' => 'ersatz-extension-desc',
392 'license-name' => 'PD',
395 $this->setMwGlobals( 'wgExtensionCredits', [ 'api' => [
398 'author' => [ 'John Smith', 'John Smith Jr.', '...' ],
399 'descriptionmsg' => [ 'another-extension-desc', 'param' ] ],
402 $data = $this->doQuery( 'extensions' );
404 $this->assertCount( 2, $data );
406 $this->assertSame( 'api', $data[0]['type'] );
408 $sharedKeys = [ 'name', 'namemsg', 'description', 'descriptionmsg', 'author', 'url',
409 'version', 'license-name' ];
410 foreach ( $sharedKeys as $key ) {
411 $this->assertSame( $val[$key], $data[0][$key] );
414 // @todo Test git info
417 Title
::newFromText( 'Special:Version/License/Ersatz Extension' )->getLinkURL(),
422 Title
::newFromText( 'Special:Version/Credits/Ersatz Extension' )->getLinkURL(),
426 $this->assertSame( 'another-extension-desc', $data[1]['descriptionmsg'] );
427 $this->assertSame( [ 'param' ], $data[1]['descriptionmsgparams'] );
428 $this->assertSame( 'John Smith, John Smith Jr., ...', $data[1]['author'] );
432 * @dataProvider rightsInfoProvider
434 public function testRightsInfo( $page, $url, $text, $expectedUrl, $expectedText ) {
435 $this->setMwGlobals( [
436 'wgRightsPage' => $page,
437 'wgRightsUrl' => $url,
438 'wgRightsText' => $text,
442 [ 'url' => $expectedUrl, 'text' => $expectedText ],
443 $this->doQuery( 'rightsinfo' )
447 public function rightsInfoProvider() {
448 $textUrl = wfExpandUrl( Title
::newFromText( 'License' ), PROTO_CURRENT
);
449 $url = 'http://license.example/';
452 'No rights info' => [ null, null, null, '', '' ],
453 'Only page' => [ 'License', null, null, $textUrl, 'License' ],
454 'Only URL' => [ null, $url, null, $url, '' ],
455 'Only text' => [ null, null, '!!!', '', '!!!' ],
456 // URL is ignored if page is specified
457 'Page and URL' => [ 'License', $url, null, $textUrl, 'License' ],
458 'URL and text' => [ null, $url, '!!!', $url, '!!!' ],
459 'Page and text' => [ 'License', null, '!!!', $textUrl, '!!!' ],
460 'Page and URL and text' => [ 'License', $url, '!!!', $textUrl, '!!!' ],
461 'Pagename "0"' => [ '0', null, null,
462 wfExpandUrl( Title
::newFromText( '0' ), PROTO_CURRENT
), '0' ],
463 'URL "0"' => [ null, '0', null, '0', '' ],
464 'Text "0"' => [ null, null, '0', '', '0' ],
468 public function testRestrictions() {
469 global $wgRestrictionTypes, $wgRestrictionLevels, $wgCascadingRestrictionLevels,
470 $wgSemiprotectedRestrictionLevels;
473 'types' => $wgRestrictionTypes,
474 'levels' => $wgRestrictionLevels,
475 'cascadinglevels' => $wgCascadingRestrictionLevels,
476 'semiprotectedlevels' => $wgSemiprotectedRestrictionLevels,
477 ], $this->doQuery( 'restrictions' ) );
481 * @dataProvider languagesProvider
483 public function testLanguages( $langCode ) {
484 $expected = Language
::fetchLanguageNames( (string)$langCode );
486 $expected = array_map(
487 function ( $code, $name ) {
493 array_keys( $expected ),
494 array_values( $expected )
497 $data = $this->doQuery( 'languages',
498 $langCode !== null ?
[ 'siinlanguagecode' => $langCode ] : [] );
500 $this->assertSame( $expected, $data );
503 public function languagesProvider() {
504 return [ [ null ], [ 'fr' ] ];
507 public function testLanguageVariants() {
508 $expectedKeys = array_filter( LanguageConverter
::$languagesWithVariants,
509 function ( $langCode ) {
510 return !Language
::factory( $langCode )->getConverter() instanceof FakeConverter
;
513 sort( $expectedKeys );
515 $this->assertSame( $expectedKeys, array_keys( $this->doQuery( 'languagevariants' ) ) );
518 public function testLanguageVariantsDisabled() {
519 $this->setMwGlobals( 'wgDisableLangConversion', true );
521 $this->assertSame( [], $this->doQuery( 'languagevariants' ) );
525 * @todo Test a skin with a description that's known to be different in a different language.
526 * Vector will do, but it's not installed by default.
528 * @todo Test that an invalid language code doesn't actually try reading any messages
530 * @dataProvider skinsProvider
532 public function testSkins( $code ) {
533 $data = $this->doQuery( 'skins', $code !== null ?
[ 'siinlanguagecode' => $code ] : [] );
535 $expectedAllowed = Skin
::getAllowedSkins();
536 $expectedDefault = Skin
::normalizeKey( 'default' );
539 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
540 $this->assertSame( $name, $data[$i]['code'] );
542 $msg = wfMessage( "skinname-$name" );
543 if ( $code && Language
::isValidCode( $code ) ) {
544 $msg->inLanguage( $code );
546 $msg->inContentLanguage();
548 if ( $msg->exists() ) {
549 $displayName = $msg->text();
551 $this->assertSame( $displayName, $data[$i]['name'] );
553 if ( !isset( $expectedAllowed[$name] ) ) {
554 $this->assertTrue( $data[$i]['unusable'], "$name must be unusable" );
556 if ( $name === $expectedDefault ) {
557 $this->assertTrue( $data[$i]['default'], "$expectedDefault must be default" );
563 public function skinsProvider() {
565 'No language specified' => [ null ],
567 'Invalid language' => [ '/invalid/' ],
571 public function testExtensionTags() {
574 $expected = array_map(
581 $this->assertSame( $expected, $this->doQuery( 'extensiontags' ) );
584 public function testFunctionHooks() {
587 $this->assertSame( $wgParser->getFunctionHooks(), $this->doQuery( 'functionhooks' ) );
590 public function testVariables() {
591 $this->assertSame( MagicWord
::getVariableIDs(), $this->doQuery( 'variables' ) );
594 public function testProtocols() {
595 global $wgUrlProtocols;
597 $this->assertSame( $wgUrlProtocols, $this->doQuery( 'protocols' ) );
600 public function testDefaultOptions() {
601 $this->assertSame( User
::getDefaultOptions(), $this->doQuery( 'defaultoptions' ) );
604 public function testUploadDialog() {
605 global $wgUploadDialog;
607 $this->assertSame( $wgUploadDialog, $this->doQuery( 'uploaddialog' ) );
610 public function testGetHooks() {
613 // Make sure there's something to report on
614 $this->setTemporaryHook( 'somehook',
620 $expectedNames = $wgHooks;
621 ksort( $expectedNames );
623 $actualNames = array_map(
627 $this->doQuery( 'showhooks' )
630 $this->assertSame( array_keys( $expectedNames ), $actualNames );
633 public function testContinuation() {
634 // We make lots and lots of URL protocols that are each 100 bytes
635 global $wgAPIMaxResultSize, $wgUrlProtocols;
637 $this->setMwGlobals( 'wgUrlProtocols', [] );
639 // Just under the limit
640 $chunks = $wgAPIMaxResultSize / 100 - 1;
642 for ( $i = 0; $i < $chunks; $i++
) {
643 $wgUrlProtocols[] = substr( str_repeat( "$i ", 50 ), 0, 100 );
646 $res = $this->doApiRequest( [
648 'meta' => 'siteinfo',
649 'siprop' => 'protocols|languages',
653 wfMessage( 'apiwarn-truncatedresult', Message
::numParam( $wgAPIMaxResultSize ) )
655 $res[0]['warnings']['result']['warnings']
658 $this->assertSame( $wgUrlProtocols, $res[0]['query']['protocols'] );
659 $this->assertArrayNotHasKey( 'languages', $res[0] );
660 $this->assertTrue( $res[0]['batchcomplete'], 'batchcomplete should be true' );
661 $this->assertSame( [ 'siprop' => 'languages', 'continue' => '-||' ], $res[0]['continue'] );