Debian Codegraph @

extensions/Math/tests/phpunit/MathRestbaseInterfaceTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Extension\Math\InvalidTeXException;
4 use MediaWiki\Extension\Math\MathRestbaseException;
5 use MediaWiki\Extension\Math\MathRestbaseInterface;
6 use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
7 use Wikimedia\Http\MultiHttpClient;
8
9 /**
10 * Test the interface to access Restbase paths
11 * /media/math/check/{type}
12 * /media/math/render/{format}/{hash}
13 *
14 * @covers \MediaWiki\Extension\Math\MathRestbaseInterface
15 *
16 * @group Math
17 *
18 * @license GPL-2.0-or-later
19 */
20 class MathRestbaseInterfaceTest extends MediaWikiIntegrationTestCase {
21 use MathMockHttpTrait;
22
23 public function testSuccess() {
24 $this->setupGoodMathRestBaseMockHttp();
25
26 $input = '\\sin x^2';
27 $rbi = new MathRestbaseInterface( $input );
28 $this->assertTrue( $rbi->getSuccess(), "Assuming that $input is valid input." );
29 $this->assertEquals( '\\sin x^{2}', $rbi->getCheckedTex() );
30 $this->assertStringContainsString( '<mi>sin</mi>', $rbi->getMathML() );
31 $this->assertStringContainsString( '/svg/RESOURCE_LOCATION', $rbi->getFullSvgUrl() );
32 }
33
34 public function testBatchEvaluate() {
35 $body = [
36 'success' => true,
37 'checked' => 'CHECKED',
38 'identifiers' => []
39 ];
40
41 $response = [
42 'code' => 200,
43 'headers' => [],
44 'body' => json_encode( $body )
45 ];
46
47 $responses = [
48 [ // for https://wikimedia.org/api/rest_v1/media/math/check/tex with input1
49 'headers' => [ 'x-resource-location' => 'deadbeef1' ],
50 'body' => json_encode( [ 'checked' => 'CHECKED1' ] + $body )
51 ] + $response,
52
53 [ // for https://wikimedia.org/api/rest_v1/media/math/check/tex with input2
54 'headers' => [ 'x-resource-location' => 'deadbeef2' ],
55 'body' => json_encode( [ 'checked' => 'CHECKED2' ] + $body )
56 ] + $response,
57
58 [ // for https://wikimedia.org/api/rest_v1/media/math/render/mml/deadbeef1
59 'body' => 'MML1'
60 ] + $response,
61
62 [ // for https://wikimedia.org/api/rest_v1/media/math/render/mml/deadbeef2
63 'body' => 'MML2'
64 ] + $response,
65 ];
66
67 $httpClient = $this->createNoOpMock( MultiHttpClient::class, [ 'runMulti' ] );
68 $httpClient->method( 'runMulti' )->willReturnCallback(
69 static function ( array $requests ) use ( &$responses ) {
70 foreach ( $requests as &$req ) {
71 $resp = array_shift( $responses );
72 $req['response'] = $resp;
73 }
74 return $requests;
75 }
76 );
77
78 $this->installMockHttp( $httpClient );
79
80 // NOTE: Using fake response, the input is ignored.
81 $rbi1 = new MathRestbaseInterface( 'input1' );
82 $rbi2 = new MathRestbaseInterface( 'input2' );
83
84 MathRestbaseInterface::batchEvaluate( [ $rbi1, $rbi2 ] );
85
86 $this->assertTrue( $rbi1->getSuccess() );
87 $this->assertEquals( 'CHECKED1', $rbi1->getCheckedTex() );
88 $this->assertEquals( 'MML1', $rbi1->getMathML() );
89
90 $this->assertTrue( $rbi2->getSuccess() );
91 $this->assertEquals( 'CHECKED2', $rbi2->getCheckedTex() );
92 $this->assertEquals( 'MML2', $rbi2->getMathML() );
93 }
94
95 public function testFail() {
96 $this->setupBadMathRestBaseMockHttp();
97
98 $input = '\\sin\\newcommand';
99 $rbi = new MathRestbaseInterface( $input );
100 $this->assertFalse( $rbi->getSuccess(), "Assuming that $input is invalid input." );
101 $this->assertNull( $rbi->getCheckedTex() );
102 $this->assertEquals( 'Illegal TeX function', $rbi->getError()->error->message );
103 }
104
105 public function testChem() {
106 $this->setupGoodChemRestBaseMockHttp();
107
108 $input = '\ce{H2O}';
109 $rbi = new MathRestbaseInterface( $input, 'chem' );
110 $this->assertTrue( $rbi->checkTeX(), "Assuming that $input is valid input." );
111 $this->assertTrue( $rbi->getSuccess(), "Assuming that $input is valid input." );
112 $this->assertEquals( '{\ce {H2O}}', $rbi->getCheckedTex() );
113 $this->assertStringContainsString( '<msubsup>', $rbi->getMathML() );
114 $this->assertStringContainsString( '<mtext>H</mtext>', $rbi->getMathML() );
115 }
116
117 public function testException() {
118 $this->setupBadMathRestBaseMockHttp();
119
120 $input = '\\sin\\newcommand';
121 $rbi = new MathRestbaseInterface( $input );
122 $this->expectException( InvalidTeXException::class );
123 $this->expectExceptionMessage( 'TeX input is invalid.' );
124 $rbi->getMathML();
125 }
126
127 public function testExceptionSvg() {
128 $this->setupBadMathRestBaseMockHttp();
129
130 $input = '\\sin\\newcommand';
131 $rbi = new MathRestbaseInterface( $input );
132 $this->expectException( InvalidTeXException::class );
133 $this->expectExceptionMessage( 'TeX input is invalid.' );
134 $rbi->getFullSvgUrl();
135 }
136
137 /**
138 * Incorporate the "details" in the error message, if the check requests passes, but the
139 * mml/svg/complete endpoints returns an error
140 */
141 public function testLateError() {
142 // phpcs:ignore Generic.Files.LineLength.TooLong
143 $input = '{"type":"https://mediawiki.org/wiki/HyperSwitch/errors/bad_request","title":"Bad Request","method":"POST","detail":["TeX parse error: Missing close brace"],"uri":"/complete"}';
144 $this->expectException( MathRestbaseException::class );
145 $this->expectExceptionMessage( 'Cannot get mml. TeX parse error: Missing close brace' );
146 MathRestbaseInterface::throwContentError( 'mml', $input );
147 }
148
149 /**
150 * Incorporate the "details" in the error message, if the check requests passes, but the
151 * mml/svg/complete endpoints returns an error
152 */
153 public function testLateErrorString() {
154 // phpcs:ignore Generic.Files.LineLength.TooLong
155 $input = '{"type":"https://mediawiki.org/wiki/HyperSwitch/errors/bad_request","title":"Bad Request","method":"POST","detail": "TeX parse error: Missing close brace","uri":"/complete"}';
156 $this->expectException( MathRestbaseException::class );
157 $this->expectExceptionMessage( 'Cannot get mml. TeX parse error: Missing close brace' );
158 MathRestbaseInterface::throwContentError( 'mml', $input );
159 }
160
161 public function testLateErrorNoDetail() {
162 $input = '';
163 $this->expectException( MathRestbaseException::class );
164 $this->expectExceptionMessage( 'Cannot get mml. Server problem.' );
165 MathRestbaseInterface::throwContentError( 'mml', $input );
166 }
167
168 public function testUrlUsedByCheckTeX() {
169 $path = 'media/math/check/tex';
170 $config = [
171 'MathFullRestbaseURL' => 'https://myWiki.test/',
172 'MathInternalRestbaseURL' => 'http://restbase.test.internal/api/myWiki/'
173 ];
174
175 $expected = [
176 'url' => "http://restbase.test.internal/api/myWiki/v1/$path",
177 'method' => 'POST',
178 'body' => [ 'type' => 'tex', 'q' => '\sin\newcommand' ]
179 ];
180
181 $response = [
182 'headers' => [
183 'x-resource-location' => 'deadbeef'
184 ],
185 'body' => json_encode( [
186 'success' => true,
187 'checked' => 'who cares',
188 'identifiers' => [],
189 ] )
190 ];
191
192 $this->expectMathRestBaseMockHttpRequest( [ $expected ], [ $response ] );
193
194 $this->overrideConfigValues( $config );
195
196 $input = '\\sin\\newcommand';
197 $rbi = new MathRestbaseInterface( $input );
198
199 $rbi->checkTeX();
200 }
201
202 public function testUrlUsedByGetML() {
203 $path1 = 'media/math/check/tex';
204 $path2 = 'media/math/render/mml/deadbeef';
205
206 $config = [
207 'MathFullRestbaseURL' => 'https://myWiki.test/',
208 'MathInternalRestbaseURL' => 'http://restbase.test.internal/api/myWiki/'
209 ];
210
211 $expectedList = [
212 [
213 'url' => "http://restbase.test.internal/api/myWiki/v1/$path1",
214 'method' => 'POST'
215 ],
216 [
217 'url' => "http://restbase.test.internal/api/myWiki/v1/$path2",
218 'method' => 'GET'
219 ],
220 ];
221
222 $response1 = [
223 'headers' => [
224 'x-resource-location' => 'deadbeef'
225 ],
226 'body' => json_encode( [
227 'success' => true,
228 'checked' => 'who cares',
229 'identifiers' => [],
230 ] )
231 ];
232 $response2 = [
233 'body' => 'who cares'
234 ];
235
236 $this->expectMathRestBaseMockHttpRequest( $expectedList, [ $response1, $response2 ] );
237
238 $this->overrideConfigValues( $config );
239
240 $input = '\\sin\\newcommand';
241 $rbi = new MathRestbaseInterface( $input );
242
243 $rbi->getMathML();
244 }
245
246 public static function provideTestGetUrl() {
247 $path = 'media/math/render/svg/2uejd9dj3jd';
248 $config = [
249 'MathFullRestbaseURL' => 'https://myWiki.test/',
250 'MathInternalRestbaseURL' => 'http://restbase.test.internal/api/myWiki/'
251 ];
252
253 yield 'External restbase URL case' => [
254 $path,
255 false,
256 $config,
257 'https://myWiki.test/v1/media/math/render/svg/2uejd9dj3jd'
258 ];
259
260 yield 'Internal restbase URL case' => [
261 $path,
262 true,
263 $config,
264 'http://restbase.test.internal/api/myWiki/v1/media/math/render/svg/2uejd9dj3jd'
265 ];
266 }
267
268 /**
269 * @dataProvider provideTestGetUrl
270 * @param string $path
271 * @param bool $internal
272 * @param array $config
273 * @param string $expected
274 */
275 public function testGetUrl( $path, $internal, $config, $expected ) {
276 $this->overrideConfigValues( $config );
277 $input = '\\sin\\newcommand';
278 $rbi = new MathRestbaseInterface( $input );
279 $actual = $rbi->getUrl( $path, $internal );
280 $this->assertSame( $expected, $actual );
281 }
282
283 public function testGetType() {
284 $tex = '\testing tex';
285 $type = 'test';
286 $interface = new MathRestbaseInterface( $tex, $type );
287
288 $this->assertEquals( $type, $interface->getType() );
289 }
290
291 public function testGetTex() {
292 $tex = '\tesing tex';
293 $interface = new MathRestbaseInterface( $tex );
294
295 $this->assertEquals( $tex, $interface->getTex() );
296 }
297
298 public function testEvaluateRestbaseCheckResponse() {
299 $response = [
300 'code' => 200,
301 'headers' => [
302 'x-resource-location' => 'deadbeef'
303 ],
304 'body' => json_encode( [
305 'success' => true,
306 'checked' => 'who cares',
307 'identifiers' => [ 'identifier1', 'identifier2' ],
308 'warnings' => [ 'Warning 1', 'Warning 2' ]
309 ] )
310 ];
311
312 $rbi = new MathRestbaseInterface();
313 $result = $rbi->evaluateRestbaseCheckResponse( $response );
314
315 $this->assertTrue( $result );
316 $this->assertTrue( $rbi->getSuccess() );
317 $this->assertSame( 'who cares', $rbi->getCheckedTex() );
318 $this->assertSame( [ 'identifier1', 'identifier2' ], $rbi->getIdentifiers() );
319 $this->assertSame( [ 'Warning 1', 'Warning 2' ], $rbi->getWarnings() );
320 $this->assertNull( $rbi->getError() );
321 }
322
323 public function testEvaluateRestbaseCheckResponseWithErrorMessage() {
324 $errorResponse = [
325 'code' => 400,
326 'body' => json_encode( [
327 'detail' => [
328 'success' => false,
329 'message' => 'Invalid input'
330 ]
331 ] )
332 ];
333
334 $rbi = new MathRestbaseInterface();
335 $result = $rbi->evaluateRestbaseCheckResponse( $errorResponse );
336
337 $this->assertFalse( $result );
338 $this->assertFalse( $rbi->getSuccess() );
339 $this->assertNull( $rbi->getCheckedTex() );
340 $this->assertNull( $rbi->getIdentifiers() );
341 $this->assertSame( [], $rbi->getWarnings() );
342 $this->assertNotNull( $rbi->getError() );
343 }
344
345 public function testEvaluateRestbaseCheckResponseWithFailure() {
346 $errorResponseWithoutDetail = [
347 'code' => 400,
348 'body' => json_encode( [
349 'message' => 'Invalid input'
350 ] )
351 ];
352
353 $rbi = new MathRestbaseInterface();
354 $result = $rbi->evaluateRestbaseCheckResponse( $errorResponseWithoutDetail );
355
356 $this->assertFalse( $result );
357 $this->assertFalse( $rbi->getSuccess() );
358 $this->assertNull( $rbi->getCheckedTex() );
359 $this->assertNull( $rbi->getIdentifiers() );
360 $this->assertSame( [], $rbi->getWarnings() );
361 $this->assertNotNull( $rbi->getError() );
362 $this->assertSame( 'Math extension cannot connect to Restbase.', $rbi->getError()->error->message );
363 }
364 }