| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\InputCheck; |
| 4 |
|
| 5 |
use MediaWiki\Message\Message; |
| 6 |
use MediaWikiIntegrationTestCase; |
| 7 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 8 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class LocalCheckerTest extends MediaWikiIntegrationTestCase { |
| 17 |
|
| 18 |
private const SAMPLE_KEY = |
| 19 |
'global:MediaWiki\Extension\Math\InputCheck\LocalChecker:d5f40adbd26ff8b19b2c33289d7334b6'; |
| 20 |
|
| 21 |
public function testValid() { |
| 22 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2' ); |
| 23 |
$this->assertNull( $checker->getError() ); |
| 24 |
$this->assertTrue( $checker->isValid() ); |
| 25 |
$this->assertNull( $checker->getError() ); |
| 26 |
$this->assertSame( '\\sin x^{2}', $checker->getValidTex() ); |
| 27 |
} |
| 28 |
|
| 29 |
public function testValidTypeTex() { |
| 30 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2', 'tex' ); |
| 31 |
$this->assertTrue( $checker->isValid() ); |
| 32 |
} |
| 33 |
|
| 34 |
public function testValidTypeChem() { |
| 35 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '{\\displaystyle {\\ce {\\cdot OHNO_{2}}}}', 'chem' ); |
| 36 |
$this->assertTrue( $checker->isValid() ); |
| 37 |
} |
| 38 |
|
| 39 |
public function testValidTypeInline() { |
| 40 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '{\\textstyle \\log2 }', 'inline-tex' ); |
| 41 |
$this->assertTrue( $checker->isValid() ); |
| 42 |
} |
| 43 |
|
| 44 |
public function testInvalidType() { |
| 45 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2', 'INVALIDTYPE' ); |
| 46 |
$this->assertInstanceOf( LocalChecker::class, $checker ); |
| 47 |
$this->assertInstanceOf( Message::class, $checker->getError() ); |
| 48 |
$this->assertFalse( $checker->isValid() ); |
| 49 |
$this->assertNull( $checker->getPresentationMathMLFragment() ); |
| 50 |
} |
| 51 |
|
| 52 |
public function testInvalid() { |
| 53 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin\newcommand' ); |
| 54 |
$this->assertFalse( $checker->isValid() ); |
| 55 |
|
| 56 |
$this->assertStringContainsString( |
| 57 |
Message::newFromKey( 'math_unknown_function', '\newcommand' ) |
| 58 |
->inContentLanguage() |
| 59 |
->escaped(), |
| 60 |
$checker->getError() |
| 61 |
->inContentLanguage() |
| 62 |
->escaped() |
| 63 |
); |
| 64 |
|
| 65 |
$this->assertNull( $checker->getValidTex() ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testErrorSyntax() { |
| 69 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\left(' ); |
| 70 |
$this->assertFalse( $checker->isValid() ); |
| 71 |
$this->assertStringContainsString( |
| 72 |
Message::newFromKey( 'math_syntax_error' ) |
| 73 |
->inContentLanguage() |
| 74 |
->escaped(), |
| 75 |
$checker->getError() |
| 76 |
->inContentLanguage() |
| 77 |
->escaped() |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
public function testGetMML() { |
| 82 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), 'e^{i \pi} + 1 = 0' ); |
| 83 |
$mml = $checker->getPresentationMathMLFragment(); |
| 84 |
$this->assertStringContainsString( '<mn>0</mn>', $mml ); |
| 85 |
} |
| 86 |
|
| 87 |
public function testGetMMLEmpty() { |
| 88 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '' ); |
| 89 |
$mml = $checker->getPresentationMathMLFragment(); |
| 90 |
$this->assertSame( '', $mml ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function testGetKey() { |
| 97 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2', 'tex' ); |
| 98 |
$this->assertSame( self::SAMPLE_KEY, $checker->getInputCacheKey() ); |
| 99 |
} |
| 100 |
|
| 101 |
public function testCache() { |
| 102 |
$fakeWAN = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); |
| 103 |
$fakeContent = [ 'status' => '+', 'output' => 'out', 'mathml' => 'mml' ]; |
| 104 |
$fakeWAN->set( self::SAMPLE_KEY, |
| 105 |
$fakeContent, |
| 106 |
WANObjectCache::TTL_INDEFINITE, |
| 107 |
[ 'version' => LocalChecker::VERSION ] ); |
| 108 |
$checker = new LocalChecker( $fakeWAN, '\sin x^2', 'tex' ); |
| 109 |
$this->assertSame( $fakeContent['output'], $checker->getValidTex() ); |
| 110 |
$this->assertSame( $fakeContent['mathml'], $checker->getPresentationMathMLFragment() ); |
| 111 |
$this->assertSame( true, $checker->isValid() ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function testRunChecks() { |
| 118 |
$fakeContent = [ |
| 119 |
'status' => '+', |
| 120 |
'mathml' => '<mi>sin</mi><mo>⁡</mo><msup><mi>x</mi>' |
| 121 |
. '<mrow data-mjx-texclass="ORD"><mn>2</mn></mrow></msup>', |
| 122 |
'output' => '\\sin x^{2}' |
| 123 |
]; |
| 124 |
$checker = new LocalChecker( WANObjectCache::newEmpty(), '\sin x^2', 'tex' ); |
| 125 |
$actual = $checker->runCheck(); |
| 126 |
$this->assertArrayEquals( $fakeContent, $actual ); |
| 127 |
} |
| 128 |
} |