2021-08-10 14:28:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math\Tests;
|
|
|
|
|
2022-06-21 09:23:05 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2021-08-10 14:28:00 +00:00
|
|
|
use MediaWiki\Extension\Math\MathConfig;
|
|
|
|
use MediaWiki\Extension\Math\MathRenderer;
|
|
|
|
use MediaWiki\Extension\Math\Render\RendererFactory;
|
|
|
|
use MediaWiki\Page\PageReferenceValue;
|
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
use ParserOptions;
|
2022-06-21 09:23:05 +00:00
|
|
|
use Psr\Log\NullLogger;
|
2021-08-10 14:28:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Database
|
|
|
|
* @covers \MediaWiki\Extension\Math\HookHandlers\ParserHooksHandler
|
|
|
|
* @coversDefaultClass \MediaWiki\Extension\Math\HookHandlers\ParserHooksHandler
|
|
|
|
*/
|
|
|
|
class ParserIntegrationTests extends MediaWikiIntegrationTestCase {
|
|
|
|
|
|
|
|
private function setupDummyRendering() {
|
2022-11-18 22:50:15 +00:00
|
|
|
$this->setMwGlobals( 'wgMathValidModes', [ MathConfig::MODE_SOURCE, MathConfig::MODE_LATEXML ] );
|
2021-08-10 14:28:00 +00:00
|
|
|
$this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'math' => MathConfig::MODE_SOURCE ] );
|
2022-06-21 09:23:05 +00:00
|
|
|
$this->setService( 'Math.RendererFactory', new class(
|
|
|
|
new ServiceOptions( RendererFactory::CONSTRUCTOR_OPTIONS, [
|
|
|
|
'MathoidCli' => false,
|
|
|
|
'MathEnableExperimentalInputFormats' => false,
|
2022-11-18 22:50:15 +00:00
|
|
|
'MathValidModes' => [ MathConfig::MODE_SOURCE ],
|
2022-06-21 09:23:05 +00:00
|
|
|
] ),
|
|
|
|
$this->getServiceContainer()->getUserOptionsLookup(),
|
|
|
|
new NullLogger()
|
|
|
|
) extends RendererFactory {
|
|
|
|
public function getRenderer(
|
|
|
|
string $tex,
|
|
|
|
array $params = [],
|
2022-11-18 22:50:15 +00:00
|
|
|
string $mode = MathConfig::MODE_MATHML
|
2022-06-21 09:23:05 +00:00
|
|
|
): MathRenderer {
|
|
|
|
return new class( $mode, $tex, $params ) extends MathRenderer {
|
|
|
|
public function __construct( $mode, $tex = '', $params = [] ) {
|
|
|
|
parent::__construct( $tex, $params );
|
|
|
|
$this->mode = $mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkTeX() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-01 14:30:18 +00:00
|
|
|
public function getHtmlOutput( bool $svg = true ): string {
|
2022-06-21 09:23:05 +00:00
|
|
|
return "<render>$this->mode:$this->tex</render>";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMathTableName() {
|
|
|
|
return 'whatever';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} );
|
2021-08-10 14:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::onParserOptionsRegister
|
|
|
|
*/
|
|
|
|
public function testMathParserOption() {
|
|
|
|
$user = $this->getTestUser()->getUserIdentity();
|
|
|
|
$defaultMode = $this->getServiceContainer()->getUserOptionsLookup()->getOption( $user, 'math' );
|
|
|
|
$this->assertSame( $defaultMode, ParserOptions::newFromUser( $user )->getOption( 'math' ) );
|
|
|
|
// 3 corresponds to 'source', see Hooks::mathModeToString
|
|
|
|
$this->getServiceContainer()->getUserOptionsManager()->setOption( $user, 'math', 3 );
|
|
|
|
$this->assertSame( MathConfig::MODE_SOURCE, ParserOptions::newFromUser( $user )->getOption( 'math' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testParserCacheIntegration() {
|
|
|
|
$this->setupDummyRendering();
|
|
|
|
|
|
|
|
$page = $this->getExistingTestPage( __METHOD__ );
|
2023-09-28 08:39:21 +00:00
|
|
|
$this->assertStatusGood(
|
|
|
|
$this->editPage( $page, '<math>TEST_FORMULA</math>' ),
|
2021-08-10 14:28:00 +00:00
|
|
|
'Sanity: edited page'
|
|
|
|
);
|
|
|
|
|
|
|
|
$parserOutputAccess = $this->getServiceContainer()->getParserOutputAccess();
|
|
|
|
|
|
|
|
// source was set as a default, so the rendering will be shared with
|
|
|
|
// canonical rendering produced by page edit
|
2022-06-21 09:23:05 +00:00
|
|
|
$parserOptions1 = ParserOptions::newCanonical( 'canonical' );
|
2021-08-10 14:28:00 +00:00
|
|
|
$parserOptions1->setOption( 'math', MathConfig::MODE_SOURCE );
|
|
|
|
$render = $parserOutputAccess->getCachedParserOutput( $page, $parserOptions1 );
|
|
|
|
$this->assertNotNull( $render );
|
|
|
|
$this->assertStringContainsString( "<render>source:TEST_FORMULA</render>", $render->getText() );
|
|
|
|
|
2023-10-29 14:19:12 +00:00
|
|
|
// Now render with 'mathml' and make sure we didn't get the cached output
|
2022-06-21 09:23:05 +00:00
|
|
|
$parserOptions2 = ParserOptions::newCanonical( 'canonical' );
|
2022-11-18 22:50:15 +00:00
|
|
|
$parserOptions2->setOption( 'math', MathConfig::MODE_MATHML );
|
2021-08-10 14:28:00 +00:00
|
|
|
$this->assertNull( $parserOutputAccess->getCachedParserOutput( $page, $parserOptions2 ) );
|
|
|
|
$renderStatus = $parserOutputAccess->getParserOutput( $page, $parserOptions2 );
|
2023-09-28 08:39:21 +00:00
|
|
|
$this->assertStatusGood( $renderStatus );
|
2021-08-10 14:28:00 +00:00
|
|
|
$this->assertStringContainsString(
|
2023-10-29 14:19:12 +00:00
|
|
|
"<render>mathml:TEST_FORMULA</render>",
|
2021-08-10 14:28:00 +00:00
|
|
|
$renderStatus->getValue()->getText()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Fetch from cache with source
|
|
|
|
$cachedWithDummy1 = $parserOutputAccess->getCachedParserOutput( $page, $parserOptions1 );
|
|
|
|
$this->assertNotNull( $cachedWithDummy1 );
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
"<render>source:TEST_FORMULA</render>",
|
|
|
|
$cachedWithDummy1->getText()
|
|
|
|
);
|
|
|
|
|
2023-10-29 14:19:12 +00:00
|
|
|
// Fetch from cache with mathml
|
2021-08-10 14:28:00 +00:00
|
|
|
$cachedWithDummy2 = $parserOutputAccess->getCachedParserOutput( $page, $parserOptions2 );
|
|
|
|
$this->assertNotNull( $cachedWithDummy2 );
|
|
|
|
$this->assertStringContainsString(
|
2023-10-29 14:19:12 +00:00
|
|
|
"<render>mathml:TEST_FORMULA</render>",
|
2021-08-10 14:28:00 +00:00
|
|
|
$cachedWithDummy2->getText()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMathInLink() {
|
|
|
|
$this->setupDummyRendering();
|
|
|
|
$po = ParserOptions::newFromAnon();
|
2022-11-18 22:50:15 +00:00
|
|
|
$po->setOption( 'math', MathConfig::MODE_SOURCE );
|
2021-08-10 14:28:00 +00:00
|
|
|
$res = $this->getServiceContainer()
|
|
|
|
->getParser()
|
|
|
|
->parse(
|
|
|
|
'[[test|<math>formula</math>]]',
|
|
|
|
PageReferenceValue::localReference( NS_MAIN, __METHOD__ ),
|
|
|
|
$po
|
|
|
|
)
|
|
|
|
->getText();
|
2023-10-29 14:19:12 +00:00
|
|
|
$this->assertStringMatchesFormat( '%A<a%S><render>mathml:formula</render></a>%A', $res );
|
2021-08-10 14:28:00 +00:00
|
|
|
}
|
|
|
|
}
|