Improve tests coverage for MathMathML

* deprecate and fix pickHost. It was broken and never used.
* simplify inputTypeSelection
* remove superfluous is_array check for the result of explode

Change-Id: I392f22f074facfe30b97d53a3002f464a471b67e
This commit is contained in:
Moritz Schubotz (physikerwelt) 2018-05-21 00:47:32 +02:00 committed by Physikerwelt
parent c1c4e42c86
commit aa2c4cf913
2 changed files with 99 additions and 15 deletions

View File

@ -38,13 +38,12 @@ class MathMathML extends MathRenderer {
$this->setMode( 'mathml' );
$this->hosts = $wgMathMathMLUrl;
if ( isset( $params['type'] ) ) {
$allowedTypes = [ 'pmml', 'ascii', 'chem' ];
if ( in_array( $params['type'], $allowedTypes ) ) {
$this->inputType = $params['type'];
}
if ( $params['type'] == 'pmml' ) {
$this->inputType = 'pmml';
$this->setMathml( '<math>' . $tex . '</math>' );
} elseif ( $params['type'] == 'ascii' ) {
$this->inputType = 'ascii';
} elseif ( $params['type'] == 'chem' ) {
$this->inputType = 'chem';
}
}
if ( !isset( $params['display'] ) && $this->getMathStyle() == 'inlineDisplaystyle' ) {
@ -194,7 +193,7 @@ class MathMathML extends MathRenderer {
$error = '';
$res = null;
if ( !$host ) {
$host = self::pickHost();
$host = $this->pickHost();
}
if ( !$post ) {
$this->getPostData();
@ -237,10 +236,11 @@ class MathMathML extends MathRenderer {
* If more than one demon is available, one is chosen at random.
*
* @return string
* @deprecated
*/
protected function pickHost() {
if ( is_array( $this->hosts ) ) {
$host = array_rand( $this->hosts );
$host = $this->hosts[array_rand( $this->hosts )];
$this->hosts = $host; // Use the same host for this class instance
} else {
$host = $this->hosts;
@ -278,7 +278,7 @@ class MathMathML extends MathRenderer {
return false;
}
$res = '';
$host = self::pickHost();
$host = $this->pickHost();
$post = $this->getPostData();
$this->lastError = '';
$requestResult = $this->makeRequest( $host, $post, $res, $this->lastError );
@ -337,15 +337,11 @@ class MathMathML extends MathRenderer {
} else {
$name = $xmlObject->getRootElement();
$elementSplit = explode( ':', $name );
if ( is_array( $elementSplit ) ) {
$localName = end( $elementSplit );
} else {
$localName = $name;
}
$localName = end( $elementSplit );
if ( in_array( $localName, $this->getAllowedRootElements() ) ) {
$out = true;
} else {
LoggerFactory::getInstance( 'Math' )->error( "Got wrong root element : $name" );
LoggerFactory::getInstance( 'Math' )->error( "Got wrong root element: $name" );
}
}
return $out;

View File

@ -32,10 +32,30 @@ class MathMathMLTest extends MediaWikiTestCase {
}
protected function setUp() {
parent::setUp(); // TODO: Change the autogenerated stub
parent::setUp();
$this->setMwGlobals( 'wgMathoidCli', false );
}
/**@covers MathMathML::__constructor */
public function testMathMLConstructorWithPmml() {
$mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'pmml' ] );
$this->assertEquals( 'pmml', $mml->getInputType() );
$this->assertEquals( '<math><mo>sin</mo></math>', $mml->getMathml() );
}
/**@covers MathMathML::__constructor */
public function testMathMLConstructorWithInvalidType() {
$mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'invalid' ] );
$this->assertEquals( 'tex', $mml->getInputType() );
}
/**@covers MathMathML::__constructor */
public function testChangeRootElemts() {
$mml = new MathMathML( '<mo>sin</mo>', [ 'type' => 'invalid' ] );
$mml->setAllowedRootElements( [ 'a','b' ] );
$this->assertEquals( [ 'a','b' ], $mml->getAllowedRootElements() );
}
/**
* Tests behavior of makeRequest() that communicates with the host.
* Testcase: Invalid request.
@ -102,6 +122,38 @@ class MathMathMLTest extends MediaWikiTestCase {
$this->assertContains( $errmsg, $error, "timeout call errormessage" );
}
/**
* Tests behavior of makeRequest() that communicates with the host.
* Test case: Get PostData.
* @covers MathMathML::makeRequest
*/
public function testMakeRequestGetPostData() {
self::setMockValues( false, true, true );
$url = 'http://example.com/timeout';
$renderer = $this->getMockBuilder( 'MathMathML' )
->setMethods( [ 'getPostData' ] )
->disableOriginalConstructor()
->getMock();
$renderer->expects( $this->once() )->method( 'getPostData' );
$renderer->makeRequest( $url, false, $res, $error, 'MathMLHttpRequestTester' );
}
/**
* Tests behavior of makeRequest() that communicates with the host.
* Test case: Get host.
* @covers MathMathML::pickHost
*/
public function testMakeRequestGetHost() {
self::setMockValues( false, true, true );
$url = 'http://example.com/timeout';
$renderer = $this->getMockBuilder( 'MathMathML' )
->setMethods( [ 'getPostData', 'pickHost' ] )
->disableOriginalConstructor()
->getMock();
$renderer->expects( $this->once() )->method( 'pickHost' );
$renderer->makeRequest( false, false, $res, $error, 'MathMLHttpRequestTester' );
}
/**
* Checks if a String is a valid MathML element
* @covers MathMathML::isValidMathML
@ -119,6 +171,22 @@ class MathMathMLTest extends MediaWikiTestCase {
'test if math expression is invalid mathml sample' );
}
/**
* @covers MathMathML::isValidMathML
*/
public function testInvalidXml() {
$renderer = $this->getMockBuilder( 'MathMathML' )
->setMethods( null )
->disableOriginalConstructor()
->getMock();
$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' );
}
public function testintegrationTestWithLinks() {
$p = new Parser();
$po = new ParserOptions();
@ -141,6 +209,26 @@ class MathMathMLTest extends MediaWikiTestCase {
$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 );
}
public function testPickHost() {
$hosts = [ 'a', 'b', 'c' ];
$this->setMwGlobals( 'wgMathMathMLUrl', $hosts );
$class = new ReflectionClass( 'MathMathML' );
$method = $class->getMethod( 'pickHost' );
$method->setAccessible( true );
srand( 0 ); // Make array_rand always return the same elements
$h1 = $hosts[array_rand( $hosts )];
$h2 = $hosts[array_rand( $hosts )];
srand( 0 );
$m = new MathMathML();
$host1 = $method->invoke( $m, [] );
$this->assertEquals( $h1, $host1 );
$host2 = $method->invoke( $m, [] );
$this->assertEquals( $host1, $host2 );
$m2 = new MathMathML();
$host3 = $method->invoke( $m2, [] );
$this->assertEquals( $h2, $host3 );
}
}
/**