Also change $wgLanguageCode along with $wgContLang, this was breaking when arriving...
[lhc/web/wiklou.git] / tests / phpunit / includes / ExtraParserTest.php
1 <?php
2
3 /**
4 * Parser-related tests that don't suit for parserTests.txt
5 */
6 class ExtraParserTest extends MediaWikiTestCase {
7
8 function setUp() {
9 global $wgMemc;
10 global $wgContLang;
11 global $wgShowDBErrorBacktrace;
12 global $wgLanguageCode;
13
14 $wgShowDBErrorBacktrace = true;
15 $wgLanguageCode = 'en';
16 $wgContLang = new Language( 'en' );
17 $wgMemc = new EmptyBagOStuff;
18
19 $this->options = new ParserOptions;
20 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
21 $this->parser = new Parser;
22 }
23
24 // Bug 8689 - Long numeric lines kill the parser
25 function testBug8689() {
26 global $wgLang;
27 global $wgUser;
28 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
29
30 if ( $wgLang === null ) $wgLang = new Language;
31
32 $t = Title::newFromText( 'Unit test' );
33 $options = ParserOptions::newFromUser( $wgUser );
34 $this->assertEquals( "<p>$longLine</p>",
35 $this->parser->parse( $longLine, $t, $options )->getText() );
36 }
37
38 /* Test the parser entry points */
39 function testParse() {
40 $title = Title::newFromText( __FUNCTION__ );
41 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
42 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
43 }
44
45 function testPreSaveTransform() {
46 global $wgUser, $wgTitle;
47 $title = Title::newFromText( __FUNCTION__ );
48 $oldTitle = $wgTitle; $wgTitle = $title; # Used by transformMsg()
49 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
50
51 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
52 $wgTitle = $oldTitle;
53 }
54
55 function testPreprocess() {
56 $title = Title::newFromText( __FUNCTION__ );
57 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
58
59 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
60 }
61
62 /**
63 * cleanSig() makes all templates substs and removes tildes
64 */
65 function testCleanSig() {
66 $title = Title::newFromText( __FUNCTION__ );
67 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
68
69 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
70 }
71
72 /**
73 * cleanSigInSig() just removes tildes
74 */
75 function testCleanSigInSig() {
76 $title = Title::newFromText( __FUNCTION__ );
77 $outputText = $this->parser->cleanSigInSig( "{{Foo}} ~~~~" );
78
79 $this->assertEquals( "{{Foo}} ", $outputText );
80 }
81
82 function testGetSection() {
83 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
84 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
85
86 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
87 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
88 }
89
90 function testReplaceSection() {
91 $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" );
92
93 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
94 }
95
96 /**
97 * Templates and comments are not affected, but noinclude/onlyinclude is.
98 */
99 function testGetPreloadText() {
100 $title = Title::newFromText( __FUNCTION__ );
101 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
102
103 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
104 }
105
106 static function statelessFetchTemplate( $title, $parser=false ) {
107 $text = "Content of ''" . $title->getFullText() . "''";
108 $deps = array();
109
110 return array(
111 'text' => $text,
112 'finalTitle' => $title,
113 'deps' => $deps );
114 }
115 }