4 * Parser-related tests that don't suit for parserTests.txt
6 class ExtraParserTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( array(
12 'wgShowDBErrorBacktrace' => true,
13 'wgLanguageCode' => 'en',
14 'wgContLang' => Language
::factory( 'en' ),
15 'wgLang' => Language
::factory( 'en' ),
16 'wgMemc' => new EmptyBagOStuff
,
17 'wgAlwaysUseTidy' => false,
18 'wgCleanSignatures' => true,
21 $this->options
= new ParserOptions
;
22 $this->options
->setTemplateCallback( array( __CLASS__
, 'statelessFetchTemplate' ) );
23 $this->parser
= new Parser
;
25 MagicWord
::clearCache();
28 // Bug 8689 - Long numeric lines kill the parser
29 function testBug8689() {
31 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
33 $t = Title
::newFromText( 'Unit test' );
34 $options = ParserOptions
::newFromUser( $wgUser );
35 $this->assertEquals( "<p>$longLine</p>",
36 $this->parser
->parse( $longLine, $t, $options )->getText() );
39 /* Test the parser entry points */
40 function testParse() {
41 $title = Title
::newFromText( __FUNCTION__
);
42 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options
);
43 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
46 function testPreSaveTransform() {
48 $title = Title
::newFromText( __FUNCTION__
);
49 $outputText = $this->parser
->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options
);
51 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
54 function testPreprocess() {
55 $title = Title
::newFromText( __FUNCTION__
);
56 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options
);
58 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
62 * cleanSig() makes all templates substs and removes tildes
64 function testCleanSig() {
65 $title = Title
::newFromText( __FUNCTION__
);
66 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
68 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
72 * cleanSig() should do nothing if disabled
74 function testCleanSigDisabled() {
75 global $wgCleanSignatures;
76 $wgCleanSignatures = false;
78 $title = Title
::newFromText( __FUNCTION__
);
79 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
81 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
85 * cleanSigInSig() just removes tildes
86 * @dataProvider provideStringsForCleanSigInSig
88 function testCleanSigInSig( $in, $out ) {
89 $this->assertEquals( Parser
::cleanSigInSig( $in), $out );
92 public static function provideStringsForCleanSigInSig() {
94 array( "{{Foo}} ~~~~", "{{Foo}} " ),
100 function testGetSection() {
101 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
102 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
104 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
105 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
108 function testReplaceSection() {
109 $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" );
111 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
115 * Templates and comments are not affected, but noinclude/onlyinclude is.
117 function testGetPreloadText() {
118 $title = Title
::newFromText( __FUNCTION__
);
119 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
121 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
124 static function statelessFetchTemplate( $title, $parser=false ) {
125 $text = "Content of ''" . $title->getFullText() . "''";
130 'finalTitle' => $title,
137 function testTrackingCategory() {
138 $title = Title
::newFromText( __FUNCTION__
);
139 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
140 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
141 $expected = array( $cat->getDBkey() );
142 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]" , $title, $this->options
);
143 $result = $parserOutput->getCategoryLinks();
144 $this->assertEquals( $expected, $result );
150 function testTrackingCategorySpecial() {
151 // Special pages shouldn't have tracking cats.
152 $title = SpecialPage
::getTitleFor( 'Contributions' );
153 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]" , $title, $this->options
);
154 $result = $parserOutput->getCategoryLinks();
155 $this->assertEmpty( $result );