4 * Parser-related tests that don't suit for parserTests.txt
6 class ExtraParserTest
extends MediaWikiTestCase
{
11 global $wgShowDBErrorBacktrace;
12 global $wgLanguageCode;
13 global $wgAlwaysUseTidy;
15 $wgShowDBErrorBacktrace = true;
16 $wgLanguageCode = 'en';
17 $wgContLang = new Language( 'en' );
18 $wgMemc = new EmptyBagOStuff
;
19 $wgAlwaysUseTidy = false;
21 $this->options
= new ParserOptions
;
22 $this->options
->setTemplateCallback( array( __CLASS__
, 'statelessFetchTemplate' ) );
23 $this->parser
= new Parser
;
26 // Bug 8689 - Long numeric lines kill the parser
27 function testBug8689() {
30 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
32 if ( $wgLang === null ) $wgLang = new Language
;
34 $t = Title
::newFromText( 'Unit test' );
35 $options = ParserOptions
::newFromUser( $wgUser );
36 $this->assertEquals( "<p>$longLine</p>",
37 $this->parser
->parse( $longLine, $t, $options )->getText() );
40 /* Test the parser entry points */
41 function testParse() {
42 $title = Title
::newFromText( __FUNCTION__
);
43 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options
);
44 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
47 function testPreSaveTransform() {
49 $title = Title
::newFromText( __FUNCTION__
);
50 $outputText = $this->parser
->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options
);
52 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
55 function testPreprocess() {
56 $title = Title
::newFromText( __FUNCTION__
);
57 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options
);
59 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
63 * cleanSig() makes all templates substs and removes tildes
65 function testCleanSig() {
66 global $wgCleanSignatures;
67 $oldCleanSignature = $wgCleanSignatures;
68 $wgCleanSignatures = true;
70 $title = Title
::newFromText( __FUNCTION__
);
71 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
73 $wgCleanSignatures = $oldCleanSignature;
75 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
79 * cleanSig() should do nothing if disabled
81 function testCleanSigDisabled() {
82 global $wgCleanSignatures;
83 $oldCleanSignature = $wgCleanSignatures;
84 $wgCleanSignatures = false;
86 $title = Title
::newFromText( __FUNCTION__
);
87 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
89 $wgCleanSignatures = $oldCleanSignature;
91 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
95 * cleanSigInSig() just removes tildes
97 function testCleanSigInSig() {
98 $title = Title
::newFromText( __FUNCTION__
);
99 $outputText = $this->parser
->cleanSigInSig( "{{Foo}} ~~~~" );
101 $this->assertEquals( "{{Foo}} ", $outputText );
104 function testGetSection() {
105 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
106 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
108 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
109 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
112 function testReplaceSection() {
113 $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" );
115 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
119 * Templates and comments are not affected, but noinclude/onlyinclude is.
121 function testGetPreloadText() {
122 $title = Title
::newFromText( __FUNCTION__
);
123 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
125 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
128 static function statelessFetchTemplate( $title, $parser=false ) {
129 $text = "Content of ''" . $title->getFullText() . "''";
134 'finalTitle' => $title,