4 * Parser-related tests that don't suit for parserTests.txt
6 class ExtraParserTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $contLang = Language
::factory( 'en' );
12 $this->setMwGlobals( array(
13 'wgShowDBErrorBacktrace' => true,
14 'wgLanguageCode' => 'en',
15 'wgContLang' => $contLang,
16 'wgLang' => Language
::factory( 'en' ),
17 'wgMemc' => new EmptyBagOStuff
,
18 'wgAlwaysUseTidy' => false,
19 'wgCleanSignatures' => true,
22 $this->options
= ParserOptions
::newFromUserAndLang( new User
, $contLang );
23 $this->options
->setTemplateCallback( array( __CLASS__
, 'statelessFetchTemplate' ) );
24 $this->parser
= new Parser
;
26 MagicWord
::clearCache();
30 * Bug 8689 - Long numeric lines kill the parser
34 function testBug8689() {
36 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
38 $t = Title
::newFromText( 'Unit test' );
39 $options = ParserOptions
::newFromUser( $wgUser );
40 $this->assertEquals( "<p>$longLine</p>",
41 $this->parser
->parse( $longLine, $t, $options )->getText() );
45 * Test the parser entry points
49 function testParse() {
50 $title = Title
::newFromText( __FUNCTION__
);
51 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
52 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
58 function testPreSaveTransform() {
60 $title = Title
::newFromText( __FUNCTION__
);
61 $outputText = $this->parser
->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options
);
63 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
69 function testPreprocess() {
70 $title = Title
::newFromText( __FUNCTION__
);
71 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options
);
73 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
77 * cleanSig() makes all templates substs and removes tildes
79 function testCleanSig() {
80 $title = Title
::newFromText( __FUNCTION__
);
81 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
83 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
87 * cleanSig() should do nothing if disabled
89 function testCleanSigDisabled() {
90 global $wgCleanSignatures;
91 $wgCleanSignatures = false;
93 $title = Title
::newFromText( __FUNCTION__
);
94 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
96 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
100 * cleanSigInSig() just removes tildes
101 * @dataProvider provideStringsForCleanSigInSig
103 function testCleanSigInSig( $in, $out ) {
104 $this->assertEquals( Parser
::cleanSigInSig( $in ), $out );
107 public static function provideStringsForCleanSigInSig() {
109 array( "{{Foo}} ~~~~", "{{Foo}} " ),
111 array( "~~~~~", "" ),
115 function testGetSection() {
116 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
117 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
119 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
120 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
123 function testReplaceSection() {
124 $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" );
126 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
130 * Templates and comments are not affected, but noinclude/onlyinclude is.
132 function testGetPreloadText() {
133 $title = Title
::newFromText( __FUNCTION__
);
134 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
136 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
139 static function statelessFetchTemplate( $title, $parser = false ) {
140 $text = "Content of ''" . $title->getFullText() . "''";
145 'finalTitle' => $title,
152 function testTrackingCategory() {
153 $title = Title
::newFromText( __FUNCTION__
);
154 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
155 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
156 $expected = array( $cat->getDBkey() );
157 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
158 $result = $parserOutput->getCategoryLinks();
159 $this->assertEquals( $expected, $result );
165 function testTrackingCategorySpecial() {
166 // Special pages shouldn't have tracking cats.
167 $title = SpecialPage
::getTitleFor( 'Contributions' );
168 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]", $title, $this->options
);
169 $result = $parserOutput->getCategoryLinks();
170 $this->assertEmpty( $result );