mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-11-27 17:01:07 +00:00
Enable most tests which were previously disabled
Additionally - remove RestbaseInterface::checkBackend. When HTTP requests are banned from unit tests this method makes no sence. MathoidCli tests were not reenabled cause it's a bit hard to mock shell acceess. Hopefully as part of Shellbox transition we can have MockShell trait in core and reenable those tests Bug: T265628 Depends-On: I350ed59af603a0504704af3265787a4be8f2dc2a Change-Id: I294d0ed905193850dc38cb3071eb9b31c8863121
This commit is contained in:
parent
8345a5ba9e
commit
da8e918fb3
|
@ -33,7 +33,8 @@
|
|||
"MediaWiki\\Extension\\Math\\": "src/"
|
||||
},
|
||||
"TestAutoloadClasses": {
|
||||
"DummyPropertyDataTypeLookup": "tests/phpunit/DummyPropertyDataTypeLookup.php"
|
||||
"DummyPropertyDataTypeLookup": "tests/phpunit/DummyPropertyDataTypeLookup.php",
|
||||
"MediaWiki\\Extension\\Math\\Tests\\MathMockHttpTrait": "tests/phpunit/MathMockHttpTrait.php"
|
||||
},
|
||||
"DefaultUserOptions": {
|
||||
"math": "mathml"
|
||||
|
|
|
@ -249,31 +249,6 @@ class MathRestbaseInterface {
|
|||
return $this->getContent( 'svg' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool|false $skipConfigCheck
|
||||
* @return bool
|
||||
*/
|
||||
public function checkBackend( $skipConfigCheck = false ) {
|
||||
try {
|
||||
$request = [
|
||||
'method' => 'GET',
|
||||
'url' => $this->getUrl( '?spec' )
|
||||
];
|
||||
} catch ( Exception $e ) {
|
||||
return false;
|
||||
}
|
||||
$serviceClient = $this->getServiceClient();
|
||||
$response = $serviceClient->run( $request );
|
||||
if ( $response['code'] === 200 ) {
|
||||
return $skipConfigCheck || $this->checkConfig();
|
||||
}
|
||||
$this->log()->error( "Restbase backend is not correctly set up.", [
|
||||
'request' => $request,
|
||||
'response' => $response
|
||||
] );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique TeX string, renders it and gets it via a public URL.
|
||||
* The method fails, if the public URL does not point to the same server, who did render
|
||||
|
|
|
@ -152,23 +152,23 @@ class MathoidCheckerTest extends MediaWikiTestCase {
|
|||
public function provideMathoidSamples() {
|
||||
yield '\ sin x' => [
|
||||
'\sin x',
|
||||
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/sinx.json' ), 200 ),
|
||||
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/mathoid/sinx.json' ), 200 ),
|
||||
[ 'valid' => true, 'checked' => '\sin x' ],
|
||||
];
|
||||
yield 'invalid F' => [
|
||||
'1+\invalid',
|
||||
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/invalidF.json' ), 400 ),
|
||||
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/mathoid/invalidF.json' ), 400 ),
|
||||
[ 'valid' => false, 'checked' => null, 'error' => 'unknown function' ],
|
||||
];
|
||||
yield 'unescaped' => [
|
||||
'1.5%',
|
||||
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/deprecated.json' ),
|
||||
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/mathoid/deprecated.json' ),
|
||||
200 ),
|
||||
[ 'valid' => true, 'checked' => '1.5\%' ],
|
||||
];
|
||||
yield 'syntax error' => [
|
||||
'\left( x',
|
||||
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/syntaxE.json' ), 400 ),
|
||||
$this->makeFakeHttpRequest( file_get_contents( __DIR__ . '/data/mathoid/syntaxE.json' ), 400 ),
|
||||
[ 'valid' => false, 'checked' => null, 'error' => 'Failed to parse' ],
|
||||
];
|
||||
}
|
||||
|
|
57
tests/phpunit/InputCheck/RestbaseCheckerTest.php
Normal file
57
tests/phpunit/InputCheck/RestbaseCheckerTest.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Extension\Math\InputCheck\RestbaseChecker;
|
||||
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
||||
|
||||
/**
|
||||
* @group Math
|
||||
*
|
||||
* @license GPL-2.0-or-later
|
||||
*
|
||||
* @covers \MediaWiki\Extension\Math\InputCheck\RestbaseChecker
|
||||
*/
|
||||
class RestbaseCheckerTest extends MediaWikiTestCase {
|
||||
use MathMockHttpTrait;
|
||||
|
||||
public function testValid() {
|
||||
$this->setupGoodMathRestBaseMockHttp();
|
||||
|
||||
$checker = new RestbaseChecker( '\sin x^2' );
|
||||
$this->assertNull( $checker->getError() );
|
||||
$this->assertTrue( $checker->isValid() );
|
||||
$this->assertNull( $checker->getError() );
|
||||
$this->assertSame( '\\sin x^{2}', $checker->getValidTex() );
|
||||
}
|
||||
|
||||
public function testInvalid() {
|
||||
$this->setupBadMathRestBaseMockHttp();
|
||||
|
||||
$checker = new RestbaseChecker( '\sin\newcommand' );
|
||||
$this->assertNull( $checker->getError() );
|
||||
$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() {
|
||||
$this->setupSyntaxErrorRestBaseMockHttp();
|
||||
|
||||
$checker = new RestbaseChecker( '\left(' );
|
||||
$this->assertFalse( $checker->isValid() );
|
||||
$this->assertStringContainsString(
|
||||
Message::newFromKey( 'math_syntax_error' )
|
||||
->inContentLanguage()
|
||||
->escaped(),
|
||||
$checker->getError()
|
||||
->inContentLanguage()
|
||||
->escaped()
|
||||
);
|
||||
}
|
||||
}
|
9
tests/phpunit/InputCheck/data/restbase/chem.json
Normal file
9
tests/phpunit/InputCheck/data/restbase/chem.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"success": true,
|
||||
"checked": "{\\ce {H2O}}",
|
||||
"requiredPackages": [
|
||||
"mhchem"
|
||||
],
|
||||
"identifiers": [],
|
||||
"endsWithDot": false
|
||||
}
|
33
tests/phpunit/InputCheck/data/restbase/fail.json
Normal file
33
tests/phpunit/InputCheck/data/restbase/fail.json
Normal file
|
@ -0,0 +1,33 @@
|
|||
{
|
||||
"type": "https://mediawiki.org/wiki/HyperSwitch/errors/bad_request",
|
||||
"title": "Bad Request",
|
||||
"method": "POST",
|
||||
"detail": {
|
||||
"error": {
|
||||
"message": "Illegal TeX function",
|
||||
"expected": [],
|
||||
"found": "\\newcommand",
|
||||
"location": {
|
||||
"start": {
|
||||
"offset": 4,
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"offset": 15,
|
||||
"line": 1,
|
||||
"column": 16
|
||||
}
|
||||
},
|
||||
"name": "SyntaxError"
|
||||
},
|
||||
"success": false,
|
||||
"warnings": [],
|
||||
"status": "F",
|
||||
"details": "\\newcommand",
|
||||
"offset": 4,
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"uri": "/texvcinfo"
|
||||
}
|
9
tests/phpunit/InputCheck/data/restbase/sinx.json
Normal file
9
tests/phpunit/InputCheck/data/restbase/sinx.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"success": true,
|
||||
"checked": "\\sin x^{2}",
|
||||
"requiredPackages": [],
|
||||
"identifiers": [
|
||||
"x"
|
||||
],
|
||||
"endsWithDot": false
|
||||
}
|
188
tests/phpunit/InputCheck/data/restbase/syntax_error.json
Normal file
188
tests/phpunit/InputCheck/data/restbase/syntax_error.json
Normal file
|
@ -0,0 +1,188 @@
|
|||
{
|
||||
"type": "https://mediawiki.org/wiki/HyperSwitch/errors/bad_request",
|
||||
"title": "Bad Request",
|
||||
"method": "POST",
|
||||
"detail": {
|
||||
"error": {
|
||||
"message": "Expected \"-\", \"[\", \"\\\\\", \"\\\\begin\", \"\\\\begin{\", \"]\", \"^\", \"_\", \"{\", [ \\t\\n\\r], [%$], [().], [,:;?!'], [/|], [0-9], [><~], [\\-+*=], or [a-zA-Z] but end of input found.",
|
||||
"expected": [
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
" ",
|
||||
"\t",
|
||||
"\n",
|
||||
"\r"
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
[
|
||||
"a",
|
||||
"z"
|
||||
],
|
||||
[
|
||||
"A",
|
||||
"Z"
|
||||
]
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
[
|
||||
"0",
|
||||
"9"
|
||||
]
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
",",
|
||||
":",
|
||||
";",
|
||||
"?",
|
||||
"!",
|
||||
"'"
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "-",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
"-",
|
||||
"+",
|
||||
"*",
|
||||
"="
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "\\",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "\\",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
">",
|
||||
"<",
|
||||
"~"
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
"%",
|
||||
"$"
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
"(",
|
||||
")",
|
||||
"."
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "class",
|
||||
"parts": [
|
||||
"/",
|
||||
"|"
|
||||
],
|
||||
"inverted": false,
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "[",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "\\",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "{",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "\\begin",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "\\begin{",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "_",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "^",
|
||||
"ignoreCase": false
|
||||
},
|
||||
{
|
||||
"type": "literal",
|
||||
"text": "]",
|
||||
"ignoreCase": false
|
||||
}
|
||||
],
|
||||
"found": null,
|
||||
"location": {
|
||||
"start": {
|
||||
"offset": 6,
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"offset": 6,
|
||||
"line": 1,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"name": "SyntaxError"
|
||||
},
|
||||
"success": false,
|
||||
"warnings": [],
|
||||
"status": "S",
|
||||
"details": "SyntaxError: Expected \"-\", \"[\", \"\\\\\", \"\\\\begin\", \"\\\\begin{\", \"]\", \"^\", \"_\", \"{\", [ \\t\\n\\r], [%$], [().], [,:;?!'], [/|], [0-9], [><~], [\\-+*=], or [a-zA-Z] but end of input found.",
|
||||
"offset": 6,
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"uri": "/texvcinfo"
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
use DataValues\NumberValue;
|
||||
use DataValues\StringValue;
|
||||
use MediaWiki\Extension\Math\MathFormatter;
|
||||
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
||||
use Wikibase\Lib\Formatters\SnakFormatter;
|
||||
|
||||
/**
|
||||
|
@ -15,25 +16,9 @@ use Wikibase\Lib\Formatters\SnakFormatter;
|
|||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class MathFormatterTest extends MediaWikiTestCase {
|
||||
use MathMockHttpTrait;
|
||||
|
||||
private const SOME_TEX = 'a^2+b^2 < c^2';
|
||||
|
||||
/** @var bool */
|
||||
protected static $hasRestbase;
|
||||
|
||||
public static function setUpBeforeClass(): void {
|
||||
$rbi = new MathRestbaseInterface();
|
||||
self::$hasRestbase = $rbi->checkBackend( true );
|
||||
}
|
||||
|
||||
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.' );
|
||||
}
|
||||
}
|
||||
private const SOME_TEX = '\sin x^2';
|
||||
|
||||
/**
|
||||
* Checks the
|
||||
|
@ -58,6 +43,8 @@ class MathFormatterTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testUnknownFormatFallsBackToMathMl() {
|
||||
$this->setupGoodMathRestBaseMockHttp( true );
|
||||
|
||||
$formatter = new MathFormatter( 'unknown/unknown' );
|
||||
$value = new StringValue( self::SOME_TEX );
|
||||
$resultFormat = $formatter->format( $value );
|
||||
|
@ -68,8 +55,10 @@ class MathFormatterTest extends MediaWikiTestCase {
|
|||
* @covers \MediaWiki\Extension\Math\MathFormatter::format
|
||||
*/
|
||||
public function testUnknownFormatFailure() {
|
||||
$this->setupBadMathRestBaseMockHttp();
|
||||
|
||||
$formatter = new MathFormatter( 'unknown/unknown' );
|
||||
$value = new StringValue( '\noTex' );
|
||||
$value = new StringValue( '\newcommand' );
|
||||
$resultFormat = $formatter->format( $value );
|
||||
$this->assertStringContainsString( 'unknown function', $resultFormat );
|
||||
}
|
||||
|
@ -82,6 +71,8 @@ class MathFormatterTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testFormatHtml() {
|
||||
$this->setupGoodMathRestBaseMockHttp( true );
|
||||
|
||||
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML );
|
||||
$value = new StringValue( self::SOME_TEX );
|
||||
$resultFormat = $formatter->format( $value );
|
||||
|
@ -89,6 +80,8 @@ class MathFormatterTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testFormatDiffHtml() {
|
||||
$this->setupGoodMathRestBaseMockHttp( true );
|
||||
|
||||
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML_DIFF );
|
||||
$value = new StringValue( self::SOME_TEX );
|
||||
$resultFormat = $formatter->format( $value );
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Extension\Math\InputCheck\RestbaseChecker;
|
||||
|
||||
/**
|
||||
* @group Math
|
||||
*
|
||||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class MathInputCheckRestbaseTest extends MediaWikiTestCase {
|
||||
/** @var bool */
|
||||
protected static $hasRestbase;
|
||||
/** @var RestbaseChecker */
|
||||
protected $BadObject;
|
||||
/** @var RestbaseChecker */
|
||||
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 {
|
||||
$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->BadObject = new RestbaseChecker( '\newcommand{\text{do evil things}}' );
|
||||
$this->GoodObject = new RestbaseChecker( '\sin\left(\frac12x\right)' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Extension\Math\InputCheck\RestbaseChecker::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 \MediaWiki\Extension\Math\InputCheck\RestbaseChecker::getError
|
||||
*/
|
||||
public function testErrorSyntax() {
|
||||
$o = new RestbaseChecker( '\left(' );
|
||||
$this->assertFalse( $o->isValid() );
|
||||
$expectedMessage = wfMessage( 'math_syntax_error' )->inContentLanguage()->escaped();
|
||||
$this->assertStringContainsString( $expectedMessage, $o->getError() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Extension\Math\InputCheck\RestbaseChecker::getError
|
||||
*/
|
||||
public function testErrorLexing() {
|
||||
$o = new RestbaseChecker( "\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 \MediaWiki\Extension\Math\InputCheck\RestbaseChecker::isValid
|
||||
*/
|
||||
public function testIsValid() {
|
||||
$this->assertFalse( $this->BadObject->isValid() );
|
||||
$this->assertTrue( $this->GoodObject->isValid() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Extension\Math\InputCheck\RestbaseChecker::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)' );
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use DataValues\StringValue;
|
||||
use MediaWiki\Extension\Math\MathMLRdfBuilder;
|
||||
use MediaWiki\Extension\Math\MathRestbaseInterface;
|
||||
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
||||
use Wikibase\DataModel\Entity\PropertyId;
|
||||
use Wikibase\DataModel\Snak\PropertyValueSnak;
|
||||
use Wikimedia\Purtle\NTriplesRdfWriter;
|
||||
|
@ -15,26 +15,10 @@ use Wikimedia\Purtle\NTriplesRdfWriter;
|
|||
* @author Moritz Schubotz (physikerwelt)
|
||||
*/
|
||||
class MathMLRdfBuilderTest extends MediaWikiTestCase {
|
||||
use MathMockHttpTrait;
|
||||
|
||||
private const ACME_PREFIX_URL = 'http://acme/';
|
||||
private const ACME_REF = 'testing';
|
||||
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." );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $test
|
||||
|
@ -56,22 +40,26 @@ class MathMLRdfBuilderTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testValidInput() {
|
||||
$triples = $this->makeCase( 'a^2' );
|
||||
$this->setupGoodMathRestBaseMockHttp();
|
||||
|
||||
$triples = $this->makeCase( '\sin x^2' );
|
||||
$this->assertStringContainsString(
|
||||
self::ACME_PREFIX_URL . self::ACME_REF . '> "<math',
|
||||
$triples
|
||||
);
|
||||
$this->assertStringContainsString( '<mi>a</mi>\n', $triples );
|
||||
$this->assertStringContainsString( '<mi>sin</mi>\n', $triples );
|
||||
$this->assertStringContainsString( '<mn>2</mn>\n', $triples );
|
||||
$this->assertStringContainsString( 'a^{2}', $triples );
|
||||
$this->assertStringContainsString( 'x^{2}', $triples );
|
||||
$this->assertStringContainsString( '^^<http://www.w3.org/1998/Math/MathML> .', $triples );
|
||||
}
|
||||
|
||||
public function testInvalidInput() {
|
||||
$triples = $this->makeCase( '\notExists' );
|
||||
$this->setupBadMathRestBaseMockHttp();
|
||||
|
||||
$triples = $this->makeCase( '\sin\newcommand' );
|
||||
$this->assertStringContainsString( '<math', $triples );
|
||||
$this->assertStringContainsString( 'unknown function', $triples );
|
||||
$this->assertStringContainsString( 'notExists', $triples );
|
||||
$this->assertStringContainsString( 'newcommand', $triples );
|
||||
$this->assertStringContainsString( '^^<http://www.w3.org/1998/Math/MathML> .', $triples );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Extension\Math\MathMathML;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
||||
use Wikimedia\TestingAccessWrapper;
|
||||
|
||||
/**
|
||||
|
@ -14,7 +14,7 @@ use Wikimedia\TestingAccessWrapper;
|
|||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class MathMathMLTest extends MediaWikiTestCase {
|
||||
use MockHttpTrait;
|
||||
use MathMockHttpTrait;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
@ -159,16 +159,6 @@ class MathMathMLTest extends MediaWikiTestCase {
|
|||
'test if math expression is invalid mathml sample' );
|
||||
}
|
||||
|
||||
public function testIntegrationTestWithLinks() {
|
||||
$this->markTestSkipped( 'All HTTP requests are banned in tests. See T265628.' );
|
||||
$p = MediaWikiServices::getInstance()->getParserFactory()->create();
|
||||
$po = ParserOptions::newFromAnon();
|
||||
$t = Title::newFromText( __METHOD__ );
|
||||
$res = $p->parse( '[[test|<math forcemathmode="png">a+b</math>]]', $t, $po )->getText();
|
||||
$this->assertStringContainsString( '</a>', $res );
|
||||
$this->assertStringContainsString( 'png', $res );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Extension\Math\MathMathML::correctSvgStyle
|
||||
* @see https://phabricator.wikimedia.org/T132563
|
||||
|
@ -184,12 +174,13 @@ class MathMathMLTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testWarning() {
|
||||
$this->markTestSkipped( 'All HTTP requests are banned in tests. See T265628.' );
|
||||
$this->setupGoodMathRestBaseMockHttp();
|
||||
$this->setMwGlobals( "wgMathDisableTexFilter", 'always' );
|
||||
|
||||
$renderer = new MathMathML();
|
||||
$rbi = $this->getMockBuilder( MathRestbaseInterface::class )
|
||||
->onlyMethods( [ 'getWarnings', 'getSuccess' ] )
|
||||
->setConstructorArgs( [ 'a+b' ] )
|
||||
->setConstructorArgs( [ '\sin x' ] )
|
||||
->getMock();
|
||||
$rbi->method( 'getWarnings' )->willReturn( [ (object)[ 'type' => 'mhchem-deprecation' ] ] );
|
||||
$rbi->method( 'getSuccess' )->willReturn( true );
|
||||
|
|
57
tests/phpunit/MathMockHttpTrait.php
Normal file
57
tests/phpunit/MathMockHttpTrait.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\Math\Tests;
|
||||
|
||||
use MockHttpTrait;
|
||||
|
||||
trait MathMockHttpTrait {
|
||||
use MockHttpTrait;
|
||||
|
||||
protected function setupBadMathRestBaseMockHttp() {
|
||||
$this->installMockHttp(
|
||||
$this->makeFakeHttpMultiClient( [ [
|
||||
'code' => 400,
|
||||
'body' => file_get_contents( __DIR__ . '/InputCheck/data/restbase/fail.json' ),
|
||||
] ] )
|
||||
);
|
||||
}
|
||||
|
||||
protected function setupSyntaxErrorRestBaseMockHttp() {
|
||||
$this->installMockHttp(
|
||||
$this->makeFakeHttpMultiClient( [ [
|
||||
'code' => 400,
|
||||
'body' => file_get_contents( __DIR__ . '/InputCheck/data/restbase/syntax_error.json' ),
|
||||
] ] )
|
||||
);
|
||||
}
|
||||
|
||||
protected function setupGoodMathRestBaseMockHttp( bool $withSvg = false ) {
|
||||
$requests = [
|
||||
$this->makeFakeHttpMultiClient( [ [
|
||||
'code' => 200,
|
||||
'headers' => [
|
||||
'x-resource-location' => 'RESOURCE_LOCATION',
|
||||
],
|
||||
'body' => file_get_contents( __DIR__ . '/InputCheck/data/restbase/sinx.json' ),
|
||||
] ] ),
|
||||
$this->makeFakeHttpMultiClient( [ file_get_contents( __DIR__ . '/data/restbase/sinx.mml' ), ] ),
|
||||
];
|
||||
if ( $withSvg ) {
|
||||
$requests[] = $this->makeFakeHttpMultiClient( [ 'SVGSVSVG', ] );
|
||||
}
|
||||
$this->installMockHttp( $requests );
|
||||
}
|
||||
|
||||
protected function setupGoodChemRestBaseMockHttp() {
|
||||
$this->installMockHttp( [
|
||||
$this->makeFakeHttpMultiClient( [ [
|
||||
'code' => 200,
|
||||
'headers' => [
|
||||
'x-resource-location' => 'RESOURCE_LOCATION',
|
||||
],
|
||||
'body' => file_get_contents( __DIR__ . '/InputCheck/data/restbase/chem.json' ),
|
||||
] ] ),
|
||||
$this->makeFakeHttpMultiClient( [ file_get_contents( __DIR__ . '/data/restbase/h2o.mml' ), ] ),
|
||||
] );
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Extension\Math\MathRenderer;
|
||||
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
||||
|
||||
/**
|
||||
* Test the database access and core functionality of MathRenderer.
|
||||
|
@ -12,28 +13,11 @@ use MediaWiki\Extension\Math\MathRenderer;
|
|||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class MathRendererTest extends MediaWikiTestCase {
|
||||
use MathMockHttpTrait;
|
||||
|
||||
private const SOME_TEX = "a+b";
|
||||
private const TEXVCCHECK_INPUT = '\forall \epsilon \exist \delta';
|
||||
private const TEXVCCHECK_OUTPUT = '\forall \epsilon \exists \delta ';
|
||||
|
||||
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." );
|
||||
}
|
||||
}
|
||||
private const TEXVCCHECK_INPUT = '\sin x^2';
|
||||
private const TEXVCCHECK_OUTPUT = '\sin x^{2}';
|
||||
|
||||
/**
|
||||
* Checks the tex and hash functions
|
||||
|
@ -95,6 +79,8 @@ class MathRendererTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testDisableCheckingAlways() {
|
||||
$this->setupGoodMathRestBaseMockHttp();
|
||||
|
||||
$this->setMwGlobals( "wgMathDisableTexFilter", 'never' );
|
||||
$renderer =
|
||||
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
||||
|
@ -109,7 +95,7 @@ class MathRendererTest extends MediaWikiTestCase {
|
|||
|
||||
/** @var MathRenderer $renderer */
|
||||
$this->assertTrue( $renderer->checkTeX() );
|
||||
// now setTex sould not be called again
|
||||
// now setTex should not be called again
|
||||
$this->assertTrue( $renderer->checkTeX() );
|
||||
}
|
||||
|
||||
|
@ -131,6 +117,8 @@ class MathRendererTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testCheckingNewUnknown() {
|
||||
$this->setupGoodMathRestBaseMockHttp();
|
||||
|
||||
$this->setMwGlobals( "wgMathDisableTexFilter", 'new' );
|
||||
$renderer =
|
||||
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
||||
|
@ -146,11 +134,13 @@ class MathRendererTest extends MediaWikiTestCase {
|
|||
|
||||
/** @var MathRenderer $renderer */
|
||||
$this->assertTrue( $renderer->checkTeX() );
|
||||
// now setTex sould not be called again
|
||||
// now setTex should not be called again
|
||||
$this->assertTrue( $renderer->checkTeX() );
|
||||
}
|
||||
|
||||
public function testCheckingNewKnown() {
|
||||
$this->setupGoodMathRestBaseMockHttp();
|
||||
|
||||
$this->setMwGlobals( "wgMathDisableTexFilter", 'new' );
|
||||
$renderer =
|
||||
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Extension\Math\MathRestbaseInterface;
|
||||
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
||||
|
||||
/**
|
||||
* Test the interface to access Restbase paths
|
||||
|
@ -14,44 +15,23 @@ use MediaWiki\Extension\Math\MathRestbaseInterface;
|
|||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class MathRestbaseInterfaceTest extends MediaWikiTestCase {
|
||||
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." );
|
||||
}
|
||||
}
|
||||
|
||||
public function testConfig() {
|
||||
$rbi = new MathRestbaseInterface();
|
||||
$this->assertTrue( $rbi->checkBackend() );
|
||||
}
|
||||
use MathMockHttpTrait;
|
||||
|
||||
public function testSuccess() {
|
||||
$this->setupGoodMathRestBaseMockHttp();
|
||||
|
||||
$input = '\\sin x^2';
|
||||
$rbi = new MathRestbaseInterface( $input );
|
||||
$this->assertTrue( $rbi->getSuccess(), "Assuming that $input is valid input." );
|
||||
$this->assertEquals( '\\sin x^{2}', $rbi->getCheckedTex() );
|
||||
$this->assertStringContainsString( '<mi>sin</mi>', $rbi->getMathML() );
|
||||
$url = $rbi->getFullSvgUrl();
|
||||
$req = MWHttpRequest::factory( $url );
|
||||
$status = $req->execute();
|
||||
$this->assertTrue( $status->isOK() );
|
||||
$this->assertStringContainsString( '</svg>', $req->getContent() );
|
||||
$this->assertStringContainsString( '/svg/RESOURCE_LOCATION', $rbi->getFullSvgUrl() );
|
||||
$this->assertStringContainsString( '/png/RESOURCE_LOCATION', $rbi->getFullPngUrl() );
|
||||
}
|
||||
|
||||
public function testFail() {
|
||||
$this->setupBadMathRestBaseMockHttp();
|
||||
|
||||
$input = '\\sin\\newcommand';
|
||||
$rbi = new MathRestbaseInterface( $input );
|
||||
$this->assertFalse( $rbi->getSuccess(), "Assuming that $input is invalid input." );
|
||||
|
@ -60,6 +40,8 @@ class MathRestbaseInterfaceTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testChem() {
|
||||
$this->setupGoodChemRestBaseMockHttp();
|
||||
|
||||
$input = '\ce{H2O}';
|
||||
$rbi = new MathRestbaseInterface( $input, 'chem' );
|
||||
$this->assertTrue( $rbi->checkTeX(), "Assuming that $input is valid input." );
|
||||
|
@ -70,7 +52,9 @@ class MathRestbaseInterfaceTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testException() {
|
||||
$input = '\\newcommand';
|
||||
$this->setupBadMathRestBaseMockHttp();
|
||||
|
||||
$input = '\\sin\\newcommand';
|
||||
$rbi = new MathRestbaseInterface( $input );
|
||||
$this->expectException( MWException::class );
|
||||
$this->expectExceptionMessage( 'TeX input is invalid.' );
|
||||
|
@ -78,7 +62,9 @@ class MathRestbaseInterfaceTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testExceptionSvg() {
|
||||
$input = '\\newcommand';
|
||||
$this->setupBadMathRestBaseMockHttp();
|
||||
|
||||
$input = '\\sin\\newcommand';
|
||||
$rbi = new MathRestbaseInterface( $input );
|
||||
$this->expectException( MWException::class );
|
||||
$this->expectExceptionMessage( 'TeX input is invalid.' );
|
||||
|
|
|
@ -31,7 +31,7 @@ class MathValidatorTest extends MediaWikiTestCase {
|
|||
|
||||
public function testValidInput() {
|
||||
$this->installMockHttp( $this->makeFakeHttpRequest( file_get_contents( __DIR__ .
|
||||
'/InputCheck/data/sinx.json' ) ) );
|
||||
'/InputCheck/data/mathoid/sinx.json' ) ) );
|
||||
$validator = new MathValidator();
|
||||
$result = $validator->validate( new StringValue( self::VADLID_TEX ) );
|
||||
$this->assertInstanceOf( \ValueValidators\Result::class, $result );
|
||||
|
@ -40,7 +40,7 @@ class MathValidatorTest extends MediaWikiTestCase {
|
|||
|
||||
public function testInvalidInput() {
|
||||
$this->installMockHttp( $this->makeFakeHttpRequest( file_get_contents( __DIR__ .
|
||||
'/InputCheck/data/invalidF.json' ), 400 ) );
|
||||
'/InputCheck/data/mathoid/invalidF.json' ), 400 ) );
|
||||
$validator = new MathValidator();
|
||||
$result = $validator->validate( new StringValue( self::INVADLID_TEX ) );
|
||||
$this->assertInstanceOf( \ValueValidators\Result::class, $result );
|
||||
|
|
17
tests/phpunit/data/restbase/h2o.mml
Normal file
17
tests/phpunit/data/restbase/h2o.mml
Normal file
|
@ -0,0 +1,17 @@
|
|||
<math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\ce {H2O}}">
|
||||
<semantics>
|
||||
<mrow class="MJX-TeXAtom-ORD">
|
||||
<msubsup>
|
||||
<mtext>H</mtext>
|
||||
<mrow class="MJX-TeXAtom-ORD">
|
||||
<mn>2</mn>
|
||||
</mrow>
|
||||
<mrow class="MJX-TeXAtom-ORD">
|
||||
<mspace width="0pt" height="0pt" depth=".2em" />
|
||||
</mrow>
|
||||
</msubsup>
|
||||
<mtext>O</mtext>
|
||||
</mrow>
|
||||
<annotation encoding="application/x-tex">{\ce {H2O}}</annotation>
|
||||
</semantics>
|
||||
</math>
|
18
tests/phpunit/data/restbase/sinx.mml
Normal file
18
tests/phpunit/data/restbase/sinx.mml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block" alttext="\sin x^{2}">
|
||||
<semantics>
|
||||
<mrow>
|
||||
<mi>sin</mi>
|
||||
<mo>
|
||||
⁡
|
||||
<!-- -->
|
||||
</mo>
|
||||
<msup>
|
||||
<mi>x</mi>
|
||||
<mrow class="MJX-TeXAtom-ORD">
|
||||
<mn>2</mn>
|
||||
</mrow>
|
||||
</msup>
|
||||
</mrow>
|
||||
<annotation encoding="application/x-tex">\sin x^{2}</annotation>
|
||||
</semantics>
|
||||
</math>
|
|
@ -2,41 +2,18 @@
|
|||
|
||||
use MediaWiki\Extension\Math\InputCheck\InputCheckFactory;
|
||||
use MediaWiki\Extension\Math\InputCheck\MathoidChecker;
|
||||
use MediaWiki\Tests\Unit\MockServiceDependenciesTrait;
|
||||
|
||||
/**
|
||||
* @method InputCheckFactory newServiceInstance( string $serviceClass, array $parameterOverrides )
|
||||
* @covers \MediaWiki\Extension\Math\InputCheck\InputCheckFactory
|
||||
*/
|
||||
class InputCheckFactoryTest extends MediaWikiUnitTestCase {
|
||||
use FactoryArgTestTrait;
|
||||
use MockServiceDependenciesTrait;
|
||||
|
||||
protected static function getFactoryClass() {
|
||||
return InputCheckFactory::class;
|
||||
}
|
||||
|
||||
protected static function getInstanceClass() {
|
||||
return MathoidChecker::class;
|
||||
}
|
||||
|
||||
protected static function getExtraClassArgCount() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is required since the FactoryArgTestTrait uses the full classname.
|
||||
* Testing without overwriting this function would result in
|
||||
*
|
||||
* ReflectionException: Method MediaWiki\Extension\Math\InputCheck\InputCheckFactory::
|
||||
* newMediaWiki\Extension\Math\InputCheck\MathoidChecker() does not exist
|
||||
*
|
||||
* see T253613
|
||||
* @return string
|
||||
*/
|
||||
protected function getFactoryMethodName() {
|
||||
return 'new' . ( new \ReflectionClass( MathoidChecker::class ) )->getShortName();
|
||||
}
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->markTestSkipped( 'FactoryArgTestTrait can not yet handle the parameter numbers.' );
|
||||
public function testNewMathoidChecker() {
|
||||
$checker = $this->newServiceInstance( InputCheckFactory::class, [] )
|
||||
->newMathoidChecker( 'FORMULA', 'TYPE' );
|
||||
$this->assertInstanceOf( MathoidChecker::class, $checker );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue