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