2022-10-10 14:09:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math\InputCheck;
|
|
|
|
|
2023-06-19 20:14:14 +00:00
|
|
|
use HashBagOStuff;
|
2022-10-10 14:09:14 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
use Message;
|
2023-06-19 20:14:14 +00:00
|
|
|
use WANObjectCache;
|
2022-10-10 14:09:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Math
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
* tbd move this to unittests
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\LocalChecker
|
|
|
|
*/
|
|
|
|
class LocalCheckerTest extends MediaWikiIntegrationTestCase {
|
2023-06-19 20:14:14 +00:00
|
|
|
|
|
|
|
private const SAMPLE_KEY =
|
|
|
|
'global:MediaWiki\Extension\Math\InputCheck\LocalChecker:d5f40adbd26ff8b19b2c33289d7334b6';
|
|
|
|
|
2022-10-10 14:09:14 +00:00
|
|
|
public function testValid() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2' );
|
2022-10-10 14:09:14 +00:00
|
|
|
$this->assertNull( $checker->getError() );
|
|
|
|
$this->assertTrue( $checker->isValid() );
|
|
|
|
$this->assertNull( $checker->getError() );
|
|
|
|
$this->assertSame( '\\sin x^{2}', $checker->getValidTex() );
|
|
|
|
}
|
|
|
|
|
2022-10-20 21:36:00 +00:00
|
|
|
public function testValidTypeTex() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2', 'tex' );
|
2022-10-10 14:09:14 +00:00
|
|
|
$this->assertTrue( $checker->isValid() );
|
|
|
|
}
|
|
|
|
|
2022-10-20 21:36:00 +00:00
|
|
|
public function testValidTypeChem() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '{\\displaystyle {\\ce {\\cdot OHNO_{2}}}}', 'chem' );
|
2022-10-20 21:36:00 +00:00
|
|
|
$this->assertTrue( $checker->isValid() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidTypeInline() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '{\\textstyle \\log2 }', 'inline-tex' );
|
2022-10-20 21:36:00 +00:00
|
|
|
$this->assertTrue( $checker->isValid() );
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:09:14 +00:00
|
|
|
public function testInvalidType() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2', 'INVALIDTYPE' );
|
2023-02-27 14:47:35 +00:00
|
|
|
$this->assertInstanceOf( LocalChecker::class, $checker );
|
|
|
|
$this->assertInstanceOf( Message::class, $checker->getError() );
|
|
|
|
$this->assertFalse( $checker->isValid() );
|
2023-06-19 20:14:14 +00:00
|
|
|
$this->assertNull( $checker->getPresentationMathMLFragment() );
|
2022-10-10 14:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalid() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin\newcommand' );
|
2022-10-10 14:09:14 +00:00
|
|
|
$this->assertFalse( $checker->isValid() );
|
|
|
|
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
Message::newFromKey( 'math_unknown_function', '\newcommand' )
|
|
|
|
->inContentLanguage()
|
|
|
|
->escaped(),
|
|
|
|
$checker->getError()
|
|
|
|
->inContentLanguage()
|
|
|
|
->escaped()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull( $checker->getValidTex() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testErrorSyntax() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\left(' );
|
2022-10-10 14:09:14 +00:00
|
|
|
$this->assertFalse( $checker->isValid() );
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
Message::newFromKey( 'math_syntax_error' )
|
|
|
|
->inContentLanguage()
|
|
|
|
->escaped(),
|
|
|
|
$checker->getError()
|
|
|
|
->inContentLanguage()
|
|
|
|
->escaped()
|
|
|
|
);
|
|
|
|
}
|
2022-12-29 15:54:46 +00:00
|
|
|
|
2023-06-19 15:39:49 +00:00
|
|
|
public function testGetMML() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), 'e^{i \pi} + 1 = 0' );
|
2023-06-19 15:39:49 +00:00
|
|
|
$mml = $checker->getPresentationMathMLFragment();
|
|
|
|
$this->assertStringContainsString( '<mn>0</mn>', $mml );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMMLEmpty() {
|
2023-06-19 20:14:14 +00:00
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '' );
|
2023-06-19 15:39:49 +00:00
|
|
|
$mml = $checker->getPresentationMathMLFragment();
|
|
|
|
$this->assertSame( '', $mml );
|
|
|
|
}
|
2023-06-19 20:14:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\LocalChecker::getInputCacheKey
|
|
|
|
*/
|
|
|
|
public function testGetKey() {
|
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2', 'tex' );
|
|
|
|
$this->assertSame( self::SAMPLE_KEY, $checker->getInputCacheKey() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCache() {
|
|
|
|
$fakeWAN = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
|
|
|
|
$fakeContent = [ 'status' => '+', 'output' => 'out', 'mathml' => 'mml' ];
|
|
|
|
$fakeWAN->set( self::SAMPLE_KEY,
|
|
|
|
$fakeContent,
|
|
|
|
WANObjectCache::TTL_INDEFINITE,
|
|
|
|
[ 'version' => LocalChecker::VERSION ] );
|
|
|
|
$checker = new LocalChecker( $fakeWAN, '\sin x^2', 'tex' );
|
|
|
|
$this->assertSame( $fakeContent['output'], $checker->getValidTex() );
|
|
|
|
$this->assertSame( $fakeContent['mathml'], $checker->getPresentationMathMLFragment() );
|
|
|
|
$this->assertSame( true, $checker->isValid() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\LocalChecker::runCheck
|
|
|
|
*/
|
|
|
|
public function testRunChecks() {
|
|
|
|
$fakeContent = [
|
|
|
|
'status' => '+',
|
|
|
|
'mathml' => '<mi>sin</mi><mo></mo><msup><mi>x</mi><mrow data-mjx-texclass="ORD"><mn>2</mn></mrow></msup>',
|
|
|
|
'output' => '\\sin x^{2}'
|
|
|
|
];
|
|
|
|
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2', 'tex' );
|
|
|
|
$actual = $checker->runCheck();
|
|
|
|
$this->assertArrayEquals( $fakeContent, $actual );
|
|
|
|
}
|
2022-10-10 14:09:14 +00:00
|
|
|
}
|