MediaWiki  1.29.1
ApiDocumentationTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
14 
16  private static $main;
17 
19  private static $testGlobals = [
20  [
21  'MiserMode' => false,
22  'AllowCategorizedRecentChanges' => false,
23  ],
24  [
25  'MiserMode' => true,
26  'AllowCategorizedRecentChanges' => true,
27  ],
28  ];
29 
34  private static function getMain() {
35  if ( !self::$main ) {
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' )
40  );
41  }
42  return self::$main;
43  }
44 
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" );
54  }
55 
61  public function testDocumentationExists( $path, array $globals ) {
62  $main = self::getMain();
63 
64  // Set configuration variables
65  $main->getContext()->setConfig( new MultiConfig( [
66  new HashConfig( $globals ),
67  RequestContext::getMain()->getConfig(),
68  ] ) );
69  foreach ( $globals as $k => $v ) {
70  $this->setMwGlobals( "wg$k", $v );
71  }
72 
73  // Fetch module.
74  $module = TestingAccessWrapper::newFromObject( $main->getModuleFromPath( $path ) );
75 
76  // Test messages for flags.
77  foreach ( $module->getHelpFlags() as $flag ) {
78  $this->checkMessage( "api-help-flag-$flag", "Flag $flag" );
79  }
80 
81  // Module description messages.
82  $this->checkMessage( $module->getDescriptionMessage(), 'Module description' );
83 
84  // Parameters. Lots of messages in here.
85  $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
86  $tags = [];
87  foreach ( $params as $name => $settings ) {
88  if ( !is_array( $settings ) ) {
89  $settings = [];
90  }
91 
92  // Basic description message
93  if ( isset( $settings[ApiBase::PARAM_HELP_MSG] ) ) {
94  $msg = $settings[ApiBase::PARAM_HELP_MSG];
95  } else {
96  $msg = "apihelp-{$path}-param-{$name}";
97  }
98  $this->checkMessage( $msg, "Parameter $name description" );
99 
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];
110  } else {
111  $msg = "apihelp-{$path}-paramvalue-{$name}-{$value}";
112  }
113  $this->checkMessage( $msg, "Parameter $name value $value" );
114  }
115  }
116 
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" );
123  }
124  }
125 
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;
131  }
132  }
133  }
134 
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" );
138  }
139 
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" );
145  }
146  }
147 
148  public static function provideDocumentationExists() {
149  $main = self::getMain();
151  array_unshift( $paths, $main->getModulePath() );
152 
153  $ret = [];
154  foreach ( $paths as $path ) {
155  foreach ( self::$testGlobals as $globals ) {
156  $g = [];
157  foreach ( $globals as $k => $v ) {
158  $g[] = "$k=" . var_export( $v, 1 );
159  }
160  $k = "Module $path with " . implode( ', ', $g );
161  $ret[$k] = [ $path, $globals ];
162  }
163  }
164  return $ret;
165  }
166 
172  protected static function getSubModulePaths( ApiModuleManager $manager ) {
173  $paths = [];
174  foreach ( $manager->getNames() as $name ) {
175  $module = $manager->getModule( $name );
176  $paths[] = $module->getModulePath();
177  $subManager = $module->getModuleManager();
178  if ( $subManager ) {
179  $paths = array_merge( $paths, self::getSubModulePaths( $subManager ) );
180  }
181  }
182  return $paths;
183  }
184 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:45
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:41
MultiConfig
Provides a fallback sequence for Config objects.
Definition: MultiConfig.php:28
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
ApiDocumentationTest\provideDocumentationExists
static provideDocumentationExists()
Definition: ApiDocumentationTest.php:148
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:128
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:91
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$params
$params
Definition: styleTest.css.php:40
ApiBase\makeMessage
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition: ApiBase.php:1642
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
ApiDocumentationTest
Checks that all API modules, core and extensions, have documentation i18n messages.
Definition: ApiDocumentationTest.php:13
getContext
getContext()
ApiBase\PARAM_HELP_MSG_APPEND
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:135
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiBase\getModuleFromPath
getModuleFromPath( $path)
Get a module from its module path.
Definition: ApiBase.php:572
ApiDocumentationTest\checkMessage
checkMessage( $msg, $what)
Test a message.
Definition: ApiDocumentationTest.php:50
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:51
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:554
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
$tag
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books $tag
Definition: hooks.txt:1028
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
ApiDocumentationTest\$main
static ApiMain $main
Definition: ApiDocumentationTest.php:16
ApiMain\getModuleManager
getModuleManager()
Overrides to return this instance's module manager.
Definition: ApiMain.php:1983
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
ApiDocumentationTest\getMain
static getMain()
Initialize/fetch the ApiMain instance for testing.
Definition: ApiDocumentationTest.php:34
ApiModuleManager\getNames
getNames( $group=null)
Get an array of modules in a specific group or all if no group is set.
Definition: ApiModuleManager.php:216
ApiModuleManager
This class holds a list of modules and handles instantiation.
Definition: ApiModuleManager.php:34
ApiDocumentationTest\$testGlobals
static array $testGlobals
Sets of globals to test.
Definition: ApiDocumentationTest.php:19
$value
$value
Definition: styleTest.css.php:45
ApiBase\GET_VALUES_FOR_HELP
const GET_VALUES_FOR_HELP
getAllowedParams() flag: When set, the result could take longer to generate, but should be more thoro...
Definition: ApiBase.php:216
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1956
ApiBase\PARAM_HELP_MSG_INFO
const PARAM_HELP_MSG_INFO
(array) Specify additional information tags for the parameter.
Definition: ApiBase.php:145
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:468
ApiDocumentationTest\testDocumentationExists
testDocumentationExists( $path, array $globals)
provideDocumentationExists
Definition: ApiDocumentationTest.php:61
$path
$path
Definition: NoLocalSettings.php:26
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiDocumentationTest\getSubModulePaths
static getSubModulePaths(ApiModuleManager $manager)
Return paths of all submodules in an ApiModuleManager, recursively.
Definition: ApiDocumentationTest.php:172
ApiModuleManager\getModule
getModule( $moduleName, $group=null, $ignoreCache=false)
Get module instance by name, or instantiate it if it does not exist.
Definition: ApiModuleManager.php:156
ApiBase\PARAM_HELP_MSG_PER_VALUE
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:160
array
the array() calling protocol came about after MediaWiki 1.4rc1.