mediawiki-extensions-Math/tests/phpunit/MathInputCheckRestbaseTest.php
Umherirrender e599afa6db Use assertStringContainsString in unit tests
Using assertContains() with string haystacks is deprecated and will not
be supported in PHPUnit 9. Refactor your test to use
assertStringContainsString() or assertStringContainsStringIgnoringCase()
instead.

Change-Id: Ic35f3c60a7f49dfe244b87192d7f161c117b37e1
2020-04-05 13:46:04 +02:00

97 lines
3.1 KiB
PHP

<?php
/**
* @group Math
*
* @license GPL-2.0-or-later
*/
class MathInputCheckRestbaseTest extends MediaWikiTestCase {
protected static $hasRestbase;
/** @var MathInputCheckRestbase */
protected $BadObject;
/** @var MathInputCheckRestbase */
protected $GoodObject;
public static function setUpBeforeClass() : void {
$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() : void {
parent::setUp();
if ( !self::$hasRestbase ) {
$this->markTestSkipped( "Can not connect to Restbase Math interface." );
}
$this->BadObject = new MathInputCheckRestbase( '\newcommand{\text{do evil things}}' );
$this->GoodObject = new MathInputCheckRestbase( '\sin\left(\frac12x\right)' );
}
/**
* @covers \MathInputCheckRestbase::getError
*/
public function testGetError() {
$this->assertNull( $this->GoodObject->getError() );
$this->assertNull( $this->BadObject->getError() );
$this->BadObject->isValid();
$this->GoodObject->isValid();
$this->assertNull( $this->GoodObject->getError() );
$expectedMessage = wfMessage(
'math_unknown_function', '\newcommand'
)->inContentLanguage()->escaped();
$this->assertStringContainsString( $expectedMessage, $this->BadObject->getError() );
}
/**
* @covers \MathInputCheckRestbase::getError
*/
public function testErrorSyntax() {
$o = new MathInputCheckRestbase( '\left(' );
$this->assertFalse( $o->isValid() );
$expectedMessage = wfMessage( 'math_syntax_error' )->inContentLanguage()->escaped();
$this->assertStringContainsString( $expectedMessage, $o->getError() );
}
/**
* @covers \MathInputCheckRestbase::getError
*/
public function testErrorLexing() {
$o = new MathInputCheckRestbase( "\x61\xCC\x81" );
$this->assertFalse( $o->isValid() );
// Lexical errors are no longer supported. The new error message
// Expected "-", "[", "\\\\",
// "\\\\begin", "\\\\begin{", "]", "^", "_", "{", [ \\t\\n\\r], [%$], [().], [,:;?!\\\'],
// [-+*=], [0-9], [><~], [\\/|] or [a-zA-Z] but "\\u0301" found.
// is more expressive anyhow.
$expectedMessage = wfMessage( 'math_syntax_error' )->inContentLanguage()->escaped();
$this->assertStringContainsString( $expectedMessage, $o->getError() );
}
/**
* @covers \MathInputCheckRestbase::isValid
*/
public function testIsValid() {
$this->assertFalse( $this->BadObject->isValid() );
$this->assertTrue( $this->GoodObject->isValid() );
}
/**
* @covers \MathInputCheckRestbase::getValidTex
*/
public function testGetValidTex() {
$this->assertNull( $this->GoodObject->getValidTex() );
$this->assertNull( $this->BadObject->getValidTex() );
$this->BadObject->isValid();
$this->GoodObject->isValid();
$this->assertNull( $this->BadObject->getValidTex() );
// Note that texvcjs has slightly diverged from texvc and enforces brackets for function
// arguments. Also the double space between frac and the arg has ben reduce to a single space.
$this->assertEquals( $this->GoodObject->getValidTex(), '\\sin \\left({\\frac {1}{2}}x\\right)' );
}
}