11a660499f54fa68a88d12a7eaf33c45311b4a54
[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 MagicWord::clearCache();
26 }
27
28 // Bug 8689 - Long numeric lines kill the parser
29 function testBug8689() {
30 global $wgLang;
31 global $wgUser;
32 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
33
34 if ( $wgLang === null ) $wgLang = new Language;
35
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() );
40 }
41
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() );
47 }
48
49 function testPreSaveTransform() {
50 global $wgUser;
51 $title = Title::newFromText( __FUNCTION__ );
52 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
53
54 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
55 }
56
57 function testPreprocess() {
58 $title = Title::newFromText( __FUNCTION__ );
59 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
60
61 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
62 }
63
64 /**
65 * cleanSig() makes all templates substs and removes tildes
66 */
67 function testCleanSig() {
68 global $wgCleanSignatures;
69 $oldCleanSignature = $wgCleanSignatures;
70 $wgCleanSignatures = true;
71
72 $title = Title::newFromText( __FUNCTION__ );
73 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
74
75 $wgCleanSignatures = $oldCleanSignature;
76
77 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
78 }
79
80 /**
81 * cleanSig() should do nothing if disabled
82 */
83 function testCleanSigDisabled() {
84 global $wgCleanSignatures;
85 $oldCleanSignature = $wgCleanSignatures;
86 $wgCleanSignatures = false;
87
88 $title = Title::newFromText( __FUNCTION__ );
89 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
90
91 $wgCleanSignatures = $oldCleanSignature;
92
93 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
94 }
95
96 /**
97 * cleanSigInSig() just removes tildes
98 * @dataProvider provideStringsForCleanSigInSig
99 */
100 function testCleanSigInSig( $in, $out ) {
101 $this->assertEquals( Parser::cleanSigInSig( $in), $out );
102 }
103
104 function provideStringsForCleanSigInSig() {
105 return array(
106 array( "{{Foo}} ~~~~", "{{Foo}} " ),
107 array( "~~~", "" ),
108 array( "~~~~~", "" ),
109 );
110 }
111
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 );
115
116 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
117 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
118 }
119
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" );
122
123 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
124 }
125
126 /**
127 * Templates and comments are not affected, but noinclude/onlyinclude is.
128 */
129 function testGetPreloadText() {
130 $title = Title::newFromText( __FUNCTION__ );
131 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
132
133 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
134 }
135
136 static function statelessFetchTemplate( $title, $parser=false ) {
137 $text = "Content of ''" . $title->getFullText() . "''";
138 $deps = array();
139
140 return array(
141 'text' => $text,
142 'finalTitle' => $title,
143 'deps' => $deps );
144 }
145
146 /**
147 * @group Database
148 */
149 function testTrackingCategory() {
150 $title = Title::newFromText( __FUNCTION__ );
151 $catName = wfMsgForContent( 'broken-file-category' );
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 );
157 }
158
159 /**
160 * @group Database
161 */
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 );
168 }
169 }