Merge "Selenium: record video of every test"
[lhc/web/wiklou.git] / tests / phpunit / includes / ExtraParserTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Parser-related tests that don't suit for parserTests.txt
7 *
8 * @group Database
9 */
10 class ExtraParserTest extends MediaWikiTestCase {
11
12 /** @var ParserOptions */
13 protected $options;
14 /** @var Parser */
15 protected $parser;
16
17 protected function setUp() {
18 parent::setUp();
19
20 $contLang = Language::factory( 'en' );
21 $this->setMwGlobals( [
22 'wgShowExceptionDetails' => true,
23 'wgCleanSignatures' => true,
24 ] );
25 $this->setUserLang( 'en' );
26 $this->setContentLang( $contLang );
27
28 // FIXME: This test should pass without setting global content language
29 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
30 $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
31 $this->parser = new Parser;
32
33 MediaWikiServices::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
34 }
35
36 /**
37 * @see T10689
38 * @covers Parser::parse
39 */
40 public function testLongNumericLinesDontKillTheParser() {
41 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
42
43 $title = Title::newFromText( 'Unit test' );
44 $options = ParserOptions::newFromUser( new User() );
45 $this->assertEquals( "<p>$longLine</p>",
46 $this->parser->parse( $longLine, $title, $options )->getText( [ 'unwrap' => true ] ) );
47 }
48
49 /**
50 * Test the parser entry points
51 * @covers Parser::parse
52 */
53 public function testParse() {
54 $title = Title::newFromText( __FUNCTION__ );
55 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
56 $this->assertEquals(
57 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
58 $parserOutput->getText( [ 'unwrap' => true ] )
59 );
60 }
61
62 /**
63 * @covers Parser::preSaveTransform
64 */
65 public function testPreSaveTransform() {
66 $title = Title::newFromText( __FUNCTION__ );
67 $outputText = $this->parser->preSaveTransform(
68 "Test\r\n{{subst:Foo}}\n{{Bar}}",
69 $title,
70 new User(),
71 $this->options
72 );
73
74 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
75 }
76
77 /**
78 * @covers Parser::preprocess
79 */
80 public function testPreprocess() {
81 $title = Title::newFromText( __FUNCTION__ );
82 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
83
84 $this->assertEquals(
85 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
86 $outputText
87 );
88 }
89
90 /**
91 * cleanSig() makes all templates substs and removes tildes
92 * @covers Parser::cleanSig
93 */
94 public function testCleanSig() {
95 $title = Title::newFromText( __FUNCTION__ );
96 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
97
98 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
99 }
100
101 /**
102 * cleanSig() should do nothing if disabled
103 * @covers Parser::cleanSig
104 */
105 public function testCleanSigDisabled() {
106 $this->setMwGlobals( 'wgCleanSignatures', false );
107
108 $title = Title::newFromText( __FUNCTION__ );
109 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
110
111 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
112 }
113
114 /**
115 * cleanSigInSig() just removes tildes
116 * @dataProvider provideStringsForCleanSigInSig
117 * @covers Parser::cleanSigInSig
118 */
119 public function testCleanSigInSig( $in, $out ) {
120 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
121 }
122
123 public static function provideStringsForCleanSigInSig() {
124 return [
125 [ "{{Foo}} ~~~~", "{{Foo}} " ],
126 [ "~~~", "" ],
127 [ "~~~~~", "" ],
128 ];
129 }
130
131 /**
132 * @covers Parser::getSection
133 */
134 public function testGetSection() {
135 $outputText2 = $this->parser->getSection(
136 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
137 . "Section 2\n== Heading 3 ==\nSection 3\n",
138 2
139 );
140 $outputText1 = $this->parser->getSection(
141 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
142 . "Section 2\n== Heading 3 ==\nSection 3\n",
143 1
144 );
145
146 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
147 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
148 }
149
150 /**
151 * @covers Parser::replaceSection
152 */
153 public function testReplaceSection() {
154 $outputText = $this->parser->replaceSection(
155 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
156 . "Section 2\n== Heading 3 ==\nSection 3\n",
157 1,
158 "New section 1"
159 );
160
161 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
162 }
163
164 /**
165 * Templates and comments are not affected, but noinclude/onlyinclude is.
166 * @covers Parser::getPreloadText
167 */
168 public function testGetPreloadText() {
169 $title = Title::newFromText( __FUNCTION__ );
170 $outputText = $this->parser->getPreloadText(
171 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
172 $title,
173 $this->options
174 );
175
176 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
177 }
178
179 /**
180 * @param Title $title
181 * @param bool $parser
182 *
183 * @return array
184 */
185 static function statelessFetchTemplate( $title, $parser = false ) {
186 $text = "Content of ''" . $title->getFullText() . "''";
187 $deps = [];
188
189 return [
190 'text' => $text,
191 'finalTitle' => $title,
192 'deps' => $deps ];
193 }
194
195 /**
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 = [ $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 * @covers Parser::parse
210 */
211 public function testTrackingCategorySpecial() {
212 // Special pages shouldn't have tracking cats.
213 $title = SpecialPage::getTitleFor( 'Contributions' );
214 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
215 $result = $parserOutput->getCategoryLinks();
216 $this->assertEmpty( $result );
217 }
218
219 /**
220 * @covers Parser::parseLinkParameter
221 * @dataProvider provideParseLinkParameter
222 */
223 public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
224 $this->parser->startExternalParse( Title::newFromText( __FUNCTION__ ),
225 $this->options, Parser::OT_HTML );
226 $output = $this->parser->parseLinkParameter( $input );
227
228 $this->assertEquals( $expected[0], $output[0], "$desc (type)" );
229
230 if ( $expected[0] === 'link-title' ) {
231 $this->assertTrue( $expected[1]->equals( $output[1] ), "$desc (target)" );
232 } else {
233 $this->assertEquals( $expected[1], $output[1], "$desc (target)" );
234 }
235
236 foreach ( $expectedLinks as $func => $expected ) {
237 $output = $this->parser->getOutput()->$func();
238 $this->assertEquals( $expected, $output, "$desc ($func)" );
239 }
240 }
241
242 public static function provideParseLinkParameter() {
243 return [
244 [
245 '',
246 [ 'no-link', false ],
247 [],
248 'Return no link when requested',
249 ],
250 [
251 'https://example.com/',
252 [ 'link-url', 'https://example.com/' ],
253 [ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
254 'External link',
255 ],
256 [
257 '//example.com/',
258 [ 'link-url', '//example.com/' ],
259 [ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
260 'External link',
261 ],
262 [
263 'Test',
264 [ 'link-title', Title::newFromText( 'Test' ) ],
265 [ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
266 'Internal link',
267 ],
268 [
269 'mw:Test',
270 [ 'link-title', Title::newFromText( 'mw:Test' ) ],
271 [ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
272 'Internal link (interwiki)',
273 ],
274 [
275 'https://',
276 [ null, false ],
277 [],
278 'Invalid link target',
279 ],
280 [
281 '<>',
282 [ null, false ],
283 [],
284 'Invalid link target',
285 ],
286 [
287 ' ',
288 [ null, false ],
289 [],
290 'Invalid link target',
291 ],
292 ];
293 }
294 }