Debian Codegraph @

tests/phpunit/includes/api/query/ApiQuerySiteinfoTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api\Query;
4
5 use MediaWiki\Language\LanguageCode;
6 use MediaWiki\Language\LanguageConverter;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\MainConfigSchema;
9 use MediaWiki\Message\Message;
10 use MediaWiki\Registration\ExtensionRegistry;
11 use MediaWiki\SiteStats\SiteStats;
12 use MediaWiki\Tests\Api\ApiTestCase;
13 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
14 use MediaWiki\Title\Title;
15 use Skin;
16 use Wikimedia\Composer\ComposerInstalled;
17 use Wikimedia\Rdbms\LoadBalancer;
18 use Wikimedia\TestingAccessWrapper;
19
20 /**
21 * @group API
22 * @group medium
23 * @group Database
24 *
25 * @covers MediaWiki\Api\ApiQuerySiteinfo
26 */
27 class ApiQuerySiteinfoTest extends ApiTestCase {
28 use TempUserTestTrait;
29
30 /** @var array[]|null */
31 private $originalRegistryLoaded = null;
32
33 protected function tearDown(): void {
34 if ( $this->originalRegistryLoaded !== null ) {
35 $reg = TestingAccessWrapper::newFromObject( ExtensionRegistry::getInstance() );
36 $reg->loaded = $this->originalRegistryLoaded;
37 $this->originalRegistryLoaded = null;
38 }
39 parent::tearDown();
40 }
41
42 // We don't try to test every single thing for every category, just a sample
43 protected function doQuery( $siprop = null, $extraParams = [] ) {
44 $params = [ 'action' => 'query', 'meta' => 'siteinfo' ];
45 if ( $siprop !== null ) {
46 $params['siprop'] = $siprop;
47 }
48 $params = array_merge( $params, $extraParams );
49
50 $res = $this->doApiRequest( $params );
51
52 $this->assertArrayNotHasKey( 'warnings', $res[0] );
53 $this->assertCount( 1, $res[0]['query'] );
54
55 return $res[0]['query'][$siprop === null ? 'general' : $siprop];
56 }
57
58 public function testGeneral() {
59 $this->overrideConfigValues( [
60 MainConfigNames::AllowExternalImagesFrom => '//localhost/',
61 MainConfigNames::MainPageIsDomainRoot => true,
62 ] );
63
64 $data = $this->doQuery();
65
66 $this->assertSame( Title::newMainPage()->getPrefixedText(), $data['mainpage'] );
67 $this->assertSame( PHP_VERSION, $data['phpversion'] );
68 $this->assertSame( [ '//localhost/' ], $data['externalimages'] );
69 $this->assertTrue( $data['mainpageisdomainroot'] );
70 }
71
72 public function testLinkPrefixCharset() {
73 $contLang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'ar' );
74 $this->setContentLang( $contLang );
75 $this->assertTrue( $contLang->linkPrefixExtension() );
76
77 $data = $this->doQuery();
78
79 $this->assertSame( $contLang->linkPrefixCharset(), $data['linkprefixcharset'] );
80 }
81
82 public function testVariants() {
83 $contLang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'zh' );
84 $converter = $this->getServiceContainer()->getLanguageConverterFactory()->getLanguageConverter( $contLang );
85 $this->setContentLang( $contLang );
86 $this->assertTrue( $converter->hasVariants() );
87
88 $data = $this->doQuery();
89
90 $expected = array_map(
91 static function ( $code ) use ( $contLang ) {
92 return [ 'code' => $code, 'name' => $contLang->getVariantname( $code ) ];
93 },
94 $converter->getVariants()
95 );
96
97 $this->assertSame( $expected, $data['variants'] );
98 }
99
100 public function testReadOnly() {
101 // Create the test user before making the DB readonly
102 $this->getTestSysop()->getUser();
103 $svc = $this->getServiceContainer()->getReadOnlyMode();
104 $svc->setReason( 'Need more donations' );
105 try {
106 $data = $this->doQuery();
107 } finally {
108 $svc->setReason( false );
109 }
110
111 $this->assertTrue( $data['readonly'] );
112 $this->assertSame( 'Need more donations', $data['readonlyreason'] );
113 }
114
115 public function testNamespacesBasic() {
116 $this->assertSame(
117 array_keys( $this->getServiceContainer()->getContentLanguage()->getFormattedNamespaces() ),
118 array_keys( $this->doQuery( 'namespaces' ) )
119 );
120 }
121
122 public function testNamespacesExtraNS() {
123 $this->overrideConfigValue( MainConfigNames::ExtraNamespaces, [ '138' => 'Testing' ] );
124 $this->assertSame(
125 array_keys( $this->getServiceContainer()->getContentLanguage()->getFormattedNamespaces() ),
126 array_keys( $this->doQuery( 'namespaces' ) )
127 );
128 }
129
130 public function testNamespacesProtection() {
131 $this->overrideConfigValue(
132 MainConfigNames::NamespaceProtection,
133 [
134 '0' => '',
135 '2' => [ '' ],
136 '4' => 'editsemiprotected',
137 '8' => [
138 'editinterface',
139 'noratelimit'
140 ],
141 '14' => [
142 'move-categorypages',
143 ''
144 ]
145 ]
146 );
147 $data = $this->doQuery( 'namespaces' );
148 $this->assertArrayNotHasKey( 'namespaceprotection', $data['0'] );
149 $this->assertArrayNotHasKey( 'namespaceprotection', $data['2'] );
150 $this->assertSame( 'editsemiprotected', $data['4']['namespaceprotection'] );
151 $this->assertSame( 'editinterface|noratelimit', $data['8']['namespaceprotection'] );
152 $this->assertSame( 'move-categorypages', $data['14']['namespaceprotection'] );
153 }
154
155 public function testNamespaceAliases() {
156 // XXX: why does this fail when the en-x-piglatin variant is enabled?
157 $this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, false );
158
159 $expected = $this->getServiceContainer()->getContentLanguage()->getNamespaceAliases();
160 $expected = array_map(
161 static function ( $key, $val ) {
162 return [ 'id' => $val, 'alias' => strtr( $key, '_', ' ' ) ];
163 },
164 array_keys( $expected ),
165 $expected
166 );
167
168 $this->assertSame( $expected, $this->doQuery( 'namespacealiases' ) );
169 }
170
171 public function testSpecialPageAliases() {
172 $this->assertSameSize(
173 $this->getServiceContainer()->getSpecialPageFactory()->getNames(),
174 $this->doQuery( 'specialpagealiases' )
175 );
176 }
177
178 public function testMagicWords() {
179 $this->assertSameSize(
180 $this->getServiceContainer()->getContentLanguage()->getMagicWords(),
181 $this->doQuery( 'magicwords' )
182 );
183 }
184
185 /**
186 * @dataProvider interwikiMapProvider
187 */
188 public function testInterwikiMap( $filter ) {
189 $this->overrideConfigValues( [
190 MainConfigNames::ExtraInterlanguageLinkPrefixes => [ 'self' ],
191 MainConfigNames::ExtraLanguageNames => [ 'self' => 'Recursion' ],
192 MainConfigNames::LocalInterwikis => [ 'self' ],
193 MainConfigNames::Server => 'https://local.example',
194 MainConfigNames::ScriptPath => '/w',
195 ] );
196
197 $this->getDb()->newInsertQueryBuilder()
198 ->insertInto( 'interwiki' )
199 ->ignore()
200 ->row( [
201 'iw_prefix' => 'self',
202 'iw_url' => 'https://local.example/w/index.php?title=$1',
203 'iw_api' => 'https://local.example/w/api.php',
204 'iw_wikiid' => 'somedbname',
205 'iw_local' => true,
206 'iw_trans' => true,
207 ] )
208 ->row( [
209 'iw_prefix' => 'foreign',
210 'iw_url' => '//foreign.example/wiki/$1',
211 'iw_api' => '',
212 'iw_wikiid' => '',
213 'iw_local' => false,
214 'iw_trans' => false,
215 ] )
216 ->caller( __METHOD__ )
217 ->execute();
218
219 $this->getServiceContainer()->getMessageCache()->enable();
220
221 $this->editPage( 'MediaWiki:Interlanguage-link-self', 'Self!' );
222 $this->editPage( 'MediaWiki:Interlanguage-link-sitename-self', 'Circular logic' );
223
224 $expected = [];
225
226 if ( $filter === null || $filter === '!local' ) {
227 $expected[] = [
228 'prefix' => 'foreign',
229 'url' => 'http://foreign.example/wiki/$1',
230 'protorel' => true,
231 ];
232 }
233 if ( $filter === null || $filter === 'local' ) {
234 $expected[] = [
235 'prefix' => 'self',
236 'local' => true,
237 'trans' => true,
238 'language' => 'Recursion',
239 'bcp47' => 'self',
240 'localinterwiki' => true,
241 'extralanglink' => true,
242 'code' => 'self',
243 'linktext' => 'Self!',
244 'sitename' => 'Circular logic',
245 'url' => 'https://local.example/w/index.php?title=$1',
246 'protorel' => false,
247 'wikiid' => 'somedbname',
248 'api' => 'https://local.example/w/api.php',
249 ];
250 }
251
252 $data = $this->doQuery( 'interwikimap',
253 $filter === null ? [] : [ 'sifilteriw' => $filter ] );
254
255 $this->assertSame( $expected, $data );
256 }
257
258 public static function interwikiMapProvider() {
259 return [ [ 'local' ], [ '!local' ], [ null ] ];
260 }
261
262 /**
263 * @dataProvider dbReplLagProvider
264 */
265 public function testDbReplLagInfo( $showHostnames, $includeAll ) {
266 if ( !$showHostnames && $includeAll ) {
267 $this->expectApiErrorCode( 'includeAllDenied' );
268 }
269
270 // Force creation of the test user before mocking the database.
271 $this->getTestSysop()->getUser();
272
273 $mockLB = $this->createNoOpMock( LoadBalancer::class, [ 'getMaxLag', 'getLagTimes',
274 'getServerName', 'getLocalDomainID' ] );
275 $mockLB->method( 'getMaxLag' )->willReturn( [ null, 7, 1 ] );
276 $mockLB->method( 'getLagTimes' )->willReturn( [ 5, 7 ] );
277 $mockLB->method( 'getServerName' )->willReturnMap( [
278 [ 0, 'apple' ],
279 [ 1, 'carrot' ]
280 ] );
281 $mockLB->method( 'getLocalDomainID' )->willReturn( 'testdomain' );
282 $this->setService( 'DBLoadBalancer', $mockLB );
283
284 $this->overrideConfigValue( MainConfigNames::ShowHostnames, $showHostnames );
285
286 $expected = [];
287 if ( $includeAll ) {
288 $expected[] = [ 'host' => $showHostnames ? 'apple' : '', 'lag' => 5 ];
289 }
290 $expected[] = [ 'host' => $showHostnames ? 'carrot' : '', 'lag' => 7 ];
291
292 $data = $this->doQuery( 'dbrepllag', $includeAll ? [ 'sishowalldb' => '' ] : [] );
293
294 $this->assertSame( $expected, $data );
295 }
296
297 public static function dbReplLagProvider() {
298 return [
299 'no hostnames, no showalldb' => [ false, false ],
300 'no hostnames, showalldb' => [ false, true ],
301 'hostnames, no showalldb' => [ true, false ],
302 'hostnames, showalldb' => [ true, true ]
303 ];
304 }
305
306 public function testStatistics() {
307 $this->setTemporaryHook( 'APIQuerySiteInfoStatisticsInfo',
308 static function ( &$data ) {
309 $data['addedstats'] = 42;
310 }
311 );
312
313 $expected = [
314 'pages' => intval( SiteStats::pages() ),
315 'articles' => intval( SiteStats::articles() ),
316 'edits' => intval( SiteStats::edits() ),
317 'images' => intval( SiteStats::images() ),
318 'users' => intval( SiteStats::users() ),
319 'activeusers' => intval( SiteStats::activeUsers() ),
320 'admins' => intval( SiteStats::numberingroup( 'sysop' ) ),
321 'jobs' => intval( SiteStats::jobs() ),
322 'addedstats' => 42,
323 ];
324
325 $this->assertSame( $expected, $this->doQuery( 'statistics' ) );
326 }
327
328 /**
329 * @dataProvider groupsProvider
330 */
331 public function testUserGroups( $numInGroup ) {
332 global $wgGroupPermissions, $wgAutopromote;
333
334 $this->setGroupPermissions( 'viscount', 'perambulate', 'yes' );
335 $this->setGroupPermissions( 'viscount', 'legislate', '0' );
336 $this->overrideConfigValues( [
337 MainConfigNames::AddGroups => [ 'viscount' => true, 'bot' => [] ],
338 MainConfigNames::RemoveGroups => [ 'viscount' => [ 'sysop' ], 'bot' => [ '*', 'earl' ] ],
339 MainConfigNames::GroupsAddToSelf => [ 'bot' => [ 'bureaucrat', 'sysop' ] ],
340 MainConfigNames::GroupsRemoveFromSelf => [ 'bot' => [ 'bot' ] ],
341 ] );
342
343 $data = $this->doQuery( 'usergroups', $numInGroup ? [ 'sinumberingroup' => '' ] : [] );
344
345 $names = array_column( $data, 'name' );
346
347 $this->assertSame( array_keys( $wgGroupPermissions ), $names );
348 $userAllGroups = $this->getServiceContainer()->getUserGroupManager()->listAllGroups();
349
350 foreach ( $data as $val ) {
351 if ( !$numInGroup ) {
352 $expectedSize = null;
353 } elseif ( $val['name'] === 'user' ) {
354 $expectedSize = SiteStats::users();
355 } elseif ( $val['name'] === '*' || isset( $wgAutopromote[$val['name']] ) ) {
356 $expectedSize = null;
357 } else {
358 $expectedSize = SiteStats::numberingroup( $val['name'] );
359 }
360
361 if ( $expectedSize === null ) {
362 $this->assertArrayNotHasKey( 'number', $val );
363 } else {
364 $this->assertSame( $expectedSize, $val['number'] );
365 }
366
367 if ( $val['name'] === 'viscount' ) {
368 $this->assertSame( [ 'perambulate' ], $val['rights'] );
369 $this->assertSame( $userAllGroups, $val['add'] );
370 } elseif ( $val['name'] === 'bot' ) {
371 $this->assertArrayNotHasKey( 'add', $val );
372 $this->assertArrayNotHasKey( 'remove', $val );
373 $this->assertSame( [ 'bureaucrat', 'sysop' ], $val['add-self'] );
374 $this->assertSame( [ 'bot' ], $val['remove-self'] );
375 }
376 }
377 }
378
379 public function testAutoCreateTempUser() {
380 $this->disableAutoCreateTempUser( [ 'reservedPattern' => null ] );
381 $this->assertSame(
382 [ 'enabled' => false ],
383 $this->doQuery( 'autocreatetempuser' ),
384 'When disabled, no other properties are present'
385 );
386
387 $this->enableAutoCreateTempUser( [
388 'reservedPattern' => null,
389 ] );
390 $this->assertArrayEquals(
391 [
392 'enabled' => true,
393 'matchPatterns' => [ '~$1' ],
394 ],
395 $this->doQuery( 'autocreatetempuser' ),
396 false,
397 true,
398 'When enabled, some properties are filled in or cleaned up'
399 );
400 }
401
402 public function testFileExtensions() {
403 // Add duplicate
404 $this->overrideConfigValue( MainConfigNames::FileExtensions, [ 'png', 'gif', 'jpg', 'png' ] );
405
406 $expected = [ [ 'ext' => 'png' ], [ 'ext' => 'gif' ], [ 'ext' => 'jpg' ] ];
407
408 $this->assertSame( $expected, $this->doQuery( 'fileextensions' ) );
409 }
410
411 public static function groupsProvider() {
412 return [
413 'numingroup' => [ true ],
414 'nonumingroup' => [ false ],
415 ];
416 }
417
418 public function testInstalledLibraries() {
419 // @todo Test no installed.json? Moving installed.json to a different name temporarily
420 // seems a bit scary, but I don't see any other way to do it.
421 //
422 // @todo Install extensions/skins somehow so that we can test they're filtered out
423 global $IP;
424
425 $path = "$IP/vendor/composer/installed.json";
426 if ( !is_file( $path ) ) {
427 $this->markTestSkipped( 'No installed libraries' );
428 }
429
430 $expected = ( new ComposerInstalled( $path ) )->getInstalledDependencies();
431
432 $expected = array_filter( $expected,
433 static function ( $info ) {
434 return !str_starts_with( $info['type'], 'mediawiki-' );
435 }
436 );
437
438 $expected = array_map(
439 static function ( $name, $info ) {
440 return [ 'name' => $name, 'version' => $info['version'] ];
441 },
442 array_keys( $expected ),
443 array_values( $expected )
444 );
445
446 $this->assertSame( $expected, $this->doQuery( 'libraries' ) );
447 }
448
449 public function testExtensions() {
450 $tmpdir = $this->getNewTempDirectory();
451 touch( "$tmpdir/ErsatzExtension.php" );
452 touch( "$tmpdir/LICENSE" );
453 touch( "$tmpdir/AUTHORS.txt" );
454
455 $val = [
456 'path' => "$tmpdir/ErsatzExtension.php",
457 'name' => 'Ersatz Extension',
458 'namemsg' => 'ersatz-extension-name',
459 'author' => 'John Smith',
460 'version' => '0.0.2',
461 'url' => 'https://www.example.com/software/ersatz-extension',
462 'description' => 'An extension that is not what it seems.',
463 'descriptionmsg' => 'ersatz-extension-desc',
464 'license-name' => 'PD',
465 ];
466
467 $this->overrideConfigValue( MainConfigNames::ExtensionCredits, [ 'api' => [
468 $val,
469 [
470 'author' => [ 'John Smith', 'John Smith Jr.', '...' ],
471 'descriptionmsg' => [ 'another-extension-desc', 'param' ] ],
472 ] ] );
473 // Make the main registry empty
474 // TODO: Make ExtensionRegistry an injected service?
475 $reg = TestingAccessWrapper::newFromObject( ExtensionRegistry::getInstance() );
476 $this->originalRegistryLoaded = $reg->loaded;
477 $reg->loaded = [];
478
479 $data = $this->doQuery( 'extensions' );
480
481 $this->assertCount( 2, $data );
482
483 $this->assertSame( 'api', $data[0]['type'] );
484
485 $sharedKeys = [ 'name', 'namemsg', 'description', 'descriptionmsg', 'author', 'url',
486 'version', 'license-name' ];
487 foreach ( $sharedKeys as $key ) {
488 $this->assertSame( $val[$key], $data[0][$key] );
489 }
490
491 // @todo Test git info
492
493 $this->assertSame(
494 Title::makeTitle( NS_SPECIAL, 'Version/License/Ersatz Extension' )->getLinkURL(),
495 $data[0]['license']
496 );
497
498 $this->assertSame(
499 Title::makeTitle( NS_SPECIAL, 'Version/Credits/Ersatz Extension' )->getLinkURL(),
500 $data[0]['credits']
501 );
502
503 $this->assertSame( 'another-extension-desc', $data[1]['descriptionmsg'] );
504 $this->assertSame( [ 'param' ], $data[1]['descriptionmsgparams'] );
505 $this->assertSame( 'John Smith, John Smith Jr., ...', $data[1]['author'] );
506 }
507
508 /**
509 * @dataProvider rightsInfoProvider
510 */
511 public function testRightsInfo( $page, $url, $text, $expectedUrl, $expectedText ) {
512 $this->overrideConfigValues( [
513 MainConfigNames::Server => 'https://local.example',
514 MainConfigNames::RightsPage => $page,
515 MainConfigNames::RightsUrl => $url,
516 MainConfigNames::RightsText => $text,
517 ] );
518 $this->assertSame(
519 [ 'url' => $expectedUrl, 'text' => $expectedText ],
520 $this->doQuery( 'rightsinfo' )
521 );
522
523 // The installer sets these options to empty string if not specified otherwise,
524 // test that this behaves the same as null.
525 $this->overrideConfigValues( [
526 MainConfigNames::RightsPage => $page ?? '',
527 MainConfigNames::RightsUrl => $url ?? '',
528 MainConfigNames::RightsText => $text ?? '',
529 ] );
530 $this->assertSame(
531 [ 'url' => $expectedUrl, 'text' => $expectedText ],
532 $this->doQuery( 'rightsinfo' ),
533 'empty string behaves the same as null'
534 );
535 }
536
537 public static function rightsInfoProvider() {
538 $licenseTitleUrl = 'https://local.example/wiki/License';
539 $licenseUrl = 'http://license.example/';
540
541 return [
542 'No rights info' => [ null, null, null, '', '' ],
543 'Only page' => [ 'License', null, null, $licenseTitleUrl, 'License' ],
544 'Only URL' => [ null, $licenseUrl, null, $licenseUrl, '' ],
545 'Only text' => [ null, null, '!!!', '', '!!!' ],
546 // URL is ignored if page is specified
547 'Page and URL' => [ 'License', $licenseUrl, null, $licenseTitleUrl, 'License' ],
548 'URL and text' => [ null, $licenseUrl, '!!!', $licenseUrl, '!!!' ],
549 'Page and text' => [ 'License', null, '!!!', $licenseTitleUrl, '!!!' ],
550 'Page and URL and text' => [ 'License', $licenseUrl, '!!!', $licenseTitleUrl, '!!!' ],
551 'Pagename "0"' => [ '0', null, null, 'https://local.example/wiki/0', '0' ],
552 'URL "0"' => [ null, '0', null, '0', '' ],
553 'Text "0"' => [ null, null, '0', '', '0' ],
554 ];
555 }
556
557 public function testRestrictions() {
558 global $wgRestrictionTypes, $wgRestrictionLevels, $wgCascadingRestrictionLevels,
559 $wgSemiprotectedRestrictionLevels;
560
561 $this->assertSame( [
562 'types' => $wgRestrictionTypes,
563 'levels' => $wgRestrictionLevels,
564 'cascadinglevels' => $wgCascadingRestrictionLevels,
565 'semiprotectedlevels' => $wgSemiprotectedRestrictionLevels,
566 ], $this->doQuery( 'restrictions' ) );
567 }
568
569 /**
570 * @dataProvider languagesProvider
571 */
572 public function testLanguages( $langCode ) {
573 $expected = $this->getServiceContainer()
574 ->getLanguageNameUtils()
575 ->getLanguageNames( (string)$langCode );
576
577 $expected = array_map(
578 static function ( $code, $name ) {
579 return [
580 'code' => $code,
581 'bcp47' => LanguageCode::bcp47( $code ),
582 'name' => $name
583 ];
584 },
585 array_keys( $expected ),
586 array_values( $expected )
587 );
588
589 $data = $this->doQuery( 'languages',
590 $langCode !== null ? [ 'siinlanguagecode' => $langCode ] : [] );
591
592 $this->assertSame( $expected, $data );
593 }
594
595 public static function languagesProvider() {
596 return [ [ null ], [ 'fr' ] ];
597 }
598
599 public function testLanguageVariants() {
600 $expectedKeys = array_filter( LanguageConverter::$languagesWithVariants,
601 function ( $langCode ) {
602 $lang = $this->getServiceContainer()->getLanguageFactory()
603 ->getLanguage( $langCode );
604 $converter = $this->getServiceContainer()->getLanguageConverterFactory()
605 ->getLanguageConverter( $lang );
606 return $converter->hasVariants();
607 }
608 );
609 sort( $expectedKeys );
610
611 $this->assertSame( $expectedKeys, array_keys( $this->doQuery( 'languagevariants' ) ) );
612 }
613
614 public function testLanguageVariantsDisabled() {
615 $this->overrideConfigValue( MainConfigNames::DisableLangConversion, true );
616
617 $this->assertSame( [], $this->doQuery( 'languagevariants' ) );
618 }
619
620 /**
621 * @todo Test a skin with a description that's known to be different in a different language.
622 * Vector will do, but it's not installed by default.
623 *
624 * @todo Test that an invalid language code doesn't actually try reading any messages
625 *
626 * @dataProvider skinsProvider
627 */
628 public function testSkins( $code ) {
629 $data = $this->doQuery( 'skins', $code !== null ? [ 'siinlanguagecode' => $code ] : [] );
630 $services = $this->getServiceContainer();
631 $skinFactory = $services->getSkinFactory();
632 $skinNames = $skinFactory->getInstalledSkins();
633 $expectedAllowed = $skinFactory->getAllowedSkins();
634 $expectedDefault = Skin::normalizeKey( 'default' );
635 $languageNameUtils = $services->getLanguageNameUtils();
636
637 $i = 0;
638 foreach ( $skinNames as $name => $displayName ) {
639 $this->assertSame( $name, $data[$i]['code'] );
640
641 $msg = wfMessage( "skinname-$name" );
642 if ( $code && $languageNameUtils->isValidCode( $code ) ) {
643 $msg->inLanguage( $code );
644 } else {
645 $msg->inContentLanguage();
646 }
647 if ( $msg->exists() ) {
648 $displayName = $msg->text();
649 }
650 $this->assertSame( $displayName, $data[$i]['name'] );
651
652 if ( !isset( $expectedAllowed[$name] ) ) {
653 $this->assertTrue( $data[$i]['unusable'], "$name must be unusable" );
654 }
655 if ( $name === $expectedDefault ) {
656 $this->assertTrue( $data[$i]['default'], "$expectedDefault must be default" );
657 }
658 $i++;
659 }
660 }
661
662 public function skinsProvider() {
663 return [
664 'No language specified' => [ null ],
665 'Czech' => [ 'cs' ],
666 'Invalid language' => [ '/invalid/' ],
667 ];
668 }
669
670 public function testExtensionTags() {
671 $expected = array_map(
672 static function ( $tag ) {
673 return "<$tag>";
674 },
675 $this->getServiceContainer()->getParser()->getTags()
676 );
677
678 $this->assertSame( $expected, $this->doQuery( 'extensiontags' ) );
679 }
680
681 public function testFunctionHooks() {
682 $this->assertSame( $this->getServiceContainer()->getParser()->getFunctionHooks(),
683 $this->doQuery( 'functionhooks' ) );
684 }
685
686 public function testVariables() {
687 $this->assertSame(
688 $this->getServiceContainer()->getMagicWordFactory()->getVariableIDs(),
689 $this->doQuery( 'variables' )
690 );
691 }
692
693 public function testProtocols() {
694 $urlProtocol = MainConfigSchema::getDefaultValue(
695 MainConfigNames::UrlProtocols
696 );
697 $this->assertSame( $urlProtocol, $this->doQuery( 'protocols' ) );
698 }
699
700 public function testDefaultOptions() {
701 $this->assertSame(
702 $this->getServiceContainer()->getUserOptionsLookup()->getDefaultOptions(),
703 $this->doQuery( 'defaultoptions' )
704 );
705 }
706
707 public function testUploadDialog() {
708 global $wgUploadDialog;
709
710 $this->assertSame( $wgUploadDialog, $this->doQuery( 'uploaddialog' ) );
711 }
712
713 public function testGetHooks() {
714 // Make sure there's something to report on
715 $this->setTemporaryHook( 'somehook',
716 static function () {
717 }
718 );
719
720 $hookContainer = $this->getServiceContainer()->getHookContainer();
721 $expectedNames = $hookContainer->getHookNames();
722 $actualNames = array_column( $this->doQuery( 'showhooks' ), 'name' );
723
724 $this->assertArrayEquals( $expectedNames, $actualNames );
725 }
726
727 public function testContinuation() {
728 // Use $wgUrlProtocols as easy example for forging the
729 // size of the API response
730 $protocol = 'foo://';
731 $size = strlen( $protocol );
732 $protocols = [ $protocol ];
733
734 $this->overrideConfigValues( [
735 MainConfigNames::UrlProtocols => [ $protocol ],
736 MainConfigNames::APIMaxResultSize => $size,
737 ] );
738
739 $res = $this->doApiRequest( [
740 'action' => 'query',
741 'meta' => 'siteinfo',
742 'siprop' => 'protocols|languages',
743 ] );
744
745 $this->assertSame(
746 wfMessage( 'apiwarn-truncatedresult', Message::numParam( $size ) )
747 ->text(),
748 $res[0]['warnings']['result']['warnings']
749 );
750
751 $this->assertSame( $protocols, $res[0]['query']['protocols'] );
752 $this->assertArrayNotHasKey( 'languages', $res[0] );
753 $this->assertTrue( $res[0]['batchcomplete'], 'batchcomplete should be true' );
754 $this->assertSame( [ 'siprop' => 'languages', 'continue' => '-||' ], $res[0]['continue'] );
755 }
756
757 /**
758 * @dataProvider provideAutopromote
759 */
760 public function testAutopromote( $config, $expected ) {
761 $this->overrideConfigValues( [
762 MainConfigNames::Autopromote => $config,
763 MainConfigNames::AutoConfirmCount => 10,
764 MainConfigNames::AutoConfirmAge => 345600,
765 ] );
766 $this->assertSame( $expected, $this->doQuery( 'autopromote' ) );
767 }
768
769 public static function provideAutopromote() {
770 yield 'simple' => [
771 [
772 // edit count >= 10 and age >= 4 days
773 'simple' => [ '&',
774 [ APCOND_EDITCOUNT, 10 ],
775 [ APCOND_AGE, 345600 ],
776 ],
777 ],
778 [
779 'simple' => [
780 'operand' => '&',
781 0 => [
782 'condname' => 'APCOND_EDITCOUNT',
783 'params' => [ 10 ]
784 ],
785 1 => [
786 'condname' => 'APCOND_AGE',
787 'params' => [ 345600 ]
788 ]
789 ]
790 ]
791 ];
792
793 // Test case to check default of null is replaced with value of appropriate $wg
794 yield 'simple-use-wg' => [
795 [
796 'simple-use-wg' => [ '&',
797 [ APCOND_EDITCOUNT, null ],
798 [ APCOND_AGE, null ],
799 ],
800 ],
801 [
802 'simple-use-wg' => [
803 'operand' => '&',
804 0 => [
805 'condname' => 'APCOND_EDITCOUNT',
806 'params' => [ 10 ]
807 ],
808 1 => [
809 'condname' => 'APCOND_AGE',
810 'params' => [ 345600 ]
811 ]
812 ]
813 ]
814 ];
815
816 yield 'trivial' => [
817 [
818 'trivial' => APCOND_EMAILCONFIRMED,
819 ],
820 [
821 'trivial' => [
822 0 => [
823 'condname' => 'APCOND_EMAILCONFIRMED',
824 'params' => []
825 ]
826 ],
827 ]
828 ];
829
830 yield 'multiple-copies-of-condition' => [
831 [
832 // In both groups 'foo' and 'bar', or in group 'baz'
833 'multiple-copies-of-condition' => [ '|',
834 [ APCOND_INGROUPS, 'foo', 'bar' ],
835 [ APCOND_INGROUPS, 'baz' ],
836 ],
837 ],
838 [
839 'multiple-copies-of-condition' => [
840 'operand' => '|',
841 0 => [
842 'condname' => 'APCOND_INGROUPS',
843 'params' => [ 'foo', 'bar' ]
844 ],
845 1 => [
846 'condname' => 'APCOND_INGROUPS',
847 'params' => [ 'baz' ]
848 ]
849 ]
850 ]
851 ];
852
853 yield 'complicated' => [
854 [
855 // confirmed email, or their edit count >= 100 and either they
856 // created their account a year ago or it has been 10 days since
857 // their first edit, or if they're in both groups 'group1' and
858 // 'group2'. Except for users in the 'bad' group.
859 'complicated' => [ '&',
860 [ '|',
861 APCOND_EMAILCONFIRMED,
862 [ '&',
863 [ APCOND_EDITCOUNT, 100 ],
864 [ '|',
865 [ APCOND_AGE, 525600 * 60 ],
866 [ APCOND_AGE_FROM_EDIT, 864000 ],
867 ],
868 ],
869 [ APCOND_INGROUPS, 'group1', 'group2' ],
870 ],
871 [ '!', [ APCOND_INGROUPS, 'bad' ] ],
872 ],
873 ],
874 [
875 'complicated' => [
876 'operand' => '&',
877 0 => [
878 'operand' => '|',
879 0 => [
880 'condname' => 'APCOND_EMAILCONFIRMED',
881 'params' => []
882 ],
883 1 => [
884 'operand' => '&',
885 0 => [
886 'condname' => 'APCOND_EDITCOUNT',
887 'params' => [ 100 ]
888 ],
889 1 => [
890 'operand' => '|',
891 0 => [
892 'condname' => 'APCOND_AGE',
893 'params' => [ 31536000 ]
894 ],
895 1 => [
896 'condname' => 'APCOND_AGE_FROM_EDIT',
897 'params' => [ 864000 ]
898 ]
899 ]
900 ],
901 2 => [
902 'condname' => 'APCOND_INGROUPS',
903 'params' => [ 'group1', 'group2' ]
904 ]
905 ],
906 1 => [
907 'operand' => '!',
908 0 => [
909 'condname' => 'APCOND_INGROUPS',
910 'params' => [ 'bad' ]
911 ]
912 ]
913 ]
914 ]
915 ];
916
917 // Find an undefined APCOND integer
918 $constants = [];
919 foreach ( get_defined_constants() as $k => $v ) {
920 if ( strpos( $k, 'APCOND_' ) !== false ) {
921 $constants[$v] = $k;
922 }
923 }
924 $bogusCond = 9000;
925 while ( isset( $constants[$bogusCond] ) ) {
926 $bogusCond++;
927 }
928 $bogusCond2 = $bogusCond + 1;
929 while ( isset( $constants[$bogusCond2] ) ) {
930 $bogusCond2++;
931 }
932
933 yield 'bad-cond-1' => [
934 [
935 // unknown APCOND constant. Might be handled by an extension that
936 // didn't define a constant with the expected name.
937 'bad-cond' => 'bogus',
938 ],
939 [
940 'bad-cond' => [
941 'bogus'
942 ],
943 ]
944 ];
945 yield 'bad-cond-2' => [
946 [
947 'bad-cond' => $bogusCond,
948 ],
949 [
950 'bad-cond' => [
951 0 => [
952 'condname' => false,
953 'params' => []
954 ]
955 ],
956 ]
957 ];
958 yield 'bad-cond-3' => [
959 [
960 'bad-cond' => [ 'bogus', 'bogus?', APCOND_EMAILCONFIRMED, $bogusCond, $bogusCond2 ],
961 ],
962 [
963 'bad-cond' => [
964 'bogus',
965 'bogus?',
966 APCOND_EMAILCONFIRMED,
967 $bogusCond,
968 $bogusCond2
969 ],
970 ]
971 ];
972 yield 'bad-cond-4' => [
973 [
974 'bad-cond' => [ '&',
975 'bogus1',
976 'bogus2',
977 APCOND_EMAILCONFIRMED,
978 $bogusCond,
979 $bogusCond2,
980 ],
981 ],
982 [
983 'bad-cond' => [
984 'operand' => '&',
985 0 => 'bogus1',
986 1 => 'bogus2',
987 2 => [
988 'condname' => 'APCOND_EMAILCONFIRMED',
989 'params' => []
990 ],
991 3 => [
992 'condname' => false,
993 'params' => []
994 ],
995 4 => [
996 'condname' => false,
997 'params' => []
998 ]
999 ]
1000 ]
1001 ];
1002 }
1003
1004 public function testAutopromoteOnceDefault() {
1005 // PHP doesn't like empty nested arrays nested in arrays...
1006 $value = [
1007 'onEdit' => [],
1008 'onView' => [],
1009 ];
1010 $this->testAutopromoteOnce( $value, $value );
1011 }
1012
1013 /**
1014 * @dataProvider provideAutopromoteOnce
1015 */
1016 public function testAutopromoteOnce( $config, $expected ) {
1017 $this->overrideConfigValues( [
1018 MainConfigNames::AutopromoteOnce => $config,
1019 MainConfigNames::AutoConfirmCount => 10,
1020 MainConfigNames::AutoConfirmAge => 345600,
1021 ] );
1022 $this->assertSame( $expected, $this->doQuery( 'autopromoteonce' ) );
1023 }
1024
1025 public static function provideAutopromoteOnce() {
1026 yield 'simple' => [
1027 [
1028 'onEdit' => [
1029 // edit count >= 10 and age >= 4 days
1030 'simple' => [ '&',
1031 [ APCOND_EDITCOUNT, 10 ],
1032 [ APCOND_AGE, 345600 ],
1033 ],
1034 ],
1035 ],
1036 [
1037 'onEdit' => [
1038 'simple' => [
1039 'operand' => '&',
1040 0 => [
1041 'condname' => 'APCOND_EDITCOUNT',
1042 'params' => [ 10 ]
1043 ],
1044 1 => [
1045 'condname' => 'APCOND_AGE',
1046 'params' => [ 345600 ]
1047 ]
1048 ]
1049 ]
1050 ]
1051 ];
1052 }
1053 }