Merge "Tests: Fix case of some methods"
[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 protected function setUp() {
9 parent::setUp();
10
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,
20 ) );
21
22 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
23 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
24 $this->parser = new Parser;
25
26 MagicWord::clearCache();
27 }
28
29 /**
30 * Bug 8689 - Long numeric lines kill the parser
31 *
32 * @group Database
33 */
34 function testBug8689() {
35 global $wgUser;
36 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
37
38 $t = Title::newFromText( 'Unit test' );
39 $options = ParserOptions::newFromUser( $wgUser );
40 $this->assertEquals( "<p>$longLine</p>",
41 $this->parser->parse( $longLine, $t, $options )->getText() );
42 }
43
44 /**
45 * Test the parser entry points
46 *
47 * @group Database
48 */
49 function testParse() {
50 $title = Title::newFromText( __FUNCTION__ );
51 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
52 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
53 }
54
55 /**
56 * @group Database
57 */
58 function testPreSaveTransform() {
59 global $wgUser;
60 $title = Title::newFromText( __FUNCTION__ );
61 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
62
63 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
64 }
65
66 /**
67 * @group Database
68 */
69 function testPreprocess() {
70 $title = Title::newFromText( __FUNCTION__ );
71 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
72
73 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
74 }
75
76 /**
77 * cleanSig() makes all templates substs and removes tildes
78 */
79 function testCleanSig() {
80 $title = Title::newFromText( __FUNCTION__ );
81 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
82
83 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
84 }
85
86 /**
87 * cleanSig() should do nothing if disabled
88 */
89 function testCleanSigDisabled() {
90 $this->setMwGlobals( 'wgCleanSignatures', false );
91
92 $title = Title::newFromText( __FUNCTION__ );
93 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
94
95 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
96 }
97
98 /**
99 * cleanSigInSig() just removes tildes
100 * @dataProvider provideStringsForCleanSigInSig
101 */
102 function testCleanSigInSig( $in, $out ) {
103 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
104 }
105
106 public static function provideStringsForCleanSigInSig() {
107 return array(
108 array( "{{Foo}} ~~~~", "{{Foo}} " ),
109 array( "~~~", "" ),
110 array( "~~~~~", "" ),
111 );
112 }
113
114 function testGetSection() {
115 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
116 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
117
118 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
119 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
120 }
121
122 function testReplaceSection() {
123 $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" );
124
125 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
126 }
127
128 /**
129 * Templates and comments are not affected, but noinclude/onlyinclude is.
130 */
131 function testGetPreloadText() {
132 $title = Title::newFromText( __FUNCTION__ );
133 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
134
135 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
136 }
137
138 static function statelessFetchTemplate( $title, $parser = false ) {
139 $text = "Content of ''" . $title->getFullText() . "''";
140 $deps = array();
141
142 return array(
143 'text' => $text,
144 'finalTitle' => $title,
145 'deps' => $deps );
146 }
147
148 /**
149 * @group Database
150 */
151 function testTrackingCategory() {
152 $title = Title::newFromText( __FUNCTION__ );
153 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
154 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
155 $expected = array( $cat->getDBkey() );
156 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
157 $result = $parserOutput->getCategoryLinks();
158 $this->assertEquals( $expected, $result );
159 }
160
161 /**
162 * @group Database
163 */
164 function testTrackingCategorySpecial() {
165 // Special pages shouldn't have tracking cats.
166 $title = SpecialPage::getTitleFor( 'Contributions' );
167 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
168 $result = $parserOutput->getCategoryLinks();
169 $this->assertEmpty( $result );
170 }
171 }