3 use MediaWiki\MediaWikiServices
;
6 * Parser-related tests that don't suit for parserTests.txt
10 class ExtraParserTest
extends MediaWikiTestCase
{
12 /** @var ParserOptions */
17 protected function setUp() {
20 $contLang = Language
::factory( 'en' );
21 $this->setMwGlobals( [
22 'wgShowExceptionDetails' => true,
23 'wgCleanSignatures' => true,
25 $this->setUserLang( 'en' );
26 $this->setContentLang( $contLang );
28 // FIXME: This test should pass without setting global content language
29 $this->options
= ParserOptions
::newFromUserAndLang( new User
, $contLang );
30 $this->options
->setTemplateCallback( [ __CLASS__
, 'statelessFetchTemplate' ] );
31 $this->parser
= new Parser
;
33 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
38 * @covers Parser::parse
40 public function testLongNumericLinesDontKillTheParser() {
41 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
43 $title = Title
::newFromText( 'Unit test' );
44 $options = ParserOptions
::newFromUser( new User() );
45 $this->assertEquals( "<p>$longLine</p>",
46 $this->parser
->parse( $longLine, $title, $options )->getText( [ 'unwrap' => true ] ) );
50 * @covers Parser::braceSubstitution
51 * @covers SpecialPageFactory::capturePath
53 public function testSpecialPageTransclusionRestoresGlobalState() {
54 $text = "{{Special:ApiHelp/help}}";
55 $title = Title
::newFromText( 'testSpecialPageTransclusionRestoresGlobalState' );
56 $options = ParserOptions
::newFromUser( new User() );
58 RequestContext
::getMain()->setTitle( $title );
59 RequestContext
::getMain()->getWikiPage()->CustomTestProp
= true;
61 $parsed = $this->parser
->parse( $text, $title, $options )->getText();
62 $this->assertContains( 'apihelp-header', $parsed );
64 // Verify that this property wasn't wiped out by the parse
65 $this->assertTrue( RequestContext
::getMain()->getWikiPage()->CustomTestProp
);
69 * Test the parser entry points
70 * @covers Parser::parse
72 public function testParse() {
73 $title = Title
::newFromText( __FUNCTION__
);
74 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
76 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
77 $parserOutput->getText( [ 'unwrap' => true ] )
82 * @covers Parser::preSaveTransform
84 public function testPreSaveTransform() {
85 $title = Title
::newFromText( __FUNCTION__
);
86 $outputText = $this->parser
->preSaveTransform(
87 "Test\r\n{{subst:Foo}}\n{{Bar}}",
93 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
97 * @covers Parser::preprocess
99 public function testPreprocess() {
100 $title = Title
::newFromText( __FUNCTION__
);
101 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
104 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
110 * cleanSig() makes all templates substs and removes tildes
111 * @covers Parser::cleanSig
113 public function testCleanSig() {
114 $title = Title
::newFromText( __FUNCTION__
);
115 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
117 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
121 * cleanSig() should do nothing if disabled
122 * @covers Parser::cleanSig
124 public function testCleanSigDisabled() {
125 $this->setMwGlobals( 'wgCleanSignatures', false );
127 $title = Title
::newFromText( __FUNCTION__
);
128 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
130 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
134 * cleanSigInSig() just removes tildes
135 * @dataProvider provideStringsForCleanSigInSig
136 * @covers Parser::cleanSigInSig
138 public function testCleanSigInSig( $in, $out ) {
139 $this->assertEquals( Parser
::cleanSigInSig( $in ), $out );
142 public static function provideStringsForCleanSigInSig() {
144 [ "{{Foo}} ~~~~", "{{Foo}} " ],
151 * @covers Parser::getSection
153 public function testGetSection() {
154 $outputText2 = $this->parser
->getSection(
155 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
156 . "Section 2\n== Heading 3 ==\nSection 3\n",
159 $outputText1 = $this->parser
->getSection(
160 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
161 . "Section 2\n== Heading 3 ==\nSection 3\n",
165 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
166 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
170 * @covers Parser::replaceSection
172 public function testReplaceSection() {
173 $outputText = $this->parser
->replaceSection(
174 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
175 . "Section 2\n== Heading 3 ==\nSection 3\n",
180 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
184 * Templates and comments are not affected, but noinclude/onlyinclude is.
185 * @covers Parser::getPreloadText
187 public function testGetPreloadText() {
188 $title = Title
::newFromText( __FUNCTION__
);
189 $outputText = $this->parser
->getPreloadText(
190 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
195 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
199 * @param Title $title
200 * @param bool $parser
204 static function statelessFetchTemplate( $title, $parser = false ) {
205 $text = "Content of ''" . $title->getFullText() . "''";
210 'finalTitle' => $title,
215 * @covers Parser::parse
217 public function testTrackingCategory() {
218 $title = Title
::newFromText( __FUNCTION__
);
219 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
220 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
221 $expected = [ $cat->getDBkey() ];
222 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
223 $result = $parserOutput->getCategoryLinks();
224 $this->assertEquals( $expected, $result );
228 * @covers Parser::parse
230 public function testTrackingCategorySpecial() {
231 // Special pages shouldn't have tracking cats.
232 $title = SpecialPage
::getTitleFor( 'Contributions' );
233 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
234 $result = $parserOutput->getCategoryLinks();
235 $this->assertEmpty( $result );
239 * @covers Parser::parseLinkParameter
240 * @dataProvider provideParseLinkParameter
242 public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
243 $this->parser
->startExternalParse( Title
::newFromText( __FUNCTION__
),
244 $this->options
, Parser
::OT_HTML
);
245 $output = $this->parser
->parseLinkParameter( $input );
247 $this->assertEquals( $expected[0], $output[0], "$desc (type)" );
249 if ( $expected[0] === 'link-title' ) {
250 $this->assertTrue( $expected[1]->equals( $output[1] ), "$desc (target)" );
252 $this->assertEquals( $expected[1], $output[1], "$desc (target)" );
255 foreach ( $expectedLinks as $func => $expected ) {
256 $output = $this->parser
->getOutput()->$func();
257 $this->assertEquals( $expected, $output, "$desc ($func)" );
261 public static function provideParseLinkParameter() {
265 [ 'no-link', false ],
267 'Return no link when requested',
270 'https://example.com/',
271 [ 'link-url', 'https://example.com/' ],
272 [ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
277 [ 'link-url', '//example.com/' ],
278 [ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
283 [ 'link-title', Title
::newFromText( 'Test' ) ],
284 [ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
289 [ 'link-title', Title
::newFromText( 'mw:Test' ) ],
290 [ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
291 'Internal link (interwiki)',
297 'Invalid link target',
303 'Invalid link target',
309 'Invalid link target',