3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\TestingAccessWrapper
;
7 * Parser-related tests that don't suit for parserTests.txt
11 class ExtraParserTest
extends MediaWikiTestCase
{
13 /** @var ParserOptions */
18 protected function setUp() {
21 $contLang = Language
::factory( 'en' );
22 $this->setMwGlobals( [
23 'wgShowExceptionDetails' => true,
24 'wgCleanSignatures' => true,
26 $this->setUserLang( 'en' );
27 $this->setContentLang( $contLang );
29 // FIXME: This test should pass without setting global content language
30 $this->options
= ParserOptions
::newFromUserAndLang( new User
, $contLang );
31 $this->options
->setTemplateCallback( [ __CLASS__
, 'statelessFetchTemplate' ] );
32 $this->parser
= new Parser
;
34 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
39 * @covers Parser::parse
41 public function testLongNumericLinesDontKillTheParser() {
42 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
44 $title = Title
::newFromText( 'Unit test' );
45 $options = ParserOptions
::newFromUser( new User() );
46 $this->assertEquals( "<p>$longLine</p>",
47 $this->parser
->parse( $longLine, $title, $options )->getText( [ 'unwrap' => true ] ) );
51 * @covers Parser::braceSubstitution
52 * @covers SpecialPageFactory::capturePath
54 public function testSpecialPageTransclusionRestoresGlobalState() {
55 $text = "{{Special:ApiHelp/help}}";
56 $title = Title
::newFromText( 'testSpecialPageTransclusionRestoresGlobalState' );
57 $options = ParserOptions
::newFromUser( new User() );
59 RequestContext
::getMain()->setTitle( $title );
60 RequestContext
::getMain()->getWikiPage()->CustomTestProp
= true;
62 $parsed = $this->parser
->parse( $text, $title, $options )->getText();
63 $this->assertContains( 'apihelp-header', $parsed );
65 // Verify that this property wasn't wiped out by the parse
66 $this->assertTrue( RequestContext
::getMain()->getWikiPage()->CustomTestProp
);
70 * Test the parser entry points
71 * @covers Parser::parse
73 public function testParse() {
74 $title = Title
::newFromText( __FUNCTION__
);
75 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
77 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
78 $parserOutput->getText( [ 'unwrap' => true ] )
83 * @covers Parser::preSaveTransform
85 public function testPreSaveTransform() {
86 $title = Title
::newFromText( __FUNCTION__
);
87 $outputText = $this->parser
->preSaveTransform(
88 "Test\r\n{{subst:Foo}}\n{{Bar}}",
94 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
98 * @covers Parser::preprocess
100 public function testPreprocess() {
101 $title = Title
::newFromText( __FUNCTION__
);
102 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
105 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
111 * cleanSig() makes all templates substs and removes tildes
112 * @covers Parser::cleanSig
114 public function testCleanSig() {
115 $title = Title
::newFromText( __FUNCTION__
);
116 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
118 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
122 * cleanSig() should do nothing if disabled
123 * @covers Parser::cleanSig
125 public function testCleanSigDisabled() {
126 $this->setMwGlobals( 'wgCleanSignatures', false );
128 $title = Title
::newFromText( __FUNCTION__
);
129 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
131 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
135 * cleanSigInSig() just removes tildes
136 * @dataProvider provideStringsForCleanSigInSig
137 * @covers Parser::cleanSigInSig
139 public function testCleanSigInSig( $in, $out ) {
140 $this->assertEquals( Parser
::cleanSigInSig( $in ), $out );
143 public static function provideStringsForCleanSigInSig() {
145 [ "{{Foo}} ~~~~", "{{Foo}} " ],
152 * @covers Parser::getSection
154 public function testGetSection() {
155 $outputText2 = $this->parser
->getSection(
156 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
157 . "Section 2\n== Heading 3 ==\nSection 3\n",
160 $outputText1 = $this->parser
->getSection(
161 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
162 . "Section 2\n== Heading 3 ==\nSection 3\n",
166 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
167 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
171 * @covers Parser::replaceSection
173 public function testReplaceSection() {
174 $outputText = $this->parser
->replaceSection(
175 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
176 . "Section 2\n== Heading 3 ==\nSection 3\n",
181 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
185 * Templates and comments are not affected, but noinclude/onlyinclude is.
186 * @covers Parser::getPreloadText
188 public function testGetPreloadText() {
189 $title = Title
::newFromText( __FUNCTION__
);
190 $outputText = $this->parser
->getPreloadText(
191 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
196 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
200 * @param Title $title
201 * @param bool $parser
205 static function statelessFetchTemplate( $title, $parser = false ) {
206 $text = "Content of ''" . $title->getFullText() . "''";
211 'finalTitle' => $title,
216 * @covers Parser::parse
218 public function testTrackingCategory() {
219 $title = Title
::newFromText( __FUNCTION__
);
220 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
221 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
222 $expected = [ $cat->getDBkey() ];
223 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
224 $result = $parserOutput->getCategoryLinks();
225 $this->assertEquals( $expected, $result );
229 * @covers Parser::parse
231 public function testTrackingCategorySpecial() {
232 // Special pages shouldn't have tracking cats.
233 $title = SpecialPage
::getTitleFor( 'Contributions' );
234 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
235 $result = $parserOutput->getCategoryLinks();
236 $this->assertEmpty( $result );
240 * @covers Parser::parseLinkParameterPrivate
241 * @dataProvider provideParseLinkParameter
243 public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
244 $this->parser
->startExternalParse( Title
::newFromText( __FUNCTION__
),
245 $this->options
, Parser
::OT_HTML
);
246 $output = TestingAccessWrapper
::newFromObject( $this->parser
)
247 ->parseLinkParameterPrivate( $input );
249 $this->assertEquals( $expected[0], $output[0], "$desc (type)" );
251 if ( $expected[0] === 'link-title' ) {
252 $this->assertTrue( $expected[1]->equals( $output[1] ), "$desc (target)" );
254 $this->assertEquals( $expected[1], $output[1], "$desc (target)" );
257 foreach ( $expectedLinks as $func => $expected ) {
258 $output = $this->parser
->getOutput()->$func();
259 $this->assertEquals( $expected, $output, "$desc ($func)" );
263 public static function provideParseLinkParameter() {
267 [ 'no-link', false ],
269 'Return no link when requested',
272 'https://example.com/',
273 [ 'link-url', 'https://example.com/' ],
274 [ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
279 [ 'link-url', '//example.com/' ],
280 [ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
285 [ 'link-title', Title
::newFromText( 'Test' ) ],
286 [ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
291 [ 'link-title', Title
::newFromText( 'mw:Test' ) ],
292 [ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
293 'Internal link (interwiki)',
299 'Invalid link target',
305 'Invalid link target',
311 'Invalid link target',