2015-03-12 18:50:11 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2017-08-16 05:50:44 +00:00
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
/**
|
|
|
|
* Test the MathML output format.
|
|
|
|
*
|
2016-02-12 16:57:37 +00:00
|
|
|
* @covers MathMathML
|
|
|
|
*
|
2014-06-03 08:15:53 +00:00
|
|
|
* @group Math
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
|
|
|
* @licence GNU GPL v2+
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-16 05:50:44 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
|
$this->setMwGlobals( 'wgMathoidCli', false );
|
|
|
|
}
|
|
|
|
|
2014-06-03 08:15:53 +00:00
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Testcase: Invalid request.
|
2018-02-05 15:24:14 +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';
|
|
|
|
|
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
2015-09-21 16:14:01 +00:00
|
|
|
->setMethods( null )
|
2014-06-03 08:15:53 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-09-21 16:14:01 +00:00
|
|
|
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error,
|
|
|
|
'MathMLHttpRequestTester' );
|
|
|
|
$this->assertEquals( false, $requestReturn,
|
|
|
|
"requestReturn is false if HTTP::post returns false." );
|
|
|
|
$this->assertEquals( false, $res,
|
|
|
|
"res is false if HTTP:post returns false." );
|
2014-06-03 08:15:53 +00:00
|
|
|
$errmsg = wfMessage( 'math_invalidresponse', '', $url, '' )->inContentLanguage()->escaped();
|
2015-09-21 16:14:01 +00:00
|
|
|
$this->assertContains( $errmsg, $error,
|
|
|
|
"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.
|
2018-02-05 15:24:14 +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';
|
2017-08-16 05:50:44 +00:00
|
|
|
/** @var MathMathML */
|
2014-06-03 08:15:53 +00:00
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
2015-09-21 16:14:01 +00:00
|
|
|
->setMethods( null )
|
2014-06-03 08:15:53 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-09-21 16:14:01 +00:00
|
|
|
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error,
|
|
|
|
'MathMLHttpRequestTester' );
|
2014-06-03 08:15:53 +00:00
|
|
|
$this->assertEquals( true, $requestReturn, "successful call return" );
|
2017-08-16 05:50:44 +00:00
|
|
|
$this->assertEquals( 'test content', $res, 'successful call' );
|
|
|
|
$this->assertEquals( $error, '', "successful call error-message" );
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
* Testcase: Timeout.
|
|
|
|
* @covers MathMathML::makeRequest
|
|
|
|
*/
|
|
|
|
public function testMakeRequestTimeout() {
|
|
|
|
self::setMockValues( false, true, true );
|
|
|
|
$url = 'http://example.com/timeout';
|
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
2015-09-21 16:14:01 +00:00
|
|
|
->setMethods( null )
|
2014-06-03 08:15:53 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-09-21 16:14:01 +00:00
|
|
|
$requestReturn = $renderer->makeRequest(
|
|
|
|
$url, '$\longcommand$', $res, $error, 'MathMLHttpRequestTester'
|
|
|
|
);
|
2014-06-03 08:15:53 +00:00
|
|
|
$this->assertEquals( false, $requestReturn, "timeout call return" );
|
|
|
|
$this->assertEquals( false, $res, "timeout call return" );
|
|
|
|
$errmsg = wfMessage( 'math_timeout', '', $url )->inContentLanguage()->escaped();
|
|
|
|
$this->assertContains( $errmsg, $error, "timeout call errormessage" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a String is a valid MathML element
|
2018-02-05 15:24:14 +00:00
|
|
|
* @covers MathMathML::isValidMathML
|
2014-06-03 08:15:53 +00:00
|
|
|
*/
|
2018-02-05 15:24:14 +00:00
|
|
|
public function testisValidMathML() {
|
2014-06-03 08:15:53 +00:00
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
2015-09-21 16:14:01 +00:00
|
|
|
->setMethods( null )
|
2014-06-03 08:15:53 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2016-03-23 21:11:05 +00:00
|
|
|
public function testintegrationTestWithLinks() {
|
|
|
|
$p = new Parser();
|
|
|
|
$po = new ParserOptions();
|
|
|
|
$t = new Title( "test" );
|
|
|
|
$res = $p->parse( '[[test|<math forcemathmode="png">a+b</math>]]', $t, $po )->getText();
|
|
|
|
$this->assertContains( '</a>', $res );
|
|
|
|
$this->assertContains( '.png', $res );
|
|
|
|
}
|
|
|
|
|
2016-04-13 12:30:48 +00:00
|
|
|
/**
|
|
|
|
* @covers MathMathML::correctSvgStyle
|
|
|
|
* @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 );
|
|
|
|
$this->assertEquals( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style );
|
|
|
|
$m->setSvg( 'style=" vertical-align:-.505ex; \n" height="2.843ex" width="28.527ex"' );
|
|
|
|
$this->assertEquals( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style );
|
|
|
|
}
|
2014-06-03 08:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class for testing
|
|
|
|
* @author physikerwelt
|
|
|
|
* @see MWHttpRequestTester
|
|
|
|
*/
|
|
|
|
class MathMLHttpRequestTester {
|
|
|
|
|
|
|
|
public static function factory() {
|
|
|
|
return new MathMLHttpRequestTester();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function execute() {
|
|
|
|
return new MathMLTestStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getContent() {
|
|
|
|
return MathMathMLTest::$content;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class for testing
|
|
|
|
* @author physikerwelt
|
|
|
|
* @see Status
|
|
|
|
*/
|
|
|
|
class MathMLTestStatus {
|
|
|
|
|
|
|
|
static function isGood() {
|
|
|
|
return MathMathMLTest::$good;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function hasMessage( $s ) {
|
|
|
|
if ( $s == 'http-timed-out' ) {
|
|
|
|
return MathMathMLTest::$timeout;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getHtml() {
|
|
|
|
return MathMathMLTest::$html;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getWikiText() {
|
|
|
|
return MathMathMLTest::$html;
|
|
|
|
}
|
|
|
|
|
2015-01-10 01:55:54 +00:00
|
|
|
}
|