2015-03-12 18:50:11 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2020-02-17 15:29:08 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-11-26 10:21:13 +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.
|
|
|
|
*
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \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
|
|
|
*/
|
|
|
|
class MathMathMLTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
// State-variables for HTTP Mockup classes
|
|
|
|
public static $content = null;
|
|
|
|
public static $good = false;
|
|
|
|
public static $html = false;
|
|
|
|
public static $timeout = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the mock values for the HTTP Mockup classes
|
|
|
|
*
|
2017-08-09 20:56:07 +00:00
|
|
|
* @param bool $good
|
2014-06-03 08:15:53 +00:00
|
|
|
* @param mixed $html HTML of the error message or false if no error is present.
|
2017-08-09 20:56:07 +00:00
|
|
|
* @param bool $timeout true if
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
|
|
|
public static function setMockValues( $good, $html, $timeout ) {
|
|
|
|
self::$good = $good;
|
|
|
|
self::$html = $html;
|
|
|
|
self::$timeout = $timeout;
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:45:11 +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
|
|
|
/**
|
|
|
|
* @covers \MathMathML::__construct
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* @covers \MathMathML::__construct
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* @covers \MathMathML::__construct
|
|
|
|
*/
|
2018-05-20 22:47:32 +00:00
|
|
|
public function testChangeRootElemts() {
|
|
|
|
$mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'invalid' ] );
|
|
|
|
$mml->setAllowedRootElements( [ 'a','b' ] );
|
2019-11-26 10:21:13 +00:00
|
|
|
$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.
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathMathML::makeRequest
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestInvalid() {
|
|
|
|
self::setMockValues( false, false, false );
|
|
|
|
$url = 'http://example.com/invalid';
|
|
|
|
|
2019-11-26 10:21:13 +00:00
|
|
|
$renderer = new MathMathML();
|
2018-06-06 10:13:04 +00:00
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error,
|
2018-06-06 10:13:04 +00:00
|
|
|
MathMLHttpRequestTester::class );
|
2019-11-26 10:21:13 +00:00
|
|
|
$this->assertFalse( $requestReturn,
|
2015-09-21 16:14:01 +00:00
|
|
|
"requestReturn is false if HTTP::post returns false." );
|
2019-11-26 10:21:13 +00:00
|
|
|
$this->assertNull( $res,
|
|
|
|
"res is null if HTTP::post returns false." );
|
2014-06-03 08:15:53 +00:00
|
|
|
$errmsg = wfMessage( 'math_invalidresponse', '', $url, '' )->inContentLanguage()->escaped();
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( $errmsg, $error,
|
2015-09-21 16:14:01 +00:00
|
|
|
"return an error if HTTP::post returns false" );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Testcase: Valid request.
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathMathML::makeRequest
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestSuccess() {
|
|
|
|
self::setMockValues( true, true, false );
|
2017-08-16 05:50:44 +00:00
|
|
|
self::$content = 'test content';
|
2014-06-03 08:15:53 +00:00
|
|
|
$url = 'http://example.com/valid';
|
2019-11-26 10:21:13 +00:00
|
|
|
$renderer = new MathMathML();
|
2018-06-06 10:13:04 +00:00
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error,
|
2018-06-06 10:13:04 +00:00
|
|
|
MathMLHttpRequestTester::class );
|
2019-11-26 10:21:13 +00:00
|
|
|
$this->assertTrue( $requestReturn, "successful call return" );
|
|
|
|
$this->assertSame( 'test content', $res, 'successful call' );
|
2021-01-29 17:01:16 +00:00
|
|
|
$this->assertSame( '', $error, "successful call error-message" );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Testcase: Timeout.
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathMathML::makeRequest
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestTimeout() {
|
|
|
|
self::setMockValues( false, true, true );
|
|
|
|
$url = 'http://example.com/timeout';
|
2019-11-26 10:21:13 +00:00
|
|
|
$renderer = new MathMathML();
|
2018-06-06 10:13:04 +00:00
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
$requestReturn = $renderer->makeRequest(
|
2018-06-06 10:13:04 +00:00
|
|
|
$url, '$\longcommand$', $res, $error, MathMLHttpRequestTester::class
|
2015-09-21 16:14:01 +00:00
|
|
|
);
|
2019-11-26 10:21:13 +00:00
|
|
|
$this->assertFalse( $requestReturn, "timeout call return" );
|
|
|
|
$this->assertFalse( $res, "timeout call return" );
|
2014-06-03 08:15:53 +00:00
|
|
|
$errmsg = wfMessage( 'math_timeout', '', $url )->inContentLanguage()->escaped();
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( $errmsg, $error, "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.
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathMathML::makeRequest
|
2018-05-20 22:47:32 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestGetPostData() {
|
|
|
|
self::setMockValues( false, true, true );
|
|
|
|
$url = 'http://example.com/timeout';
|
2018-06-06 10:13:04 +00:00
|
|
|
$renderer = $this->getMockBuilder( MathMathML::class )
|
2018-05-20 22:47:32 +00:00
|
|
|
->setMethods( [ 'getPostData' ] )
|
|
|
|
->getMock();
|
|
|
|
$renderer->expects( $this->once() )->method( 'getPostData' );
|
2018-06-06 10:13:04 +00:00
|
|
|
|
|
|
|
/** @var MathMathML $renderer */
|
|
|
|
$renderer->makeRequest( $url, false, $res, $error, MathMLHttpRequestTester::class );
|
2018-05-20 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Test case: Get host.
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathMathML::pickHost
|
2018-05-20 22:47:32 +00:00
|
|
|
*/
|
|
|
|
public function testMakeRequestGetHost() {
|
|
|
|
self::setMockValues( false, true, true );
|
2018-06-06 10:13:04 +00:00
|
|
|
$renderer = $this->getMockBuilder( MathMathML::class )
|
2018-05-20 22:47:32 +00:00
|
|
|
->setMethods( [ 'getPostData', 'pickHost' ] )
|
|
|
|
->getMock();
|
|
|
|
$renderer->expects( $this->once() )->method( 'pickHost' );
|
2018-06-06 10:13:04 +00:00
|
|
|
|
|
|
|
/** @var MathMathML $renderer */
|
|
|
|
$renderer->makeRequest( false, false, $res, $error, MathMLHttpRequestTester::class );
|
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
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \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
|
|
|
/**
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \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-03-23 21:11:05 +00:00
|
|
|
public function testintegrationTestWithLinks() {
|
2020-10-15 17:49:12 +00:00
|
|
|
$this->markTestSkipped( 'All HTTP requests are banned in tests. See T265628.' );
|
2020-02-17 15:29:08 +00:00
|
|
|
$p = MediaWikiServices::getInstance()->getParserFactory()->create();
|
2020-03-07 02:15:46 +00:00
|
|
|
$po = ParserOptions::newFromAnon();
|
2020-03-07 13:07:53 +00:00
|
|
|
$t = Title::newFromText( __METHOD__ );
|
2016-03-23 21:11:05 +00:00
|
|
|
$res = $p->parse( '[[test|<math forcemathmode="png">a+b</math>]]', $t, $po )->getText();
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( '</a>', $res );
|
|
|
|
$this->assertStringContainsString( 'png', $res );
|
2016-03-23 21:11:05 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 12:30:48 +00:00
|
|
|
/**
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \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
|
|
|
|
|
|
|
public function testPickHost() {
|
|
|
|
$hosts = [ 'a', 'b', 'c' ];
|
|
|
|
$this->setMwGlobals( 'wgMathMathMLUrl', $hosts );
|
|
|
|
srand( 0 ); // Make array_rand always return the same elements
|
|
|
|
$h1 = $hosts[array_rand( $hosts )];
|
|
|
|
$h2 = $hosts[array_rand( $hosts )];
|
|
|
|
srand( 0 );
|
2019-11-26 10:21:13 +00:00
|
|
|
/** @var MathMathML $m */
|
|
|
|
$m = TestingAccessWrapper::newFromObject( new MathMathML() );
|
|
|
|
$host1 = $m->pickHost();
|
|
|
|
$this->assertSame( $h1, $host1, 'first call' );
|
|
|
|
$host2 = $m->pickHost();
|
|
|
|
$this->assertSame( $host1, $host2, 'second call' );
|
|
|
|
/** @var MathMathML $m2 */
|
|
|
|
$m2 = TestingAccessWrapper::newFromObject( new MathMathML() );
|
|
|
|
$host3 = $m2->pickHost();
|
|
|
|
$this->assertSame( $h2, $host3, 'third call' );
|
2018-05-20 22:47:32 +00:00
|
|
|
}
|
2018-06-25 16:08:42 +00:00
|
|
|
|
|
|
|
public function testWarning() {
|
2020-10-15 17:49:12 +00:00
|
|
|
$this->markTestSkipped( 'All HTTP requests are banned in tests. See T265628.' );
|
2018-06-25 16:08:42 +00:00
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'always' );
|
|
|
|
$renderer = new MathMathML();
|
|
|
|
$rbi = $this->getMockBuilder( MathRestbaseInterface::class )
|
|
|
|
->setMethods( [ 'getWarnings', 'getSuccess' ] )
|
|
|
|
->setConstructorArgs( [ 'a+b' ] )
|
|
|
|
->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
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|