| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\HookContainer\HookContainer; |
| 5 |
use MediaWiki\HookContainer\StaticHookRegistry; |
| 6 |
use MediaWiki\Language\Language; |
| 7 |
use MediaWiki\MediaWikiServices; |
| 8 |
use MediaWiki\Message\Message; |
| 9 |
use MediaWiki\ParamValidator\TypeDef\ArrayDef; |
| 10 |
use MediaWiki\Permissions\SimpleAuthority; |
| 11 |
use MediaWiki\Request\WebRequest; |
| 12 |
use MediaWiki\Rest\CorsUtils; |
| 13 |
use MediaWiki\Rest\EntryPoint; |
| 14 |
use MediaWiki\Rest\Handler; |
| 15 |
use MediaWiki\Rest\PathTemplateMatcher\PathMatcher; |
| 16 |
use MediaWiki\Rest\RequestData; |
| 17 |
use MediaWiki\Rest\ResponseFactory; |
| 18 |
use MediaWiki\Rest\Router; |
| 19 |
use MediaWiki\Rest\Validator\Validator; |
| 20 |
use MediaWiki\Session\Session; |
| 21 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 22 |
use MediaWiki\Title\Title; |
| 23 |
use MediaWiki\User\UserIdentityValue; |
| 24 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 25 |
use Wikimedia\ParamValidator\ParamValidator; |
| 26 |
use Wikimedia\Stats\StatsFactory; |
| 27 |
use Wikimedia\TestingAccessWrapper; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class RestStructureTest extends MediaWikiIntegrationTestCase { |
| 39 |
use DummyServicesTrait; |
| 40 |
use JsonSchemaAssertionTrait; |
| 41 |
|
| 42 |
private const SPEC_FILES = [ |
| 43 |
'https://spec.openapis.org/oas/3.0/schema/2021-09-28#' => |
| 44 |
MW_INSTALL_PATH . '/tests/phpunit/integration/includes/' . |
| 45 |
'Rest/Handler/data/OpenApi-3.0.json', |
| 46 |
|
| 47 |
'http://json-schema.org/draft-04/schema#' => |
| 48 |
MW_INSTALL_PATH . '/vendor/justinrainbow/json-schema/dist/' . |
| 49 |
'schema/json-schema-draft-04.json', |
| 50 |
|
| 51 |
'https://www.mediawiki.org/schema/mwapi-1.0#' => |
| 52 |
MW_INSTALL_PATH . '/docs/rest/mwapi-1.0.json', |
| 53 |
|
| 54 |
'https://www.mediawiki.org/schema/discovery-1.0#' => |
| 55 |
MW_INSTALL_PATH . '/docs/rest/discovery-1.0.json', |
| 56 |
]; |
| 57 |
|
| 58 |
|
| 59 |
private $router = null; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private function getFakeServiceContainer(): MediaWikiServices { |
| 67 |
$realConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 68 |
|
| 69 |
$objectFactory = $this->getDummyObjectFactory(); |
| 70 |
$hookContainer = new HookContainer( |
| 71 |
new StaticHookRegistry(), |
| 72 |
$objectFactory |
| 73 |
); |
| 74 |
|
| 75 |
$services = $this->createNoOpMock( |
| 76 |
MediaWikiServices::class, |
| 77 |
[ |
| 78 |
'getMainConfig', |
| 79 |
'getHookContainer', |
| 80 |
'getObjectFactory', |
| 81 |
'getLocalServerObjectCache', |
| 82 |
'getStatsFactory', |
| 83 |
] |
| 84 |
); |
| 85 |
$services->method( 'getMainConfig' )->willReturn( $realConfig ); |
| 86 |
$services->method( 'getHookContainer' )->willReturn( $hookContainer ); |
| 87 |
$services->method( 'getObjectFactory' )->willReturn( $objectFactory ); |
| 88 |
$services->method( 'getLocalServerObjectCache' )->willReturn( new EmptyBagOStuff() ); |
| 89 |
$services->method( 'getStatsFactory' )->willReturn( StatsFactory::newNull() ); |
| 90 |
|
| 91 |
return $services; |
| 92 |
} |
| 93 |
|
| 94 |
private function getRouterForDataProviders(): Router { |
| 95 |
static $router = null; |
| 96 |
|
| 97 |
if ( !$router ) { |
| 98 |
$language = $this->createNoOpMock( Language::class, [ 'getCode' ] ); |
| 99 |
$language->method( 'getCode' )->willReturn( 'en' ); |
| 100 |
|
| 101 |
$title = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for RestStructureTest' ); |
| 102 |
$authority = new SimpleAuthority( new UserIdentityValue( 0, 'Testor' ), [] ); |
| 103 |
|
| 104 |
$request = $this->createNoOpMock( WebRequest::class, [ 'getSession' ] ); |
| 105 |
$request->method( 'getSession' )->willReturn( $this->createNoOpMock( Session::class ) ); |
| 106 |
|
| 107 |
$context = $this->createNoOpMock( |
| 108 |
RequestContext::class, |
| 109 |
[ 'getLanguage', 'getTitle', 'getAuthority', 'getRequest' ] |
| 110 |
); |
| 111 |
$context->method( 'getLanguage' )->willReturn( $language ); |
| 112 |
$context->method( 'getTitle' )->willReturn( $title ); |
| 113 |
$context->method( 'getAuthority' )->willReturn( $authority ); |
| 114 |
$context->method( 'getRequest' )->willReturn( $request ); |
| 115 |
|
| 116 |
$responseFactory = $this->createNoOpMock( ResponseFactory::class ); |
| 117 |
$cors = $this->createNoOpMock( CorsUtils::class ); |
| 118 |
|
| 119 |
$services = $this->getFakeServiceContainer(); |
| 120 |
|
| 121 |
|
| 122 |
$entryPoint = TestingAccessWrapper::newFromClass( EntryPoint::class ); |
| 123 |
$router = $entryPoint->createRouter( |
| 124 |
$services, |
| 125 |
$context, |
| 126 |
new RequestData(), |
| 127 |
$responseFactory, |
| 128 |
$cors |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
return $router; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private function getTestRouter(): Router { |
| 141 |
if ( !$this->router ) { |
| 142 |
$language = $this->createNoOpMock( Language::class, [ 'getCode' ] ); |
| 143 |
$language->method( 'getCode' )->willReturn( 'en' ); |
| 144 |
|
| 145 |
$title = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for RestStructureTest' ); |
| 146 |
$authority = new SimpleAuthority( new UserIdentityValue( 0, 'Testor' ), [] ); |
| 147 |
|
| 148 |
$request = $this->createNoOpMock( WebRequest::class, [ 'getSession' ] ); |
| 149 |
$request->method( 'getSession' )->willReturn( $this->createNoOpMock( Session::class ) ); |
| 150 |
|
| 151 |
$context = $this->createNoOpMock( |
| 152 |
RequestContext::class, |
| 153 |
[ 'getLanguage', 'getTitle', 'getAuthority', 'getRequest' ] |
| 154 |
); |
| 155 |
$context->method( 'getLanguage' )->willReturn( $language ); |
| 156 |
$context->method( 'getTitle' )->willReturn( $title ); |
| 157 |
$context->method( 'getAuthority' )->willReturn( $authority ); |
| 158 |
$context->method( 'getRequest' )->willReturn( $request ); |
| 159 |
|
| 160 |
$responseFactory = $this->createNoOpMock( ResponseFactory::class ); |
| 161 |
$cors = $this->createNoOpMock( CorsUtils::class ); |
| 162 |
|
| 163 |
$this->router = EntryPoint::createRouter( |
| 164 |
$this->getServiceContainer(), $context, new RequestData(), $responseFactory, $cors |
| 165 |
); |
| 166 |
} |
| 167 |
return $this->router; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function testPathParameters( string $moduleName, string $method, string $path ): void { |
| 174 |
$router = $this->getTestRouter(); |
| 175 |
$module = $router->getModule( $moduleName ); |
| 176 |
|
| 177 |
$request = new RequestData( [ 'method' => $method ] ); |
| 178 |
$handler = $module->getHandlerForPath( $path, $request, false ); |
| 179 |
|
| 180 |
$params = $handler->getParamSettings(); |
| 181 |
$dataName = $this->dataName(); |
| 182 |
|
| 183 |
|
| 184 |
$matcher = TestingAccessWrapper::newFromObject( new PathMatcher ); |
| 185 |
$pathParams = []; |
| 186 |
foreach ( explode( '/', $path ) as $part ) { |
| 187 |
$param = $matcher->getParamName( $part ); |
| 188 |
if ( $param !== false ) { |
| 189 |
$this->assertArrayHasKey( $param, $params, "Path parameter $param exists" ); |
| 190 |
$this->assertSame( 'path', $params[$param][Handler::PARAM_SOURCE] ?? null, |
| 191 |
"$dataName: Path parameter {{$param}} must have PARAM_SOURCE = 'path'" ); |
| 192 |
$pathParams[$param] = true; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
foreach ( $params as $param => $settings ) { |
| 198 |
if ( ( $settings[Handler::PARAM_SOURCE] ?? null ) === 'path' && |
| 199 |
!isset( $pathParams[$param] ) |
| 200 |
) { |
| 201 |
$this->assertFalse( $settings[ParamValidator::PARAM_REQUIRED] ?? false, |
| 202 |
"$dataName, parameter $param: PARAM_REQUIRED cannot be true for a path parameter " |
| 203 |
. 'not in the path' |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
$this->addToAssertionCount( 1 ); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function testBodyParameters( string $moduleName, string $method, string $path ): void { |
| 216 |
$router = $this->getTestRouter(); |
| 217 |
$module = $router->getModule( $moduleName ); |
| 218 |
|
| 219 |
$request = new RequestData( [ 'method' => $method ] ); |
| 220 |
$handler = $module->getHandlerForPath( $path, $request, false ); |
| 221 |
|
| 222 |
$bodySettings = $handler->getBodyParamSettings(); |
| 223 |
|
| 224 |
if ( !$bodySettings ) { |
| 225 |
$this->addToAssertionCount( 1 ); |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
foreach ( $bodySettings as $settings ) { |
| 230 |
$this->assertArrayHasKey( Handler::PARAM_SOURCE, $settings ); |
| 231 |
$this->assertSame( 'body', $settings[Handler::PARAM_SOURCE] ); |
| 232 |
|
| 233 |
if ( isset( $settings[ ArrayDef::PARAM_SCHEMA ] ) ) { |
| 234 |
try { |
| 235 |
$this->assertValidJsonSchema( $settings[ ArrayDef::PARAM_SCHEMA ] ); |
| 236 |
} catch ( LogicException $e ) { |
| 237 |
$this->fail( "Invalid JSON schema for parameter {$settings['name']}: " . $e->getMessage() ); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function testBodyParametersNotInParamSettings( string $moduleName, string $method, string $path ): void { |
| 247 |
$router = $this->getTestRouter(); |
| 248 |
$module = $router->getModule( $moduleName ); |
| 249 |
|
| 250 |
$request = new RequestData( [ 'method' => $method ] ); |
| 251 |
$handler = $module->getHandlerForPath( $path, $request, false ); |
| 252 |
|
| 253 |
$paramSettings = $handler->getParamSettings(); |
| 254 |
|
| 255 |
if ( !$paramSettings ) { |
| 256 |
$this->addToAssertionCount( 1 ); |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
foreach ( $paramSettings as $settings ) { |
| 261 |
$this->assertArrayHasKey( Handler::PARAM_SOURCE, $settings ); |
| 262 |
$this->assertNotSame( 'body', $settings[Handler::PARAM_SOURCE] ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
public function provideModules(): Iterator { |
| 267 |
$router = $this->getRouterForDataProviders(); |
| 268 |
|
| 269 |
foreach ( $router->getModuleIds() as $name ) { |
| 270 |
yield "Module '$name'" => [ $name ]; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
public function provideRoutes(): Iterator { |
| 275 |
$router = $this->getRouterForDataProviders(); |
| 276 |
|
| 277 |
foreach ( $router->getModuleIds() as $moduleName ) { |
| 278 |
$module = $router->getModule( $moduleName ); |
| 279 |
|
| 280 |
foreach ( $module->getDefinedPaths() as $path => $methods ) { |
| 281 |
|
| 282 |
foreach ( $methods as $method ) { |
| 283 |
|
| 284 |
|
| 285 |
yield "Handler in module '$moduleName' for $method $path" |
| 286 |
=> [ $moduleName, $method, $path ]; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public function testParameters( string $moduleName, string $method, string $path ): void { |
| 296 |
$router = $this->getTestRouter(); |
| 297 |
$module = $router->getModule( $moduleName ); |
| 298 |
|
| 299 |
$request = new RequestData( [ 'method' => $method ] ); |
| 300 |
$handler = $module->getHandlerForPath( $path, $request, false ); |
| 301 |
|
| 302 |
$params = $handler->getParamSettings(); |
| 303 |
foreach ( $params as $param => $settings ) { |
| 304 |
$method = $routeSpec['method'] ?? 'GET'; |
| 305 |
$method = implode( ",", (array)$method ); |
| 306 |
|
| 307 |
$this->assertParameter( $param, $settings, "Handler {$method} {$path}, parameter $param" ); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
private function assertParameter( string $name, $settings, $msg ) { |
| 312 |
$router = TestingAccessWrapper::newFromObject( $this->getTestRouter() ); |
| 313 |
|
| 314 |
$dataName = $this->dataName(); |
| 315 |
$this->assertNotSame( '', $name, "$msg: $dataName: Name cannot be empty" ); |
| 316 |
|
| 317 |
$paramValidator = TestingAccessWrapper::newFromObject( $router->restValidator )->paramValidator; |
| 318 |
$ret = $paramValidator->checkSettings( $name, $settings, [ 'source' => 'unspecified' ] ); |
| 319 |
|
| 320 |
|
| 321 |
$ret['allowedKeys'][] = Handler::PARAM_SOURCE; |
| 322 |
$ret['allowedKeys'][] = Handler::PARAM_DESCRIPTION; |
| 323 |
if ( !in_array( $settings[Handler::PARAM_SOURCE] ?? '', Validator::KNOWN_PARAM_SOURCES, true ) ) { |
| 324 |
$ret['issues'][Handler::PARAM_SOURCE] = "PARAM_SOURCE must be one of " . implode( ', ', Validator::KNOWN_PARAM_SOURCES ); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
if ( isset( $settings[ParamValidator::PARAM_TYPE] ) && $settings[ParamValidator::PARAM_TYPE] === 'array' ) { |
| 329 |
$this->fail( "$msg: $dataName: 'array' type is not allowed in getParamSettings" ); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
if ( is_array( $settings ) ) { |
| 334 |
$keys = array_diff( |
| 335 |
array_keys( $settings ), |
| 336 |
$ret['allowedKeys'] |
| 337 |
); |
| 338 |
if ( $keys ) { |
| 339 |
$this->addWarning( |
| 340 |
"$msg: $dataName: Unrecognized settings keys were used: " . implode( ', ', $keys ) |
| 341 |
); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if ( count( $ret['issues'] ) === 1 ) { |
| 346 |
$this->fail( "$msg: $dataName: Validation failed: " . reset( $ret['issues'] ) ); |
| 347 |
} elseif ( $ret['issues'] ) { |
| 348 |
$this->fail( "$msg: $dataName: Validation failed:\n* " . implode( "\n* ", $ret['issues'] ) ); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
$done = []; |
| 353 |
foreach ( $ret['messages'] as $msg ) { |
| 354 |
|
| 355 |
$key = $msg->getKey(); |
| 356 |
if ( !isset( $done[$key] ) ) { |
| 357 |
$done[$key] = true; |
| 358 |
$this->assertTrue( Message::newFromKey( $key )->exists(), |
| 359 |
"$msg: $dataName: Parameter message $key exists" ); |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
public function testRoutePathAndMethodForDuplicates() { |
| 365 |
$router = $this->getTestRouter(); |
| 366 |
$routes = []; |
| 367 |
|
| 368 |
foreach ( $router->getModuleIds() as $moduleName ) { |
| 369 |
$module = $router->getModule( $moduleName ); |
| 370 |
$paths = $module->getDefinedPaths(); |
| 371 |
|
| 372 |
foreach ( $paths as $path => $methods ) { |
| 373 |
foreach ( $methods as $method ) { |
| 374 |
|
| 375 |
|
| 376 |
$key = "$moduleName: $method $path"; |
| 377 |
|
| 378 |
$this->assertArrayNotHasKey( $key, $routes, "{$key} already exists in routes" ); |
| 379 |
$routes[$key] = true; |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
public function provideModuleDefinitionFiles() { |
| 386 |
$conf = MediaWikiServices::getInstance()->getMainConfig(); |
| 387 |
$entryPoint = TestingAccessWrapper::newFromClass( EntryPoint::class ); |
| 388 |
$routeFiles = $entryPoint->getRouteFiles( $conf ); |
| 389 |
|
| 390 |
foreach ( $routeFiles as $file ) { |
| 391 |
$moduleSpec = self::loadJsonData( $file ); |
| 392 |
if ( !isset( $moduleSpec->mwapi ) ) { |
| 393 |
|
| 394 |
continue; |
| 395 |
} |
| 396 |
yield $file => [ $moduleSpec ]; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public function testModuleDefinitionFiles( stdClass $moduleSpec ) { |
| 404 |
$schemaFile = MW_INSTALL_PATH . '/docs/rest/mwapi-1.0.json'; |
| 405 |
|
| 406 |
$this->assertMatchesJsonSchema( $schemaFile, $moduleSpec, self::SPEC_FILES ); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public function testGetModuleDescription( string $moduleName ): void { |
| 413 |
static $infoSchema = [ '$ref' => |
| 414 |
'https://www.mediawiki.org/schema/discovery-1.0#/definitions/Module' |
| 415 |
]; |
| 416 |
|
| 417 |
$router = $this->getTestRouter(); |
| 418 |
$module = $router->getModule( $moduleName ); |
| 419 |
$info = $module->getModuleDescription(); |
| 420 |
|
| 421 |
$this->assertMatchesJsonSchema( $infoSchema, $info, self::SPEC_FILES ); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function testGetOpenApiInfo( string $moduleName ): void { |
| 428 |
static $infoSchema = [ '$ref' => |
| 429 |
'https://spec.openapis.org/oas/3.0/schema/2021-09-28#/definitions/Info' |
| 430 |
]; |
| 431 |
|
| 432 |
$router = $this->getTestRouter(); |
| 433 |
$module = $router->getModule( $moduleName ); |
| 434 |
$info = $module->getOpenApiInfo(); |
| 435 |
|
| 436 |
$this->assertMatchesJsonSchema( $infoSchema, $info, self::SPEC_FILES ); |
| 437 |
} |
| 438 |
|
| 439 |
} |