Removed hidden usage of $wgTitle in parser and the workarround in ExtraParserTest.php
[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;
47 $title = Title::newFromText( __FUNCTION__ );
48 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
49
50 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
51 }
52
53 function testPreprocess() {
54 $title = Title::newFromText( __FUNCTION__ );
55 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
56
57 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
58 }
59
60 /**
61 * cleanSig() makes all templates substs and removes tildes
62 */
63 function testCleanSig() {
64 $title = Title::newFromText( __FUNCTION__ );
65 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
66
67 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
68 }
69
70 /**
71 * cleanSigInSig() just removes tildes
72 */
73 function testCleanSigInSig() {
74 $title = Title::newFromText( __FUNCTION__ );
75 $outputText = $this->parser->cleanSigInSig( "{{Foo}} ~~~~" );
76
77 $this->assertEquals( "{{Foo}} ", $outputText );
78 }
79
80 function testGetSection() {
81 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
82 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
83
84 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
85 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
86 }
87
88 function testReplaceSection() {
89 $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" );
90
91 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
92 }
93
94 /**
95 * Templates and comments are not affected, but noinclude/onlyinclude is.
96 */
97 function testGetPreloadText() {
98 $title = Title::newFromText( __FUNCTION__ );
99 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
100
101 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
102 }
103
104 static function statelessFetchTemplate( $title, $parser=false ) {
105 $text = "Content of ''" . $title->getFullText() . "''";
106 $deps = array();
107
108 return array(
109 'text' => $text,
110 'finalTitle' => $title,
111 'deps' => $deps );
112 }
113 }