4 * Parser-related tests that don't suit for parserTests.txt
8 class ExtraParserTest
extends MediaWikiTestCase
{
10 /** @var ParserOptions */
15 protected function setUp() {
18 $contLang = Language
::factory( 'en' );
19 $this->setMwGlobals( [
20 'wgShowDBErrorBacktrace' => true,
21 'wgCleanSignatures' => true,
23 $this->setUserLang( 'en' );
24 $this->setContentLang( $contLang );
26 // FIXME: This test should pass without setting global content language
27 $this->options
= ParserOptions
::newFromUserAndLang( new User
, $contLang );
28 $this->options
->setTemplateCallback( [ __CLASS__
, 'statelessFetchTemplate' ] );
29 $this->parser
= new Parser
;
31 MagicWord
::clearCache();
36 * @covers Parser::parse
38 public function testLongNumericLinesDontKillTheParser() {
39 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
41 $title = Title
::newFromText( 'Unit test' );
42 $options = ParserOptions
::newFromUser( new User() );
43 $this->assertEquals( "<p>$longLine</p>",
44 $this->parser
->parse( $longLine, $title, $options )->getText() );
48 * Test the parser entry points
49 * @covers Parser::parse
51 public function testParse() {
52 $title = Title
::newFromText( __FUNCTION__
);
53 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
55 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
56 $parserOutput->getText()
61 * @covers Parser::preSaveTransform
63 public function testPreSaveTransform() {
64 $title = Title
::newFromText( __FUNCTION__
);
65 $outputText = $this->parser
->preSaveTransform(
66 "Test\r\n{{subst:Foo}}\n{{Bar}}",
72 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
76 * @covers Parser::preprocess
78 public function testPreprocess() {
79 $title = Title
::newFromText( __FUNCTION__
);
80 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
83 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
89 * cleanSig() makes all templates substs and removes tildes
90 * @covers Parser::cleanSig
92 public function testCleanSig() {
93 $title = Title
::newFromText( __FUNCTION__
);
94 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
96 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
100 * cleanSig() should do nothing if disabled
101 * @covers Parser::cleanSig
103 public function testCleanSigDisabled() {
104 $this->setMwGlobals( 'wgCleanSignatures', false );
106 $title = Title
::newFromText( __FUNCTION__
);
107 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
109 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
113 * cleanSigInSig() just removes tildes
114 * @dataProvider provideStringsForCleanSigInSig
115 * @covers Parser::cleanSigInSig
117 public function testCleanSigInSig( $in, $out ) {
118 $this->assertEquals( Parser
::cleanSigInSig( $in ), $out );
121 public static function provideStringsForCleanSigInSig() {
123 [ "{{Foo}} ~~~~", "{{Foo}} " ],
130 * @covers Parser::getSection
132 public function testGetSection() {
133 $outputText2 = $this->parser
->getSection(
134 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
135 . "Section 2\n== Heading 3 ==\nSection 3\n",
138 $outputText1 = $this->parser
->getSection(
139 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
140 . "Section 2\n== Heading 3 ==\nSection 3\n",
144 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
145 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
149 * @covers Parser::replaceSection
151 public function testReplaceSection() {
152 $outputText = $this->parser
->replaceSection(
153 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
154 . "Section 2\n== Heading 3 ==\nSection 3\n",
159 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
163 * Templates and comments are not affected, but noinclude/onlyinclude is.
164 * @covers Parser::getPreloadText
166 public function testGetPreloadText() {
167 $title = Title
::newFromText( __FUNCTION__
);
168 $outputText = $this->parser
->getPreloadText(
169 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
174 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
178 * @param Title $title
179 * @param bool $parser
183 static function statelessFetchTemplate( $title, $parser = false ) {
184 $text = "Content of ''" . $title->getFullText() . "''";
189 'finalTitle' => $title,
195 * @covers Parser::parse
197 public function testTrackingCategory() {
198 $title = Title
::newFromText( __FUNCTION__
);
199 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
200 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
201 $expected = [ $cat->getDBkey() ];
202 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
203 $result = $parserOutput->getCategoryLinks();
204 $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 );