Debian Codegraph @

extensions/Math/tests/phpunit/MathParserIntegrationTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\ServiceOptions;
4 use MediaWiki\Extension\Math\MathConfig;
5 use MediaWiki\Extension\Math\MathRenderer;
6 use MediaWiki\Extension\Math\Render\RendererFactory;
7 use MediaWiki\Page\PageReferenceValue;
8 use MediaWiki\Parser\ParserOptions;
9 use Psr\Log\NullLogger;
10 use Wikimedia\ObjectCache\WANObjectCache;
11
12 /**
13 * @group Database
14 * @covers \MediaWiki\Extension\Math\HookHandlers\ParserHooksHandler
15 * @coversDefaultClass \MediaWiki\Extension\Math\HookHandlers\ParserHooksHandler
16 */
17 class MathParserIntegrationTest extends MediaWikiIntegrationTestCase {
18
19 private function setupDummyRendering() {
20 $this->overrideConfigValue( 'MathValidModes', [ MathConfig::MODE_SOURCE, MathConfig::MODE_LATEXML ] );
21 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'math' => MathConfig::MODE_SOURCE ] );
22 $this->setService( 'Math.RendererFactory', new class(
23 new ServiceOptions( RendererFactory::CONSTRUCTOR_OPTIONS, [
24 'MathoidCli' => false,
25 'MathEnableExperimentalInputFormats' => false,
26 'MathValidModes' => [ MathConfig::MODE_SOURCE ],
27 ] ),
28 $this->createNoOpMock( MathConfig::class ),
29 $this->getServiceContainer()->getUserOptionsLookup(),
30 new NullLogger(),
31 WANObjectCache::newEmpty(),
32 ) extends RendererFactory {
33 public function getRenderer(
34 string $tex,
35 array $params = [],
36 string $mode = MathConfig::MODE_LATEXML
37 ): MathRenderer {
38 return new class( $mode, $tex, $params ) extends MathRenderer {
39 public function __construct( $mode, $tex = '', $params = [] ) {
40 parent::__construct( $tex, $params );
41 $this->mode = $mode;
42 }
43
44 public function render() {
45 return true;
46 }
47
48 public function checkTeX() {
49 return true;
50 }
51
52 public function getHtmlOutput( bool $svg = true ): string {
53 return "<render>$this->mode:$this->tex</render>";
54 }
55
56 protected function getMathTableName() {
57 return 'whatever';
58 }
59 };
60 }
61 } );
62 }
63
64 /**
65 * @covers ::onParserOptionsRegister
66 */
67 public function testMathParserOption() {
68 $user = $this->getTestUser()->getUserIdentity();
69 $defaultMode = $this->getServiceContainer()->getUserOptionsLookup()->getOption( $user, 'math' );
70 $this->assertSame( $defaultMode, ParserOptions::newFromUser( $user )->getOption( 'math' ) );
71 // 3 corresponds to 'source', see Hooks::mathModeToString
72 $this->getServiceContainer()->getUserOptionsManager()->setOption( $user, 'math', 3 );
73 $this->assertSame( MathConfig::MODE_SOURCE, ParserOptions::newFromUser( $user )->getOption( 'math' ) );
74 }
75
76 public function testParserCacheIntegration() {
77 $this->setupDummyRendering();
78
79 $page = $this->getExistingTestPage( __METHOD__ );
80 $this->assertStatusGood(
81 $this->editPage( $page, '<math>TEST_FORMULA</math>' ),
82 'Sanity: edited page'
83 );
84
85 $parserOutputAccess = $this->getServiceContainer()->getParserOutputAccess();
86
87 // source was set as a default, so the rendering will be shared with
88 // canonical rendering produced by page edit
89 $parserOptions1 = ParserOptions::newCanonical( 'canonical' );
90 $parserOptions1->setOption( 'math', MathConfig::MODE_SOURCE );
91 $render = $parserOutputAccess->getCachedParserOutput( $page, $parserOptions1 );
92 $this->assertNotNull( $render );
93 $this->assertStringContainsString( "<render>source:TEST_FORMULA</render>", $render->getRawText() );
94
95 // Now render with 'mathml' and make sure we didn't get the cached output
96 $parserOptions2 = ParserOptions::newCanonical( 'canonical' );
97 $parserOptions2->setOption( 'math', MathConfig::MODE_LATEXML );
98 $this->assertNull( $parserOutputAccess->getCachedParserOutput( $page, $parserOptions2 ) );
99 $renderStatus = $parserOutputAccess->getParserOutput( $page, $parserOptions2 );
100 $this->assertStatusGood( $renderStatus );
101 $this->assertStringContainsString(
102 "<render>latexml:TEST_FORMULA</render>",
103 $renderStatus->getValue()->getText()
104 );
105
106 // Fetch from cache with source
107 $cachedWithDummy1 = $parserOutputAccess->getCachedParserOutput( $page, $parserOptions1 );
108 $this->assertNotNull( $cachedWithDummy1 );
109 $this->assertStringContainsString(
110 "<render>source:TEST_FORMULA</render>",
111 $cachedWithDummy1->getRawText()
112 );
113
114 // Fetch from cache with mathml
115 $cachedWithDummy2 = $parserOutputAccess->getCachedParserOutput( $page, $parserOptions2 );
116 $this->assertNotNull( $cachedWithDummy2 );
117 $this->assertStringContainsString(
118 "<render>latexml:TEST_FORMULA</render>",
119 $cachedWithDummy2->getRawText()
120 );
121 }
122
123 public function testMathInLink() {
124 $this->setupDummyRendering();
125 $po = ParserOptions::newFromAnon();
126 $po->setOption( 'math', MathConfig::MODE_SOURCE );
127 $res = $this->getServiceContainer()
128 ->getParser()
129 ->parse(
130 '[[test|<math>formula</math>]]',
131 PageReferenceValue::localReference( NS_MAIN, __METHOD__ ),
132 $po
133 )
134 ->getRawText();
135 $this->assertStringMatchesFormat( '%A<a%S><render>source:formula</render></a>%A', $res );
136 }
137 }