Make the testCleanSig work no matter what $wgCleanSignatures; is in LocalSettings...
[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 global $wgAlwaysUseTidy;
14
15 $wgShowDBErrorBacktrace = true;
16 $wgLanguageCode = 'en';
17 $wgContLang = new Language( 'en' );
18 $wgMemc = new EmptyBagOStuff;
19 $wgAlwaysUseTidy = false;
20
21 $this->options = new ParserOptions;
22 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
23 $this->parser = new Parser;
24 }
25
26 // Bug 8689 - Long numeric lines kill the parser
27 function testBug8689() {
28 global $wgLang;
29 global $wgUser;
30 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
31
32 if ( $wgLang === null ) $wgLang = new Language;
33
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() );
38 }
39
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() );
45 }
46
47 function testPreSaveTransform() {
48 global $wgUser;
49 $title = Title::newFromText( __FUNCTION__ );
50 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
51
52 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
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 global $wgCleanSignatures;
67 $oldCleanSignature = $wgCleanSignatures;
68 $wgCleanSignatures = true;
69
70 $title = Title::newFromText( __FUNCTION__ );
71 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
72
73 $wgCleanSignatures = $oldCleanSignature;
74
75 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
76 }
77
78 /**
79 * cleanSig() should do nothing if disabled
80 */
81 function testCleanSigDisabled() {
82 global $wgCleanSignatures;
83 $oldCleanSignature = $wgCleanSignatures;
84 $wgCleanSignatures = false;
85
86 $title = Title::newFromText( __FUNCTION__ );
87 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
88
89 $wgCleanSignatures = $oldCleanSignature;
90
91 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
92 }
93
94 /**
95 * cleanSigInSig() just removes tildes
96 */
97 function testCleanSigInSig() {
98 $title = Title::newFromText( __FUNCTION__ );
99 $outputText = $this->parser->cleanSigInSig( "{{Foo}} ~~~~" );
100
101 $this->assertEquals( "{{Foo}} ", $outputText );
102 }
103
104 function testGetSection() {
105 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
106 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
107
108 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
109 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
110 }
111
112 function testReplaceSection() {
113 $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" );
114
115 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
116 }
117
118 /**
119 * Templates and comments are not affected, but noinclude/onlyinclude is.
120 */
121 function testGetPreloadText() {
122 $title = Title::newFromText( __FUNCTION__ );
123 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
124
125 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
126 }
127
128 static function statelessFetchTemplate( $title, $parser=false ) {
129 $text = "Content of ''" . $title->getFullText() . "''";
130 $deps = array();
131
132 return array(
133 'text' => $text,
134 'finalTitle' => $title,
135 'deps' => $deps );
136 }
137 }