2023-12-04 22:21:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Rest\Handler;
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Config\HashConfig;
|
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2024-02-08 14:56:54 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2023-12-04 22:21:07 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
|
use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer;
|
2023-10-30 16:04:41 +00:00
|
|
|
use MediaWiki\Rest\Handler;
|
|
|
|
|
use MediaWiki\Rest\Handler\ModuleSpecHandler;
|
2023-12-04 22:21:07 +00:00
|
|
|
use MediaWiki\Rest\Reporter\MWErrorReporter;
|
|
|
|
|
use MediaWiki\Rest\RequestData;
|
|
|
|
|
use MediaWiki\Rest\RequestInterface;
|
|
|
|
|
use MediaWiki\Rest\ResponseFactory;
|
|
|
|
|
use MediaWiki\Rest\Router;
|
|
|
|
|
use MediaWiki\Rest\Validator\Validator;
|
|
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
|
use Wikimedia\Message\ITextFormatter;
|
|
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-30 16:04:41 +00:00
|
|
|
* @covers \MediaWiki\Rest\Handler\ModuleSpecHandler
|
2023-12-04 22:21:07 +00:00
|
|
|
*
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2023-10-30 16:04:41 +00:00
|
|
|
class ModuleSpecHandlerTest extends MediaWikiIntegrationTestCase {
|
2023-12-04 22:21:07 +00:00
|
|
|
use HandlerTestTrait;
|
|
|
|
|
|
|
|
|
|
private function createRouter(
|
2024-02-21 20:51:29 +00:00
|
|
|
RequestInterface $request,
|
|
|
|
|
$specFile
|
2023-12-04 22:21:07 +00:00
|
|
|
): Router {
|
|
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
|
|
|
|
|
$conf = $services->getMainConfig();
|
|
|
|
|
|
|
|
|
|
$authority = $context->getAuthority();
|
|
|
|
|
$authorizer = new StaticBasicAuthorizer();
|
|
|
|
|
|
|
|
|
|
$objectFactory = $services->getObjectFactory();
|
|
|
|
|
$restValidator = new Validator( $objectFactory,
|
|
|
|
|
$request,
|
|
|
|
|
$authority
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$formatter = new class implements ITextFormatter {
|
|
|
|
|
public function getLangCode() {
|
|
|
|
|
return 'qqx';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function format( MessageValue $message ) {
|
|
|
|
|
return $message->dump();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$responseFactory = new ResponseFactory( [ $formatter ] );
|
|
|
|
|
|
|
|
|
|
return ( new Router(
|
2024-02-21 20:51:29 +00:00
|
|
|
[ $specFile ],
|
|
|
|
|
[],
|
2023-12-04 22:21:07 +00:00
|
|
|
new ServiceOptions( Router::CONSTRUCTOR_OPTIONS, $conf ),
|
|
|
|
|
$services->getLocalServerObjectCache(),
|
|
|
|
|
$responseFactory,
|
|
|
|
|
$authorizer,
|
|
|
|
|
$authority,
|
|
|
|
|
$objectFactory,
|
|
|
|
|
$restValidator,
|
|
|
|
|
new MWErrorReporter(),
|
|
|
|
|
$services->getHookContainer(),
|
|
|
|
|
$context->getRequest()->getSession()
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function newHandler() {
|
|
|
|
|
$config = new HashConfig( [
|
|
|
|
|
MainConfigNames::RightsUrl => '',
|
|
|
|
|
MainConfigNames::RightsText => '',
|
|
|
|
|
MainConfigNames::EmergencyContact => '',
|
|
|
|
|
MainConfigNames::Sitename => '',
|
|
|
|
|
] );
|
2023-10-30 16:04:41 +00:00
|
|
|
return new ModuleSpecHandler(
|
2023-12-04 22:21:07 +00:00
|
|
|
$config
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 16:04:41 +00:00
|
|
|
public static function provideGetInfoSpecSuccess() {
|
2024-02-21 20:51:29 +00:00
|
|
|
yield 'named module' => [
|
2023-10-30 16:04:41 +00:00
|
|
|
[
|
2024-02-21 20:51:29 +00:00
|
|
|
'pathParams' => [ 'module' => 'mock', 'version' => 'v1' ],
|
|
|
|
|
],
|
|
|
|
|
__DIR__ . '/SpecTestRoutes.json'
|
|
|
|
|
];
|
|
|
|
|
yield 'OpenAPI module' => [
|
|
|
|
|
[
|
|
|
|
|
'pathParams' => [ 'module' => 'mock', 'version' => 'v1' ],
|
|
|
|
|
],
|
|
|
|
|
__DIR__ . '/SpecTestOpenAPIModule.json'
|
2023-10-30 16:04:41 +00:00
|
|
|
];
|
|
|
|
|
yield 'prefix-less module' => [
|
|
|
|
|
[
|
2024-02-21 20:51:29 +00:00
|
|
|
'pathParams' => [ 'module' => '-' ],
|
|
|
|
|
],
|
|
|
|
|
__DIR__ . '/SpecTestFlatRoutes.json'
|
2023-10-30 16:04:41 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetInfoSpecSuccess
|
|
|
|
|
*/
|
2024-02-21 20:51:29 +00:00
|
|
|
public function testGetInfoSpecSuccess( $params, $specFile ) {
|
2023-10-30 16:04:41 +00:00
|
|
|
$request = new RequestData( $params );
|
2023-12-04 22:21:07 +00:00
|
|
|
|
2024-02-21 20:51:29 +00:00
|
|
|
$router = $this->createRouter( $request, $specFile );
|
2023-12-04 22:21:07 +00:00
|
|
|
|
|
|
|
|
$handler = $this->newHandler();
|
|
|
|
|
$response = $this->executeHandler(
|
|
|
|
|
$handler,
|
|
|
|
|
$request,
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
$router
|
|
|
|
|
);
|
|
|
|
|
$this->assertSame( 200, $response->getStatusCode() );
|
|
|
|
|
$this->assertArrayHasKey( 'Content-Type', $response->getHeaders() );
|
|
|
|
|
$this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) );
|
|
|
|
|
$data = json_decode( $response->getBody(), true );
|
|
|
|
|
$this->assertIsArray( $data, 'Body must be a JSON array' );
|
|
|
|
|
}
|
2023-10-30 16:04:41 +00:00
|
|
|
|
|
|
|
|
public static function newFooBarHandler() {
|
|
|
|
|
return new class extends Handler {
|
|
|
|
|
public function execute() {
|
|
|
|
|
return 'foo bar';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 22:21:07 +00:00
|
|
|
}
|