2021-08-11 18:15:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math\Tests;
|
|
|
|
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
|
|
|
use MediaWiki\Extension\Math\MathConfig;
|
2024-10-20 07:27:34 +00:00
|
|
|
use MediaWiki\Registration\ExtensionRegistry;
|
2022-07-17 09:59:09 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-08-11 18:15:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\MathConfig
|
|
|
|
*/
|
2022-07-17 09:59:09 +00:00
|
|
|
class MathConfigTest extends TestCase {
|
2021-08-11 18:15:35 +00:00
|
|
|
|
|
|
|
private const TEST_DEFAULT = 'test-default';
|
2022-07-17 09:59:09 +00:00
|
|
|
private const DUMMY_URL = 'https://example.com/api.php';
|
2021-08-11 18:15:35 +00:00
|
|
|
|
2022-07-17 09:59:09 +00:00
|
|
|
private function newMathConfig(
|
|
|
|
array $configOverrides,
|
|
|
|
ExtensionRegistry $registry = null
|
|
|
|
): MathConfig {
|
2021-08-11 18:15:35 +00:00
|
|
|
return new MathConfig(
|
|
|
|
new ServiceOptions( MathConfig::CONSTRUCTOR_OPTIONS, $configOverrides + [
|
|
|
|
'MathDisableTexFilter' => MathConfig::ALWAYS,
|
|
|
|
'MathValidModes' => [ MathConfig::MODE_SOURCE ],
|
2022-07-17 09:59:09 +00:00
|
|
|
'MathEntitySelectorFallbackUrl' => self::DUMMY_URL,
|
|
|
|
] ),
|
2022-11-22 11:29:44 +00:00
|
|
|
$registry ?? ExtensionRegistry::getInstance()
|
2021-08-11 18:15:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-20 22:26:33 +00:00
|
|
|
public static function provideTexCheckDisabled() {
|
2021-08-11 18:15:35 +00:00
|
|
|
yield 'always' => [ 'always', MathConfig::ALWAYS ];
|
|
|
|
yield 'never' => [ 'never', MathConfig::NEVER ];
|
|
|
|
yield 'new' => [ 'new', MathConfig::NEW ];
|
|
|
|
yield 'true' => [ true, MathConfig::NEVER ];
|
|
|
|
yield 'false' => [ false, MathConfig::ALWAYS ];
|
|
|
|
yield 'garbage' => [ 'garbage', MathConfig::ALWAYS ];
|
|
|
|
yield 'wrong case' => [ 'NEVER', MathConfig::NEVER ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTexCheckDisabled
|
|
|
|
*/
|
|
|
|
public function testTexCheckDisabled( $configValue, string $expected ) {
|
|
|
|
$mathConfig = $this->newMathConfig( [ 'MathDisableTexFilter' => $configValue ] );
|
|
|
|
$this->assertSame( $expected, $mathConfig->texCheckDisabled() );
|
|
|
|
}
|
|
|
|
|
2023-05-20 22:26:33 +00:00
|
|
|
public static function provideNormalizeRenderingMode() {
|
2021-08-11 18:15:35 +00:00
|
|
|
yield 'legacy user option' => [ 1, self::TEST_DEFAULT ];
|
|
|
|
yield 'source user option' => [ 3, MathConfig::MODE_SOURCE ];
|
|
|
|
yield 'mathml user option' => [ 5, MathConfig::MODE_MATHML ];
|
|
|
|
yield 'latexml user option' => [ 7, MathConfig::MODE_LATEXML ];
|
2023-02-28 19:20:34 +00:00
|
|
|
yield 'native user option' => [ 8, MathConfig::MODE_NATIVE_MML ];
|
2024-08-30 17:35:39 +00:00
|
|
|
yield 'mathjax option' => [ 9, MathConfig::MODE_NATIVE_JAX ];
|
|
|
|
|
2021-08-11 18:15:35 +00:00
|
|
|
yield 'source string' => [ 'source', MathConfig::MODE_SOURCE ];
|
|
|
|
yield 'mathml string' => [ 'mathml', MathConfig::MODE_MATHML ];
|
|
|
|
yield 'latexml string' => [ 'latexml', MathConfig::MODE_LATEXML ];
|
2023-02-28 19:20:34 +00:00
|
|
|
yield 'native string' => [ 'native', MathConfig::MODE_NATIVE_MML ];
|
2024-01-02 12:10:40 +00:00
|
|
|
yield 'mathjax string' => [ 'mathjax', MathConfig::MODE_NATIVE_JAX ];
|
|
|
|
yield 'wrong capitalization' => [ 'LaTeXmL', MathConfig::MODE_LATEXML ];
|
2021-08-11 18:15:35 +00:00
|
|
|
yield 'unrecognized' => [ 'garbage', self::TEST_DEFAULT ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideNormalizeRenderingMode
|
|
|
|
*/
|
|
|
|
public function testNormalizeRenderingMode( $input, string $expected ) {
|
|
|
|
$this->assertSame( $expected, MathConfig::normalizeRenderingMode( $input, self::TEST_DEFAULT ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetValidRenderingModes() {
|
|
|
|
$mathConfig = $this->newMathConfig( [
|
2022-11-18 22:50:15 +00:00
|
|
|
'MathValidModes' => [
|
2024-08-30 17:35:39 +00:00
|
|
|
MathConfig::MODE_NATIVE_MML,
|
2022-11-18 22:50:15 +00:00
|
|
|
MathConfig::MODE_SOURCE,
|
|
|
|
'this will be converted to mathml' ],
|
2021-08-11 18:15:35 +00:00
|
|
|
] );
|
2022-07-17 09:59:09 +00:00
|
|
|
$actualModes = $mathConfig->getValidRenderingModes();
|
|
|
|
$this->assertCount( 2, $actualModes );
|
2024-08-30 17:35:39 +00:00
|
|
|
$this->assertContains( MathConfig::MODE_NATIVE_MML, $actualModes );
|
2022-11-18 22:50:15 +00:00
|
|
|
$this->assertContains( MathConfig::MODE_SOURCE, $actualModes );
|
2021-08-11 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 22:26:33 +00:00
|
|
|
public static function provideIsValidRenderingMode() {
|
2021-08-11 18:15:35 +00:00
|
|
|
yield 'valid' => [ MathConfig::MODE_MATHML, true ];
|
|
|
|
yield 'garbage' => [ 'garbage', false ];
|
|
|
|
yield 'does not normalize' => [ 0, false ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideIsValidRenderingMode
|
|
|
|
*/
|
|
|
|
public function testIsValidRenderingMode( $mode, $expected ) {
|
|
|
|
$mathConfig = $this->newMathConfig( [
|
2022-11-18 22:50:15 +00:00
|
|
|
'MathValidModes' => [ MathConfig::MODE_MATHML ],
|
2021-08-11 18:15:35 +00:00
|
|
|
] );
|
|
|
|
$this->assertSame( $expected, $mathConfig->isValidRenderingMode( $mode ) );
|
|
|
|
}
|
2021-08-12 00:59:21 +00:00
|
|
|
|
|
|
|
public function testGetValidRenderingModeKeys() {
|
|
|
|
$mathConfig = $this->newMathConfig( [
|
2022-11-18 22:50:15 +00:00
|
|
|
'MathValidModes' => [ MathConfig::MODE_MATHML ],
|
2021-08-12 00:59:21 +00:00
|
|
|
] );
|
2022-07-17 09:59:09 +00:00
|
|
|
$this->assertEquals(
|
2024-03-15 09:02:10 +00:00
|
|
|
[ 'mathml' => 'mw-math-mathml' ],
|
2022-07-17 09:59:09 +00:00
|
|
|
$mathConfig->getValidRenderingModeKeys() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEntitySelectorUrlWikibase() {
|
|
|
|
$mathConfig = $this->newMathConfig( [] );
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ) ) {
|
|
|
|
$localUrl = $mathConfig->getMathEntitySelectorUrl();
|
|
|
|
$this->assertStringContainsString( 'api.php', $localUrl );
|
|
|
|
$this->assertStringNotContainsString( 'example.com', $localUrl );
|
|
|
|
} else {
|
|
|
|
$this->markTestSkipped( 'Wikibase Client is not loaded partially skipping test.' );
|
|
|
|
}
|
2021-08-12 00:59:21 +00:00
|
|
|
}
|
2022-07-17 09:59:09 +00:00
|
|
|
|
|
|
|
public function testEntitySelectorUrlFallback() {
|
|
|
|
$mathConfig = $this->newMathConfig( [], new class extends ExtensionRegistry {
|
|
|
|
public function isLoaded( $name, $constraint = '*' ): bool {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
$url = $mathConfig->getMathEntitySelectorUrl();
|
|
|
|
$this->assertEquals( self::DUMMY_URL, $url );
|
|
|
|
}
|
|
|
|
|
2021-08-11 18:15:35 +00:00
|
|
|
}
|