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