Merge "Corrected grammatical error."
[lhc/web/wiklou.git] / tests / phpunit / structure / ApiStructureTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\TestingAccessWrapper;
5
6 /**
7 * Checks that all API modules, core and extensions, conform to the conventions:
8 * - have documentation i18n messages (the test won't catch everything since
9 * i18n messages can vary based on the wiki configuration, but it should
10 * catch many cases for forgotten i18n)
11 * - do not have inconsistencies in the parameter definitions
12 *
13 * @group API
14 * @coversNothing
15 */
16 class ApiStructureTest extends MediaWikiTestCase {
17
18 /** @var ApiMain */
19 private static $main;
20
21 /** @var array Sets of globals to test. Each array element is input to HashConfig */
22 private static $testGlobals = [
23 [
24 'MiserMode' => false,
25 ],
26 [
27 'MiserMode' => true,
28 ],
29 ];
30
31 /**
32 * Values are an array, where each array value is a permitted type. A type
33 * can be a string, which is the name of an internal type or a
34 * class/interface. Or it can be an array, in which case the value must be
35 * an array whose elements are the types given in the array (e.g., [
36 * 'string', integer' ] means an array whose entries are strings and/or
37 * integers).
38 */
39 private static $paramTypes = [
40 // ApiBase::PARAM_DFLT => as appropriate for PARAM_TYPE
41 ApiBase::PARAM_ISMULTI => [ 'boolean' ],
42 ApiBase::PARAM_TYPE => [ 'string', [ 'string' ] ],
43 ApiBase::PARAM_MAX => [ 'integer' ],
44 ApiBase::PARAM_MAX2 => [ 'integer' ],
45 ApiBase::PARAM_MIN => [ 'integer' ],
46 ApiBase::PARAM_ALLOW_DUPLICATES => [ 'boolean' ],
47 ApiBase::PARAM_DEPRECATED => [ 'boolean' ],
48 ApiBase::PARAM_REQUIRED => [ 'boolean' ],
49 ApiBase::PARAM_RANGE_ENFORCE => [ 'boolean' ],
50 ApiBase::PARAM_HELP_MSG => [ 'string', 'array', Message::class ],
51 ApiBase::PARAM_HELP_MSG_APPEND => [ [ 'string', 'array', Message::class ] ],
52 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'array' ] ],
53 ApiBase::PARAM_VALUE_LINKS => [ [ 'string' ] ],
54 ApiBase::PARAM_HELP_MSG_PER_VALUE => [ [ 'string', 'array', Message::class ] ],
55 ApiBase::PARAM_SUBMODULE_MAP => [ [ 'string' ] ],
56 ApiBase::PARAM_SUBMODULE_PARAM_PREFIX => [ 'string' ],
57 ApiBase::PARAM_ALL => [ 'boolean', 'string' ],
58 ApiBase::PARAM_EXTRA_NAMESPACES => [ [ 'integer' ] ],
59 ApiBase::PARAM_SENSITIVE => [ 'boolean' ],
60 ApiBase::PARAM_DEPRECATED_VALUES => [ 'array' ],
61 ApiBase::PARAM_ISMULTI_LIMIT1 => [ 'integer' ],
62 ApiBase::PARAM_ISMULTI_LIMIT2 => [ 'integer' ],
63 ApiBase::PARAM_MAX_BYTES => [ 'integer' ],
64 ApiBase::PARAM_MAX_CHARS => [ 'integer' ],
65 ApiBase::PARAM_TEMPLATE_VARS => [ 'array' ],
66 ];
67
68 // param => [ other param that must be present => required value or null ]
69 private static $paramRequirements = [
70 ApiBase::PARAM_ALLOW_DUPLICATES => [ ApiBase::PARAM_ISMULTI => true ],
71 ApiBase::PARAM_ALL => [ ApiBase::PARAM_ISMULTI => true ],
72 ApiBase::PARAM_ISMULTI_LIMIT1 => [
73 ApiBase::PARAM_ISMULTI => true,
74 ApiBase::PARAM_ISMULTI_LIMIT2 => null,
75 ],
76 ApiBase::PARAM_ISMULTI_LIMIT2 => [
77 ApiBase::PARAM_ISMULTI => true,
78 ApiBase::PARAM_ISMULTI_LIMIT1 => null,
79 ],
80 ];
81
82 // param => type(s) allowed for this param ('array' is any array)
83 private static $paramAllowedTypes = [
84 ApiBase::PARAM_MAX => [ 'integer', 'limit' ],
85 ApiBase::PARAM_MAX2 => 'limit',
86 ApiBase::PARAM_MIN => [ 'integer', 'limit' ],
87 ApiBase::PARAM_RANGE_ENFORCE => 'integer',
88 ApiBase::PARAM_VALUE_LINKS => 'array',
89 ApiBase::PARAM_HELP_MSG_PER_VALUE => 'array',
90 ApiBase::PARAM_SUBMODULE_MAP => 'submodule',
91 ApiBase::PARAM_SUBMODULE_PARAM_PREFIX => 'submodule',
92 ApiBase::PARAM_ALL => 'array',
93 ApiBase::PARAM_EXTRA_NAMESPACES => 'namespace',
94 ApiBase::PARAM_DEPRECATED_VALUES => 'array',
95 ApiBase::PARAM_MAX_BYTES => [ 'NULL', 'string', 'text', 'password' ],
96 ApiBase::PARAM_MAX_CHARS => [ 'NULL', 'string', 'text', 'password' ],
97 ];
98
99 private static $paramProhibitedTypes = [
100 ApiBase::PARAM_ISMULTI => [ 'boolean', 'limit', 'upload' ],
101 ApiBase::PARAM_ALL => 'namespace',
102 ApiBase::PARAM_SENSITIVE => 'password',
103 ];
104
105 private static $constantNames = null;
106
107 /**
108 * Initialize/fetch the ApiMain instance for testing
109 * @return ApiMain
110 */
111 private static function getMain() {
112 if ( !self::$main ) {
113 self::$main = new ApiMain( RequestContext::getMain() );
114 self::$main->getContext()->setLanguage( 'en' );
115 self::$main->getContext()->setTitle(
116 Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for ApiStructureTest' )
117 );
118 }
119 return self::$main;
120 }
121
122 /**
123 * Test a message
124 * @param Message $msg
125 * @param string $what Which message is being checked
126 */
127 private function checkMessage( $msg, $what ) {
128 $msg = ApiBase::makeMessage( $msg, self::getMain()->getContext() );
129 $this->assertInstanceOf( Message::class, $msg, "$what message" );
130 $this->assertTrue( $msg->exists(), "$what message {$msg->getKey()} exists" );
131 }
132
133 /**
134 * @dataProvider provideDocumentationExists
135 * @param string $path Module path
136 * @param array $globals Globals to set
137 */
138 public function testDocumentationExists( $path, array $globals ) {
139 $main = self::getMain();
140
141 // Set configuration variables
142 $main->getContext()->setConfig( new MultiConfig( [
143 new HashConfig( $globals ),
144 RequestContext::getMain()->getConfig(),
145 ] ) );
146 foreach ( $globals as $k => $v ) {
147 $this->setMwGlobals( "wg$k", $v );
148 }
149
150 // Fetch module.
151 $module = TestingAccessWrapper::newFromObject( $main->getModuleFromPath( $path ) );
152
153 // Test messages for flags.
154 foreach ( $module->getHelpFlags() as $flag ) {
155 $this->checkMessage( "api-help-flag-$flag", "Flag $flag" );
156 }
157
158 // Module description messages.
159 $this->checkMessage( $module->getSummaryMessage(), 'Module summary' );
160 $this->checkMessage( $module->getExtendedDescription(), 'Module help top text' );
161
162 // Parameters. Lots of messages in here.
163 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
164 $tags = [];
165 foreach ( $params as $name => $settings ) {
166 if ( !is_array( $settings ) ) {
167 $settings = [];
168 }
169
170 // Basic description message
171 if ( isset( $settings[ApiBase::PARAM_HELP_MSG] ) ) {
172 $msg = $settings[ApiBase::PARAM_HELP_MSG];
173 } else {
174 $msg = "apihelp-{$path}-param-{$name}";
175 }
176 $this->checkMessage( $msg, "Parameter $name description" );
177
178 // If param-per-value is in use, each value's message
179 if ( isset( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
180 $this->assertInternalType( 'array', $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE],
181 "Parameter $name PARAM_HELP_MSG_PER_VALUE is array" );
182 $this->assertInternalType( 'array', $settings[ApiBase::PARAM_TYPE],
183 "Parameter $name PARAM_TYPE is array for msg-per-value mode" );
184 $valueMsgs = $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE];
185 foreach ( $settings[ApiBase::PARAM_TYPE] as $value ) {
186 if ( isset( $valueMsgs[$value] ) ) {
187 $msg = $valueMsgs[$value];
188 } else {
189 $msg = "apihelp-{$path}-paramvalue-{$name}-{$value}";
190 }
191 $this->checkMessage( $msg, "Parameter $name value $value" );
192 }
193 }
194
195 // Appended messages (e.g. "disabled in miser mode")
196 if ( isset( $settings[ApiBase::PARAM_HELP_MSG_APPEND] ) ) {
197 $this->assertInternalType( 'array', $settings[ApiBase::PARAM_HELP_MSG_APPEND],
198 "Parameter $name PARAM_HELP_MSG_APPEND is array" );
199 foreach ( $settings[ApiBase::PARAM_HELP_MSG_APPEND] as $i => $msg ) {
200 $this->checkMessage( $msg, "Parameter $name HELP_MSG_APPEND #$i" );
201 }
202 }
203
204 // Info tags (e.g. "only usable in mode 1") are typically shared by
205 // several parameters, so accumulate them and test them later.
206 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
207 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
208 $tags[array_shift( $i )] = 1;
209 }
210 }
211 }
212
213 // Info tags (e.g. "only usable in mode 1") accumulated above
214 foreach ( $tags as $tag => $dummy ) {
215 $this->checkMessage( "apihelp-{$path}-paraminfo-{$tag}", "HELP_MSG_INFO tag $tag" );
216 }
217
218 // Messages for examples.
219 foreach ( $module->getExamplesMessages() as $qs => $msg ) {
220 $this->assertStringStartsNotWith( 'api.php?', $qs,
221 "Query string must not begin with 'api.php?'" );
222 $this->checkMessage( $msg, "Example $qs" );
223 }
224 }
225
226 public static function provideDocumentationExists() {
227 $main = self::getMain();
228 $paths = self::getSubModulePaths( $main->getModuleManager() );
229 array_unshift( $paths, $main->getModulePath() );
230
231 $ret = [];
232 foreach ( $paths as $path ) {
233 foreach ( self::$testGlobals as $globals ) {
234 $g = [];
235 foreach ( $globals as $k => $v ) {
236 $g[] = "$k=" . var_export( $v, 1 );
237 }
238 $k = "Module $path with " . implode( ', ', $g );
239 $ret[$k] = [ $path, $globals ];
240 }
241 }
242 return $ret;
243 }
244
245 /**
246 * @dataProvider provideParameterConsistency
247 * @param string $path
248 */
249 public function testParameterConsistency( $path ) {
250 $main = self::getMain();
251 $module = TestingAccessWrapper::newFromObject( $main->getModuleFromPath( $path ) );
252
253 $paramsPlain = $module->getFinalParams();
254 $paramsForHelp = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
255
256 // avoid warnings about empty tests when no parameter needs to be checked
257 $this->assertTrue( true );
258
259 if ( self::$constantNames === null ) {
260 self::$constantNames = [];
261
262 foreach ( ( new ReflectionClass( 'ApiBase' ) )->getConstants() as $key => $val ) {
263 if ( substr( $key, 0, 6 ) === 'PARAM_' ) {
264 self::$constantNames[$val] = $key;
265 }
266 }
267 }
268
269 foreach ( [ $paramsPlain, $paramsForHelp ] as $params ) {
270 foreach ( $params as $param => $config ) {
271 if ( !is_array( $config ) ) {
272 $config = [ ApiBase::PARAM_DFLT => $config ];
273 }
274 if ( !isset( $config[ApiBase::PARAM_TYPE] ) ) {
275 $config[ApiBase::PARAM_TYPE] = isset( $config[ApiBase::PARAM_DFLT] )
276 ? gettype( $config[ApiBase::PARAM_DFLT] )
277 : 'NULL';
278 }
279
280 foreach ( self::$paramTypes as $key => $types ) {
281 if ( !isset( $config[$key] ) ) {
282 continue;
283 }
284 $keyName = self::$constantNames[$key];
285 $this->validateType( $types, $config[$key], $param, $keyName );
286 }
287
288 foreach ( self::$paramRequirements as $key => $required ) {
289 if ( !isset( $config[$key] ) ) {
290 continue;
291 }
292 foreach ( $required as $requireKey => $requireVal ) {
293 $this->assertArrayHasKey( $requireKey, $config,
294 "$param: When " . self::$constantNames[$key] . " is set, " .
295 self::$constantNames[$requireKey] . " must also be set" );
296 if ( $requireVal !== null ) {
297 $this->assertSame( $requireVal, $config[$requireKey],
298 "$param: When " . self::$constantNames[$key] . " is set, " .
299 self::$constantNames[$requireKey] . " must equal " .
300 var_export( $requireVal, true ) );
301 }
302 }
303 }
304
305 foreach ( self::$paramAllowedTypes as $key => $allowedTypes ) {
306 if ( !isset( $config[$key] ) ) {
307 continue;
308 }
309
310 $actualType = is_array( $config[ApiBase::PARAM_TYPE] )
311 ? 'array' : $config[ApiBase::PARAM_TYPE];
312
313 $this->assertContains(
314 $actualType,
315 (array)$allowedTypes,
316 "$param: " . self::$constantNames[$key] .
317 " can only be used with PARAM_TYPE " .
318 implode( ', ', (array)$allowedTypes )
319 );
320 }
321
322 foreach ( self::$paramProhibitedTypes as $key => $prohibitedTypes ) {
323 if ( !isset( $config[$key] ) ) {
324 continue;
325 }
326
327 $actualType = is_array( $config[ApiBase::PARAM_TYPE] )
328 ? 'array' : $config[ApiBase::PARAM_TYPE];
329
330 $this->assertNotContains(
331 $actualType,
332 (array)$prohibitedTypes,
333 "$param: " . self::$constantNames[$key] .
334 " cannot be used with PARAM_TYPE " .
335 implode( ', ', (array)$prohibitedTypes )
336 );
337 }
338
339 if ( isset( $config[ApiBase::PARAM_DFLT] ) ) {
340 $this->assertFalse(
341 isset( $config[ApiBase::PARAM_REQUIRED] ) &&
342 $config[ApiBase::PARAM_REQUIRED],
343 "$param: A required parameter cannot have a default" );
344
345 $this->validateDefault( $param, $config );
346 }
347
348 if ( $config[ApiBase::PARAM_TYPE] === 'limit' ) {
349 $this->assertTrue(
350 isset( $config[ApiBase::PARAM_MAX] ) &&
351 isset( $config[ApiBase::PARAM_MAX2] ),
352 "$param: PARAM_MAX and PARAM_MAX2 are required for limits"
353 );
354 $this->assertGreaterThanOrEqual(
355 $config[ApiBase::PARAM_MAX],
356 $config[ApiBase::PARAM_MAX2],
357 "$param: PARAM_MAX cannot be greater than PARAM_MAX2"
358 );
359 }
360
361 if (
362 isset( $config[ApiBase::PARAM_MIN] ) &&
363 isset( $config[ApiBase::PARAM_MAX] )
364 ) {
365 $this->assertGreaterThanOrEqual(
366 $config[ApiBase::PARAM_MIN],
367 $config[ApiBase::PARAM_MAX],
368 "$param: PARAM_MIN cannot be greater than PARAM_MAX"
369 );
370 }
371
372 if ( isset( $config[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
373 $this->assertTrue(
374 isset( $config[ApiBase::PARAM_MIN] ) ||
375 isset( $config[ApiBase::PARAM_MAX] ),
376 "$param: PARAM_RANGE_ENFORCE can only be set together with " .
377 "PARAM_MIN or PARAM_MAX"
378 );
379 }
380
381 if ( isset( $config[ApiBase::PARAM_DEPRECATED_VALUES] ) ) {
382 foreach ( $config[ApiBase::PARAM_DEPRECATED_VALUES] as $key => $unused ) {
383 $this->assertContains( $key, $config[ApiBase::PARAM_TYPE],
384 "$param: Deprecated value \"$key\" is not allowed, " .
385 "how can it be deprecated?" );
386 }
387 }
388
389 if (
390 isset( $config[ApiBase::PARAM_ISMULTI_LIMIT1] ) ||
391 isset( $config[ApiBase::PARAM_ISMULTI_LIMIT2] )
392 ) {
393 $this->assertGreaterThanOrEqual( 0, $config[ApiBase::PARAM_ISMULTI_LIMIT1],
394 "$param: PARAM_ISMULTI_LIMIT1 cannot be negative" );
395 // Zero for both doesn't make sense, but you could have
396 // zero for non-bots
397 $this->assertGreaterThanOrEqual( 1, $config[ApiBase::PARAM_ISMULTI_LIMIT2],
398 "$param: PARAM_ISMULTI_LIMIT2 cannot be negative or zero" );
399 $this->assertGreaterThanOrEqual(
400 $config[ApiBase::PARAM_ISMULTI_LIMIT1],
401 $config[ApiBase::PARAM_ISMULTI_LIMIT2],
402 "$param: PARAM_ISMULTI limit cannot be smaller for users with " .
403 "apihighlimits rights" );
404 }
405
406 if ( isset( $config[ApiBase::PARAM_MAX_BYTES] ) ) {
407 $this->assertGreaterThanOrEqual( 1, $config[ApiBase::PARAM_MAX_BYTES],
408 "$param: PARAM_MAX_BYTES cannot be negative or zero" );
409 }
410
411 if ( isset( $config[ApiBase::PARAM_MAX_CHARS] ) ) {
412 $this->assertGreaterThanOrEqual( 1, $config[ApiBase::PARAM_MAX_CHARS],
413 "$param: PARAM_MAX_CHARS cannot be negative or zero" );
414 }
415
416 if (
417 isset( $config[ApiBase::PARAM_MAX_BYTES] ) &&
418 isset( $config[ApiBase::PARAM_MAX_CHARS] )
419 ) {
420 // Length of a string in chars is always <= length in bytes,
421 // so PARAM_MAX_CHARS is pointless if > PARAM_MAX_BYTES
422 $this->assertGreaterThanOrEqual(
423 $config[ApiBase::PARAM_MAX_CHARS],
424 $config[ApiBase::PARAM_MAX_BYTES],
425 "$param: PARAM_MAX_BYTES cannot be less than PARAM_MAX_CHARS"
426 );
427 }
428
429 if ( isset( $config[ApiBase::PARAM_TEMPLATE_VARS] ) ) {
430 $this->assertNotSame( [], $config[ApiBase::PARAM_TEMPLATE_VARS],
431 "$param: PARAM_TEMPLATE_VARS cannot be empty" );
432 foreach ( $config[ApiBase::PARAM_TEMPLATE_VARS] as $key => $target ) {
433 $this->assertRegExp( '/^[^{}]+$/', $key,
434 "$param: PARAM_TEMPLATE_VARS key may not contain '{' or '}'" );
435
436 $this->assertContains( '{' . $key . '}', $param,
437 "$param: Name must contain PARAM_TEMPLATE_VARS key {" . $key . "}" );
438 $this->assertArrayHasKey( $target, $params,
439 "$param: PARAM_TEMPLATE_VARS target parameter '$target' does not exist" );
440 $config2 = $params[$target];
441 $this->assertTrue( !empty( $config2[ApiBase::PARAM_ISMULTI] ),
442 "$param: PARAM_TEMPLATE_VARS target parameter '$target' must have PARAM_ISMULTI = true" );
443
444 if ( isset( $config2[ApiBase::PARAM_TEMPLATE_VARS] ) ) {
445 $this->assertNotSame( $param, $target,
446 "$param: PARAM_TEMPLATE_VARS cannot target itself" );
447
448 $this->assertArraySubset(
449 $config2[ApiBase::PARAM_TEMPLATE_VARS],
450 $config[ApiBase::PARAM_TEMPLATE_VARS],
451 true,
452 "$param: PARAM_TEMPLATE_VARS target parameter '$target': "
453 . "the target's PARAM_TEMPLATE_VARS must be a subset of the original."
454 );
455 }
456 }
457
458 $keys = implode( '|',
459 array_map(
460 function ( $key ) {
461 return preg_quote( $key, '/' );
462 },
463 array_keys( $config[ApiBase::PARAM_TEMPLATE_VARS] )
464 )
465 );
466 $this->assertRegExp( '/^(?>[^{}]+|\{(?:' . $keys . ')\})+$/', $param,
467 "$param: Name may not contain '{' or '}' other than as defined by PARAM_TEMPLATE_VARS" );
468 } else {
469 $this->assertRegExp( '/^[^{}]+$/', $param,
470 "$param: Name may not contain '{' or '}' without PARAM_TEMPLATE_VARS" );
471 }
472 }
473 }
474 }
475
476 /**
477 * Throws if $value does not match one of the types specified in $types.
478 *
479 * @param array $types From self::$paramTypes array
480 * @param mixed $value Value to check
481 * @param string $param Name of param we're checking, for error messages
482 * @param string $desc Description for error messages
483 */
484 private function validateType( $types, $value, $param, $desc ) {
485 if ( count( $types ) === 1 ) {
486 // Only one type allowed
487 if ( is_string( $types[0] ) ) {
488 $this->assertType( $types[0], $value, "$param: $desc type" );
489 } else {
490 // Array whose values have specified types, recurse
491 $this->assertInternalType( 'array', $value, "$param: $desc type" );
492 foreach ( $value as $subvalue ) {
493 $this->validateType( $types[0], $subvalue, $param, "$desc value" );
494 }
495 }
496 } else {
497 // Multiple options
498 foreach ( $types as $type ) {
499 if ( is_string( $type ) ) {
500 if ( class_exists( $type ) || interface_exists( $type ) ) {
501 if ( $value instanceof $type ) {
502 return;
503 }
504 } elseif ( gettype( $value ) === $type ) {
505 return;
506 }
507 } else {
508 // Array whose values have specified types, recurse
509 try {
510 $this->validateType( [ $type ], $value, $param, "$desc type" );
511 // Didn't throw, so we're good
512 return;
513 } catch ( Exception $unused ) {
514 }
515 }
516 }
517 // Doesn't match any of them
518 $this->fail( "$param: $desc has incorrect type" );
519 }
520 }
521
522 /**
523 * Asserts that $default is a valid default for $type.
524 *
525 * @param string $param Name of param, for error messages
526 * @param array $config Array of configuration options for this parameter
527 */
528 private function validateDefault( $param, $config ) {
529 $type = $config[ApiBase::PARAM_TYPE];
530 $default = $config[ApiBase::PARAM_DFLT];
531
532 if ( !empty( $config[ApiBase::PARAM_ISMULTI] ) ) {
533 if ( $default === '' ) {
534 // The empty array is fine
535 return;
536 }
537 $defaults = explode( '|', $default );
538 $config[ApiBase::PARAM_ISMULTI] = false;
539 foreach ( $defaults as $defaultValue ) {
540 // Only allow integers in their simplest form with no leading
541 // or trailing characters etc.
542 if ( $type === 'integer' && $defaultValue === (string)(int)$defaultValue ) {
543 $defaultValue = (int)$defaultValue;
544 }
545 $config[ApiBase::PARAM_DFLT] = $defaultValue;
546 $this->validateDefault( $param, $config );
547 }
548 return;
549 }
550 switch ( $type ) {
551 case 'boolean':
552 $this->assertFalse( $default,
553 "$param: Boolean params may only default to false" );
554 break;
555
556 case 'integer':
557 $this->assertInternalType( 'integer', $default,
558 "$param: Default $default is not an integer" );
559 break;
560
561 case 'limit':
562 if ( $default === 'max' ) {
563 break;
564 }
565 $this->assertInternalType( 'integer', $default,
566 "$param: Default $default is neither an integer nor \"max\"" );
567 break;
568
569 case 'namespace':
570 $validValues = MediaWikiServices::getInstance()->getNamespaceInfo()->
571 getValidNamespaces();
572 if (
573 isset( $config[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
574 is_array( $config[ApiBase::PARAM_EXTRA_NAMESPACES] )
575 ) {
576 $validValues = array_merge(
577 $validValues,
578 $config[ApiBase::PARAM_EXTRA_NAMESPACES]
579 );
580 }
581 $this->assertContains( $default, $validValues,
582 "$param: Default $default is not a valid namespace" );
583 break;
584
585 case 'NULL':
586 case 'password':
587 case 'string':
588 case 'submodule':
589 case 'tags':
590 case 'text':
591 $this->assertInternalType( 'string', $default,
592 "$param: Default $default is not a string" );
593 break;
594
595 case 'timestamp':
596 if ( $default === 'now' ) {
597 return;
598 }
599 $this->assertNotFalse( wfTimestamp( TS_MW, $default ),
600 "$param: Default $default is not a valid timestamp" );
601 break;
602
603 case 'user':
604 // @todo Should we make user validation a public static method
605 // in ApiBase() or something so we don't have to resort to
606 // this? Or in User for that matter.
607 $wrapper = TestingAccessWrapper::newFromObject( new ApiMain() );
608 try {
609 $wrapper->validateUser( $default, '' );
610 } catch ( ApiUsageException $e ) {
611 $this->fail( "$param: Default $default is not a valid username/IP address" );
612 }
613 break;
614
615 default:
616 if ( is_array( $type ) ) {
617 $this->assertContains( $default, $type,
618 "$param: Default $default is not any of " .
619 implode( ', ', $type ) );
620 } else {
621 $this->fail( "Unrecognized type $type" );
622 }
623 }
624 }
625
626 /**
627 * @return array List of API module paths to test
628 */
629 public static function provideParameterConsistency() {
630 $main = self::getMain();
631 $paths = self::getSubModulePaths( $main->getModuleManager() );
632 array_unshift( $paths, $main->getModulePath() );
633
634 $ret = [];
635 foreach ( $paths as $path ) {
636 $ret[] = [ $path ];
637 }
638 return $ret;
639 }
640
641 /**
642 * Return paths of all submodules in an ApiModuleManager, recursively
643 * @param ApiModuleManager $manager
644 * @return string[]
645 */
646 protected static function getSubModulePaths( ApiModuleManager $manager ) {
647 $paths = [];
648 foreach ( $manager->getNames() as $name ) {
649 $module = $manager->getModule( $name );
650 $paths[] = $module->getModulePath();
651 $subManager = $module->getModuleManager();
652 if ( $subManager ) {
653 $paths = array_merge( $paths, self::getSubModulePaths( $subManager ) );
654 }
655 }
656 return $paths;
657 }
658 }