2020-05-12 18:22:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math\InputCheck;
|
|
|
|
|
|
|
|
use HashBagOStuff;
|
2021-10-01 01:16:45 +00:00
|
|
|
use MediaWiki\Extension\Math\MathMathML;
|
2020-05-12 18:22:37 +00:00
|
|
|
use MediaWiki\Http\HttpRequestFactory;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MediaWikiTestCase;
|
2021-02-02 20:30:28 +00:00
|
|
|
use MockHttpTrait;
|
2020-05-12 18:22:37 +00:00
|
|
|
use WANObjectCache;
|
|
|
|
|
|
|
|
class MathoidCheckerTest extends MediaWikiTestCase {
|
2021-02-02 20:30:28 +00:00
|
|
|
use MockHttpTrait;
|
|
|
|
|
2020-05-12 18:22:37 +00:00
|
|
|
private const SAMPLE_KEY = 'global:MediaWiki\Extension\Math\InputCheck\MathoidChecker:' .
|
2021-02-02 20:30:28 +00:00
|
|
|
'eb27aefff6e58e58dcefa22102531a58';
|
2020-05-12 18:22:37 +00:00
|
|
|
|
|
|
|
public function provideTexExamples() {
|
|
|
|
return [
|
2021-02-02 20:30:28 +00:00
|
|
|
[ '\sin x', 'eb27aefff6e58e58dcefa22102531a58' ],
|
|
|
|
[ '\sin_x', '7b33c69d6eac9126d41b663b93896951' ],
|
2020-05-12 18:22:37 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-23 00:46:48 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker::getCacheKey
|
2020-05-12 18:22:37 +00:00
|
|
|
* @dataProvider provideTexExamples
|
|
|
|
*/
|
|
|
|
public function testCacheKey( string $input, string $expected ) {
|
|
|
|
$checker = $this->getMathoidChecker( $input );
|
|
|
|
$realKey = $checker->getCacheKey();
|
|
|
|
$this->assertStringEndsWith( $expected, $realKey );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker::getCheckResponse
|
|
|
|
*/
|
|
|
|
public function testResponseFromCache() {
|
|
|
|
$fakeWAN = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
|
2021-02-02 20:30:28 +00:00
|
|
|
$fakeWAN->set( self::SAMPLE_KEY,
|
|
|
|
[ 999, 'expected' ],
|
|
|
|
WANObjectCache::TTL_INDEFINITE,
|
|
|
|
[ 'version' => MathoidChecker::VERSION ] );
|
2020-05-12 18:22:37 +00:00
|
|
|
// double check that the fake works
|
2021-02-02 20:30:28 +00:00
|
|
|
$this->assertSame( [ 999, 'expected' ], $fakeWAN->get( self::SAMPLE_KEY ) );
|
2020-05-12 18:22:37 +00:00
|
|
|
$this->setService( 'MainWANObjectCache', $fakeWAN );
|
|
|
|
$checker = $this->getMathoidChecker();
|
2021-02-02 20:30:28 +00:00
|
|
|
$this->assertSame( [ 999, 'expected' ], $checker->getCheckResponse() );
|
2020-05-12 18:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker::getCheckResponse
|
|
|
|
*/
|
|
|
|
public function testResponseFromResponse() {
|
|
|
|
$fakeWAN = WANObjectCache::newEmpty();
|
|
|
|
$fakeWAN->set( self::SAMPLE_KEY, 'expected' );
|
|
|
|
// double check that the fake does not works
|
|
|
|
$this->assertSame( false, $fakeWAN->get( self::SAMPLE_KEY ) );
|
|
|
|
$this->setService( 'MainWANObjectCache', $fakeWAN );
|
|
|
|
$this->setFakeRequest( 200, 'expected' );
|
|
|
|
$checker = $this->getMathoidChecker();
|
|
|
|
$this->assertSame( [ 200, 'expected' ], $checker->getCheckResponse() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker::getCheckResponse
|
|
|
|
*/
|
|
|
|
public function testFailedResponse() {
|
|
|
|
$fakeWAN = WANObjectCache::newEmpty();
|
|
|
|
$fakeWAN->set( self::SAMPLE_KEY, 'expected' );
|
|
|
|
// double check that the fake does not works
|
|
|
|
$this->assertSame( false, $fakeWAN->get( self::SAMPLE_KEY ) );
|
|
|
|
$this->setService( 'MainWANObjectCache', $fakeWAN );
|
|
|
|
$this->setFakeRequest( 401, false );
|
|
|
|
$checker = $this->getMathoidChecker();
|
|
|
|
$this->expectException( 'MWException' );
|
|
|
|
$checker->getCheckResponse();
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:30:28 +00:00
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker::isValid
|
|
|
|
* @dataProvider provideMathoidSamples
|
|
|
|
* @param string $input LaTeX input to check
|
|
|
|
* @param HttpRequestFactory $request fake mathoid response
|
|
|
|
* @param array $expeted
|
|
|
|
*/
|
|
|
|
public function testIsValid( $input, $request, $expeted ) {
|
|
|
|
$this->installMockHttp( $request );
|
|
|
|
$checker = $this->getMathoidChecker( $input );
|
|
|
|
$this->assertSame( $expeted['valid'], $checker->isValid() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker::getValidTex
|
|
|
|
* @dataProvider provideMathoidSamples
|
|
|
|
* @param string $input LaTeX input to check
|
|
|
|
* @param HttpRequestFactory $request fake mathoid response
|
|
|
|
* @param array $expeted
|
|
|
|
*/
|
|
|
|
public function testGetChecked( $input, $request, $expeted ) {
|
|
|
|
$this->installMockHttp( $request );
|
|
|
|
$checker = $this->getMathoidChecker( $input );
|
|
|
|
$this->assertSame( $expeted['checked'], $checker->getValidTex() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker::getError
|
|
|
|
* @dataProvider provideMathoidSamples
|
|
|
|
* @param string $input LaTeX input to check
|
|
|
|
* @param HttpRequestFactory $request fake mathoid response
|
|
|
|
* @param array $expeted
|
|
|
|
*/
|
|
|
|
public function testGetError( $input, $request, $expeted ) {
|
|
|
|
$this->installMockHttp( $request );
|
|
|
|
$checker = $this->getMathoidChecker( $input );
|
|
|
|
if ( array_key_exists( 'error', $expeted ) ) {
|
2021-08-05 01:08:52 +00:00
|
|
|
$checkerError = $checker->getError();
|
|
|
|
$this->assertNotNull( $checkerError );
|
|
|
|
$renderedCheckerError = ( new MathMathML( 'a' ) )
|
|
|
|
->getError( $checkerError->getKey(), ...$checkerError->getParams() );
|
|
|
|
$this->assertStringContainsString( $expeted['error'], $renderedCheckerError );
|
2021-02-02 20:30:28 +00:00
|
|
|
} else {
|
|
|
|
$this->assertNull( $checker->getError() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:22:37 +00:00
|
|
|
/**
|
|
|
|
* @param string $tex
|
|
|
|
* @return MathoidChecker
|
|
|
|
*/
|
|
|
|
private function getMathoidChecker( $tex = '\sin x' ): MathoidChecker {
|
|
|
|
return MediaWikiServices::getInstance()
|
|
|
|
->getService( 'Math.CheckerFactory' )
|
|
|
|
->newMathoidChecker( $tex, 'tex' );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setFakeRequest( $returnStatus, $content ): void {
|
|
|
|
$fakeHTTP = $this->createMock( HttpRequestFactory::class );
|
|
|
|
$fakeRequest = $this->createMock( \MWHttpRequest::class );
|
|
|
|
$fakeRequest->expects( $this->once() )->method( 'execute' )->willReturn( true );
|
|
|
|
$fakeRequest->expects( $this->once() )->method( 'getStatus' )->willReturn( $returnStatus );
|
|
|
|
if ( $content ) {
|
|
|
|
$fakeRequest->expects( $this->once() )->method( 'getContent' )->willReturn( $content );
|
|
|
|
} else {
|
|
|
|
$fakeRequest->expects( $this->never() )->method( 'getContent' );
|
|
|
|
}
|
|
|
|
$fakeHTTP->expects( $this->once() )->method( 'create' )->willReturn( $fakeRequest );
|
|
|
|
$this->setService( 'HttpRequestFactory', $fakeHTTP );
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:30:28 +00:00
|
|
|
public function provideMathoidSamples() {
|
|
|
|
yield '\ sin x' => [
|
|
|
|
'\sin x',
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/mathoid/sinx.json' ), 200 ),
|
2021-02-02 20:30:28 +00:00
|
|
|
[ 'valid' => true, 'checked' => '\sin x' ],
|
|
|
|
];
|
|
|
|
yield 'invalid F' => [
|
|
|
|
'1+\invalid',
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/mathoid/invalidF.json' ), 400 ),
|
2021-02-02 20:30:28 +00:00
|
|
|
[ 'valid' => false, 'checked' => null, 'error' => 'unknown function' ],
|
|
|
|
];
|
|
|
|
yield 'unescaped' => [
|
|
|
|
'1.5%',
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/mathoid/deprecated.json' ),
|
2021-02-02 20:30:28 +00:00
|
|
|
200 ),
|
|
|
|
[ 'valid' => true, 'checked' => '1.5\%' ],
|
|
|
|
];
|
|
|
|
yield 'syntax error' => [
|
|
|
|
'\left( x',
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/mathoid/syntaxE.json' ), 400 ),
|
2021-02-02 20:30:28 +00:00
|
|
|
[ 'valid' => false, 'checked' => null, 'error' => 'Failed to parse' ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:22:37 +00:00
|
|
|
}
|