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