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;
|
2021-04-07 22:22:05 +00:00
|
|
|
use MediaWiki\Extension\Math\MathFormatter;
|
2021-08-11 13:38:51 +00:00
|
|
|
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
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
|
|
|
|
*
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\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
|
|
|
*/
|
2021-10-11 22:51:10 +00:00
|
|
|
class MathFormatterTest extends MediaWikiIntegrationTestCase {
|
2021-08-11 13:38:51 +00:00
|
|
|
use MathMockHttpTrait;
|
2016-01-25 15:38:53 +00:00
|
|
|
|
2021-08-11 13:38:51 +00:00
|
|
|
private const SOME_TEX = '\sin x^2';
|
2015-12-14 20:26:29 +00:00
|
|
|
|
2021-08-12 12:54:49 +00:00
|
|
|
protected function setUp(): void {
|
|
|
|
$this->markTestSkippedIfExtensionNotLoaded( 'WikibaseClient' );
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
/**
|
|
|
|
* Checks the
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\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() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupGoodMathRestBaseMockHttp( true );
|
|
|
|
|
2018-04-13 13:50:34 +00:00
|
|
|
$formatter = new MathFormatter( 'unknown/unknown' );
|
|
|
|
$value = new StringValue( self::SOME_TEX );
|
|
|
|
$resultFormat = $formatter->format( $value );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( '</math>', $resultFormat );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 17:02:33 +00:00
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathFormatter::format
|
2018-05-20 17:02:33 +00:00
|
|
|
*/
|
|
|
|
public function testUnknownFormatFailure() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupBadMathRestBaseMockHttp();
|
|
|
|
|
2018-05-20 17:02:33 +00:00
|
|
|
$formatter = new MathFormatter( 'unknown/unknown' );
|
2021-08-11 13:38:51 +00:00
|
|
|
$value = new StringValue( '\newcommand' );
|
2018-05-20 17:02:33 +00:00
|
|
|
$resultFormat = $formatter->format( $value );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( 'unknown function', $resultFormat );
|
2018-05-20 17:02:33 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupGoodMathRestBaseMockHttp( true );
|
|
|
|
|
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 );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( '</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() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupGoodMathRestBaseMockHttp( true );
|
|
|
|
|
2016-02-25 11:55:36 +00:00
|
|
|
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML_DIFF );
|
|
|
|
$value = new StringValue( self::SOME_TEX );
|
|
|
|
$resultFormat = $formatter->format( $value );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( '</math>', $resultFormat, 'Result must contain math-tag' );
|
|
|
|
$this->assertStringContainsString( '</h4>', $resultFormat, 'Result must contain a <h4> tag' );
|
|
|
|
$this->assertStringContainsString( '</code>', $resultFormat, 'Result must contain a <code> tag' );
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
'wb-details',
|
|
|
|
$resultFormat,
|
|
|
|
'Result must contain wb-details class'
|
|
|
|
);
|
|
|
|
$this->assertStringContainsString(
|
2016-02-25 11:55:36 +00:00
|
|
|
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
|
|
|
}
|