Merge "ObjectCacheFactory: use Tracer telemetry"
[mediawiki.git] / tests / phpunit / includes / content / WikitextContentHandlerIntegrationTest.php
blob51b3d858b51ad4f46de72d9abff137b3345c8e17
1 <?php
3 use MediaWiki\Content\Renderer\ContentParseParams;
4 use MediaWiki\Interwiki\ClassicInterwikiLookup;
5 use MediaWiki\Linker\LinkTarget;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Parser\ParserOptions;
8 use MediaWiki\Title\Title;
9 use MediaWiki\Title\TitleValue;
10 use Wikimedia\Parsoid\Parsoid;
12 /**
13 * @group ContentHandler
14 * @group Database
15 * ^--- needed, because we do need the database to test link updates
16 * @covers \MediaWiki\Content\WikitextContentHandler
18 class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegrationTest {
19 protected function setUp(): void {
20 parent::setUp();
22 // Set up temporary interwiki links for 'en' and 'google'
23 $defaults = [
24 'iw_local' => 0,
25 'iw_api' => '/w/api.php',
26 'iw_url' => ''
28 $this->overrideConfigValue(
29 MainConfigNames::InterwikiCache,
30 ClassicInterwikiLookup::buildCdbHash( [
32 'iw_prefix' => 'en',
33 'iw_url' => 'https://en.wikipedia.org/wiki/$1',
34 'iw_wikiid' => 'enwiki',
35 ] + $defaults,
37 'iw_prefix' => 'google',
38 'iw_url' => 'https://google.com/?q=$1',
39 'iw_wikiid' => 'google',
40 ] + $defaults,
41 ] )
43 // Limit reporting affects the options used
44 $this->overrideConfigValue(
45 MainConfigNames::EnableParserLimitReporting,
46 true
50 public static function provideGetParserOutput() {
51 $commonOptions = [
52 'collapsibleSections',
53 'disableContentConversion',
54 'interfaceMessage',
55 'isPreview',
56 'maxIncludeSize',
57 'suppressSectionEditLinks',
58 'useParsoid',
59 'wrapclass',
60 'expensiveParserFunctionLimit',
61 'maxPPExpandDepth',
62 'maxPPNodeCount',
64 $commonParsoidOptions = array_merge( $commonOptions, [
65 // Currently no options specific to parsoid parses
66 ] );
67 $commonLegacyOptions = array_merge( $commonOptions, [
68 'disableTitleConversion',
69 'targetLanguage',
70 ] );
71 $parsoidVersion =
72 'data-mw-parsoid-version="' . Parsoid::version() . '" ' .
73 'data-mw-html-version="' . Parsoid::defaultHTMLVersion() . '"';
75 yield 'Basic render' => [
76 'title' => 'WikitextContentTest_testGetParserOutput',
77 'model' => CONTENT_MODEL_WIKITEXT,
78 'text' => "hello ''world''\n",
79 'expectedHtml' => '<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">' . "<p>hello <i>world</i>\n</p></div>",
80 'expectedFields' => [
81 'Links' => [
83 'Sections' => [
85 'UsedOptions' => $commonLegacyOptions,
88 yield 'Basic Parsoid render' => [
89 'title' => 'WikitextContentTest_testGetParserOutput',
90 'model' => CONTENT_MODEL_WIKITEXT,
91 'text' => "hello ''world''\n",
92 'expectedHtml' => "<div class=\"mw-content-ltr mw-parser-output\" lang=\"en\" dir=\"ltr\" $parsoidVersion><section data-mw-section-id=\"0\" id=\"mwAQ\"><p id=\"mwAg\">hello <i id=\"mwAw\">world</i></p>\n</section></div>",
93 'expectedFields' => [
94 'Links' => [
96 'Sections' => [
98 'UsedOptions' => $commonParsoidOptions,
100 'options' => [ 'useParsoid' => true, 'suppressSectionEditLinks' => true ],
102 yield 'Parsoid render (redirect page)' => [
103 'title' => 'WikitextContentTest_testGetParserOutput',
104 'model' => CONTENT_MODEL_WIKITEXT,
105 'text' => "#REDIRECT [[Main Page]]",
106 'expectedHtml' => "<div class=\"mw-content-ltr mw-parser-output\" lang=\"en\" dir=\"ltr\" $parsoidVersion><div class=\"redirectMsg\"><p>Redirect to:</p><ul class=\"redirectText\"><li><a href=\"/w/index.php?title=Main_Page&amp;action=edit&amp;redlink=1\" class=\"new\" title=\"Main Page (page does not exist)\">Main Page</a></li></ul></div><section data-mw-section-id=\"0\" id=\"mwAQ\"><link rel=\"mw:PageProp/redirect\" href=\"./Main_Page\" id=\"mwAg\"/></section></div>",
107 'expectedFields' => [
108 'Links' => [
109 [ 'Main_Page' => 0 ],
111 'Sections' => [
113 'UsedOptions' => $commonParsoidOptions,
115 'options' => [ 'useParsoid' => true, 'suppressSectionEditLinks' => true ],
117 yield 'Parsoid render (section edit links)' => [
118 'title' => 'WikitextContentTest_testGetParserOutput',
119 'model' => CONTENT_MODEL_WIKITEXT,
120 'text' => "== Hello ==",
121 'expectedHtml' => "<div class=\"mw-content-ltr mw-parser-output\" lang=\"en\" dir=\"ltr\" $parsoidVersion id=\"mwAw\">" . '<section data-mw-section-id="0" id="mwAQ"></section><section data-mw-section-id="1" id="mwAg"><div class="mw-heading mw-heading2" id="mwBA"><h2 id="Hello">Hello</h2><span class="mw-editsection" id="mwBQ"><span class="mw-editsection-bracket" id="mwBg">[</span><a href="/w/index.php?title=WikitextContentTest_testGetParserOutput&amp;action=edit&amp;section=1" title="Edit section: Hello" id="mwBw"><span id="mwCA">edit</span></a><span class="mw-editsection-bracket" id="mwCQ">]</span></span></div></section></div>',
122 'expectedFields' => [
123 'Links' => [
125 'Sections' => [
127 'toclevel' => 1,
128 'level' => '2',
129 'line' => 'Hello',
130 'number' => '1',
131 'index' => '1',
132 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
133 'byteoffset' => 0,
134 'anchor' => 'Hello',
135 'linkAnchor' => 'Hello',
138 'UsedOptions' => $commonParsoidOptions,
140 'options' => [ 'useParsoid' => true ],
142 yield 'Links' => [
143 'title' => 'WikitextContentTest_testGetParserOutput',
144 'model' => CONTENT_MODEL_WIKITEXT,
145 'text' => "[[title that does not really exist]]",
146 'expectedHtml' => null,
147 'expectedFields' => [
148 'Links' => [
149 [ 'Title_that_does_not_really_exist' => 0, ],
151 'Sections' => [
155 yield 'TOC' => [
156 'title' => 'WikitextContentTest_testGetParserOutput',
157 'model' => CONTENT_MODEL_WIKITEXT,
158 'text' => "==One==\n==Two==\n==Three==\n==Four==\n<h2>Five</h2>\n===Six+Seven %2525===",
159 'expectedHtml' => null,
160 'expectedFields' => [
161 'Links' => [
163 'Sections' => [
165 'toclevel' => 1,
166 'level' => '2',
167 'line' => 'One',
168 'number' => '1',
169 'index' => '1',
170 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
171 'byteoffset' => 0,
172 'anchor' => 'One',
173 'linkAnchor' => 'One',
176 'toclevel' => 1,
177 'level' => '2',
178 'line' => 'Two',
179 'number' => '2',
180 'index' => '2',
181 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
182 'byteoffset' => 8,
183 'anchor' => 'Two',
184 'linkAnchor' => 'Two',
187 'toclevel' => 1,
188 'level' => '2',
189 'line' => 'Three',
190 'number' => '3',
191 'index' => '3',
192 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
193 'byteoffset' => 16,
194 'anchor' => 'Three',
195 'linkAnchor' => 'Three',
198 'toclevel' => 1,
199 'level' => '2',
200 'line' => 'Four',
201 'number' => '4',
202 'index' => '4',
203 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
204 'byteoffset' => 26,
205 'anchor' => 'Four',
206 'linkAnchor' => 'Four',
209 'toclevel' => 1,
210 'level' => '2',
211 'line' => 'Five',
212 'number' => '5',
213 'index' => '',
214 'fromtitle' => false,
215 'byteoffset' => null,
216 'anchor' => 'Five',
217 'linkAnchor' => 'Five',
220 'toclevel' => 2,
221 'level' => '3',
222 'line' => 'Six+Seven %2525',
223 'number' => '5.1',
224 'index' => '5',
225 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
226 'byteoffset' => 49,
227 'anchor' => 'Six+Seven_%2525',
228 'linkAnchor' => 'Six+Seven_%252525',
236 * @dataProvider provideGetParserOutput
238 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
239 $expectedFields = null, $options = null
241 $this->overrideConfigValues( [
242 MainConfigNames::ScriptPath => '/w',
243 MainConfigNames::Script => '/w/index.php',
244 MainConfigNames::FragmentMode => [ 'html5' ],
245 ] );
247 $parserOptions = null;
248 if ( $options ) {
249 $parserOptions = ParserOptions::newFromAnon();
250 foreach ( $options as $key => $val ) {
251 $parserOptions->setOption( $key, $val );
254 parent::testGetParserOutput(
255 $title, $model, $text, $expectedHtml, $expectedFields, $parserOptions
260 * @dataProvider provideMakeRedirectContent
261 * @param LinkTarget $target
262 * @param string $expectedWT Serialized wikitext form of the content object built
263 * @param string $expectedTarget Expected target string in the HTML redirect
265 public function testMakeRedirectContent( LinkTarget $target, string $expectedWT, string $expectedTarget ) {
266 $handler = $this->getServiceContainer()->getContentHandlerFactory()
267 ->getContentHandler( CONTENT_MODEL_WIKITEXT );
268 $content = $handler->makeRedirectContent( Title::newFromLinkTarget( $target ) );
269 $this->assertEquals( $expectedWT, $content->serialize() );
271 // Check that an appropriate redirect header was added to the
272 // ParserOutput
273 $parserOutput = $handler->getParserOutput(
274 $content,
275 new ContentParseParams( Title::newMainPage() )
277 $redirectHeader = $parserOutput->getRedirectHeader();
278 $this->assertStringContainsString( '<div class="redirectMsg">', $redirectHeader );
279 $this->assertMatchesRegularExpression( '!<a[^<>]+>' . $expectedTarget . '</a>!', $redirectHeader );
282 public static function provideMakeRedirectContent() {
283 return [
284 [ new TitleValue( NS_MAIN, 'Hello' ), '#REDIRECT [[Hello]]', 'Hello' ],
285 [ new TitleValue( NS_TEMPLATE, 'Hello' ), '#REDIRECT [[Template:Hello]]', 'Template:Hello' ],
286 [ new TitleValue( NS_MAIN, 'Hello', 'section' ), '#REDIRECT [[Hello#section]]', 'Hello#section' ],
287 [ new TitleValue( NS_USER, 'John doe', 'section' ), '#REDIRECT [[User:John doe#section]]', 'User:John doe#section' ],
288 [ new TitleValue( NS_MEDIAWIKI, 'FOOBAR' ), '#REDIRECT [[MediaWiki:FOOBAR]]', 'MediaWiki:FOOBAR' ],
289 [ new TitleValue( NS_CATEGORY, 'Foo' ), '#REDIRECT [[:Category:Foo]]', 'Category:Foo' ],
290 [ new TitleValue( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]', 'en:Foo' ],
291 [ new TitleValue( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]', 'en:Foo' ],
293 new TitleValue( NS_MAIN, 'Bar', 'fragment', 'google' ),
294 '#REDIRECT [[google:Bar#fragment]]',
295 'google:Bar#fragment'