2015-03-12 18:50:11 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
use MediaWiki\Extension\Math\MathMathML;
|
2021-10-01 01:16:45 +00:00
|
|
|
use MediaWiki\Extension\Math\MathRestbaseInterface;
|
2021-08-11 13:38:51 +00:00
|
|
|
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
2021-08-05 01:08:52 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2017-08-16 05:50:44 +00:00
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
/**
|
|
|
|
* Test the MathML output format.
|
|
|
|
*
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
2014-06-03 08:15:53 +00:00
|
|
|
* @group Math
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
2018-04-13 14:06:39 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
2021-10-11 22:51:10 +00:00
|
|
|
class MathMathMLTest extends MediaWikiIntegrationTestCase {
|
2021-08-11 13:38:51 +00:00
|
|
|
use MathMockHttpTrait;
|
2014-06-03 08:15:53 +00:00
|
|
|
|
2021-07-22 11:56:28 +00:00
|
|
|
protected function setUp(): void {
|
2018-05-20 22:47:32 +00:00
|
|
|
parent::setUp();
|
2017-08-16 05:50:44 +00:00
|
|
|
$this->setMwGlobals( 'wgMathoidCli', false );
|
|
|
|
}
|
|
|
|
|
2019-11-26 09:59:54 +00:00
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::__construct
|
2019-11-26 09:59:54 +00:00
|
|
|
*/
|
2018-05-20 22:47:32 +00:00
|
|
|
public function testMathMLConstructorWithPmml() {
|
|
|
|
$mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'pmml' ] );
|
2019-11-26 10:21:13 +00:00
|
|
|
$this->assertSame( 'pmml', $mml->getInputType() );
|
|
|
|
$this->assertSame( '<math><mo>sin</mo></math>', $mml->getMathml() );
|
2018-05-20 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 09:59:54 +00:00
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::__construct
|
2019-11-26 09:59:54 +00:00
|
|
|
*/
|
2018-05-20 22:47:32 +00:00
|
|
|
public function testMathMLConstructorWithInvalidType() {
|
|
|
|
$mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'invalid' ] );
|
2019-11-26 10:21:13 +00:00
|
|
|
$this->assertSame( 'tex', $mml->getInputType() );
|
2018-05-20 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 09:59:54 +00:00
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::__construct
|
2019-11-26 09:59:54 +00:00
|
|
|
*/
|
2018-05-20 22:47:32 +00:00
|
|
|
public function testChangeRootElemts() {
|
|
|
|
$mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'invalid' ] );
|
2023-08-19 02:51:13 +00:00
|
|
|
$mml->setAllowedRootElements( [ 'a', 'b' ] );
|
|
|
|
$this->assertSame( [ 'a', 'b' ], $mml->getAllowedRootElements() );
|
2018-05-20 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Testcase: Invalid request.
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::makeRequest
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestInvalid() {
|
2021-08-03 20:59:02 +00:00
|
|
|
$url = 'http://example.com/invalid';
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgMathMathMLUrl' => $url,
|
|
|
|
] );
|
2021-08-02 20:46:44 +00:00
|
|
|
$this->installMockHttp(
|
|
|
|
$this->makeFakeHttpRequest( 'Method Not Allowed', 405 )
|
|
|
|
);
|
2014-06-03 08:15:53 +00:00
|
|
|
|
2019-11-26 10:21:13 +00:00
|
|
|
$renderer = new MathMathML();
|
2021-08-05 01:08:52 +00:00
|
|
|
$requestReturn = $renderer->makeRequest();
|
|
|
|
$this->assertFalse( $requestReturn->isGood(),
|
2022-12-11 14:04:59 +00:00
|
|
|
"requestReturn is false if MediaWiki\\Http\\HttpRequestFactory::post returns false." );
|
2021-08-05 01:08:52 +00:00
|
|
|
$this->assertNull( $requestReturn->getValue(),
|
2022-12-11 14:04:59 +00:00
|
|
|
"result value is null if MediaWiki\\Http\\HttpRequestFactory::post returns false." );
|
2021-08-05 01:08:52 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
$requestReturn->hasMessage(
|
|
|
|
wfMessage( 'math_invalidresponse', '', $url, 'Method Not Allowed' )
|
|
|
|
),
|
2022-12-11 14:04:59 +00:00
|
|
|
"return an error if MediaWiki\\Http\\HttpRequestFactory::post returns false"
|
2021-08-05 01:08:52 +00:00
|
|
|
);
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Testcase: Valid request.
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::makeRequest
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestSuccess() {
|
2021-08-02 20:46:44 +00:00
|
|
|
$this->installMockHttp(
|
|
|
|
$this->makeFakeHttpRequest( 'test content' )
|
|
|
|
);
|
2019-11-26 10:21:13 +00:00
|
|
|
$renderer = new MathMathML();
|
2018-06-06 10:13:04 +00:00
|
|
|
|
2021-08-05 01:08:52 +00:00
|
|
|
$requestReturn = $renderer->makeRequest();
|
|
|
|
$this->assertTrue( $requestReturn->isGood(), "successful call return" );
|
|
|
|
$this->assertSame( 'test content', $requestReturn->getValue(), 'successful call' );
|
|
|
|
$this->assertArrayEquals( [], $requestReturn->getErrors(), "successful call error-message" );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Testcase: Timeout.
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::makeRequest
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestTimeout() {
|
2021-08-03 20:59:02 +00:00
|
|
|
$url = 'http://example.com/timeout';
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgMathMathMLUrl' => $url,
|
|
|
|
] );
|
2021-08-02 20:46:44 +00:00
|
|
|
$this->installMockHttp(
|
|
|
|
$this->makeFakeTimeoutRequest()
|
|
|
|
);
|
2019-11-26 10:21:13 +00:00
|
|
|
$renderer = new MathMathML();
|
2018-06-06 10:13:04 +00:00
|
|
|
|
2021-08-05 01:08:52 +00:00
|
|
|
$requestReturn = $renderer->makeRequest();
|
|
|
|
$this->assertFalse( $requestReturn->isGood(), "timeout call return" );
|
|
|
|
$this->assertNull( $requestReturn->getValue(), "timeout call return" );
|
|
|
|
$this->assertTrue(
|
|
|
|
$requestReturn->hasMessage( wfMessage( 'math_timeout', '', $url ) ),
|
|
|
|
"timeout call errormessage"
|
|
|
|
);
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:47:32 +00:00
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Test case: Get PostData.
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::makeRequest
|
2018-05-20 22:47:32 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestGetPostData() {
|
2021-08-02 20:46:44 +00:00
|
|
|
$this->installMockHttp( $this->makeFakeHttpRequest() );
|
2018-06-06 10:13:04 +00:00
|
|
|
$renderer = $this->getMockBuilder( MathMathML::class )
|
2021-05-04 08:06:42 +00:00
|
|
|
->onlyMethods( [ 'getPostData' ] )
|
2018-05-20 22:47:32 +00:00
|
|
|
->getMock();
|
|
|
|
$renderer->expects( $this->once() )->method( 'getPostData' );
|
2018-06-06 10:13:04 +00:00
|
|
|
|
|
|
|
/** @var MathMathML $renderer */
|
2021-08-05 01:08:52 +00:00
|
|
|
$renderer->makeRequest();
|
2018-05-20 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
/**
|
|
|
|
* Checks if a String is a valid MathML element
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::isValidMathML
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
2018-02-05 15:24:14 +00:00
|
|
|
public function testisValidMathML() {
|
2019-11-26 10:21:13 +00:00
|
|
|
$renderer = new MathMathML();
|
2014-06-03 08:15:53 +00:00
|
|
|
$validSample = '<math>content</math>';
|
|
|
|
$invalidSample = '<notmath />';
|
2015-09-21 16:14:01 +00:00
|
|
|
$this->assertTrue( $renderer->isValidMathML( $validSample ),
|
|
|
|
'test if math expression is valid mathml sample' );
|
|
|
|
$this->assertFalse( $renderer->isValidMathML( $invalidSample ),
|
|
|
|
'test if math expression is invalid mathml sample' );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:47:32 +00:00
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::isValidMathML
|
2018-05-20 22:47:32 +00:00
|
|
|
*/
|
|
|
|
public function testInvalidXml() {
|
2019-11-26 10:21:13 +00:00
|
|
|
$renderer = new MathMathML();
|
2018-05-20 22:47:32 +00:00
|
|
|
$invalidSample = '<mat';
|
|
|
|
$this->assertFalse( $renderer->isValidMathML( $invalidSample ),
|
|
|
|
'test if math expression is invalid mathml sample' );
|
|
|
|
$renderer->setXMLValidation( false );
|
|
|
|
$this->assertTrue( $renderer->isValidMathML( $invalidSample ),
|
|
|
|
'test if math expression is invalid mathml sample' );
|
|
|
|
}
|
|
|
|
|
2016-04-13 12:30:48 +00:00
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMathML::correctSvgStyle
|
2016-04-13 12:30:48 +00:00
|
|
|
* @see https://phabricator.wikimedia.org/T132563
|
|
|
|
*/
|
|
|
|
public function testMathMLStyle() {
|
|
|
|
$m = new MathMathML();
|
|
|
|
$m->setSvg( 'style="vertical-align:-.505ex" height="2.843ex" width="28.527ex"' );
|
|
|
|
$style = '';
|
|
|
|
$m->correctSvgStyle( $style );
|
2019-11-26 10:21:13 +00:00
|
|
|
$this->assertSame( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style );
|
2016-04-13 12:30:48 +00:00
|
|
|
$m->setSvg( 'style=" vertical-align:-.505ex; \n" height="2.843ex" width="28.527ex"' );
|
2019-11-26 10:21:13 +00:00
|
|
|
$this->assertSame( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style );
|
2016-04-13 12:30:48 +00:00
|
|
|
}
|
2018-05-20 22:47:32 +00:00
|
|
|
|
2018-06-25 16:08:42 +00:00
|
|
|
public function testWarning() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupGoodMathRestBaseMockHttp();
|
2018-06-25 16:08:42 +00:00
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'always' );
|
2021-08-11 13:38:51 +00:00
|
|
|
|
2018-06-25 16:08:42 +00:00
|
|
|
$renderer = new MathMathML();
|
|
|
|
$rbi = $this->getMockBuilder( MathRestbaseInterface::class )
|
2021-05-04 08:06:42 +00:00
|
|
|
->onlyMethods( [ 'getWarnings', 'getSuccess' ] )
|
2021-08-11 13:38:51 +00:00
|
|
|
->setConstructorArgs( [ '\sin x' ] )
|
2018-06-25 16:08:42 +00:00
|
|
|
->getMock();
|
|
|
|
$rbi->method( 'getWarnings' )->willReturn( [ (object)[ 'type' => 'mhchem-deprecation' ] ] );
|
|
|
|
$rbi->method( 'getSuccess' )->willReturn( true );
|
|
|
|
$renderer->setRestbaseInterface( $rbi );
|
|
|
|
$renderer->render();
|
2019-11-26 11:01:30 +00:00
|
|
|
$parser = $this->createMock( Parser::class );
|
2018-06-25 16:08:42 +00:00
|
|
|
$parser->method( 'addTrackingCategory' )->willReturn( true );
|
|
|
|
$parser->expects( $this->once() )
|
|
|
|
->method( 'addTrackingCategory' )
|
|
|
|
->with( 'math-tracking-category-mhchem-deprecation' );
|
|
|
|
$renderer->addTrackingCategories( $parser );
|
|
|
|
}
|
2018-11-05 17:29:56 +00:00
|
|
|
|
|
|
|
public function testGetHtmlOutputQID() {
|
|
|
|
$math = new MathMathML( "a+b", [ "qid" => "Q123" ] );
|
|
|
|
$out = $math->getHtmlOutput();
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( "data-qid=\"Q123\"", $out );
|
2018-11-05 17:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetHtmlOutputInvalidQID() {
|
|
|
|
// test with not valid ID. An ID must match /Q\d+/
|
|
|
|
$math = new MathMathML( "a+b", [ "qid" => "123" ] );
|
|
|
|
$out = $math->getHtmlOutput();
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringNotContainsString( "data-qid", $out );
|
2018-11-05 17:29:56 +00:00
|
|
|
}
|
2021-08-05 01:08:52 +00:00
|
|
|
|
|
|
|
public function testEmpty() {
|
|
|
|
// TODO: Once render returns status, we won't need TestingAccessWrapper anymore.
|
|
|
|
$math = TestingAccessWrapper::newFromObject( new MathMathML( '' ) );
|
|
|
|
$renderStatus = $math->doRender();
|
|
|
|
$this->assertFalse( $renderStatus->isGood() );
|
|
|
|
$this->assertTrue( $renderStatus->hasMessage( 'math_empty_tex' ) );
|
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|