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->options
->setWrapOutputClass( false );
30 $this->parser
= new Parser
;
32 MagicWord
::clearCache();
37 * @covers Parser::parse
39 public function testLongNumericLinesDontKillTheParser() {
40 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
42 $title = Title
::newFromText( 'Unit test' );
43 $options = ParserOptions
::newFromUser( new User() );
44 $options->setWrapOutputClass( false );
45 $this->assertEquals( "<p>$longLine</p>",
46 $this->parser
->parse( $longLine, $title, $options )->getText() );
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()
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,
197 * @covers Parser::parse
199 public function testTrackingCategory() {
200 $title = Title
::newFromText( __FUNCTION__
);
201 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
202 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
203 $expected = [ $cat->getDBkey() ];
204 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
205 $result = $parserOutput->getCategoryLinks();
206 $this->assertEquals( $expected, $result );
211 * @covers Parser::parse
213 public function testTrackingCategorySpecial() {
214 // Special pages shouldn't have tracking cats.
215 $title = SpecialPage
::getTitleFor( 'Contributions' );
216 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
217 $result = $parserOutput->getCategoryLinks();
218 $this->assertEmpty( $result );