2015-12-14 20:26:29 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
|
|
|
use DataValues\StringValue;
|
|
|
|
use DataValues\NumberValue;
|
2016-02-25 11:55:36 +00:00
|
|
|
use Wikibase\Lib\SnakFormatter;
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
/**
|
|
|
|
* Test the results of MathFormatter
|
|
|
|
*
|
2016-02-12 16:57:37 +00:00
|
|
|
* @covers MathFormatter
|
|
|
|
*
|
2015-12-14 20:26:29 +00:00
|
|
|
* @group Math
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
|
|
|
* @licence GNU GPL v2+
|
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;
|
|
|
|
|
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
$rbi = new MathRestbaseInterface();
|
|
|
|
self::$hasRestbase = $rbi->checkBackend( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
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
|
|
|
|
* @covers MathFormatter::__construct()
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
|
|
|
|
*/
|
|
|
|
public function testNotStringValue() {
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
|
2015-12-14 20:26:29 +00:00
|
|
|
$formatter->format( new NumberValue( 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
|
|
|
|
*/
|
|
|
|
public function testNullValue() {
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
|
2015-12-14 20:26:29 +00:00
|
|
|
$formatter->format( null );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testUnknownFormat() {
|
2016-01-25 15:38:53 +00:00
|
|
|
new MathFormatter( 'unknown/unknown' );
|
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
|
|
|
}
|