MediaWiki  1.28.0
ApiDocumentationTest.php
Go to the documentation of this file.
1 <?php
2 
12 
14  private static $main;
15 
17  private static $testGlobals = [
18  [
19  'MiserMode' => false,
20  'AllowCategorizedRecentChanges' => false,
21  ],
22  [
23  'MiserMode' => true,
24  'AllowCategorizedRecentChanges' => true,
25  ],
26  ];
27 
32  private static function getMain() {
33  if ( !self::$main ) {
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' )
38  );
39  }
40  return self::$main;
41  }
42 
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" );
52  }
53 
59  public function testDocumentationExists( $path, array $globals ) {
60  $main = self::getMain();
61 
62  // Set configuration variables
63  $main->getContext()->setConfig( new MultiConfig( [
64  new HashConfig( $globals ),
65  RequestContext::getMain()->getConfig(),
66  ] ) );
67  foreach ( $globals as $k => $v ) {
68  $this->setMwGlobals( "wg$k", $v );
69  }
70 
71  // Fetch module.
73 
74  // Test messages for flags.
75  foreach ( $module->getHelpFlags() as $flag ) {
76  $this->checkMessage( "api-help-flag-$flag", "Flag $flag" );
77  }
78 
79  // Module description messages.
80  $this->checkMessage( $module->getDescriptionMessage(), 'Module description' );
81 
82  // Parameters. Lots of messages in here.
83  $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
84  $tags = [];
85  foreach ( $params as $name => $settings ) {
86  if ( !is_array( $settings ) ) {
87  $settings = [];
88  }
89 
90  // Basic description message
91  if ( isset( $settings[ApiBase::PARAM_HELP_MSG] ) ) {
92  $msg = $settings[ApiBase::PARAM_HELP_MSG];
93  } else {
94  $msg = "apihelp-{$path}-param-{$name}";
95  }
96  $this->checkMessage( $msg, "Parameter $name description" );
97 
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];
108  } else {
109  $msg = "apihelp-{$path}-paramvalue-{$name}-{$value}";
110  }
111  $this->checkMessage( $msg, "Parameter $name value $value" );
112  }
113  }
114 
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" );
121  }
122  }
123 
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;
129  }
130  }
131  }
132 
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" );
136  }
137 
138  // Messages for examples.
139  foreach ( $module->getExamplesMessages() as $qs => $msg ) {
140  $this->checkMessage( $msg, "Example $qs" );
141  }
142  }
143 
144  public static function provideDocumentationExists() {
145  $main = self::getMain();
146  $paths = self::getSubModulePaths( $main->getModuleManager() );
147  array_unshift( $paths, $main->getModulePath() );
148 
149  $ret = [];
150  foreach ( $paths as $path ) {
151  foreach ( self::$testGlobals as $globals ) {
152  $g = [];
153  foreach ( $globals as $k => $v ) {
154  $g[] = "$k=" . var_export( $v, 1 );
155  }
156  $k = "Module $path with " . implode( ', ', $g );
157  $ret[$k] = [ $path, $globals ];
158  }
159  }
160  return $ret;
161  }
162 
168  protected static function getSubModulePaths( ApiModuleManager $manager ) {
169  $paths = [];
170  foreach ( $manager->getNames() as $name ) {
171  $module = $manager->getModule( $name );
172  $paths[] = $module->getModulePath();
173  $subManager = $module->getModuleManager();
174  if ( $subManager ) {
175  $paths = array_merge( $paths, self::getSubModulePaths( $subManager ) );
176  }
177  }
178  return $paths;
179  }
180 }
getModuleManager()
Overrides to return this instance's module manager.
Definition: ApiMain.php:1812
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:88
the array() calling protocol came about after MediaWiki 1.4rc1.
This class holds a list of modules and handles instantiation.
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:1936
const GET_VALUES_FOR_HELP
getAllowedParams() flag: When set, the result could take longer to generate, but should be more thoro...
Definition: ApiBase.php:190
const PARAM_HELP_MSG_INFO
(array) Specify additional information tags for the parameter.
Definition: ApiBase.php:142
$value
const NS_SPECIAL
Definition: Defines.php:45
static makeMessage($msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition: ApiBase.php:1511
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:157
static array $testGlobals
Sets of globals to test.
static getSubModulePaths(ApiModuleManager $manager)
Return paths of all submodules in an ApiModuleManager, recursively.
getModule($moduleName, $group=null, $ignoreCache=false)
Get module instance by name, or instantiate it if it does not exist.
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:132
static getMain()
Static methods.
getModulePath()
Get the path to this module.
Definition: ApiBase.php:521
getContext()
getContext()
Get the base IContextSource object.
$params
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:43
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1007
Provides a fallback sequence for Config objects.
Definition: MultiConfig.php:28
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
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter...
Definition: ApiBase.php:125
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
Checks that all API modules, core and extensions, have documentation i18n messages.
checkMessage($msg, $what)
Test a message.
static array static getMain()
Initialize/fetch the ApiMain instance for testing.
static newFromObject($object)
Return the same object, without access restrictions.
testDocumentationExists($path, array $globals)
provideDocumentationExists
getNames($group=null)
Get an array of modules in a specific group or all if no group is set.
setMwGlobals($pairs, $value=null)
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
getModuleFromPath($path)
Get a module from its module path.
Definition: ApiBase.php:539
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300