2015-11-26 19:31:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the interface to access Restbase paths
|
|
|
|
* /media/math/check/{type}
|
|
|
|
* /media/math/render/{format}/{hash}
|
|
|
|
*
|
2016-02-12 16:57:37 +00:00
|
|
|
* @covers MathRestbaseInterface
|
|
|
|
*
|
2015-11-26 19:31:09 +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
|
2015-11-26 19:31:09 +00:00
|
|
|
*/
|
|
|
|
class MathRestbaseInterfaceTest extends MediaWikiTestCase {
|
|
|
|
protected static $hasRestbase;
|
|
|
|
|
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
$rbi = new MathRestbaseInterface();
|
|
|
|
self::$hasRestbase = $rbi->checkBackend( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up the fixture, for example, opens a network connection.
|
|
|
|
* This method is called before a test is executed.
|
|
|
|
*/
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
if ( !self::$hasRestbase ) {
|
|
|
|
$this->markTestSkipped( "Can not connect to Restbase Math interface." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfig() {
|
|
|
|
$rbi = new MathRestbaseInterface();
|
|
|
|
$this->assertTrue( $rbi->checkBackend() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSuccess() {
|
|
|
|
$input = '\\sin x^2';
|
|
|
|
$rbi = new MathRestbaseInterface( $input );
|
|
|
|
$this->assertTrue( $rbi->getSuccess(), "Assuming that $input is valid input." );
|
|
|
|
$this->assertEquals( '\\sin x^{2}', $rbi->getCheckedTex() );
|
|
|
|
$this->assertContains( '<mi>sin</mi>', $rbi->getMathML() );
|
|
|
|
$url = $rbi->getFullSvgUrl();
|
|
|
|
$req = MWHttpRequest::factory( $url );
|
|
|
|
$status = $req->execute();
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
$this->assertContains( '</svg>', $req->getContent() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFail() {
|
|
|
|
$input = '\\sin\\newcommand';
|
|
|
|
$rbi = new MathRestbaseInterface( $input );
|
|
|
|
$this->assertFalse( $rbi->getSuccess(), "Assuming that $input is invalid input." );
|
|
|
|
$this->assertEquals( '', $rbi->getCheckedTex() );
|
|
|
|
$this->assertEquals( 'Illegal TeX function', $rbi->getError()->error->message );
|
|
|
|
}
|
|
|
|
|
2016-01-29 13:05:54 +00:00
|
|
|
public function testChem() {
|
|
|
|
$input = '\ce{H2O}';
|
|
|
|
$rbi = new MathRestbaseInterface( $input, 'chem' );
|
|
|
|
$this->assertTrue( $rbi->checkTeX(), "Assuming that $input is valid input." );
|
|
|
|
$this->assertTrue( $rbi->getSuccess(), "Assuming that $input is valid input." );
|
|
|
|
$this->assertEquals( '{\ce {H2O}}', $rbi->getCheckedTex() );
|
|
|
|
$this->assertContains( '<msubsup>', $rbi->getMathML() );
|
|
|
|
$this->assertContains( '<mtext>H</mtext>', $rbi->getMathML() );
|
|
|
|
}
|
2015-11-26 19:31:09 +00:00
|
|
|
/**
|
|
|
|
* @expectedException MWException
|
|
|
|
* @expectedExceptionMessage TeX input is invalid.
|
|
|
|
*/
|
|
|
|
public function testException() {
|
|
|
|
$input = '\\newcommand';
|
|
|
|
$rbi = new MathRestbaseInterface( $input );
|
|
|
|
$rbi->getMathML();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException MWException
|
|
|
|
* @expectedExceptionMessage TeX input is invalid.
|
|
|
|
*/
|
|
|
|
public function testExceptionSvg() {
|
|
|
|
$input = '\\newcommand';
|
|
|
|
$rbi = new MathRestbaseInterface( $input );
|
|
|
|
$rbi->getFullSvgUrl();
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:04 +00:00
|
|
|
/**
|
|
|
|
* Incorporate the "details" in the error message, if the check requests passes, but the
|
|
|
|
* mml/svg/complete endpoints returns an error
|
|
|
|
* @expectedException MWException
|
|
|
|
* @expectedExceptionMessage Cannot get mml. TeX parse error: Missing close brace
|
|
|
|
*/
|
|
|
|
public function testLateError() {
|
2018-05-04 02:00:55 +00:00
|
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
2016-08-17 17:41:04 +00:00
|
|
|
$input = '{"type":"https://mediawiki.org/wiki/HyperSwitch/errors/bad_request","title":"Bad Request","method":"POST","detail":["TeX parse error: Missing close brace"],"uri":"/complete"}';
|
|
|
|
MathRestbaseInterface::throwContentError( 'mml', $input );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Incorporate the "details" in the error message, if the check requests passes, but the
|
|
|
|
* mml/svg/complete endpoints returns an error
|
|
|
|
* @expectedException MWException
|
|
|
|
* @expectedExceptionMessage Cannot get mml. TeX parse error: Missing close brace
|
|
|
|
*/
|
|
|
|
public function testLateErrorString() {
|
2018-05-04 02:00:55 +00:00
|
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
2016-08-17 17:41:04 +00:00
|
|
|
$input = '{"type":"https://mediawiki.org/wiki/HyperSwitch/errors/bad_request","title":"Bad Request","method":"POST","detail": "TeX parse error: Missing close brace","uri":"/complete"}';
|
|
|
|
MathRestbaseInterface::throwContentError( 'mml', $input );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException MWException
|
|
|
|
* @expectedExceptionMessage Cannot get mml. Server problem.
|
|
|
|
*/
|
|
|
|
public function testLateErrorNoDetail() {
|
|
|
|
$input = '';
|
|
|
|
MathRestbaseInterface::throwContentError( 'mml', $input );
|
|
|
|
}
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|