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( array(
20 'wgShowDBErrorBacktrace' => true,
21 'wgLanguageCode' => 'en',
22 'wgContLang' => $contLang,
23 'wgLang' => Language
::factory( 'en' ),
24 'wgMemc' => new EmptyBagOStuff
,
25 'wgCleanSignatures' => true,
28 $this->options
= ParserOptions
::newFromUserAndLang( new User
, $contLang );
29 $this->options
->setTemplateCallback( array( __CLASS__
, 'statelessFetchTemplate' ) );
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 $this->assertEquals( "<p>$longLine</p>",
45 $this->parser
->parse( $longLine, $title, $options )->getText() );
49 * Test the parser entry points
50 * @covers Parser::parse
52 public function testParse() {
53 $title = Title
::newFromText( __FUNCTION__
);
54 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
56 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
57 $parserOutput->getText()
62 * @covers Parser::preSaveTransform
64 public function testPreSaveTransform() {
65 $title = Title
::newFromText( __FUNCTION__
);
66 $outputText = $this->parser
->preSaveTransform(
67 "Test\r\n{{subst:Foo}}\n{{Bar}}",
73 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
77 * @covers Parser::preprocess
79 public function testPreprocess() {
80 $title = Title
::newFromText( __FUNCTION__
);
81 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
84 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
90 * cleanSig() makes all templates substs and removes tildes
91 * @covers Parser::cleanSig
93 public function testCleanSig() {
94 $title = Title
::newFromText( __FUNCTION__
);
95 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
97 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
101 * cleanSig() should do nothing if disabled
102 * @covers Parser::cleanSig
104 public function testCleanSigDisabled() {
105 $this->setMwGlobals( 'wgCleanSignatures', false );
107 $title = Title
::newFromText( __FUNCTION__
);
108 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
110 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
114 * cleanSigInSig() just removes tildes
115 * @dataProvider provideStringsForCleanSigInSig
116 * @covers Parser::cleanSigInSig
118 public function testCleanSigInSig( $in, $out ) {
119 $this->assertEquals( Parser
::cleanSigInSig( $in ), $out );
122 public static function provideStringsForCleanSigInSig() {
124 array( "{{Foo}} ~~~~", "{{Foo}} " ),
126 array( "~~~~~", "" ),
131 * @covers Parser::getSection
133 public function testGetSection() {
134 $outputText2 = $this->parser
->getSection(
135 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
136 . "Section 2\n== Heading 3 ==\nSection 3\n",
139 $outputText1 = $this->parser
->getSection(
140 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
141 . "Section 2\n== Heading 3 ==\nSection 3\n",
145 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
146 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
150 * @covers Parser::replaceSection
152 public function testReplaceSection() {
153 $outputText = $this->parser
->replaceSection(
154 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
155 . "Section 2\n== Heading 3 ==\nSection 3\n",
160 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
164 * Templates and comments are not affected, but noinclude/onlyinclude is.
165 * @covers Parser::getPreloadText
167 public function testGetPreloadText() {
168 $title = Title
::newFromText( __FUNCTION__
);
169 $outputText = $this->parser
->getPreloadText(
170 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
175 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
179 * @param Title $title
180 * @param bool $parser
184 static function statelessFetchTemplate( $title, $parser = false ) {
185 $text = "Content of ''" . $title->getFullText() . "''";
190 '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 = array( $cat->getDBkey() );
203 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
204 $result = $parserOutput->getCategoryLinks();
205 $this->assertEquals( $expected, $result );
210 * @covers Parser::parse
212 public function testTrackingCategorySpecial() {
213 // Special pages shouldn't have tracking cats.
214 $title = SpecialPage
::getTitleFor( 'Contributions' );
215 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
216 $result = $parserOutput->getCategoryLinks();
217 $this->assertEmpty( $result );