2013-09-24 00:41:09 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
/**
|
2013-09-24 00:41:09 +00:00
|
|
|
|
* Test the LaTeXML output format.
|
|
|
|
|
*
|
|
|
|
|
* @group Math
|
|
|
|
|
*/
|
|
|
|
|
class MathMathMLTest extends MediaWikiTestCase {
|
2013-05-14 21:49:06 +00:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
*
|
|
|
|
|
* @param boolean $good
|
|
|
|
|
* @param mixed $html HTML of the error message or false if no error is present.
|
|
|
|
|
* @param boolean $timeout true if
|
|
|
|
|
*/
|
|
|
|
|
public static function setMockValues( $good, $html, $timeout ) {
|
|
|
|
|
self::$good = $good;
|
|
|
|
|
self::$html = $html;
|
|
|
|
|
self::$timeout = $timeout;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 00:41:09 +00:00
|
|
|
|
/**
|
|
|
|
|
* Test rendering the string '0' see
|
|
|
|
|
* https://trac.mathweb.org/LaTeXML/ticket/1752
|
|
|
|
|
*/
|
|
|
|
|
public function testSpecialCase0() {
|
|
|
|
|
global $wgMathFastDisplay;
|
|
|
|
|
$wgMathFastDisplay = false;
|
|
|
|
|
$this->markTestSkipped( "Bug in LaTeXML" );
|
|
|
|
|
$renderer = MathRenderer::getRenderer( '0', array( ), MW_MATH_MATHML );
|
|
|
|
|
$expected = '0</cn>';
|
|
|
|
|
$this->assertTrue( $renderer->render() );
|
|
|
|
|
$this->assertContains( $expected, $renderer->getHtmlOutput(), 'Rendering the String "0"' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test rendering the string '0' see
|
|
|
|
|
* https://trac.mathweb.org/LaTeXML/ticket/1752
|
|
|
|
|
*/
|
|
|
|
|
public function testSpecialCaseText() {
|
|
|
|
|
global $wgMathFastDisplay;
|
|
|
|
|
if ( wfGetDB( DB_MASTER )->getType() === 'sqlite' ) {
|
|
|
|
|
$this->markTestSkipped( "SQLite has global indices. We cannot " .
|
|
|
|
|
"create the `unitest_math` table, its math_inputhash index " .
|
|
|
|
|
"would conflict with the one from the real `math` table."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wgMathFastDisplay = false;
|
|
|
|
|
$renderer = MathRenderer::getRenderer( 'x^2+\text{a sample Text}', array( ), MW_MATH_MATHML );
|
|
|
|
|
$expected = 'a sample Text</mtext>';
|
|
|
|
|
$this->assertTrue( $renderer->render() );
|
|
|
|
|
$this->assertContains( $expected, $renderer->getHtmlOutput(), 'Rendering the String "\text{CR}"' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
|
* Testcase: Invalid request.
|
|
|
|
|
* @covers MathTexvc::makeRequest
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeRequestInvalid() {
|
|
|
|
|
self::setMockValues( false, false, false );
|
|
|
|
|
$url = 'http://example.com/invalid';
|
|
|
|
|
|
2013-09-24 00:41:09 +00:00
|
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
|
|
|
|
->setMethods( NULL )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error
|
2013-09-24 00:41:09 +00:00
|
|
|
|
, 'LaTeXMLHttpRequestTester' );
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$this->assertEquals( false, $requestReturn
|
2013-09-24 00:41:09 +00:00
|
|
|
|
, "requestReturn is false if HTTP::post returns false." );
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$this->assertEquals( false, $res
|
2013-09-24 00:41:09 +00:00
|
|
|
|
, "res is false if HTTP:post returns false." );
|
|
|
|
|
$errmsg = wfMessage( 'math_latexml_invalidresponse', $url, '' )
|
|
|
|
|
->inContentLanguage()->escaped();
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$this->assertContains( $errmsg, $error
|
2013-09-24 00:41:09 +00:00
|
|
|
|
, "return an error if HTTP::post returns false" );
|
2013-05-14 21:49:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
|
* Testcase: Valid request.
|
|
|
|
|
* @covers MathTexvc::makeRequest
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeRequestSuccess() {
|
|
|
|
|
self::setMockValues( true, true, false );
|
|
|
|
|
$url = 'http://example.com/valid';
|
2013-09-24 00:41:09 +00:00
|
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
|
|
|
|
->setMethods( NULL )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$requestReturn = $renderer->makeRequest( $url, 'a+b', $res, $error
|
2013-09-24 00:41:09 +00:00
|
|
|
|
, 'LaTeXMLHttpRequestTester' );
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$this->assertEquals( true, $requestReturn, "successful call return" );
|
|
|
|
|
$this->isTrue( $res, "successfull call" );
|
2013-07-07 12:42:31 +00:00
|
|
|
|
$this->assertEquals( $error, '', "successfull call errormessage" );
|
2013-05-14 21:49:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests behavior of makeRequest() that communicates with the host.
|
|
|
|
|
* Testcase: Timeout.
|
2013-09-24 00:41:09 +00:00
|
|
|
|
* @covers MathMathML::makeRequest
|
2013-05-14 21:49:06 +00:00
|
|
|
|
*/
|
|
|
|
|
public function testMakeRequestTimeout() {
|
|
|
|
|
self::setMockValues( false, true, true );
|
|
|
|
|
$url = 'http://example.com/timeout';
|
2013-09-24 00:41:09 +00:00
|
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
|
|
|
|
->setMethods( NULL )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$requestReturn = $renderer->makeRequest( $url, '$\longcommand$', $res
|
2013-09-24 00:41:09 +00:00
|
|
|
|
, $error, 'LaTeXMLHttpRequestTester' );
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$this->assertEquals( false, $requestReturn, "timeout call return" );
|
|
|
|
|
$this->assertEquals( false, $res, "timeout call return" );
|
|
|
|
|
$errmsg = wfMessage( 'math_latexml_timeout', $url )
|
2013-09-24 00:41:09 +00:00
|
|
|
|
->inContentLanguage()->escaped();
|
2013-05-14 21:49:06 +00:00
|
|
|
|
$this->assertContains( $errmsg, $error, "timeout call errormessage" );
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 00:41:09 +00:00
|
|
|
|
// public function testisValidXML() {
|
|
|
|
|
// $validSample = '<math>content</math>';
|
|
|
|
|
// $invalidSample = '<notmath />';
|
|
|
|
|
// $this->assertTrue( MathMathML::isValidMathML( $validSample ), 'test if math expression is valid mathml sample' );
|
|
|
|
|
// $this->assertFalse( MathMathML::isValidMathML( $invalidSample ), 'test if math expression is invalid mathml sample' );
|
|
|
|
|
// }
|
|
|
|
|
|
2013-07-07 12:42:31 +00:00
|
|
|
|
/**
|
|
|
|
|
* Checks if a String is a valid MathML element
|
2013-09-24 00:41:09 +00:00
|
|
|
|
* @covers MathMathML::isValidXML
|
2013-07-07 12:42:31 +00:00
|
|
|
|
*/
|
|
|
|
|
public function testisValidXML() {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
|
|
|
|
->setMethods( NULL )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2013-07-07 12:42:31 +00:00
|
|
|
|
$validSample = '<math>content</math>';
|
|
|
|
|
$invalidSample = '<notmath />';
|
2013-09-24 00:41:09 +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' );
|
2013-07-25 07:14:30 +00:00
|
|
|
|
}
|
2013-07-07 12:42:31 +00:00
|
|
|
|
|
2013-07-25 07:14:30 +00:00
|
|
|
|
/**
|
|
|
|
|
* Tests the serialiazation of the LaTeXML settings
|
2013-09-24 00:41:09 +00:00
|
|
|
|
* @covers MathMathML::serializeSettings
|
2013-07-25 07:14:30 +00:00
|
|
|
|
*/
|
|
|
|
|
public function testSerializeSettings() {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
$renderer = $this->getMockBuilder( 'MathMathML' )
|
|
|
|
|
->setMethods( NULL )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2013-07-25 07:14:30 +00:00
|
|
|
|
$sampleSettings = array(
|
2013-09-24 00:41:09 +00:00
|
|
|
|
'k1' => 'v1',
|
|
|
|
|
'k2&=' => 'v2 + & *üö',
|
2013-07-25 07:14:30 +00:00
|
|
|
|
'k3' => array(
|
|
|
|
|
'v3A', 'v3b'
|
2013-09-24 00:41:09 +00:00
|
|
|
|
) );
|
2013-07-25 07:14:30 +00:00
|
|
|
|
$expected = 'k1=v1&k2%26%3D=v2+%2B+%26+%2A%C3%BC%C3%B6&k3=v3A&k3=v3b';
|
2013-09-24 00:41:09 +00:00
|
|
|
|
$this->assertEquals( $expected, $renderer->serializeSettings( $sampleSettings ), 'test serialization of array settings' );
|
|
|
|
|
$this->assertEquals( $expected, $renderer->serializeSettings( $expected ), 'test serialization of a string setting' );
|
2013-07-07 12:42:31 +00:00
|
|
|
|
}
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Checks the basic functionallity
|
|
|
|
|
* i.e. if the span element is generated right.
|
|
|
|
|
*/
|
|
|
|
|
public function testIntegration() {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
global $wgMathLaTeXMLTimeout;
|
|
|
|
|
global $wgMathFastDisplay;
|
|
|
|
|
$wgMathFastDisplay = false;
|
|
|
|
|
$wgMathLaTeXMLTimeout = 20;
|
|
|
|
|
$renderer = MathRenderer::getRenderer( "a+b", array( ), MW_MATH_MATHML );
|
|
|
|
|
$this->assertTrue( $renderer->render( true ) );
|
|
|
|
|
$real = str_replace( "\n", '', $renderer->getHtmlOutput() );
|
|
|
|
|
$expected = '<plus';
|
|
|
|
|
$this->assertContains( $expected, $real
|
2013-05-14 21:49:06 +00:00
|
|
|
|
, "Rendering of a+b in plain Text mode" );
|
|
|
|
|
}
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class for testing
|
|
|
|
|
* @author physikerwelt
|
|
|
|
|
* @see MWHttpRequestTester
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
class LaTeXMLHttpRequestTester {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
public static function factory() {
|
|
|
|
|
return new LaTeXMLHttpRequestTester();
|
|
|
|
|
}
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
public static function execute() {
|
|
|
|
|
return new LaTeXMLTestStatus();
|
|
|
|
|
}
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
public static function getContent() {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
return MathMathMLTest::$content;
|
2013-05-14 21:49:06 +00:00
|
|
|
|
}
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class for testing
|
|
|
|
|
* @author physikerwelt
|
|
|
|
|
* @see Status
|
|
|
|
|
*/
|
|
|
|
|
class LaTeXMLTestStatus {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
static function isGood() {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
return MathMathMLTest::$good;
|
2013-05-14 21:49:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function hasMessage( $s ) {
|
|
|
|
|
if ( $s == 'http-timed-out' ) {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
return MathMathMLTest::$timeout;
|
2013-05-14 21:49:06 +00:00
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
static function getHtml() {
|
2013-09-24 00:41:09 +00:00
|
|
|
|
return MathMathMLTest::$html;
|
2013-05-14 21:49:06 +00:00
|
|
|
|
}
|
2013-09-24 00:41:09 +00:00
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
|
}
|