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();
29 // Bug 8689 - Long numeric lines kill the parser
30 function testBug8689() {
32 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
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 $title = Title
::newFromText( __FUNCTION__
);
67 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
69 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
73 * cleanSig() should do nothing if disabled
75 function testCleanSigDisabled() {
76 global $wgCleanSignatures;
77 $wgCleanSignatures = false;
79 $title = Title
::newFromText( __FUNCTION__
);
80 $outputText = $this->parser
->cleanSig( "{{Foo}} ~~~~" );
82 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
86 * cleanSigInSig() just removes tildes
87 * @dataProvider provideStringsForCleanSigInSig
89 function testCleanSigInSig( $in, $out ) {
90 $this->assertEquals( Parser
::cleanSigInSig( $in), $out );
93 public static function provideStringsForCleanSigInSig() {
95 array( "{{Foo}} ~~~~", "{{Foo}} " ),
101 function testGetSection() {
102 $outputText2 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
103 $outputText1 = $this->parser
->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
105 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
106 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
109 function testReplaceSection() {
110 $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" );
112 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
116 * Templates and comments are not affected, but noinclude/onlyinclude is.
118 function testGetPreloadText() {
119 $title = Title
::newFromText( __FUNCTION__
);
120 $outputText = $this->parser
->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options
);
122 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
125 static function statelessFetchTemplate( $title, $parser=false ) {
126 $text = "Content of ''" . $title->getFullText() . "''";
131 'finalTitle' => $title,
138 function testTrackingCategory() {
139 $title = Title
::newFromText( __FUNCTION__
);
140 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
141 $cat = Title
::makeTitleSafe( NS_CATEGORY
, $catName );
142 $expected = array( $cat->getDBkey() );
143 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]" , $title, $this->options
);
144 $result = $parserOutput->getCategoryLinks();
145 $this->assertEquals( $expected, $result );
151 function testTrackingCategorySpecial() {
152 // Special pages shouldn't have tracking cats.
153 $title = SpecialPage
::getTitleFor( 'Contributions' );
154 $parserOutput = $this->parser
->parse( "[[file:nonexistent]]" , $title, $this->options
);
155 $result = $parserOutput->getCategoryLinks();
156 $this->assertEmpty( $result );