3 use Wikimedia\TestingAccessWrapper
;
6 * Checks that all API modules, core and extensions, have documentation i18n messages
8 * It won't catch everything since i18n messages can vary based on the wiki
9 * configuration, but it should catch many cases for forgotten i18n.
13 class ApiDocumentationTest
extends MediaWikiTestCase
{
18 /** @var array Sets of globals to test. Each array element is input to HashConfig */
19 private static $testGlobals = [
22 'AllowCategorizedRecentChanges' => false,
26 'AllowCategorizedRecentChanges' => true,
31 * Initialize/fetch the ApiMain instance for testing
34 private static function getMain() {
36 self
::$main = new ApiMain( RequestContext
::getMain() );
37 self
::$main->getContext()->setLanguage( 'en' );
38 self
::$main->getContext()->setTitle(
39 Title
::makeTitle( NS_SPECIAL
, 'Badtitle/dummy title for ApiDocumentationTest' )
48 * @param string $what Which message is being checked
50 private function checkMessage( $msg, $what ) {
51 $msg = ApiBase
::makeMessage( $msg, self
::getMain()->getContext() );
52 $this->assertInstanceOf( 'Message', $msg, "$what message" );
53 $this->assertTrue( $msg->exists(), "$what message {$msg->getKey()} exists" );
57 * @dataProvider provideDocumentationExists
58 * @param string $path Module path
59 * @param array $globals Globals to set
61 public function testDocumentationExists( $path, array $globals ) {
62 $main = self
::getMain();
64 // Set configuration variables
65 $main->getContext()->setConfig( new MultiConfig( [
66 new HashConfig( $globals ),
67 RequestContext
::getMain()->getConfig(),
69 foreach ( $globals as $k => $v ) {
70 $this->setMwGlobals( "wg$k", $v );
74 $module = TestingAccessWrapper
::newFromObject( $main->getModuleFromPath( $path ) );
76 // Test messages for flags.
77 foreach ( $module->getHelpFlags() as $flag ) {
78 $this->checkMessage( "api-help-flag-$flag", "Flag $flag" );
81 // Module description messages.
82 $this->checkMessage( $module->getDescriptionMessage(), 'Module description' );
84 // Parameters. Lots of messages in here.
85 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
87 foreach ( $params as $name => $settings ) {
88 if ( !is_array( $settings ) ) {
92 // Basic description message
93 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG
] ) ) {
94 $msg = $settings[ApiBase
::PARAM_HELP_MSG
];
96 $msg = "apihelp-{$path}-param-{$name}";
98 $this->checkMessage( $msg, "Parameter $name description" );
100 // If param-per-value is in use, each value's message
101 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
] ) ) {
102 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
],
103 "Parameter $name PARAM_HELP_MSG_PER_VALUE is array" );
104 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_TYPE
],
105 "Parameter $name PARAM_TYPE is array for msg-per-value mode" );
106 $valueMsgs = $settings[ApiBase
::PARAM_HELP_MSG_PER_VALUE
];
107 foreach ( $settings[ApiBase
::PARAM_TYPE
] as $value ) {
108 if ( isset( $valueMsgs[$value] ) ) {
109 $msg = $valueMsgs[$value];
111 $msg = "apihelp-{$path}-paramvalue-{$name}-{$value}";
113 $this->checkMessage( $msg, "Parameter $name value $value" );
117 // Appended messages (e.g. "disabled in miser mode")
118 if ( isset( $settings[ApiBase
::PARAM_HELP_MSG_APPEND
] ) ) {
119 $this->assertInternalType( 'array', $settings[ApiBase
::PARAM_HELP_MSG_APPEND
],
120 "Parameter $name PARAM_HELP_MSG_APPEND is array" );
121 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_APPEND
] as $i => $msg ) {
122 $this->checkMessage( $msg, "Parameter $name HELP_MSG_APPEND #$i" );
126 // Info tags (e.g. "only usable in mode 1") are typically shared by
127 // several parameters, so accumulate them and test them later.
128 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
129 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
130 $tags[array_shift( $i )] = 1;
135 // Info tags (e.g. "only usable in mode 1") accumulated above
136 foreach ( $tags as $tag => $dummy ) {
137 $this->checkMessage( "apihelp-{$path}-paraminfo-{$tag}", "HELP_MSG_INFO tag $tag" );
140 // Messages for examples.
141 foreach ( $module->getExamplesMessages() as $qs => $msg ) {
142 $this->assertStringStartsNotWith( 'api.php?', $qs,
143 "Query string must not begin with 'api.php?'" );
144 $this->checkMessage( $msg, "Example $qs" );
148 public static function provideDocumentationExists() {
149 $main = self
::getMain();
150 $paths = self
::getSubModulePaths( $main->getModuleManager() );
151 array_unshift( $paths, $main->getModulePath() );
154 foreach ( $paths as $path ) {
155 foreach ( self
::$testGlobals as $globals ) {
157 foreach ( $globals as $k => $v ) {
158 $g[] = "$k=" . var_export( $v, 1 );
160 $k = "Module $path with " . implode( ', ', $g );
161 $ret[$k] = [ $path, $globals ];
168 * Return paths of all submodules in an ApiModuleManager, recursively
169 * @param ApiModuleManager $manager
172 protected static function getSubModulePaths( ApiModuleManager
$manager ) {
174 foreach ( $manager->getNames() as $name ) {
175 $module = $manager->getModule( $name );
176 $paths[] = $module->getModulePath();
177 $subManager = $module->getModuleManager();
179 $paths = array_merge( $paths, self
::getSubModulePaths( $subManager ) );