2015-12-14 20:26:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use DataValues\NumberValue;
|
2020-01-14 07:43:50 +00:00
|
|
|
use DataValues\StringValue;
|
2021-04-07 22:22:05 +00:00
|
|
|
use MediaWiki\Extension\Math\MathValidator;
|
2015-12-14 20:26:29 +00:00
|
|
|
|
2016-02-12 16:57:37 +00:00
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathValidator
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
|
|
|
* @group Math
|
|
|
|
*
|
2018-04-13 14:06:39 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2016-02-12 16:57:37 +00:00
|
|
|
*/
|
2015-12-14 20:26:29 +00:00
|
|
|
class MathValidatorTest extends MediaWikiTestCase {
|
2021-02-09 18:39:18 +00:00
|
|
|
use MockHttpTrait;
|
2015-12-14 20:26:29 +00:00
|
|
|
|
2021-02-09 18:39:18 +00:00
|
|
|
private const VADLID_TEX = "\sin x";
|
|
|
|
private const INVADLID_TEX = "\\notExists";
|
2015-12-14 20:26:29 +00:00
|
|
|
|
2019-10-11 17:45:11 +00:00
|
|
|
protected function setUp() : void {
|
2015-12-14 20:26:29 +00:00
|
|
|
parent::setUp();
|
2021-02-09 18:39:18 +00:00
|
|
|
$this->setMwGlobals( 'wgMathUseRestBase', false );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotStringValue() {
|
|
|
|
$validator = new MathValidator();
|
2019-10-16 02:28:43 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2015-12-14 20:26:29 +00:00
|
|
|
$validator->validate( new NumberValue( 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullValue() {
|
|
|
|
$validator = new MathValidator();
|
2019-10-16 02:28:43 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2015-12-14 20:26:29 +00:00
|
|
|
$validator->validate( null );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidInput() {
|
2021-02-09 18:39:18 +00:00
|
|
|
$this->installMockHttp( $this->makeFakeHttpRequest( file_get_contents( __DIR__ .
|
|
|
|
'/InputCheck/data/sinx.json' ) ) );
|
2015-12-14 20:26:29 +00:00
|
|
|
$validator = new MathValidator();
|
|
|
|
$result = $validator->validate( new StringValue( self::VADLID_TEX ) );
|
2020-01-17 16:14:26 +00:00
|
|
|
$this->assertInstanceOf( \ValueValidators\Result::class, $result );
|
2015-12-14 20:26:29 +00:00
|
|
|
$this->assertTrue( $result->isValid() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidInput() {
|
2021-02-09 18:39:18 +00:00
|
|
|
$this->installMockHttp( $this->makeFakeHttpRequest( file_get_contents( __DIR__ .
|
|
|
|
'/InputCheck/data/invalidF.json' ), 400 ) );
|
2015-12-14 20:26:29 +00:00
|
|
|
$validator = new MathValidator();
|
|
|
|
$result = $validator->validate( new StringValue( self::INVADLID_TEX ) );
|
2020-01-17 16:14:26 +00:00
|
|
|
$this->assertInstanceOf( \ValueValidators\Result::class, $result );
|
2015-12-14 20:26:29 +00:00
|
|
|
$this->assertFalse( $result->isValid() );
|
|
|
|
}
|
|
|
|
}
|