2015-12-14 20:26:29 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
|
|
|
use DataValues\NumberValue;
|
2020-01-14 07:43:50 +00:00
|
|
|
use DataValues\StringValue;
|
2019-06-07 15:04:12 +00:00
|
|
|
use Wikibase\Lib\Formatters\SnakFormatter;
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
/**
|
|
|
|
* Test the results of MathFormatter
|
|
|
|
*
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathFormatter
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
2015-12-14 20:26:29 +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-12-14 20:26:29 +00:00
|
|
|
*/
|
|
|
|
class MathFormatterTest extends MediaWikiTestCase {
|
2016-01-25 15:38:53 +00:00
|
|
|
|
2016-02-25 11:55:36 +00:00
|
|
|
const SOME_TEX = 'a^2+b^2 < c^2';
|
2015-12-14 20:26:29 +00:00
|
|
|
|
|
|
|
protected static $hasRestbase;
|
|
|
|
|
2019-11-02 00:18:33 +00:00
|
|
|
public static function setUpBeforeClass() : void {
|
2015-12-14 20:26:29 +00:00
|
|
|
$rbi = new MathRestbaseInterface();
|
|
|
|
self::$hasRestbase = $rbi->checkBackend( true );
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:45:11 +00:00
|
|
|
protected function setUp() : void {
|
2015-12-14 20:26:29 +00:00
|
|
|
parent::setUp();
|
2016-01-25 15:38:53 +00:00
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
if ( !self::$hasRestbase ) {
|
2016-01-25 15:38:53 +00:00
|
|
|
$this->markTestSkipped( 'Can not connect to Restbase Math interface.' );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathFormatter::__construct
|
2015-12-14 20:26:29 +00:00
|
|
|
*/
|
|
|
|
public function testBasics() {
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
|
2015-12-14 20:26:29 +00:00
|
|
|
// check if the format input was corretly passed to the class
|
2016-02-25 11:55:36 +00:00
|
|
|
$this->assertSame( SnakFormatter::FORMAT_PLAIN, $formatter->getFormat(), 'test getFormat' );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotStringValue() {
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
|
2019-10-16 02:28:43 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2015-12-14 20:26:29 +00:00
|
|
|
$formatter->format( new NumberValue( 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullValue() {
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
|
2019-10-16 02:28:43 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2015-12-14 20:26:29 +00:00
|
|
|
$formatter->format( null );
|
|
|
|
}
|
|
|
|
|
2018-04-13 13:50:34 +00:00
|
|
|
public function testUnknownFormatFallsBackToMathMl() {
|
|
|
|
$formatter = new MathFormatter( 'unknown/unknown' );
|
|
|
|
$value = new StringValue( self::SOME_TEX );
|
|
|
|
$resultFormat = $formatter->format( $value );
|
|
|
|
$this->assertContains( '</math>', $resultFormat );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 17:02:33 +00:00
|
|
|
/**
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathFormatter::format
|
2018-05-20 17:02:33 +00:00
|
|
|
*/
|
|
|
|
public function testUnknownFormatFailure() {
|
|
|
|
$formatter = new MathFormatter( 'unknown/unknown' );
|
|
|
|
$value = new StringValue( '\noTex' );
|
|
|
|
$resultFormat = $formatter->format( $value );
|
|
|
|
$this->assertContains( 'unknown function', $resultFormat );
|
|
|
|
}
|
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
public function testFormatPlain() {
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
|
2015-12-14 20:26:29 +00:00
|
|
|
$value = new StringValue( self::SOME_TEX );
|
|
|
|
$resultFormat = $formatter->format( $value );
|
2016-01-25 15:38:53 +00:00
|
|
|
$this->assertSame( self::SOME_TEX, $resultFormat );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormatHtml() {
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML );
|
2015-12-14 20:26:29 +00:00
|
|
|
$value = new StringValue( self::SOME_TEX );
|
|
|
|
$resultFormat = $formatter->format( $value );
|
2016-01-25 15:38:53 +00:00
|
|
|
$this->assertContains( '</math>', $resultFormat, 'Result must contain math-tag' );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 11:55:36 +00:00
|
|
|
public function testFormatDiffHtml() {
|
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML_DIFF );
|
|
|
|
$value = new StringValue( self::SOME_TEX );
|
|
|
|
$resultFormat = $formatter->format( $value );
|
|
|
|
$this->assertContains( '</math>', $resultFormat, 'Result must contain math-tag' );
|
|
|
|
$this->assertContains( '</h4>', $resultFormat, 'Result must contain a <h4> tag' );
|
|
|
|
$this->assertContains( '</code>', $resultFormat, 'Result must contain a <code> tag' );
|
|
|
|
$this->assertContains( 'wb-details', $resultFormat, 'Result must contain wb-details class' );
|
|
|
|
$this->assertContains(
|
|
|
|
htmlspecialchars( self::SOME_TEX ),
|
|
|
|
$resultFormat,
|
|
|
|
'Result must contain the TeX source'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
public function testFormatXWiki() {
|
|
|
|
$tex = self::SOME_TEX;
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_WIKI );
|
2015-12-14 20:26:29 +00:00
|
|
|
$value = new StringValue( self::SOME_TEX );
|
|
|
|
$resultFormat = $formatter->format( $value );
|
2016-01-25 15:38:53 +00:00
|
|
|
$this->assertSame( "<math>$tex</math>", $resultFormat, 'Tex wasn\'t properly wrapped' );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
2016-01-25 15:38:53 +00:00
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|