wiki.techinc.nl/tests/phpunit/includes/content/WikitextContentHandlerIntegrationTest.php

295 lines
9.8 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\Content\Renderer\ContentParseParams;
use MediaWiki\Interwiki\ClassicInterwikiLookup;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MainConfigNames;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleValue;
/**
* @group ContentHandler
* @group Database
* ^--- needed, because we do need the database to test link updates
*/
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,
] )
);
$this->getServiceContainer()->resetServiceForTesting( 'InterwikiLookup' );
}
public static function provideGetParserOutput() {
$commonOptions = [
'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',
] );
yield 'Basic render' => [
'title' => 'WikitextContentTest_testGetParserOutput',
'model' => CONTENT_MODEL_WIKITEXT,
'text' => "hello ''world''\n",
parser: Move lang/dir and mw-content-ltr to ParserOutput::getText == Skin::wrapHTML == Skin::wrapHTML no longer has to perform any guessing of the ParserOutput language. Nor does it have to special wiki pages vs special pages in this regard. Yay, code removal. == ImagePage == On URLs like /wiki/File:Example.jpg, the main output handler is ImagePage::view. This calls the parent Article::view to handle most of its output. Article::view obtains the ParserOptions, and then fetches ParserOutput, and then adds `<div class=mw-parser-output>` and its metadata to OutputPage. Before this change, ImagePage::view was creating a wrapper based on "predicting" what language the ParserOutput will contain. It couldn't call the new OutputPage::getContentLanguage or some equivalent as Article::view wouldn't have populated that yet. This leaky abstraction is fixed by this change as now the `<div>` from ParserOutput no longer comes with a "please wrap it properly" contract that Article subclasses couldn't possibly implement correctly (it coudln't wrap it after the fact because Article::view writes to OutputPage directly). RECENT (T310445): A special case was recently added for file pages about translated SVGs. For those, we decide which language to use for the "fullMedia" thumb atop the page. This was recently changed as part of T310445 from a hardcoded $wgLanguageCode (site content lang) to new problematic Title::getPageViewLanguage, which tries to guestimate the page language of the rendered ParserOutput and then gets the preferred variant for the current user. The motivation for this was to support language variants but used Title::getPageViewLanguage as a kitchen sink to achieve that minor side-effect. The only part of this now-deprecated method that we actually need is LanguageConverter::getPreferredVariant(). Test plan: Covered by ImagePageTest. == Skin mainpage-title == RECENT (T331095, T298715): A special case was added to Skin::getTemplateData that powers the mainpage-title interface message feature. This is empty by default, but when created via MediaWiki:mainpage-title allows interface admins to replace the H1 with a custom and localised page heading. A few months ago, in Ifc9f0a7174, Title::getPageViewLanguage was applied here to support language variants. Replace with the same fix as for ImagePage. Revert back to Message::inContentLanguage() but refactor to inLanguage() via MediaWikiServices::getContentLanguage so that LanguageConverter::getPreferredVariant can be applied. == EditPage == This was doing similar "predicting" of the ParserOutput language to create an empty preview placeholder for use by preview.js. Now that ApiParse (via ParserOutput::getText) returns a usable element without any secret "you magically know the right class, lang, and dir" contract, this placeholder is no longer needed. Test Plan: * EditPage: Default preview 1. index.php?title=Main_Page&action=edit 2. Show preview 3. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> * EditPage: JS preview 1. Preferences > Editing > Show preview without reload 2. index.php?title=Main_Page&action=edit 3. Show preview 4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> 5. Type something and 'Show preview' again 6. Assert old element gone, new text is shown, and new element attributes are the same as the above. == McrUndoAction == Same as EditPage basically, but without the JS preview use case. == DifferenceEngine == Test: 1. Open /w/index.php?title=Main_Page&diff=0 (this shows the latest diff, can do manually by viewing /wiki/Main_Page, click "View history", click "Compare selected revisions") 2. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> 3. Open /w/index.php?title=Main_Page&diff=0&action=render 4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> == Special:ExpandTemplates == Test: 1. /wiki/Special:ExpandTemplates 2. Write "Hello". 3. "OK" 4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> Bug: T341244 Depends-On: Icd9c079f5896ee83d86b9c2699636dc81d25a14c Depends-On: I4e7484b3b94f1cb6062e7cef9f20626b650bb4b1 Depends-On: I90b88f3b3a3bbeba4f48d118f92f54864997e105 Change-Id: Ib130a055e46764544af0f1a46d2bc2b3a7ee85b7
2023-10-04 04:45:07 +00:00
'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",
parser: Move lang/dir and mw-content-ltr to ParserOutput::getText == Skin::wrapHTML == Skin::wrapHTML no longer has to perform any guessing of the ParserOutput language. Nor does it have to special wiki pages vs special pages in this regard. Yay, code removal. == ImagePage == On URLs like /wiki/File:Example.jpg, the main output handler is ImagePage::view. This calls the parent Article::view to handle most of its output. Article::view obtains the ParserOptions, and then fetches ParserOutput, and then adds `<div class=mw-parser-output>` and its metadata to OutputPage. Before this change, ImagePage::view was creating a wrapper based on "predicting" what language the ParserOutput will contain. It couldn't call the new OutputPage::getContentLanguage or some equivalent as Article::view wouldn't have populated that yet. This leaky abstraction is fixed by this change as now the `<div>` from ParserOutput no longer comes with a "please wrap it properly" contract that Article subclasses couldn't possibly implement correctly (it coudln't wrap it after the fact because Article::view writes to OutputPage directly). RECENT (T310445): A special case was recently added for file pages about translated SVGs. For those, we decide which language to use for the "fullMedia" thumb atop the page. This was recently changed as part of T310445 from a hardcoded $wgLanguageCode (site content lang) to new problematic Title::getPageViewLanguage, which tries to guestimate the page language of the rendered ParserOutput and then gets the preferred variant for the current user. The motivation for this was to support language variants but used Title::getPageViewLanguage as a kitchen sink to achieve that minor side-effect. The only part of this now-deprecated method that we actually need is LanguageConverter::getPreferredVariant(). Test plan: Covered by ImagePageTest. == Skin mainpage-title == RECENT (T331095, T298715): A special case was added to Skin::getTemplateData that powers the mainpage-title interface message feature. This is empty by default, but when created via MediaWiki:mainpage-title allows interface admins to replace the H1 with a custom and localised page heading. A few months ago, in Ifc9f0a7174, Title::getPageViewLanguage was applied here to support language variants. Replace with the same fix as for ImagePage. Revert back to Message::inContentLanguage() but refactor to inLanguage() via MediaWikiServices::getContentLanguage so that LanguageConverter::getPreferredVariant can be applied. == EditPage == This was doing similar "predicting" of the ParserOutput language to create an empty preview placeholder for use by preview.js. Now that ApiParse (via ParserOutput::getText) returns a usable element without any secret "you magically know the right class, lang, and dir" contract, this placeholder is no longer needed. Test Plan: * EditPage: Default preview 1. index.php?title=Main_Page&action=edit 2. Show preview 3. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> * EditPage: JS preview 1. Preferences > Editing > Show preview without reload 2. index.php?title=Main_Page&action=edit 3. Show preview 4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> 5. Type something and 'Show preview' again 6. Assert old element gone, new text is shown, and new element attributes are the same as the above. == McrUndoAction == Same as EditPage basically, but without the JS preview use case. == DifferenceEngine == Test: 1. Open /w/index.php?title=Main_Page&diff=0 (this shows the latest diff, can do manually by viewing /wiki/Main_Page, click "View history", click "Compare selected revisions") 2. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> 3. Open /w/index.php?title=Main_Page&diff=0&action=render 4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> == Special:ExpandTemplates == Test: 1. /wiki/Special:ExpandTemplates 2. Write "Hello". 3. "OK" 4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr> Bug: T341244 Depends-On: Icd9c079f5896ee83d86b9c2699636dc81d25a14c Depends-On: I4e7484b3b94f1cb6062e7cef9f20626b650bb4b1 Depends-On: I90b88f3b3a3bbeba4f48d118f92f54864997e105 Change-Id: Ib130a055e46764544af0f1a46d2bc2b3a7ee85b7
2023-10-04 04:45:07 +00:00
'expectedHtml' => '<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">' . "<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">' . "<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>",
'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" 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>',
'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
* @covers \WikitextContentHandler::fillParserOutput
*/
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
* @covers \WikitextContentHandler::makeRedirectContent
* @covers \WikitextContentHandler::getParserOutput
*/
public function testMakeRedirectContent( LinkTarget $target, string $expectedWT, string $expectedTarget ) {
$this->getServiceContainer()->resetServiceForTesting( 'ContentLanguage' );
$this->getServiceContainer()->resetServiceForTesting( 'MagicWordFactory' );
$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() )
);
$actual = $parserOutput->getText();
$this->assertStringContainsString( '<div class="redirectMsg">', $actual );
$this->assertMatchesRegularExpression( '!<a[^<>]+>' . $expectedTarget . '</a>!', $actual );
}
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'
],
];
}
}