Debian Codegraph @

tests/phpunit/integration/includes/Rest/Handler/ModuleSpecHandlerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use JsonSchemaAssertionTrait;
6 use MediaWiki\Config\ServiceOptions;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer;
9 use MediaWiki\Rest\Handler;
10 use MediaWiki\Rest\Handler\ModuleSpecHandler;
11 use MediaWiki\Rest\Reporter\MWErrorReporter;
12 use MediaWiki\Rest\RequestData;
13 use MediaWiki\Rest\RequestInterface;
14 use MediaWiki\Rest\ResponseFactory;
15 use MediaWiki\Rest\Router;
16 use MediaWiki\Rest\Validator\Validator;
17 use MediaWikiIntegrationTestCase;
18 use PHPUnit\Framework\Assert;
19 use PHPUnit\Framework\Constraint\Constraint;
20 use Wikimedia\Message\ITextFormatter;
21 use Wikimedia\Message\MessageSpecifier;
22 use Wikimedia\ParamValidator\ParamValidator;
23
24 /**
25 * @covers \MediaWiki\Rest\Handler\ModuleSpecHandler
26 *
27 * @group Database
28 */
29 class ModuleSpecHandlerTest extends MediaWikiIntegrationTestCase {
30 use HandlerTestTrait;
31 use JsonSchemaAssertionTrait;
32
33 private function createRouter(
34 RequestInterface $request,
35 $specFile
36 ): Router {
37 $services = $this->getServiceContainer();
38
39 $conf = $services->getMainConfig();
40
41 $authority = $this->mockRegisteredUltimateAuthority();
42 $authorizer = new StaticBasicAuthorizer();
43
44 $objectFactory = $services->getObjectFactory();
45 $restValidator = new Validator( $objectFactory,
46 $request,
47 $authority
48 );
49
50 $formatter = new class implements ITextFormatter {
51 public function getLangCode() {
52 return 'qqx';
53 }
54
55 public function format( MessageSpecifier $message ): string {
56 return $message->dump();
57 }
58 };
59 $responseFactory = new ResponseFactory( [ $formatter ] );
60
61 return ( new Router(
62 [ $specFile ],
63 [],
64 new ServiceOptions( Router::CONSTRUCTOR_OPTIONS, $conf ),
65 $services->getLocalServerObjectCache(),
66 $responseFactory,
67 $authorizer,
68 $authority,
69 $objectFactory,
70 $restValidator,
71 new MWErrorReporter(),
72 $services->getHookContainer(),
73 $this->getSession( true )
74 ) );
75 }
76
77 private function newHandler() {
78 $config = $this->getServiceContainer()->getMainConfig();
79 return new ModuleSpecHandler(
80 $config
81 );
82 }
83
84 private function assertWellFormedOAS( array $spec ) {
85 $this->assertMatchesJsonSchema(
86 __DIR__ . '/data/OpenApi-3.0.json',
87 $spec
88 );
89 }
90
91 private static function assertContainsRecursive(
92 array $expected,
93 array $actual,
94 string $message = ''
95 ) {
96 foreach ( $expected as $key => $value ) {
97 Assert::assertArrayHasKey( $key, $actual, $message );
98
99 if ( is_array( $value ) ) {
100 Assert::assertIsArray( $actual[$key], $message );
101
102 self::assertContainsRecursive( $value, $actual[$key], $message );
103 } elseif ( $value instanceof Constraint ) {
104 $value->evaluate( $actual[$key], $message );
105 } else {
106 Assert::assertSame( $value, $actual[$key], $message );
107 }
108 }
109 }
110
111 public static function provideGetInfoSpecSuccess() {
112 yield 'module and version' => [
113 __DIR__ . '/SpecTestModule.json',
114 [
115 'pathParams' => [ 'module' => 'mock', 'version' => 'v1' ]
116 ],
117 [
118 'info' => [
119 'title' => 'mock/v1 Module',
120 'version' => '1.3-test',
121 'contact' => [
122 'email' => '[email protected]'
123 ],
124 ],
125 'servers' => [
126 [ 'url' => 'https://example.com:1234/api/mock/v1' ]
127 ],
128 'paths' => [
129 '/foo/bar' => [
130 'get' => [
131 'parameters' => [ [ 'name' => 'q', 'in' => 'query' ] ],
132 'responses' => [ 200 => [ 'description' => 'OK' ] ]
133 ],
134 'post' => [
135 'requestBody' => [
136 'required' => true,
137 'content' => [
138 'application/json' => [
139 'schema' => [
140 'type' => 'object',
141 'required' => [ 'b' ],
142 'properties' => [
143 'b' => [ 'type' => 'string' ]
144 ],
145 ]
146 ]
147 ]
148 ],
149 'responses' => [ 200 => [ 'description' => 'OK' ] ]
150 ],
151 ]
152 ],
153 'components' => [
154 'schemas' => [
155 'boolean-param' => [ 'type' => 'boolean' ],
156 ],
157 'responses' => [
158 'GenericErrorResponse' => self::anything(),
159 ],
160 ]
161 ]
162 ];
163 yield 'prefix-less module' => [
164 __DIR__ . '/SpecTestFlatRoutes.json',
165 [
166 'pathParams' => [ 'module' => '-' ]
167 ],
168 [
169 'info' => [
170 'title' => 'Extra Routes',
171 'version' => 'undefined',
172 'license' => [
173 'name' => 'Test License',
174 'url' => 'https://example.com/license',
175 ],
176 ],
177 'servers' => [
178 [ 'url' => 'https://example.com:1234/api' ]
179 ],
180 'paths' => [
181 '/mock/v1/foo/bar' => [
182 'get' => [ 'responses' => [ 200 => [ 'description' => 'OK' ] ] ],
183 ]
184 ],
185 ]
186 ];
187 }
188
189 /**
190 * @dataProvider provideGetInfoSpecSuccess
191 */
192 public function testGetInfoSpecSuccess( $specFile, $params, $expected ) {
193 $this->overrideConfigValues( [
194 MainConfigNames::RightsText => 'Test License',
195 MainConfigNames::RightsUrl => 'https://example.com/license',
196 MainConfigNames::EmergencyContact => '[email protected]',
197 MainConfigNames::CanonicalServer => 'https://example.com:1234',
198 MainConfigNames::RestPath => '/api',
199 ] );
200
201 $request = new RequestData( $params );
202
203 $router = $this->createRouter( $request, $specFile );
204
205 $handler = $this->newHandler();
206 $response = $this->executeHandler(
207 $handler,
208 $request,
209 [],
210 [],
211 [],
212 [],
213 null,
214 null,
215 $router
216 );
217 $this->assertSame( 200, $response->getStatusCode() );
218 $this->assertArrayHasKey( 'Content-Type', $response->getHeaders() );
219 $this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) );
220 $data = json_decode( (string)$response->getBody(), true );
221
222 $this->assertIsArray( $data, 'Body must be a JSON array' );
223 $this->assertWellFormedOAS( $data );
224 $this->assertContainsRecursive( $expected, $data );
225 }
226
227 public static function newFooBarHandler() {
228 return new class extends Handler {
229 public function getParamSettings() {
230 return [
231 'q' => [
232 Handler::PARAM_SOURCE => 'query',
233 ParamValidator::PARAM_REQUIRED => 'false',
234 ],
235 ];
236 }
237
238 public function getBodyParamSettings(): array {
239 return [
240 'b' => [
241 Handler::PARAM_SOURCE => 'body',
242 ParamValidator::PARAM_REQUIRED => 'true',
243 ],
244 ];
245 }
246
247 public function execute() {
248 return 'foo bar';
249 }
250 };
251 }
252
253 }