| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Api\ApiBase; |
| 4 |
use MediaWiki\Api\ApiDisabled; |
| 5 |
use MediaWiki\Api\ApiMain; |
| 6 |
use MediaWiki\Api\ApiModuleManager; |
| 7 |
use MediaWiki\Api\ApiQueryDisabled; |
| 8 |
use MediaWiki\Context\RequestContext; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Message\Message; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use Wikimedia\TestingAccessWrapper; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class ApiStructureTest extends MediaWikiIntegrationTestCase { |
| 26 |
|
| 27 |
|
| 28 |
private static $main; |
| 29 |
|
| 30 |
|
| 31 |
private static $testGlobals = [ |
| 32 |
[ |
| 33 |
MainConfigNames::MiserMode => false, |
| 34 |
], |
| 35 |
[ |
| 36 |
MainConfigNames::MiserMode => true, |
| 37 |
], |
| 38 |
]; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private static function getMain() { |
| 45 |
if ( !self::$main ) { |
| 46 |
self::$main = new ApiMain( RequestContext::getMain() ); |
| 47 |
self::$main->getContext()->setLanguage( 'en' ); |
| 48 |
self::$main->getContext()->setTitle( |
| 49 |
Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for ApiStructureTest' ) |
| 50 |
); |
| 51 |
|
| 52 |
|
| 53 |
self::$main->getModuleManager()->addModule( 'disabled', 'action', ApiDisabled::class ); |
| 54 |
self::$main->getModuleFromPath( 'query' ) |
| 55 |
->getModuleManager()->addModule( 'query-disabled', 'meta', ApiQueryDisabled::class ); |
| 56 |
} |
| 57 |
return self::$main; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private function checkMessage( $msg, $what ) { |
| 66 |
|
| 67 |
$msg = Message::newFromSpecifier( $msg ); |
| 68 |
$this->assertTrue( $msg->exists(), "API $what message \"{$msg->getKey()}\" must exist. Did you forgot to add it to your i18n/en.json?" ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function testDocumentationExists( $path, array $globals ) { |
| 77 |
|
| 78 |
$this->overrideConfigValues( $globals ); |
| 79 |
|
| 80 |
$main = self::getMain(); |
| 81 |
|
| 82 |
|
| 83 |
$module = TestingAccessWrapper::newFromObject( $main->getModuleFromPath( $path ) ); |
| 84 |
|
| 85 |
|
| 86 |
foreach ( $module->getHelpFlags() as $flag ) { |
| 87 |
$this->checkMessage( "api-help-flag-$flag", "Flag $flag" ); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
$this->checkMessage( $module->getSummaryMessage(), 'Module summary' ); |
| 92 |
$extendedDesc = $module->getExtendedDescription(); |
| 93 |
if ( is_array( $extendedDesc ) && is_array( $extendedDesc[0] ) ) { |
| 94 |
|
| 95 |
|
| 96 |
$extendedDesc = Message::newFallbackSequence( $extendedDesc[0] ) |
| 97 |
->params( array_slice( $extendedDesc, 1 ) ); |
| 98 |
} |
| 99 |
$this->checkMessage( $extendedDesc, 'Module help top text' ); |
| 100 |
|
| 101 |
|
| 102 |
foreach ( $module->getExamplesMessages() as $qs => $msg ) { |
| 103 |
$this->assertStringStartsNotWith( 'api.php?', $qs, |
| 104 |
"Query string must not begin with 'api.php?'" ); |
| 105 |
$this->checkMessage( $msg, "Example $qs" ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public static function provideDocumentationExists() { |
| 110 |
$main = self::getMain(); |
| 111 |
$paths = self::getSubModulePaths( $main->getModuleManager() ); |
| 112 |
array_unshift( $paths, $main->getModulePath() ); |
| 113 |
|
| 114 |
$ret = []; |
| 115 |
foreach ( $paths as $path ) { |
| 116 |
foreach ( self::$testGlobals as $globals ) { |
| 117 |
$g = []; |
| 118 |
foreach ( $globals as $k => $v ) { |
| 119 |
$g[] = "$k=" . var_export( $v, 1 ); |
| 120 |
} |
| 121 |
$k = "Module $path with " . implode( ', ', $g ); |
| 122 |
$ret[$k] = [ $path, $globals ]; |
| 123 |
} |
| 124 |
} |
| 125 |
return $ret; |
| 126 |
} |
| 127 |
|
| 128 |
private function doTestParameters( string $path, array $params, string $name ): void { |
| 129 |
$main = self::getMain(); |
| 130 |
|
| 131 |
$dataName = $this->dataName(); |
| 132 |
$this->assertNotSame( '', $name, "$dataName: Name cannot be empty" ); |
| 133 |
$this->assertArrayHasKey( $name, $params, "$dataName: Existence check" ); |
| 134 |
|
| 135 |
$ret = $main->getParamValidator()->checkSettings( |
| 136 |
$main->getModuleFromPath( $path ), $params, $name, [] |
| 137 |
); |
| 138 |
|
| 139 |
|
| 140 |
if ( is_array( $params[$name] ) ) { |
| 141 |
$keys = array_diff( |
| 142 |
array_keys( $params[$name] ), |
| 143 |
$ret['allowedKeys'] |
| 144 |
); |
| 145 |
if ( $keys ) { |
| 146 |
|
| 147 |
$this->addWarning( |
| 148 |
"$dataName: Unrecognized settings keys were used: " . implode( ', ', $keys ) |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if ( count( $ret['issues'] ) === 1 ) { |
| 154 |
$this->fail( "$dataName: Validation failed: " . reset( $ret['issues'] ) ); |
| 155 |
} elseif ( $ret['issues'] ) { |
| 156 |
$this->fail( "$dataName: Validation failed:\n* " . implode( "\n* ", $ret['issues'] ) ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
$done = []; |
| 161 |
foreach ( $ret['messages'] as $msg ) { |
| 162 |
|
| 163 |
$key = $msg->getKey(); |
| 164 |
if ( !isset( $done[$key] ) ) { |
| 165 |
$done[$key] = true; |
| 166 |
$this->checkMessage( $key, "$dataName: Parameter" ); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function testParameters( string $path, string $argset, array $args, ApiMain $main ): void { |
| 175 |
$module = $main->getModuleFromPath( $path ); |
| 176 |
$params = $module->getFinalParams( ...$args ); |
| 177 |
if ( !$params ) { |
| 178 |
$this->addToAssertionCount( 1 ); |
| 179 |
return; |
| 180 |
} |
| 181 |
foreach ( $params as $param => $_ ) { |
| 182 |
$this->doTestParameters( $path, $params, $param ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
public static function provideParameters(): Iterator { |
| 187 |
$main = self::getMain(); |
| 188 |
$paths = self::getSubModulePaths( $main->getModuleManager() ); |
| 189 |
array_unshift( $paths, $main->getModulePath() ); |
| 190 |
$argsets = [ |
| 191 |
'plain' => [], |
| 192 |
'for help' => [ ApiBase::GET_VALUES_FOR_HELP ], |
| 193 |
]; |
| 194 |
|
| 195 |
foreach ( $paths as $path ) { |
| 196 |
foreach ( $argsets as $argset => $args ) { |
| 197 |
|
| 198 |
|
| 199 |
yield "Module $path, argset $argset" => [ $path, $argset, $args, $main ]; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
protected static function getSubModulePaths( ApiModuleManager $manager ) { |
| 210 |
$paths = []; |
| 211 |
foreach ( $manager->getNames() as $name ) { |
| 212 |
$module = $manager->getModule( $name ); |
| 213 |
$paths[] = $module->getModulePath(); |
| 214 |
$subManager = $module->getModuleManager(); |
| 215 |
if ( $subManager ) { |
| 216 |
$paths = array_merge( $paths, self::getSubModulePaths( $subManager ) ); |
| 217 |
} |
| 218 |
} |
| 219 |
return $paths; |
| 220 |
} |
| 221 |
} |