4 * Checks that all API modules, core and extensions, have documentation i18n messages
6 * It won't catch everything since i18n messages can vary based on the wiki
7 * configuration, but it should catch many cases for forgotten i18n.
11 class ApiDocumentationTest
extends MediaWikiTestCase
{
16 /** @var array Sets of globals to test. Each array element is input to HashConfig */
17 private static $testGlobals = [
20 'AllowCategorizedRecentChanges' => false,
24 'AllowCategorizedRecentChanges' => true,
29 * Initialize/fetch the ApiMain instance for testing
32 private static function getMain() {
34 self
::$main = new ApiMain( RequestContext
::getMain() );
35 self
::$main->getContext()->setLanguage( 'en' );
36 self
::$main->getContext()->setTitle(
37 Title
::makeTitle( NS_SPECIAL
, 'Badtitle/dummy title for ApiDocumentationTest' )
46 * @param string $what Which message is being checked
48 private function checkMessage( $msg, $what ) {
49 $msg = ApiBase
::makeMessage( $msg, self
::getMain()->getContext() );
50 $this->assertInstanceOf( 'Message', $msg, "$what message" );
51 $this->assertTrue( $msg->exists(), "$what message {$msg->getKey()} exists" );
55 * @dataProvider provideDocumentationExists
56 * @param string $path Module path
57 * @param array $globals Globals to set
59 public function testDocumentationExists( $path, array $globals ) {
60 $main = self
::getMain();
62 // Set configuration variables
63 $main->getContext()->setConfig( new MultiConfig( [
64 new HashConfig( $globals ),
65 RequestContext
::getMain()->getConfig(),
67 foreach ( $globals as $k => $v ) {
68 $this->setMwGlobals( "wg$k", $v );
72 $module = TestingAccessWrapper
::newFromObject( $main->getModuleFromPath( $path ) );
74 // Test messages for flags.
75 foreach ( $module->getHelpFlags() as $flag ) {
76 $this->checkMessage( "api-help-flag-$flag", "Flag $flag" );
79 // Module description messages.
80 $this->checkMessage( $module->getDescriptionMessage(), 'Module description' );
82 // Parameters. Lots of messages in here.
83 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
85 foreach ( $params as $name => $settings ) {
86 if ( !is_array( $settings ) ) {
90 // Basic description message
91 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG
] ) ) {
92 $msg = $settings[ApiBase
::PARAM_HELP_MSG
];
94 $msg = "apihelp-{$path}-param-{$name}";
96 $this->checkMessage( $msg, "Parameter $name description" );
98 // If param-per-value is in use, each value's message
99 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
] ) ) {
100 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
],
101 "Parameter $name PARAM_HELP_MSG_PER_VALUE is array" );
102 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_TYPE
],
103 "Parameter $name PARAM_TYPE is array for msg-per-value mode" );
104 $valueMsgs = $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
];
105 foreach ( $settings[ApiBase
::PARAM_TYPE
] as $value ) {
106 if ( isset( $valueMsgs[$value] ) ) {
107 $msg = $valueMsgs[$value];
109 $msg = "apihelp-{$path}-paramvalue-{$name}-{$value}";
111 $this->checkMessage( $msg, "Parameter $name value $value" );
115 // Appended messages (e.g. "disabled in miser mode")
116 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG_APPEND
] ) ) {
117 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_HELP_MSG_APPEND
],
118 "Parameter $name PARAM_HELP_MSG_APPEND is array" );
119 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_APPEND
] as $i => $msg ) {
120 $this->checkMessage( $msg, "Parameter $name HELP_MSG_APPEND #$i" );
124 // Info tags (e.g. "only usable in mode 1") are typically shared by
125 // several parameters, so accumulate them and test them later.
126 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
127 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
128 $tags[array_shift( $i )] = 1;
133 // Info tags (e.g. "only usable in mode 1") accumulated above
134 foreach ( $tags as $tag => $dummy ) {
135 $this->checkMessage( "apihelp-{$path}-paraminfo-{$tag}", "HELP_MSG_INFO tag $tag" );
138 // Messages for examples.
139 foreach ( $module->getExamplesMessages() as $qs => $msg ) {
140 $this->assertStringStartsNotWith( 'api.php?', $qs,
141 "Query string must not begin with 'api.php?'" );
142 $this->checkMessage( $msg, "Example $qs" );
146 public static function provideDocumentationExists() {
147 $main = self
::getMain();
148 $paths = self
::getSubModulePaths( $main->getModuleManager() );
149 array_unshift( $paths, $main->getModulePath() );
152 foreach ( $paths as $path ) {
153 foreach ( self
::$testGlobals as $globals ) {
155 foreach ( $globals as $k => $v ) {
156 $g[] = "$k=" . var_export( $v, 1 );
158 $k = "Module $path with " . implode( ', ', $g );
159 $ret[$k] = [ $path, $globals ];
166 * Return paths of all submodules in an ApiModuleManager, recursively
167 * @param ApiModuleManager $manager
170 protected static function getSubModulePaths( ApiModuleManager
$manager ) {
172 foreach ( $manager->getNames() as $name ) {
173 $module = $manager->getModule( $name );
174 $paths[] = $module->getModulePath();
175 $subManager = $module->getModuleManager();
177 $paths = array_merge( $paths, self
::getSubModulePaths( $subManager ) );