Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / structure / ApiStructureTest.php
blobc9c4149e1c419dd8678bff2cf3f81b21226a31ad
1 <?php
3 use MediaWiki\Api\ApiBase;
4 use MediaWiki\Api\ApiDisabled;
5 use MediaWiki\Api\ApiMain;
6 use MediaWiki\Api\ApiModuleManager;
7 use MediaWiki\Api\ApiQueryDisabled;
8 use MediaWiki\Context\RequestContext;
9 use MediaWiki\MainConfigNames;
10 use MediaWiki\Message\Message;
11 use MediaWiki\Title\Title;
12 use Wikimedia\TestingAccessWrapper;
14 /**
15 * Checks that all API modules, core and extensions, conform to the conventions:
16 * - have documentation i18n messages (the test won't catch everything since
17 * i18n messages can vary based on the wiki configuration, but it should
18 * catch many cases for forgotten i18n)
19 * - do not have inconsistencies in the parameter definitions
21 * @group API
22 * @group Database
23 * @coversNothing
25 class ApiStructureTest extends MediaWikiIntegrationTestCase {
27 /** @var ApiMain */
28 private static $main;
30 /** @var array Sets of globals to test. Each array element is input to HashConfig */
31 private static $testGlobals = [
33 MainConfigNames::MiserMode => false,
36 MainConfigNames::MiserMode => true,
40 /**
41 * Initialize/fetch the ApiMain instance for testing
42 * @return ApiMain
44 private static function getMain() {
45 if ( !self::$main ) {
46 self::$main = new ApiMain( RequestContext::getMain() );
47 self::$main->getContext()->setLanguage( 'en' );
48 self::$main->getContext()->setTitle(
49 Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for ApiStructureTest' )
52 // Inject ApiDisabled and ApiQueryDisabled so they can be tested too
53 self::$main->getModuleManager()->addModule( 'disabled', 'action', ApiDisabled::class );
54 self::$main->getModuleFromPath( 'query' )
55 ->getModuleManager()->addModule( 'query-disabled', 'meta', ApiQueryDisabled::class );
57 return self::$main;
60 /**
61 * Test a message
62 * @param string|array|Message $msg Message definition, see Message::newFromSpecifier()
63 * @param string $what Which message is being checked
65 private function checkMessage( $msg, $what ) {
66 // Message::newFromSpecifier() will throw and fail the test if the specifier isn't valid
67 $msg = Message::newFromSpecifier( $msg );
68 $this->assertTrue( $msg->exists(), "API $what message \"{$msg->getKey()}\" must exist. Did you forgot to add it to your i18n/en.json?" );
71 /**
72 * @dataProvider provideDocumentationExists
73 * @param string $path Module path
74 * @param array $globals Globals to set
76 public function testDocumentationExists( $path, array $globals ) {
77 // Set configuration variables
78 $this->overrideConfigValues( $globals );
80 $main = self::getMain();
82 // Fetch module.
83 $module = TestingAccessWrapper::newFromObject( $main->getModuleFromPath( $path ) );
85 // Test messages for flags.
86 foreach ( $module->getHelpFlags() as $flag ) {
87 $this->checkMessage( "api-help-flag-$flag", "Flag $flag" );
90 // Module description messages.
91 $this->checkMessage( $module->getSummaryMessage(), 'Module summary' );
92 $extendedDesc = $module->getExtendedDescription();
93 if ( is_array( $extendedDesc ) && is_array( $extendedDesc[0] ) ) {
94 // The definition in getExtendedDescription() may also specify fallback keys. This is weird,
95 // and it was never needed for other API doc messages, so it's only supported here.
96 $extendedDesc = Message::newFallbackSequence( $extendedDesc[0] )
97 ->params( array_slice( $extendedDesc, 1 ) );
99 $this->checkMessage( $extendedDesc, 'Module help top text' );
101 // Messages for examples.
102 foreach ( $module->getExamplesMessages() as $qs => $msg ) {
103 $this->assertStringStartsNotWith( 'api.php?', $qs,
104 "Query string must not begin with 'api.php?'" );
105 $this->checkMessage( $msg, "Example $qs" );
109 public static function provideDocumentationExists() {
110 $main = self::getMain();
111 $paths = self::getSubModulePaths( $main->getModuleManager() );
112 array_unshift( $paths, $main->getModulePath() );
114 $ret = [];
115 foreach ( $paths as $path ) {
116 foreach ( self::$testGlobals as $globals ) {
117 $g = [];
118 foreach ( $globals as $k => $v ) {
119 $g[] = "$k=" . var_export( $v, 1 );
121 $k = "Module $path with " . implode( ', ', $g );
122 $ret[$k] = [ $path, $globals ];
125 return $ret;
128 private function doTestParameters( string $path, array $params, string $name ): void {
129 $main = self::getMain();
131 $dataName = $this->dataName();
132 $this->assertNotSame( '', $name, "$dataName: Name cannot be empty" );
133 $this->assertArrayHasKey( $name, $params, "$dataName: Existence check" );
135 $ret = $main->getParamValidator()->checkSettings(
136 $main->getModuleFromPath( $path ), $params, $name, []
139 // Warn about unknown keys. Don't fail, they might be for forward- or back-compat.
140 if ( is_array( $params[$name] ) ) {
141 $keys = array_diff(
142 array_keys( $params[$name] ),
143 $ret['allowedKeys']
145 if ( $keys ) {
146 // Don't fail for this, for back-compat
147 $this->addWarning(
148 "$dataName: Unrecognized settings keys were used: " . implode( ', ', $keys )
153 if ( count( $ret['issues'] ) === 1 ) {
154 $this->fail( "$dataName: Validation failed: " . reset( $ret['issues'] ) );
155 } elseif ( $ret['issues'] ) {
156 $this->fail( "$dataName: Validation failed:\n* " . implode( "\n* ", $ret['issues'] ) );
159 // Check message existence
160 $done = [];
161 foreach ( $ret['messages'] as $msg ) {
162 // We don't really care about the parameters, so do it simply
163 $key = $msg->getKey();
164 if ( !isset( $done[$key] ) ) {
165 $done[$key] = true;
166 $this->checkMessage( $key, "$dataName: Parameter" );
172 * @dataProvider provideParameters
174 public function testParameters( string $path, string $argset, array $args, ApiMain $main ): void {
175 $module = $main->getModuleFromPath( $path );
176 $params = $module->getFinalParams( ...$args );
177 if ( !$params ) {
178 $this->addToAssertionCount( 1 );
179 return;
181 foreach ( $params as $param => $_ ) {
182 $this->doTestParameters( $path, $params, $param );
186 public static function provideParameters(): Iterator {
187 $main = self::getMain();
188 $paths = self::getSubModulePaths( $main->getModuleManager() );
189 array_unshift( $paths, $main->getModulePath() );
190 $argsets = [
191 'plain' => [],
192 'for help' => [ ApiBase::GET_VALUES_FOR_HELP ],
195 foreach ( $paths as $path ) {
196 foreach ( $argsets as $argset => $args ) {
197 // NOTE: Retrieving the module parameters here may have side effects such as DB queries that
198 // should be avoided in data providers (T341731). So do that in the test method instead.
199 yield "Module $path, argset $argset" => [ $path, $argset, $args, $main ];
205 * Return paths of all submodules in an ApiModuleManager, recursively
206 * @param ApiModuleManager $manager
207 * @return string[]
209 protected static function getSubModulePaths( ApiModuleManager $manager ) {
210 $paths = [];
211 foreach ( $manager->getNames() as $name ) {
212 $module = $manager->getModule( $name );
213 $paths[] = $module->getModulePath();
214 $subManager = $module->getModuleManager();
215 if ( $subManager ) {
216 $paths = array_merge( $paths, self::getSubModulePaths( $subManager ) );
219 return $paths;