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();
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 array( "{{Foo}} ~~~~", "{{Foo}} " ),
125 array( "~~~~~", "" ),
130 * @covers Parser::getSection
132 public function testGetSection() {
133 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
134 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
136 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
137 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
141 * @covers Parser::replaceSection
143 public function testReplaceSection() {
144 $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" );
146 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
150 * Templates and comments are not affected, but noinclude/onlyinclude is.
151 * @covers Parser::getPreloadText
153 public function testGetPreloadText() {
154 $title = Title
::newFromText( __FUNCTION__
);
155 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
157 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
161 * @param Title $title
162 * @param bool $parser
166 static function statelessFetchTemplate( $title, $parser = false ) {
167 $text = "Content of ''" . $title->getFullText() . "''";
172 'finalTitle' => $title,
178 * @covers Parser::parse
180 public function testTrackingCategory() {
181 $title = Title
::newFromText( __FUNCTION__
);
182 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
183 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
184 $expected = array( $cat->getDBkey() );
185 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
186 $result = $parserOutput->getCategoryLinks();
187 $this->assertEquals( $expected, $result );
192 * @covers Parser::parse
194 public function testTrackingCategorySpecial() {
195 // Special pages shouldn't have tracking cats.
196 $title = SpecialPage
::getTitleFor( 'Contributions' );
197 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
198 $result = $parserOutput->getCategoryLinks();
199 $this->assertEmpty( $result );