Debian Codegraph @

extensions/Math/tests/phpunit/InputCheck/MathoidCheckerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Math\InputCheck;
4
5 use MediaWiki\Extension\Math\Math;
6 use MediaWiki\Extension\Math\MathMathML;
7 use MediaWiki\Http\HttpRequestFactory;
8 use MediaWikiIntegrationTestCase;
9 use MockHttpTrait;
10 use RuntimeException;
11 use Wikimedia\ObjectCache\EmptyBagOStuff;
12 use Wikimedia\ObjectCache\WANObjectCache;
13
14 /**
15 * @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker
16 */
17 class MathoidCheckerTest extends MediaWikiIntegrationTestCase {
18 use MockHttpTrait;
19
20 private const SAMPLE_KEY = 'global:MediaWiki\Extension\Math\InputCheck\MathoidChecker:' .
21 'eb27aefff6e58e58dcefa22102531a58';
22
23 public static function provideTexExamples() {
24 return [
25 [ '\sin x', 'eb27aefff6e58e58dcefa22102531a58' ],
26 [ '\sin_x', '7b33c69d6eac9126d41b663b93896951' ],
27 ];
28 }
29
30 /**
31 * @dataProvider provideTexExamples
32 */
33 public function testCacheKey( string $input, string $expected ) {
34 $checker = $this->getMathoidChecker( $input );
35 $realKey = $checker->getCacheKey();
36 $this->assertStringEndsWith( $expected, $realKey );
37 }
38
39 public function testResponseFromCache() {
40 $wanCache = $this->getServiceContainer()->getMainWANObjectCache();
41 $wanCache->set( self::SAMPLE_KEY,
42 [ 999, 'expected' ],
43 WANObjectCache::TTL_INDEFINITE,
44 [ 'version' => MathoidChecker::VERSION ]
45 );
46
47 $checker = $this->getMathoidChecker();
48 $this->assertSame( [ 999, 'expected' ], $checker->getCheckResponse() );
49 }
50
51 public function testResponseWithPurge() {
52 $wanCache = $this->getServiceContainer()->getMainWANObjectCache();
53 $wanCache->set( self::SAMPLE_KEY,
54 [ 999, 'unexpected' ],
55 WANObjectCache::TTL_INDEFINITE,
56 [ 'version' => MathoidChecker::VERSION ]
57 );
58
59 $this->setFakeRequest( 200, 'expected' );
60 $checker = $this->getMathoidChecker( '\sin x', true );
61 $this->assertSame( [ 200, 'expected' ], $checker->getCheckResponse() );
62 }
63
64 public function testResponseFromResponse() {
65 $this->setMainCache( new EmptyBagOStuff() );
66
67 $this->setFakeRequest( 200, 'expected' );
68 $checker = $this->getMathoidChecker();
69 $this->assertSame( [ 200, 'expected' ], $checker->getCheckResponse() );
70 }
71
72 public function testFailedResponse() {
73 $this->setMainCache( new EmptyBagOStuff() );
74
75 $this->setFakeRequest( 401, false );
76 $checker = $this->getMathoidChecker();
77 $this->expectException( RuntimeException::class );
78 $checker->getCheckResponse();
79 }
80
81 /**
82 * @dataProvider provideMathoidSamples
83 * @param string $input LaTeX input to check
84 * @param string $mockRequestBody
85 * @param int $mockResponseStatus
86 * @param array $expeted
87 */
88 public function testIsValid( $input, $mockRequestBody, $mockResponseStatus, $expeted ) {
89 $request = $this->makeFakeHttpRequest( $mockRequestBody, $mockResponseStatus );
90 $this->installMockHttp( $request );
91 $checker = $this->getMathoidChecker( $input );
92 $this->assertSame( $expeted['valid'], $checker->isValid() );
93 }
94
95 /**
96 * @dataProvider provideMathoidSamples
97 * @param string $input LaTeX input to check
98 * @param string $mockRequestBody
99 * @param int $mockResponseStatus
100 * @param array $expeted
101 */
102 public function testGetChecked( $input, $mockRequestBody, $mockResponseStatus, $expeted ) {
103 $request = $this->makeFakeHttpRequest( $mockRequestBody, $mockResponseStatus );
104 $this->installMockHttp( $request );
105 $checker = $this->getMathoidChecker( $input );
106 $this->assertSame( $expeted['checked'], $checker->getValidTex() );
107 }
108
109 /**
110 * @dataProvider provideMathoidSamples
111 * @param string $input LaTeX input to check
112 * @param string $mockRequestBody
113 * @param int $mockResponseStatus
114 * @param array $expeted
115 */
116 public function testGetError( $input, $mockRequestBody, $mockResponseStatus, $expeted ) {
117 $request = $this->makeFakeHttpRequest( $mockRequestBody, $mockResponseStatus );
118 $this->installMockHttp( $request );
119 $checker = $this->getMathoidChecker( $input );
120 if ( array_key_exists( 'error', $expeted ) ) {
121 $checkerError = $checker->getError();
122 $this->assertNotNull( $checkerError );
123 $renderedCheckerError = ( new MathMathML( 'a' ) )
124 ->getError( $checkerError->getKey(), ...$checkerError->getParams() );
125 $this->assertStringContainsString( $expeted['error'], $renderedCheckerError );
126 } else {
127 $this->assertNull( $checker->getError() );
128 }
129 }
130
131 /**
132 * @param string $tex
133 * @param bool $purge
134 * @return MathoidChecker
135 */
136 private function getMathoidChecker( string $tex = '\sin x', bool $purge = false ): MathoidChecker {
137 return Math::getCheckerFactory()
138 ->newMathoidChecker( $tex, 'tex', $purge );
139 }
140
141 private function setFakeRequest( $returnStatus, $content ): void {
142 $fakeHTTP = $this->createMock( HttpRequestFactory::class );
143 $fakeRequest = $this->createMock( \MWHttpRequest::class );
144 $fakeRequest->expects( $this->once() )->method( 'execute' )->willReturn( true );
145 $fakeRequest->expects( $this->once() )->method( 'getStatus' )->willReturn( $returnStatus );
146 if ( $content ) {
147 $fakeRequest->expects( $this->once() )->method( 'getContent' )->willReturn( $content );
148 } else {
149 $fakeRequest->expects( $this->never() )->method( 'getContent' );
150 }
151 $fakeHTTP->expects( $this->once() )->method( 'create' )->willReturn( $fakeRequest );
152 $this->setService( 'HttpRequestFactory', $fakeHTTP );
153 }
154
155 public static function provideMathoidSamples() {
156 yield '\ sin x' => [
157 '\sin x',
158 file_get_contents( __DIR__ . '/data/mathoid/sinx.json' ), 200,
159 [ 'valid' => true, 'checked' => '\sin x' ],
160 ];
161 yield 'invalid F' => [
162 '1+\invalid',
163 file_get_contents( __DIR__ . '/data/mathoid/invalidF.json' ), 400,
164 [ 'valid' => false, 'checked' => null, 'error' => 'unknown function' ],
165 ];
166 yield 'unescaped' => [
167 '1.5%',
168 file_get_contents( __DIR__ . '/data/mathoid/deprecated.json' ), 200,
169 [ 'valid' => true, 'checked' => '1.5\%' ],
170 ];
171 yield 'syntax error' => [
172 '\left( x',
173 file_get_contents( __DIR__ . '/data/mathoid/syntaxE.json' ), 400,
174 [ 'valid' => false, 'checked' => null, 'error' => 'Failed to parse' ],
175 ];
176 }
177
178 }