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 * Test the parser entry points
51 * @covers Parser::parse
53 public function testParse() {
54 $title = Title
::newFromText( __FUNCTION__
);
55 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
57 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
58 $parserOutput->getText( [ 'unwrap' => true ] )
63 * @covers Parser::preSaveTransform
65 public function testPreSaveTransform() {
66 $title = Title
::newFromText( __FUNCTION__
);
67 $outputText = $this->parser
->preSaveTransform(
68 "Test\r\n{{subst:Foo}}\n{{Bar}}",
74 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
78 * @covers Parser::preprocess
80 public function testPreprocess() {
81 $title = Title
::newFromText( __FUNCTION__
);
82 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
85 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
91 * cleanSig() makes all templates substs and removes tildes
92 * @covers Parser::cleanSig
94 public function testCleanSig() {
95 $title = Title
::newFromText( __FUNCTION__
);
96 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
98 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
102 * cleanSig() should do nothing if disabled
103 * @covers Parser::cleanSig
105 public function testCleanSigDisabled() {
106 $this->setMwGlobals( 'wgCleanSignatures', false );
108 $title = Title
::newFromText( __FUNCTION__
);
109 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
111 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
115 * cleanSigInSig() just removes tildes
116 * @dataProvider provideStringsForCleanSigInSig
117 * @covers Parser::cleanSigInSig
119 public function testCleanSigInSig( $in, $out ) {
120 $this->assertEquals( Parser
::cleanSigInSig( $in ), $out );
123 public static function provideStringsForCleanSigInSig() {
125 [ "{{Foo}} ~~~~", "{{Foo}} " ],
132 * @covers Parser::getSection
134 public function testGetSection() {
135 $outputText2 = $this->parser
->getSection(
136 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
137 . "Section 2\n== Heading 3 ==\nSection 3\n",
140 $outputText1 = $this->parser
->getSection(
141 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
142 . "Section 2\n== Heading 3 ==\nSection 3\n",
146 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
147 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
151 * @covers Parser::replaceSection
153 public function testReplaceSection() {
154 $outputText = $this->parser
->replaceSection(
155 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
156 . "Section 2\n== Heading 3 ==\nSection 3\n",
161 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
165 * Templates and comments are not affected, but noinclude/onlyinclude is.
166 * @covers Parser::getPreloadText
168 public function testGetPreloadText() {
169 $title = Title
::newFromText( __FUNCTION__
);
170 $outputText = $this->parser
->getPreloadText(
171 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
176 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
180 * @param Title $title
181 * @param bool $parser
185 static function statelessFetchTemplate( $title, $parser = false ) {
186 $text = "Content of ''" . $title->getFullText() . "''";
191 'finalTitle' => $title,
196 * @covers Parser::parse
198 public function testTrackingCategory() {
199 $title = Title
::newFromText( __FUNCTION__
);
200 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
201 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
202 $expected = [ $cat->getDBkey() ];
203 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
204 $result = $parserOutput->getCategoryLinks();
205 $this->assertEquals( $expected, $result );
209 * @covers Parser::parse
211 public function testTrackingCategorySpecial() {
212 // Special pages shouldn't have tracking cats.
213 $title = SpecialPage
::getTitleFor( 'Contributions' );
214 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
215 $result = $parserOutput->getCategoryLinks();
216 $this->assertEmpty( $result );