Debian Codegraph @

extensions/Math/tests/phpunit/MathMathMLTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Extension\Math\MathMathML;
4 use MediaWiki\Extension\Math\MathRestbaseInterface;
5 use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
6 use MediaWiki\Parser\Parser;
7 use Wikimedia\TestingAccessWrapper;
8
9 /**
10 * Test the MathML output format.
11 *
12 * @covers \MediaWiki\Extension\Math\MathMathML
13 *
14 * @group Math
15 *
16 * @license GPL-2.0-or-later
17 */
18 class MathMathMLTest extends MediaWikiIntegrationTestCase {
19 use MathMockHttpTrait;
20
21 protected function setUp(): void {
22 parent::setUp();
23 $this->overrideConfigValue( 'MathoidCli', false );
24 }
25
26 /**
27 * @covers \MediaWiki\Extension\Math\MathMathML::__construct
28 */
29 public function testMathMLConstructorWithPmml() {
30 $mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'pmml' ] );
31 $this->assertSame( 'pmml', $mml->getInputType() );
32 $this->assertSame( '<math><mo>sin</mo></math>', $mml->getMathml() );
33 }
34
35 /**
36 * @covers \MediaWiki\Extension\Math\MathMathML::__construct
37 */
38 public function testMathMLConstructorWithInvalidType() {
39 $mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'invalid' ] );
40 $this->assertSame( 'tex', $mml->getInputType() );
41 }
42
43 /**
44 * @covers \MediaWiki\Extension\Math\MathMathML::__construct
45 */
46 public function testChangeRootElemts() {
47 $mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'invalid' ] );
48 $mml->setAllowedRootElements( [ 'a', 'b' ] );
49 $this->assertSame( [ 'a', 'b' ], $mml->getAllowedRootElements() );
50 }
51
52 /**
53 * Tests behavior of makeRequest() that communicates with the host.
54 * Testcase: Invalid request.
55 * @covers \MediaWiki\Extension\Math\MathMathML::makeRequest
56 */
57 public function testMakeRequestInvalid() {
58 $url = 'http://example.com/invalid';
59 $this->overrideConfigValue( 'MathMathMLUrl', $url );
60 $this->installMockHttp(
61 $this->makeFakeHttpRequest( 'Method Not Allowed', 405 )
62 );
63
64 $renderer = new MathMathML();
65 $requestReturn = $renderer->makeRequest();
66 $this->assertNull( $requestReturn->getValue(),
67 "result value is null if MediaWiki\\Http\\HttpRequestFactory::post returns false." );
68 $this->assertStatusError( 'math_invalidresponse', $requestReturn,
69 "return an error if MediaWiki\\Http\\HttpRequestFactory::post returns false"
70 );
71 }
72
73 /**
74 * Tests behavior of makeRequest() that communicates with the host.
75 * Testcase: Valid request.
76 * @covers \MediaWiki\Extension\Math\MathMathML::makeRequest
77 */
78 public function testMakeRequestSuccess() {
79 $this->installMockHttp(
80 $this->makeFakeHttpRequest( 'test content' )
81 );
82 $renderer = new MathMathML();
83
84 $requestReturn = $renderer->makeRequest();
85 $this->assertStatusGood( $requestReturn, 'successful call return' );
86 $this->assertSame( 'test content', $requestReturn->getValue(), 'successful call' );
87 }
88
89 /**
90 * Tests behavior of makeRequest() that communicates with the host.
91 * Testcase: Timeout.
92 * @covers \MediaWiki\Extension\Math\MathMathML::makeRequest
93 */
94 public function testMakeRequestTimeout() {
95 $url = 'http://example.com/timeout';
96 $this->overrideConfigValue( 'MathMathMLUrl', $url );
97 $this->installMockHttp(
98 $this->makeFakeTimeoutRequest()
99 );
100 $renderer = new MathMathML();
101
102 $requestReturn = $renderer->makeRequest();
103 $this->assertNull( $requestReturn->getValue(), "timeout call return" );
104 $this->assertStatusError( 'math_timeout', $requestReturn,
105 "timeout call errormessage"
106 );
107 }
108
109 /**
110 * Tests behavior of makeRequest() that communicates with the host.
111 * Test case: Get PostData.
112 * @covers \MediaWiki\Extension\Math\MathMathML::makeRequest
113 */
114 public function testMakeRequestGetPostData() {
115 $this->installMockHttp( $this->makeFakeHttpRequest() );
116 $renderer = $this->getMockBuilder( MathMathML::class )
117 ->onlyMethods( [ 'getPostData' ] )
118 ->getMock();
119 $renderer->expects( $this->once() )->method( 'getPostData' );
120
121 /** @var MathMathML $renderer */
122 $renderer->makeRequest();
123 }
124
125 /**
126 * Checks if a String is a valid MathML element
127 * @covers \MediaWiki\Extension\Math\MathMathML::isValidMathML
128 */
129 public function testisValidMathML() {
130 $renderer = new MathMathML();
131 $validSample = '<math>content</math>';
132 $invalidSample = '<notmath />';
133 $this->assertTrue( $renderer->isValidMathML( $validSample ),
134 'test if math expression is valid mathml sample' );
135 $this->assertFalse( $renderer->isValidMathML( $invalidSample ),
136 'test if math expression is invalid mathml sample' );
137 }
138
139 /**
140 * @covers \MediaWiki\Extension\Math\MathMathML::isValidMathML
141 */
142 public function testInvalidXml() {
143 $renderer = new MathMathML();
144 $invalidSample = '<mat';
145 $this->assertFalse( $renderer->isValidMathML( $invalidSample ),
146 'test if math expression is invalid mathml sample' );
147 $renderer->setXMLValidation( false );
148 $this->assertTrue( $renderer->isValidMathML( $invalidSample ),
149 'test if math expression is invalid mathml sample' );
150 }
151
152 /**
153 * @covers \MediaWiki\Extension\Math\MathMathML::correctSvgStyle
154 * @see https://phabricator.wikimedia.org/T132563
155 */
156 public function testMathMLStyle() {
157 $m = new MathMathML();
158 $m->setSvg( 'style="vertical-align:-.505ex" height="2.843ex" width="28.527ex"' );
159 $style = '';
160 $m->correctSvgStyle( $style );
161 $this->assertSame( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style );
162 $m->setSvg( 'style=" vertical-align:-.505ex; \n" height="2.843ex" width="28.527ex"' );
163 $this->assertSame( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style );
164 }
165
166 public function testWarning() {
167 $this->setupGoodMathRestBaseMockHttp();
168 $this->overrideConfigValue( 'MathDisableTexFilter', 'always' );
169
170 $renderer = new MathMathML();
171 $rbi = $this->getMockBuilder( MathRestbaseInterface::class )
172 ->onlyMethods( [ 'getWarnings', 'getSuccess' ] )
173 ->setConstructorArgs( [ '\sin x' ] )
174 ->getMock();
175 $rbi->method( 'getWarnings' )->willReturn( [ (object)[ 'type' => 'mhchem-deprecation' ] ] );
176 $rbi->method( 'getSuccess' )->willReturn( true );
177 $renderer->setRestbaseInterface( $rbi );
178 $renderer->render();
179 $parser = $this->createMock( Parser::class );
180 $parser->method( 'addTrackingCategory' )->willReturn( true );
181 $parser->expects( $this->once() )
182 ->method( 'addTrackingCategory' )
183 ->with( 'math-tracking-category-mhchem-deprecation' );
184 $renderer->addTrackingCategories( $parser );
185 }
186
187 public function testGetHtmlOutputQID() {
188 $math = new MathMathML( "a+b", [ "qid" => "Q123" ] );
189 $out = $math->getHtmlOutput();
190 $this->assertStringContainsString( "data-qid=\"Q123\"", $out );
191 }
192
193 public function testGetHtmlOutputInvalidQID() {
194 // test with not valid ID. An ID must match /Q\d+/
195 $math = new MathMathML( "a+b", [ "qid" => "123" ] );
196 $out = $math->getHtmlOutput();
197 $this->assertStringNotContainsString( "data-qid", $out );
198 }
199
200 public function testGetHtmlOutputNoSvg() {
201 $math = new MathMathML( "a+b" );
202 $out = $math->getHtmlOutput( false );
203 $this->assertStringNotContainsString( "<svg", $out );
204 $this->assertStringNotContainsString( "mwe-math-mathml-a11y", $out );
205 $this->assertStringContainsString( "mwe-math-mathml-", $out );
206 }
207
208 public function testEmpty() {
209 // TODO: Once render returns status, we won't need TestingAccessWrapper anymore.
210 $math = TestingAccessWrapper::newFromObject( new MathMathML( '' ) );
211 $renderStatus = $math->doRender();
212 $this->assertStatusError( 'math_empty_tex', $renderStatus );
213 }
214 }