Merge "SpecialMovepage: Convert form to use OOUI controls"
[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 * @group Database
7 */
8 class ExtraParserTest extends MediaWikiTestCase {
9
10 /** @var ParserOptions */
11 protected $options;
12 /** @var Parser */
13 protected $parser;
14
15 protected function setUp() {
16 parent::setUp();
17
18 $contLang = Language::factory( 'en' );
19 $this->setMwGlobals( array(
20 'wgShowDBErrorBacktrace' => true,
21 'wgLanguageCode' => 'en',
22 'wgContLang' => $contLang,
23 'wgLang' => Language::factory( 'en' ),
24 'wgMemc' => new EmptyBagOStuff,
25 'wgCleanSignatures' => true,
26 ) );
27
28 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
29 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
30 $this->parser = new Parser;
31
32 MagicWord::clearCache();
33 }
34
35 /**
36 * @see Bug 8689
37 * @covers Parser::parse
38 */
39 public function testLongNumericLinesDontKillTheParser() {
40 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
41
42 $title = Title::newFromText( 'Unit test' );
43 $options = ParserOptions::newFromUser( new User() );
44 $this->assertEquals( "<p>$longLine</p>",
45 $this->parser->parse( $longLine, $title, $options )->getText() );
46 }
47
48 /**
49 * Test the parser entry points
50 * @covers Parser::parse
51 */
52 public function testParse() {
53 $title = Title::newFromText( __FUNCTION__ );
54 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
55 $this->assertEquals(
56 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
57 $parserOutput->getText()
58 );
59 }
60
61 /**
62 * @covers Parser::preSaveTransform
63 */
64 public function testPreSaveTransform() {
65 $title = Title::newFromText( __FUNCTION__ );
66 $outputText = $this->parser->preSaveTransform(
67 "Test\r\n{{subst:Foo}}\n{{Bar}}",
68 $title,
69 new User(),
70 $this->options
71 );
72
73 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
74 }
75
76 /**
77 * @covers Parser::preprocess
78 */
79 public function testPreprocess() {
80 $title = Title::newFromText( __FUNCTION__ );
81 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
82
83 $this->assertEquals(
84 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
85 $outputText
86 );
87 }
88
89 /**
90 * cleanSig() makes all templates substs and removes tildes
91 * @covers Parser::cleanSig
92 */
93 public function testCleanSig() {
94 $title = Title::newFromText( __FUNCTION__ );
95 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
96
97 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
98 }
99
100 /**
101 * cleanSig() should do nothing if disabled
102 * @covers Parser::cleanSig
103 */
104 public function testCleanSigDisabled() {
105 $this->setMwGlobals( 'wgCleanSignatures', false );
106
107 $title = Title::newFromText( __FUNCTION__ );
108 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
109
110 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
111 }
112
113 /**
114 * cleanSigInSig() just removes tildes
115 * @dataProvider provideStringsForCleanSigInSig
116 * @covers Parser::cleanSigInSig
117 */
118 public function testCleanSigInSig( $in, $out ) {
119 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
120 }
121
122 public static function provideStringsForCleanSigInSig() {
123 return array(
124 array( "{{Foo}} ~~~~", "{{Foo}} " ),
125 array( "~~~", "" ),
126 array( "~~~~~", "" ),
127 );
128 }
129
130 /**
131 * @covers Parser::getSection
132 */
133 public function testGetSection() {
134 $outputText2 = $this->parser->getSection(
135 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
136 . "Section 2\n== Heading 3 ==\nSection 3\n",
137 2
138 );
139 $outputText1 = $this->parser->getSection(
140 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
141 . "Section 2\n== Heading 3 ==\nSection 3\n",
142 1
143 );
144
145 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
146 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
147 }
148
149 /**
150 * @covers Parser::replaceSection
151 */
152 public function testReplaceSection() {
153 $outputText = $this->parser->replaceSection(
154 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
155 . "Section 2\n== Heading 3 ==\nSection 3\n",
156 1,
157 "New section 1"
158 );
159
160 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
161 }
162
163 /**
164 * Templates and comments are not affected, but noinclude/onlyinclude is.
165 * @covers Parser::getPreloadText
166 */
167 public function testGetPreloadText() {
168 $title = Title::newFromText( __FUNCTION__ );
169 $outputText = $this->parser->getPreloadText(
170 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
171 $title,
172 $this->options
173 );
174
175 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
176 }
177
178 /**
179 * @param Title $title
180 * @param bool $parser
181 *
182 * @return array
183 */
184 static function statelessFetchTemplate( $title, $parser = false ) {
185 $text = "Content of ''" . $title->getFullText() . "''";
186 $deps = array();
187
188 return array(
189 'text' => $text,
190 'finalTitle' => $title,
191 'deps' => $deps );
192 }
193
194 /**
195 * @group Database
196 * @covers Parser::parse
197 */
198 public function testTrackingCategory() {
199 $title = Title::newFromText( __FUNCTION__ );
200 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
201 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
202 $expected = array( $cat->getDBkey() );
203 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
204 $result = $parserOutput->getCategoryLinks();
205 $this->assertEquals( $expected, $result );
206 }
207
208 /**
209 * @group Database
210 * @covers Parser::parse
211 */
212 public function testTrackingCategorySpecial() {
213 // Special pages shouldn't have tracking cats.
214 $title = SpecialPage::getTitleFor( 'Contributions' );
215 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
216 $result = $parserOutput->getCategoryLinks();
217 $this->assertEmpty( $result );
218 }
219 }