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
96 * @dataProvider provideStringsForCleanSigInSig
98 function testCleanSigInSig( $in, $out ) {
99 $this->assertEquals( Parser
::cleanSigInSig( $in), $out );
102 function provideStringsForCleanSigInSig() {
104 array( "{{Foo}} ~~~~", "{{Foo}} " ),
106 array( "~~~~~", "" ),
110 function testGetSection() {
111 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
112 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
114 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
115 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
118 function testReplaceSection() {
119 $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" );
121 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
125 * Templates and comments are not affected, but noinclude/onlyinclude is.
127 function testGetPreloadText() {
128 $title = Title
::newFromText( __FUNCTION__
);
129 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
131 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
134 static function statelessFetchTemplate( $title, $parser=false ) {
135 $text = "Content of ''" . $title->getFullText() . "''";
140 'finalTitle' => $title,
143 function testTrackingCategory() {
144 $title = Title
::newFromText( __FUNCTION__
);
145 $catName = wfMsgForContent( 'broken-file-category' );
146 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
147 $expected = array( $cat->getDBkey() );
148 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]" , $title, $this->options
);
149 $result = $parserOutput->getCategoryLinks();
150 $this->assertEquals( $expected, $result );
152 function testTrackingCategorySpecial() {
153 // Special pages shouldn't have tracking cats.
154 $title = SpecialPage
::getTitleFor( 'Contributions' );
155 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]" , $title, $this->options
);
156 $result = $parserOutput->getCategoryLinks();
157 $this->assertEmpty( $result );