mediawiki-extensions-Math/tests/phpunit/MathFormatterTest.php
Petr Pchelko da8e918fb3 Enable most tests which were previously disabled
Additionally - remove RestbaseInterface::checkBackend.
When HTTP requests are banned from unit tests this method
makes no sence.

MathoidCli tests were not reenabled cause it's a bit hard
to mock shell acceess. Hopefully as part of Shellbox
transition we can have MockShell trait in core and reenable
those tests

Bug: T265628
Depends-On: I350ed59af603a0504704af3265787a4be8f2dc2a
Change-Id: I294d0ed905193850dc38cb3071eb9b31c8863121
2021-08-12 08:57:58 -07:00

112 lines
3.6 KiB
PHP

<?php
use DataValues\NumberValue;
use DataValues\StringValue;
use MediaWiki\Extension\Math\MathFormatter;
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
use Wikibase\Lib\Formatters\SnakFormatter;
/**
* Test the results of MathFormatter
*
* @covers \MediaWiki\Extension\Math\MathFormatter
*
* @group Math
*
* @license GPL-2.0-or-later
*/
class MathFormatterTest extends MediaWikiTestCase {
use MathMockHttpTrait;
private const SOME_TEX = '\sin x^2';
/**
* Checks the
* @covers \MediaWiki\Extension\Math\MathFormatter::__construct
*/
public function testBasics() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
// check if the format input was corretly passed to the class
$this->assertSame( SnakFormatter::FORMAT_PLAIN, $formatter->getFormat(), 'test getFormat' );
}
public function testNotStringValue() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$this->expectException( InvalidArgumentException::class );
$formatter->format( new NumberValue( 0 ) );
}
public function testNullValue() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$this->expectException( InvalidArgumentException::class );
$formatter->format( null );
}
public function testUnknownFormatFallsBackToMathMl() {
$this->setupGoodMathRestBaseMockHttp( true );
$formatter = new MathFormatter( 'unknown/unknown' );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertStringContainsString( '</math>', $resultFormat );
}
/**
* @covers \MediaWiki\Extension\Math\MathFormatter::format
*/
public function testUnknownFormatFailure() {
$this->setupBadMathRestBaseMockHttp();
$formatter = new MathFormatter( 'unknown/unknown' );
$value = new StringValue( '\newcommand' );
$resultFormat = $formatter->format( $value );
$this->assertStringContainsString( 'unknown function', $resultFormat );
}
public function testFormatPlain() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertSame( self::SOME_TEX, $resultFormat );
}
public function testFormatHtml() {
$this->setupGoodMathRestBaseMockHttp( true );
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertStringContainsString( '</math>', $resultFormat, 'Result must contain math-tag' );
}
public function testFormatDiffHtml() {
$this->setupGoodMathRestBaseMockHttp( true );
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML_DIFF );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$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(
htmlspecialchars( self::SOME_TEX ),
$resultFormat,
'Result must contain the TeX source'
);
}
public function testFormatXWiki() {
$tex = self::SOME_TEX;
$formatter = new MathFormatter( SnakFormatter::FORMAT_WIKI );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertSame( "<math>$tex</math>", $resultFormat, 'Tex wasn\'t properly wrapped' );
}
}