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
;
13 * @group ContentHandler
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
{
22 // Set up temporary interwiki links for 'en' and 'google'
25 'iw_api' => '/w/api.php',
28 $this->overrideConfigValue(
29 MainConfigNames
::InterwikiCache
,
30 ClassicInterwikiLookup
::buildCdbHash( [
33 'iw_url' => 'https://en.wikipedia.org/wiki/$1',
34 'iw_wikiid' => 'enwiki',
37 'iw_prefix' => 'google',
38 'iw_url' => 'https://google.com/?q=$1',
39 'iw_wikiid' => 'google',
43 // Limit reporting affects the options used
44 $this->overrideConfigValue(
45 MainConfigNames
::EnableParserLimitReporting
,
50 public static function provideGetParserOutput() {
52 'collapsibleSections',
53 'disableContentConversion',
57 'suppressSectionEditLinks',
60 'expensiveParserFunctionLimit',
64 $commonParsoidOptions = array_merge( $commonOptions, [
65 // Currently no options specific to parsoid parses
67 $commonLegacyOptions = array_merge( $commonOptions, [
68 'disableTitleConversion',
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>",
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>",
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&action=edit&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' => [
109 [ 'Main_Page' => 0 ],
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&action=edit&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' => [
132 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
135 'linkAnchor' => 'Hello',
138 'UsedOptions' => $commonParsoidOptions,
140 'options' => [ 'useParsoid' => true ],
143 'title' => 'WikitextContentTest_testGetParserOutput',
144 'model' => CONTENT_MODEL_WIKITEXT
,
145 'text' => "[[title that does not really exist]]",
146 'expectedHtml' => null,
147 'expectedFields' => [
149 [ 'Title_that_does_not_really_exist' => 0, ],
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' => [
170 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
173 'linkAnchor' => 'One',
181 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
184 'linkAnchor' => 'Two',
192 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
195 'linkAnchor' => 'Three',
203 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
206 'linkAnchor' => 'Four',
214 'fromtitle' => false,
215 'byteoffset' => null,
217 'linkAnchor' => 'Five',
222 'line' => 'Six+Seven %2525',
225 'fromtitle' => 'WikitextContentTest_testGetParserOutput',
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' ],
247 $parserOptions = null;
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
273 $parserOutput = $handler->getParserOutput(
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() {
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'