903a6d2593edee7e8911ca2d553922552f926af0
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
;
25 MagicWord
::clearCache();
28 // Bug 8689 - Long numeric lines kill the parser
29 function testBug8689() {
32 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
34 if ( $wgLang === null ) $wgLang = new Language
;
36 $t = Title
::newFromText( 'Unit test' );
37 $options = ParserOptions
::newFromUser( $wgUser );
38 $this->assertEquals( "<p>$longLine</p>",
39 $this->parser
->parse( $longLine, $t, $options )->getText() );
42 /* Test the parser entry points */
43 function testParse() {
44 $title = Title
::newFromText( __FUNCTION__
);
45 $parserOutput = $this->parser
->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options
);
46 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
49 function testPreSaveTransform() {
51 $title = Title
::newFromText( __FUNCTION__
);
52 $outputText = $this->parser
->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options
);
54 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
57 function testPreprocess() {
58 $title = Title
::newFromText( __FUNCTION__
);
59 $outputText = $this->parser
->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options
);
61 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
65 * cleanSig() makes all templates substs and removes tildes
67 function testCleanSig() {
68 global $wgCleanSignatures;
69 $oldCleanSignature = $wgCleanSignatures;
70 $wgCleanSignatures = true;
72 $title = Title
::newFromText( __FUNCTION__
);
73 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
75 $wgCleanSignatures = $oldCleanSignature;
77 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
81 * cleanSig() should do nothing if disabled
83 function testCleanSigDisabled() {
84 global $wgCleanSignatures;
85 $oldCleanSignature = $wgCleanSignatures;
86 $wgCleanSignatures = false;
88 $title = Title
::newFromText( __FUNCTION__
);
89 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
91 $wgCleanSignatures = $oldCleanSignature;
93 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
97 * cleanSigInSig() just removes tildes
98 * @dataProvider provideStringsForCleanSigInSig
100 function testCleanSigInSig( $in, $out ) {
101 $this->assertEquals( Parser
::cleanSigInSig( $in), $out );
104 function provideStringsForCleanSigInSig() {
106 array( "{{Foo}} ~~~~", "{{Foo}} " ),
108 array( "~~~~~", "" ),
112 function testGetSection() {
113 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
114 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
116 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
117 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
120 function testReplaceSection() {
121 $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" );
123 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
127 * Templates and comments are not affected, but noinclude/onlyinclude is.
129 function testGetPreloadText() {
130 $title = Title
::newFromText( __FUNCTION__
);
131 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
133 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
136 static function statelessFetchTemplate( $title, $parser=false ) {
137 $text = "Content of ''" . $title->getFullText() . "''";
142 'finalTitle' => $title,
149 function testTrackingCategory() {
150 $title = Title
::newFromText( __FUNCTION__
);
151 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
152 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
153 $expected = array( $cat->getDBkey() );
154 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]" , $title, $this->options
);
155 $result = $parserOutput->getCategoryLinks();
156 $this->assertEquals( $expected, $result );
162 function testTrackingCategorySpecial() {
163 // Special pages shouldn't have tracking cats.
164 $title = SpecialPage
::getTitleFor( 'Contributions' );
165 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]" , $title, $this->options
);
166 $result = $parserOutput->getCategoryLinks();
167 $this->assertEmpty( $result );