Merge "Use Mathoid to validate Wikibase formuale"

This commit is contained in:
jenkins-bot 2021-02-23 21:06:52 +00:00 committed by Gerrit Code Review
commit 7bb3d281da
3 changed files with 20 additions and 19 deletions

View file

@ -121,6 +121,9 @@
"MathoidCli": {
"value": false
},
"MathUseRestBase": {
"value": true
},
"MathValidModes": {
"description": "To access this at run-time, use MathRenderer::getValidModes(). Do not use the non-normalized configuration directly.",
"value": [

View file

@ -2,6 +2,7 @@
use DataValues\StringValue;
use MediaWiki\Extension\Math\InputCheck\RestbaseChecker;
use MediaWiki\MediaWikiServices;
use ValueValidators\Error;
use ValueValidators\Result;
use ValueValidators\ValueValidator;
@ -22,14 +23,20 @@ class MathValidator implements ValueValidator {
* @throws InvalidArgumentException if not called with a StringValue
*/
public function validate( $value ) {
global $wgMathUseRestBase;
if ( !( $value instanceof StringValue ) ) {
throw new InvalidArgumentException( '$value must be a StringValue' );
}
// get input String from value
$tex = $value->getValue();
if ( $wgMathUseRestBase ) {
$checker = new RestbaseChecker( $tex );
} else {
$checker = MediaWikiServices::getInstance()
->getService( 'Math.CheckerFactory' )
->newMathoidChecker( $tex, 'tex' );
}
if ( $checker->isValid() ) {
return Result::newSuccess();
}

View file

@ -11,27 +11,14 @@ use DataValues\StringValue;
* @license GPL-2.0-or-later
*/
class MathValidatorTest extends MediaWikiTestCase {
private const VADLID_TEX = "a^2+b^2=c^2";
use MockHttpTrait;
private const VADLID_TEX = "\sin x";
private const INVADLID_TEX = "\\notExists";
protected static $hasRestbase;
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 {
$this->markTestSkipped( 'All HTTP requests are banned in tests. See T265628.' );
parent::setUp();
if ( !self::$hasRestbase ) {
$this->markTestSkipped( "Can not connect to Restbase Math interface." );
}
$this->setMwGlobals( 'wgMathUseRestBase', false );
}
public function testNotStringValue() {
@ -47,6 +34,8 @@ class MathValidatorTest extends MediaWikiTestCase {
}
public function testValidInput() {
$this->installMockHttp( $this->makeFakeHttpRequest( file_get_contents( __DIR__ .
'/InputCheck/data/sinx.json' ) ) );
$validator = new MathValidator();
$result = $validator->validate( new StringValue( self::VADLID_TEX ) );
$this->assertInstanceOf( \ValueValidators\Result::class, $result );
@ -54,6 +43,8 @@ class MathValidatorTest extends MediaWikiTestCase {
}
public function testInvalidInput() {
$this->installMockHttp( $this->makeFakeHttpRequest( file_get_contents( __DIR__ .
'/InputCheck/data/invalidF.json' ), 400 ) );
$validator = new MathValidator();
$result = $validator->validate( new StringValue( self::INVADLID_TEX ) );
$this->assertInstanceOf( \ValueValidators\Result::class, $result );