2015-12-22 16:59:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
2015-12-22 16:59:19 +00:00
|
|
|
/**
|
2017-07-28 17:41:13 +00:00
|
|
|
* Checks that all API modules, core and extensions, conform to the conventions:
|
|
|
|
|
* - have documentation i18n messages (the test won't catch everything since
|
|
|
|
|
* i18n messages can vary based on the wiki configuration, but it should
|
|
|
|
|
* catch many cases for forgotten i18n)
|
|
|
|
|
* - do not have inconsistencies in the parameter definitions
|
2015-12-22 16:59:19 +00:00
|
|
|
*
|
|
|
|
|
* @group API
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class ApiStructureTest extends MediaWikiIntegrationTestCase {
|
2015-12-22 16:59:19 +00:00
|
|
|
|
|
|
|
|
/** @var ApiMain */
|
|
|
|
|
private static $main;
|
|
|
|
|
|
|
|
|
|
/** @var array Sets of globals to test. Each array element is input to HashConfig */
|
2016-02-17 09:09:32 +00:00
|
|
|
private static $testGlobals = [
|
|
|
|
|
[
|
2015-12-22 16:59:19 +00:00
|
|
|
'MiserMode' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-12-22 16:59:19 +00:00
|
|
|
'MiserMode' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-12-22 16:59:19 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize/fetch the ApiMain instance for testing
|
|
|
|
|
* @return ApiMain
|
|
|
|
|
*/
|
|
|
|
|
private static function getMain() {
|
|
|
|
|
if ( !self::$main ) {
|
|
|
|
|
self::$main = new ApiMain( RequestContext::getMain() );
|
|
|
|
|
self::$main->getContext()->setLanguage( 'en' );
|
2016-02-02 14:27:05 +00:00
|
|
|
self::$main->getContext()->setTitle(
|
2017-07-28 17:41:13 +00:00
|
|
|
Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for ApiStructureTest' )
|
2016-02-02 14:27:05 +00:00
|
|
|
);
|
2020-01-13 14:18:18 +00:00
|
|
|
|
|
|
|
|
// Inject ApiDisabled and ApiQueryDisabled so they can be tested too
|
|
|
|
|
self::$main->getModuleManager()->addModule( 'disabled', 'action', ApiDisabled::class );
|
|
|
|
|
self::$main->getModuleFromPath( 'query' )
|
|
|
|
|
->getModuleManager()->addModule( 'query-disabled', 'meta', ApiQueryDisabled::class );
|
2015-12-22 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
return self::$main;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test a message
|
|
|
|
|
* @param Message $msg
|
|
|
|
|
* @param string $what Which message is being checked
|
|
|
|
|
*/
|
|
|
|
|
private function checkMessage( $msg, $what ) {
|
|
|
|
|
$msg = ApiBase::makeMessage( $msg, self::getMain()->getContext() );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( Message::class, $msg, "$what message" );
|
2015-12-22 16:59:19 +00:00
|
|
|
$this->assertTrue( $msg->exists(), "$what message {$msg->getKey()} exists" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideDocumentationExists
|
|
|
|
|
* @param string $path Module path
|
|
|
|
|
* @param array $globals Globals to set
|
|
|
|
|
*/
|
|
|
|
|
public function testDocumentationExists( $path, array $globals ) {
|
|
|
|
|
$main = self::getMain();
|
|
|
|
|
|
|
|
|
|
// Set configuration variables
|
2016-02-17 09:09:32 +00:00
|
|
|
$main->getContext()->setConfig( new MultiConfig( [
|
2015-12-22 16:59:19 +00:00
|
|
|
new HashConfig( $globals ),
|
|
|
|
|
RequestContext::getMain()->getConfig(),
|
2016-02-17 09:09:32 +00:00
|
|
|
] ) );
|
2015-12-22 16:59:19 +00:00
|
|
|
foreach ( $globals as $k => $v ) {
|
2016-03-18 13:55:54 +00:00
|
|
|
$this->setMwGlobals( "wg$k", $v );
|
2015-12-22 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch module.
|
|
|
|
|
$module = TestingAccessWrapper::newFromObject( $main->getModuleFromPath( $path ) );
|
|
|
|
|
|
|
|
|
|
// Test messages for flags.
|
|
|
|
|
foreach ( $module->getHelpFlags() as $flag ) {
|
|
|
|
|
$this->checkMessage( "api-help-flag-$flag", "Flag $flag" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Module description messages.
|
2017-05-26 17:50:06 +00:00
|
|
|
$this->checkMessage( $module->getSummaryMessage(), 'Module summary' );
|
|
|
|
|
$this->checkMessage( $module->getExtendedDescription(), 'Module help top text' );
|
2015-12-22 16:59:19 +00:00
|
|
|
|
|
|
|
|
// Messages for examples.
|
|
|
|
|
foreach ( $module->getExamplesMessages() as $qs => $msg ) {
|
2016-11-08 21:29:04 +00:00
|
|
|
$this->assertStringStartsNotWith( 'api.php?', $qs,
|
|
|
|
|
"Query string must not begin with 'api.php?'" );
|
2015-12-22 16:59:19 +00:00
|
|
|
$this->checkMessage( $msg, "Example $qs" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideDocumentationExists() {
|
|
|
|
|
$main = self::getMain();
|
|
|
|
|
$paths = self::getSubModulePaths( $main->getModuleManager() );
|
|
|
|
|
array_unshift( $paths, $main->getModulePath() );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [];
|
2015-12-22 16:59:19 +00:00
|
|
|
foreach ( $paths as $path ) {
|
|
|
|
|
foreach ( self::$testGlobals as $globals ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$g = [];
|
2015-12-22 16:59:19 +00:00
|
|
|
foreach ( $globals as $k => $v ) {
|
|
|
|
|
$g[] = "$k=" . var_export( $v, 1 );
|
|
|
|
|
}
|
2016-03-08 08:13:12 +00:00
|
|
|
$k = "Module $path with " . implode( ', ', $g );
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret[$k] = [ $path, $globals ];
|
2015-12-22 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 17:41:13 +00:00
|
|
|
/**
|
2020-01-15 21:08:43 +00:00
|
|
|
* @dataProvider provideParameters
|
2017-07-28 17:41:13 +00:00
|
|
|
*/
|
2020-01-15 21:08:43 +00:00
|
|
|
public function testParameters( string $path, array $params, string $name ) : void {
|
2017-07-28 17:41:13 +00:00
|
|
|
$main = self::getMain();
|
2018-03-20 17:34:03 +00:00
|
|
|
|
2020-01-15 21:08:43 +00:00
|
|
|
$dataName = $this->dataName();
|
|
|
|
|
$this->assertNotSame( '', $name, "$dataName: Name cannot be empty" );
|
|
|
|
|
$this->assertArrayHasKey( $name, $params, "$dataName: Sanity check" );
|
2018-03-20 17:34:03 +00:00
|
|
|
|
2020-01-15 21:08:43 +00:00
|
|
|
$ret = $main->getParamValidator()->checkSettings(
|
|
|
|
|
$main->getModuleFromPath( $path ), $params, $name, []
|
|
|
|
|
);
|
2018-03-20 17:34:03 +00:00
|
|
|
|
2020-01-15 21:08:43 +00:00
|
|
|
// Warn about unknown keys. Don't fail, they might be for forward- or back-compat.
|
|
|
|
|
if ( is_array( $params[$name] ) ) {
|
|
|
|
|
$keys = array_diff(
|
|
|
|
|
array_keys( $params[$name] ),
|
|
|
|
|
$ret['allowedKeys']
|
|
|
|
|
);
|
|
|
|
|
if ( $keys ) {
|
|
|
|
|
// Don't fail for this, for back-compat
|
|
|
|
|
$this->addWarning(
|
|
|
|
|
"$dataName: Unrecognized settings keys were used: " . implode( ', ', $keys )
|
|
|
|
|
);
|
2018-03-20 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 21:08:43 +00:00
|
|
|
if ( count( $ret['issues'] ) === 1 ) {
|
|
|
|
|
$this->fail( "$dataName: Validation failed: " . reset( $ret['issues'] ) );
|
|
|
|
|
} elseif ( $ret['issues'] ) {
|
|
|
|
|
$this->fail( "$dataName: Validation failed:\n* " . implode( "\n* ", $ret['issues'] ) );
|
2018-03-20 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-15 21:08:43 +00:00
|
|
|
// Check message existence
|
|
|
|
|
$done = [];
|
|
|
|
|
foreach ( $ret['messages'] as $msg ) {
|
|
|
|
|
// We don't really care about the parameters, so do it simply
|
|
|
|
|
$key = $msg->getKey();
|
|
|
|
|
if ( !isset( $done[$key] ) ) {
|
|
|
|
|
$done[$key] = true;
|
|
|
|
|
$this->checkMessage( $key, "$dataName: Parameter" );
|
2018-03-20 17:34:03 +00:00
|
|
|
}
|
2017-07-28 17:41:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 21:08:43 +00:00
|
|
|
public static function provideParameters() : Iterator {
|
2017-07-28 17:41:13 +00:00
|
|
|
$main = self::getMain();
|
|
|
|
|
$paths = self::getSubModulePaths( $main->getModuleManager() );
|
|
|
|
|
array_unshift( $paths, $main->getModulePath() );
|
2020-01-15 21:08:43 +00:00
|
|
|
$argsets = [
|
|
|
|
|
'plain' => [],
|
|
|
|
|
'for help' => [ ApiBase::GET_VALUES_FOR_HELP ],
|
|
|
|
|
];
|
2017-07-28 17:41:13 +00:00
|
|
|
|
|
|
|
|
foreach ( $paths as $path ) {
|
2020-01-15 21:08:43 +00:00
|
|
|
$module = $main->getModuleFromPath( $path );
|
|
|
|
|
foreach ( $argsets as $argset => $args ) {
|
|
|
|
|
$params = $module->getFinalParams( ...$args );
|
|
|
|
|
foreach ( $params as $param => $dummy ) {
|
|
|
|
|
yield "Module $path, $argset, parameter $param" => [ $path, $params, $param ];
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-28 17:41:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-22 16:59:19 +00:00
|
|
|
/**
|
|
|
|
|
* Return paths of all submodules in an ApiModuleManager, recursively
|
|
|
|
|
* @param ApiModuleManager $manager
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
protected static function getSubModulePaths( ApiModuleManager $manager ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$paths = [];
|
2015-12-22 16:59:19 +00:00
|
|
|
foreach ( $manager->getNames() as $name ) {
|
|
|
|
|
$module = $manager->getModule( $name );
|
|
|
|
|
$paths[] = $module->getModulePath();
|
|
|
|
|
$subManager = $module->getModuleManager();
|
|
|
|
|
if ( $subManager ) {
|
|
|
|
|
$paths = array_merge( $paths, self::getSubModulePaths( $subManager ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $paths;
|
|
|
|
|
}
|
|
|
|
|
}
|