| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Tests; |
| 4 |
|
| 5 |
use MediaWiki\Config\ServiceOptions; |
| 6 |
use MediaWiki\Extension\Math\MathConfig; |
| 7 |
use MediaWiki\Registration\ExtensionRegistry; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class MathConfigTest extends TestCase { |
| 14 |
|
| 15 |
private const TEST_DEFAULT = 'test-default'; |
| 16 |
private const DUMMY_URL = 'https://example.com/api.php'; |
| 17 |
|
| 18 |
private function newMathConfig( |
| 19 |
array $configOverrides, |
| 20 |
?ExtensionRegistry $registry = null |
| 21 |
): MathConfig { |
| 22 |
return new MathConfig( |
| 23 |
new ServiceOptions( MathConfig::CONSTRUCTOR_OPTIONS, $configOverrides + [ |
| 24 |
'MathDisableTexFilter' => MathConfig::ALWAYS, |
| 25 |
'MathValidModes' => [ MathConfig::MODE_SOURCE ], |
| 26 |
'MathEntitySelectorFallbackUrl' => self::DUMMY_URL, |
| 27 |
] ), |
| 28 |
$registry ?? ExtensionRegistry::getInstance() |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
public static function provideTexCheckDisabled() { |
| 33 |
yield 'always' => [ 'always', MathConfig::ALWAYS ]; |
| 34 |
yield 'never' => [ 'never', MathConfig::NEVER ]; |
| 35 |
yield 'new' => [ 'new', MathConfig::NEW ]; |
| 36 |
yield 'true' => [ true, MathConfig::NEVER ]; |
| 37 |
yield 'false' => [ false, MathConfig::ALWAYS ]; |
| 38 |
yield 'garbage' => [ 'garbage', MathConfig::ALWAYS ]; |
| 39 |
yield 'wrong case' => [ 'NEVER', MathConfig::NEVER ]; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function testTexCheckDisabled( $configValue, string $expected ) { |
| 46 |
$mathConfig = $this->newMathConfig( [ 'MathDisableTexFilter' => $configValue ] ); |
| 47 |
$this->assertSame( $expected, $mathConfig->texCheckDisabled() ); |
| 48 |
} |
| 49 |
|
| 50 |
public static function provideNormalizeRenderingMode() { |
| 51 |
yield 'legacy user option' => [ 1, self::TEST_DEFAULT ]; |
| 52 |
yield 'source user option' => [ 3, MathConfig::MODE_SOURCE ]; |
| 53 |
yield 'mathml user option' => [ 5, MathConfig::MODE_MATHML ]; |
| 54 |
yield 'latexml user option' => [ 7, MathConfig::MODE_LATEXML ]; |
| 55 |
yield 'native user option' => [ 8, MathConfig::MODE_NATIVE_MML ]; |
| 56 |
yield 'mathjax option' => [ 9, MathConfig::MODE_NATIVE_JAX ]; |
| 57 |
|
| 58 |
yield 'source string' => [ 'source', MathConfig::MODE_SOURCE ]; |
| 59 |
yield 'mathml string' => [ 'mathml', MathConfig::MODE_MATHML ]; |
| 60 |
yield 'latexml string' => [ 'latexml', MathConfig::MODE_LATEXML ]; |
| 61 |
yield 'native string' => [ 'native', MathConfig::MODE_NATIVE_MML ]; |
| 62 |
yield 'mathjax string' => [ 'mathjax', MathConfig::MODE_NATIVE_JAX ]; |
| 63 |
yield 'wrong capitalization' => [ 'LaTeXmL', MathConfig::MODE_LATEXML ]; |
| 64 |
yield 'unrecognized' => [ 'garbage', self::TEST_DEFAULT ]; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function testNormalizeRenderingMode( $input, string $expected ) { |
| 71 |
$this->assertSame( $expected, MathConfig::normalizeRenderingMode( $input, self::TEST_DEFAULT ) ); |
| 72 |
} |
| 73 |
|
| 74 |
public function testGetValidRenderingModes() { |
| 75 |
$mathConfig = $this->newMathConfig( [ |
| 76 |
'MathValidModes' => [ |
| 77 |
MathConfig::MODE_NATIVE_MML, |
| 78 |
MathConfig::MODE_SOURCE, |
| 79 |
'this will be converted to mathml' ], |
| 80 |
] ); |
| 81 |
$actualModes = $mathConfig->getValidRenderingModes(); |
| 82 |
$this->assertCount( 2, $actualModes ); |
| 83 |
$this->assertContains( MathConfig::MODE_NATIVE_MML, $actualModes ); |
| 84 |
$this->assertContains( MathConfig::MODE_SOURCE, $actualModes ); |
| 85 |
} |
| 86 |
|
| 87 |
public static function provideIsValidRenderingMode() { |
| 88 |
yield 'valid' => [ MathConfig::MODE_MATHML, true ]; |
| 89 |
yield 'garbage' => [ 'garbage', false ]; |
| 90 |
yield 'does not normalize' => [ 0, false ]; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function testIsValidRenderingMode( $mode, $expected ) { |
| 97 |
$mathConfig = $this->newMathConfig( [ |
| 98 |
'MathValidModes' => [ MathConfig::MODE_MATHML ], |
| 99 |
] ); |
| 100 |
$this->assertSame( $expected, $mathConfig->isValidRenderingMode( $mode ) ); |
| 101 |
} |
| 102 |
|
| 103 |
public function testGetValidRenderingModeKeys() { |
| 104 |
$mathConfig = $this->newMathConfig( [ |
| 105 |
'MathValidModes' => [ MathConfig::MODE_MATHML ], |
| 106 |
] ); |
| 107 |
$this->assertEquals( |
| 108 |
[ 'mathml' => 'mw-math-mathml' ], |
| 109 |
$mathConfig->getValidRenderingModeKeys() ); |
| 110 |
} |
| 111 |
|
| 112 |
public function testEntitySelectorUrlWikibase() { |
| 113 |
$mathConfig = $this->newMathConfig( [] ); |
| 114 |
if ( ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ) ) { |
| 115 |
$localUrl = $mathConfig->getMathEntitySelectorUrl(); |
| 116 |
$this->assertStringContainsString( 'api.php', $localUrl ); |
| 117 |
$this->assertStringNotContainsString( 'example.com', $localUrl ); |
| 118 |
} else { |
| 119 |
$this->markTestSkipped( 'Wikibase Client is not loaded partially skipping test.' ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public function testEntitySelectorUrlFallback() { |
| 124 |
$mathConfig = $this->newMathConfig( [], new class extends ExtensionRegistry { |
| 125 |
public function isLoaded( $name, $constraint = '*' ): bool { |
| 126 |
return false; |
| 127 |
} |
| 128 |
} ); |
| 129 |
$url = $mathConfig->getMathEntitySelectorUrl(); |
| 130 |
$this->assertEquals( self::DUMMY_URL, $url ); |
| 131 |
} |
| 132 |
|
| 133 |
} |