3 use Wikimedia\TestingAccessWrapper
;
6 * Checks that all API modules, core and extensions, conform to the conventions:
7 * - have documentation i18n messages (the test won't catch everything since
8 * i18n messages can vary based on the wiki configuration, but it should
9 * catch many cases for forgotten i18n)
10 * - do not have inconsistencies in the parameter definitions
14 class ApiStructureTest
extends MediaWikiTestCase
{
19 /** @var array Sets of globals to test. Each array element is input to HashConfig */
20 private static $testGlobals = [
23 'AllowCategorizedRecentChanges' => false,
27 'AllowCategorizedRecentChanges' => true,
32 * Initialize/fetch the ApiMain instance for testing
35 private static function getMain() {
37 self
::$main = new ApiMain( RequestContext
::getMain() );
38 self
::$main->getContext()->setLanguage( 'en' );
39 self
::$main->getContext()->setTitle(
40 Title
::makeTitle( NS_SPECIAL
, 'Badtitle/dummy title for ApiStructureTest' )
49 * @param string $what Which message is being checked
51 private function checkMessage( $msg, $what ) {
52 $msg = ApiBase
::makeMessage( $msg, self
::getMain()->getContext() );
53 $this->assertInstanceOf( 'Message', $msg, "$what message" );
54 $this->assertTrue( $msg->exists(), "$what message {$msg->getKey()} exists" );
58 * @dataProvider provideDocumentationExists
59 * @param string $path Module path
60 * @param array $globals Globals to set
62 public function testDocumentationExists( $path, array $globals ) {
63 $main = self
::getMain();
65 // Set configuration variables
66 $main->getContext()->setConfig( new MultiConfig( [
67 new HashConfig( $globals ),
68 RequestContext
::getMain()->getConfig(),
70 foreach ( $globals as $k => $v ) {
71 $this->setMwGlobals( "wg$k", $v );
75 $module = TestingAccessWrapper
::newFromObject( $main->getModuleFromPath( $path ) );
77 // Test messages for flags.
78 foreach ( $module->getHelpFlags() as $flag ) {
79 $this->checkMessage( "api-help-flag-$flag", "Flag $flag" );
82 // Module description messages.
83 $this->checkMessage( $module->getSummaryMessage(), 'Module summary' );
84 $this->checkMessage( $module->getExtendedDescription(), 'Module help top text' );
86 // Parameters. Lots of messages in here.
87 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
89 foreach ( $params as $name => $settings ) {
90 if ( !is_array( $settings ) ) {
94 // Basic description message
95 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG
] ) ) {
96 $msg = $settings[ApiBase
::PARAM_HELP_MSG
];
98 $msg = "apihelp-{$path}-param-{$name}";
100 $this->checkMessage( $msg, "Parameter $name description" );
102 // If param-per-value is in use, each value's message
103 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
] ) ) {
104 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
],
105 "Parameter $name PARAM_HELP_MSG_PER_VALUE is array" );
106 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_TYPE
],
107 "Parameter $name PARAM_TYPE is array for msg-per-value mode" );
108 $valueMsgs = $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
];
109 foreach ( $settings[ApiBase
::PARAM_TYPE
] as $value ) {
110 if ( isset( $valueMsgs[$value] ) ) {
111 $msg = $valueMsgs[$value];
113 $msg = "apihelp-{$path}-paramvalue-{$name}-{$value}";
115 $this->checkMessage( $msg, "Parameter $name value $value" );
119 // Appended messages (e.g. "disabled in miser mode")
120 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG_APPEND
] ) ) {
121 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_HELP_MSG_APPEND
],
122 "Parameter $name PARAM_HELP_MSG_APPEND is array" );
123 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_APPEND
] as $i => $msg ) {
124 $this->checkMessage( $msg, "Parameter $name HELP_MSG_APPEND #$i" );
128 // Info tags (e.g. "only usable in mode 1") are typically shared by
129 // several parameters, so accumulate them and test them later.
130 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
131 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
132 $tags[array_shift( $i )] = 1;
137 // Info tags (e.g. "only usable in mode 1") accumulated above
138 foreach ( $tags as $tag => $dummy ) {
139 $this->checkMessage( "apihelp-{$path}-paraminfo-{$tag}", "HELP_MSG_INFO tag $tag" );
142 // Messages for examples.
143 foreach ( $module->getExamplesMessages() as $qs => $msg ) {
144 $this->assertStringStartsNotWith( 'api.php?', $qs,
145 "Query string must not begin with 'api.php?'" );
146 $this->checkMessage( $msg, "Example $qs" );
150 public static function provideDocumentationExists() {
151 $main = self
::getMain();
152 $paths = self
::getSubModulePaths( $main->getModuleManager() );
153 array_unshift( $paths, $main->getModulePath() );
156 foreach ( $paths as $path ) {
157 foreach ( self
::$testGlobals as $globals ) {
159 foreach ( $globals as $k => $v ) {
160 $g[] = "$k=" . var_export( $v, 1 );
162 $k = "Module $path with " . implode( ', ', $g );
163 $ret[$k] = [ $path, $globals ];
170 * @dataProvider provideParameterConsistency
171 * @param string $path
173 public function testParameterConsistency( $path ) {
174 $main = self
::getMain();
175 $module = TestingAccessWrapper
::newFromObject( $main->getModuleFromPath( $path ) );
177 $paramsPlain = $module->getFinalParams();
178 $paramsForHelp = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
180 // avoid warnings about empty tests when no parameter needs to be checked
181 $this->assertTrue( true );
183 foreach ( [ $paramsPlain, $paramsForHelp ] as $params ) {
184 foreach ( $params as $param => $config ) {
186 isset( $config[ApiBase
::PARAM_ISMULTI_LIMIT1
] )
187 ||
isset( $config[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
189 $this->assertTrue( !empty( $config[ApiBase
::PARAM_ISMULTI
] ), $param
190 . ': PARAM_ISMULTI_LIMIT* only makes sense when PARAM_ISMULTI is true' );
191 $this->assertTrue( isset( $config[ApiBase
::PARAM_ISMULTI_LIMIT1
] )
192 && isset( $config[ApiBase
::PARAM_ISMULTI_LIMIT2
] ), $param
193 . ': PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2 must be used together' );
194 $this->assertType( 'int', $config[ApiBase
::PARAM_ISMULTI_LIMIT1
], $param
195 . 'PARAM_ISMULTI_LIMIT1 must be an integer' );
196 $this->assertType( 'int', $config[ApiBase
::PARAM_ISMULTI_LIMIT2
], $param
197 . 'PARAM_ISMULTI_LIMIT2 must be an integer' );
198 $this->assertGreaterThanOrEqual( $config[ApiBase
::PARAM_ISMULTI_LIMIT1
],
199 $config[ApiBase
::PARAM_ISMULTI_LIMIT2
], $param
200 . 'PARAM_ISMULTI limit cannot be smaller for users with apihighlimits rights' );
207 * @return array List of API module paths to test
209 public static function provideParameterConsistency() {
210 $main = self
::getMain();
211 $paths = self
::getSubModulePaths( $main->getModuleManager() );
212 array_unshift( $paths, $main->getModulePath() );
215 foreach ( $paths as $path ) {
222 * Return paths of all submodules in an ApiModuleManager, recursively
223 * @param ApiModuleManager $manager
226 protected static function getSubModulePaths( ApiModuleManager
$manager ) {
228 foreach ( $manager->getNames() as $name ) {
229 $module = $manager->getModule( $name );
230 $paths[] = $module->getModulePath();
231 $subManager = $module->getModuleManager();
233 $paths = array_merge( $paths, self
::getSubModulePaths( $subManager ) );