4 * Parser-related tests that don't suit for parserTests.txt
6 class ExtraParserTest
extends MediaWikiTestCase
{
8 /** @var ParserOptions */
13 protected function setUp() {
16 $contLang = Language
::factory( 'en' );
17 $this->setMwGlobals( array(
18 'wgShowDBErrorBacktrace' => true,
19 'wgLanguageCode' => 'en',
20 'wgContLang' => $contLang,
21 'wgLang' => Language
::factory( 'en' ),
22 'wgMemc' => new EmptyBagOStuff
,
23 'wgAlwaysUseTidy' => false,
24 'wgCleanSignatures' => true,
27 $this->options
= ParserOptions
::newFromUserAndLang( new User
, $contLang );
28 $this->options
->setTemplateCallback( array( __CLASS__
, 'statelessFetchTemplate' ) );
29 $this->parser
= new Parser
;
31 MagicWord
::clearCache();
35 * Bug 8689 - Long numeric lines kill the parser
36 * @covers Parser::parse
38 public function testBug8689() {
40 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
42 $t = Title
::newFromText( 'Unit test' );
43 $options = ParserOptions
::newFromUser( $wgUser );
44 $this->assertEquals( "<p>$longLine</p>",
45 $this->parser
->parse( $longLine, $t, $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
);
55 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
59 * @covers Parser::preSaveTransform
61 public function testPreSaveTransform() {
63 $title = Title
::newFromText( __FUNCTION__
);
64 $outputText = $this->parser
->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options
);
66 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
70 * @covers Parser::preprocess
72 public function testPreprocess() {
73 $title = Title
::newFromText( __FUNCTION__
);
74 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
76 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
80 * cleanSig() makes all templates substs and removes tildes
81 * @covers Parser::cleanSig
83 public function testCleanSig() {
84 $title = Title
::newFromText( __FUNCTION__
);
85 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
87 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
91 * cleanSig() should do nothing if disabled
92 * @covers Parser::cleanSig
94 public function testCleanSigDisabled() {
95 $this->setMwGlobals( 'wgCleanSignatures', false );
97 $title = Title
::newFromText( __FUNCTION__
);
98 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
100 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
104 * cleanSigInSig() just removes tildes
105 * @dataProvider provideStringsForCleanSigInSig
106 * @covers Parser::cleanSigInSig
108 public function testCleanSigInSig( $in, $out ) {
109 $this->assertEquals( Parser
::cleanSigInSig( $in ), $out );
112 public static function provideStringsForCleanSigInSig() {
114 array( "{{Foo}} ~~~~", "{{Foo}} " ),
116 array( "~~~~~", "" ),
121 * @covers Parser::getSection
123 public function testGetSection() {
124 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
125 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
127 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
128 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
132 * @covers Parser::replaceSection
134 public function testReplaceSection() {
135 $outputText = $this->parser
->replaceSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1, "New section 1" );
137 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
141 * Templates and comments are not affected, but noinclude/onlyinclude is.
142 * @covers Parser::getPreloadText
144 public function testGetPreloadText() {
145 $title = Title
::newFromText( __FUNCTION__
);
146 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
148 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
151 static function statelessFetchTemplate( $title, $parser = false ) {
152 $text = "Content of ''" . $title->getFullText() . "''";
157 'finalTitle' => $title,
163 * @covers Parser::parse
165 public function testTrackingCategory() {
166 $title = Title
::newFromText( __FUNCTION__
);
167 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
168 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
169 $expected = array( $cat->getDBkey() );
170 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
171 $result = $parserOutput->getCategoryLinks();
172 $this->assertEquals( $expected, $result );
177 * @covers Parser::parse
179 public function testTrackingCategorySpecial() {
180 // Special pages shouldn't have tracking cats.
181 $title = SpecialPage
::getTitleFor( 'Contributions' );
182 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
183 $result = $parserOutput->getCategoryLinks();
184 $this->assertEmpty( $result );