Debian Codegraph @

tests/phpunit/includes/parser/ExtraParserTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Parser;
4
5 use MediaWiki\Context\RequestContext;
6 use MediaWiki\Interwiki\ClassicInterwikiLookup;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Parser\Parser;
9 use MediaWiki\Parser\ParserOptions;
10 use MediaWiki\SpecialPage\SpecialPage;
11 use MediaWiki\Title\Title;
12 use MediaWiki\User\User;
13 use MediaWikiIntegrationTestCase;
14 use Wikimedia\TestingAccessWrapper;
15
16 /**
17 * Parser-related tests that don't suit for parserTests.txt
18 *
19 * @group Database
20 */
21 class ExtraParserTest extends MediaWikiIntegrationTestCase {
22
23 /** @var ParserOptions */
24 protected $options;
25 /** @var Parser */
26 protected $parser;
27
28 protected function setUp(): void {
29 parent::setUp();
30
31 $this->setUserLang( 'en' );
32 $this->overrideConfigValues( [
33 MainConfigNames::ShowExceptionDetails => true,
34 MainConfigNames::CleanSignatures => true,
35 MainConfigNames::LanguageCode => 'en',
36 ] );
37
38 $services = $this->getServiceContainer();
39 $contLang = $services->getContentLanguage();
40
41 // FIXME: This test should pass without setting global content language
42 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
43 $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
44
45 $this->parser = $services->getParserFactory()->create();
46 }
47
48 /**
49 * @see T10689
50 * @covers \MediaWiki\Parser\Parser::parse
51 */
52 public function testLongNumericLinesDontKillTheParser() {
53 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
54
55 $title = Title::makeTitle( NS_MAIN, 'Unit test' );
56 $options = ParserOptions::newFromUser( new User() );
57 $this->assertEquals( "<p>$longLine</p>",
58 $this->parser->parse( $longLine, $title, $options )->getRawText() );
59 }
60
61 /**
62 * @covers \MediaWiki\Parser\Parser::braceSubstitution
63 * @covers \MediaWiki\SpecialPage\SpecialPageFactory::capturePath
64 */
65 public function testSpecialPageTransclusionRestoresGlobalState() {
66 $text = "{{Special:ApiHelp/help}}";
67 $title = Title::makeTitle( NS_MAIN, 'TestSpecialPageTransclusionRestoresGlobalState' );
68 $options = ParserOptions::newFromUser( new User() );
69
70 RequestContext::getMain()->setTitle( $title );
71
72 $parsed = $this->parser->parse( $text, $title, $options )->getRawText();
73 $this->assertStringContainsString( 'apihelp-header', $parsed );
74 }
75
76 /**
77 * Test the parser entry points
78 * @covers \MediaWiki\Parser\Parser::parse
79 */
80 public function testParse() {
81 $title = Title::newFromText( __FUNCTION__ );
82 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
83 $this->assertEquals(
84 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
85 $parserOutput->getRawText()
86 );
87 }
88
89 /**
90 * @covers \MediaWiki\Parser\Parser::preSaveTransform
91 */
92 public function testPreSaveTransform() {
93 $title = Title::newFromText( __FUNCTION__ );
94 $outputText = $this->parser->preSaveTransform(
95 "Test\r\n{{subst:Foo}}\n{{Bar}}",
96 $title,
97 new User(),
98 $this->options
99 );
100 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
101
102 $outputText = $this->parser->preSaveTransform(
103 "hello\n\n{{subst:ns:0}}",
104 $title,
105 new User(),
106 $this->options
107 );
108 $this->assertEquals( "hello", $outputText,
109 "Pre-save transform removes trailing whitespace after substituting templates" );
110 }
111
112 /**
113 * @covers \MediaWiki\Parser\Parser::preprocess
114 */
115 public function testPreprocess() {
116 $title = Title::newFromText( __FUNCTION__ );
117 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
118
119 $this->assertEquals(
120 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
121 $outputText
122 );
123 }
124
125 /**
126 * cleanSig() makes all templates substs and removes tildes
127 * @covers \MediaWiki\Parser\Parser::cleanSig
128 */
129 public function testCleanSig() {
130 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
131
132 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
133 }
134
135 /**
136 * cleanSig() should do nothing if disabled
137 * @covers \MediaWiki\Parser\Parser::cleanSig
138 */
139 public function testCleanSigDisabled() {
140 $this->overrideConfigValue( MainConfigNames::CleanSignatures, false );
141
142 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
143
144 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
145 }
146
147 /**
148 * cleanSigInSig() just removes tildes
149 * @dataProvider provideStringsForCleanSigInSig
150 * @covers \MediaWiki\Parser\Parser::cleanSigInSig
151 */
152 public function testCleanSigInSig( $in, $out ) {
153 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
154 }
155
156 public static function provideStringsForCleanSigInSig() {
157 return [
158 [ "{{Foo}} ~~~~", "{{Foo}} " ],
159 [ "~~~", "" ],
160 [ "~~~~~", "" ],
161 ];
162 }
163
164 /**
165 * @covers \MediaWiki\Parser\Parser::getSection
166 */
167 public function testGetSection() {
168 $outputText2 = $this->parser->getSection(
169 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
170 . "Section 2\n== Heading 3 ==\nSection 3\n",
171 2
172 );
173 $outputText1 = $this->parser->getSection(
174 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
175 . "Section 2\n== Heading 3 ==\nSection 3\n",
176 1
177 );
178
179 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
180 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
181 }
182
183 /**
184 * @covers \MediaWiki\Parser\Parser::replaceSection
185 */
186 public function testReplaceSection() {
187 $outputText = $this->parser->replaceSection(
188 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
189 . "Section 2\n== Heading 3 ==\nSection 3\n",
190 1,
191 "New section 1"
192 );
193
194 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
195 }
196
197 /**
198 * Templates and comments are not affected, but noinclude/onlyinclude is.
199 * @covers \MediaWiki\Parser\Parser::getPreloadText
200 */
201 public function testGetPreloadText() {
202 $title = Title::newFromText( __FUNCTION__ );
203 $outputText = $this->parser->getPreloadText(
204 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
205 $title,
206 $this->options
207 );
208
209 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
210 }
211
212 /**
213 * @param Title $title
214 * @param bool $parser
215 *
216 * @return array
217 */
218 public static function statelessFetchTemplate( $title, $parser = false ) {
219 $text = "Content of ''" . $title->getFullText() . "''";
220 $deps = [];
221
222 return [
223 'text' => $text,
224 'finalTitle' => $title,
225 'deps' => $deps ];
226 }
227
228 /**
229 * @covers \MediaWiki\Parser\Parser::parse
230 */
231 public function testTrackingCategory() {
232 $title = Title::newFromText( __FUNCTION__ );
233 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
234 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
235 $expected = [ $cat->getDBkey() ];
236 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
237 $result = $parserOutput->getCategoryNames();
238 $this->assertEquals( $expected, $result );
239 }
240
241 /**
242 * @covers \MediaWiki\Parser\Parser::parse
243 */
244 public function testTrackingCategorySpecial() {
245 // Special pages shouldn't have tracking cats.
246 $title = SpecialPage::getTitleFor( 'Contributions' );
247 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
248 $result = $parserOutput->getCategoryNames();
249 $this->assertSame( [], $result );
250 }
251
252 /**
253 * @covers \MediaWiki\Parser\Parser::parseLinkParameter
254 * @dataProvider provideParseLinkParameter
255 */
256 public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
257 static $testInterwikis = [
258 [
259 'iw_prefix' => 'local',
260 'iw_url' => 'http://doesnt.matter.invalid/$1',
261 'iw_api' => '',
262 'iw_wikiid' => '',
263 'iw_local' => 0
264 ],
265 [
266 'iw_prefix' => 'mw',
267 'iw_url' => 'https://www.mediawiki.org/wiki/$1',
268 'iw_api' => 'https://www.mediawiki.org/w/api.php',
269 'iw_wikiid' => '',
270 'iw_local' => 0
271 ]
272 ];
273 $this->overrideConfigValue(
274 MainConfigNames::InterwikiCache,
275 ClassicInterwikiLookup::buildCdbHash( $testInterwikis )
276 );
277
278 $this->parser->startExternalParse(
279 Title::newFromText( __FUNCTION__ ),
280 $this->options,
281 Parser::OT_HTML
282 );
283 $output = TestingAccessWrapper::newFromObject( $this->parser )
284 ->parseLinkParameter( $input );
285
286 $this->assertEquals( $expected[0], $output[0], "$desc (type)" );
287
288 if ( $expected[0] === 'link-title' ) {
289 $this->assertTrue(
290 $output[1]->equals( Title::newFromText( $expected[1] ) ),
291 "$desc (target); link list title instance matches new title instance"
292 );
293 } else {
294 $this->assertEquals( $expected[1], $output[1], "$desc (target)" );
295 }
296
297 foreach ( $expectedLinks as $func => $expected ) {
298 $output = $this->parser->getOutput()->$func();
299 $this->assertEquals( $expected, $output, "$desc ($func)" );
300 }
301 }
302
303 public static function provideParseLinkParameter() {
304 return [
305 [
306 '',
307 [ 'no-link', false ],
308 [],
309 'Return no link when requested',
310 ],
311 [
312 'https://example.com/',
313 [ 'link-url', 'https://example.com/' ],
314 [ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
315 'External link',
316 ],
317 [
318 '//example.com/',
319 [ 'link-url', '//example.com/' ],
320 [ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
321 'External link',
322 ],
323 [
324 'Test',
325 [ 'link-title', 'Test' ],
326 [ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
327 'Internal link',
328 ],
329 [
330 'mw:Test',
331 [ 'link-title', 'mw:Test' ],
332 [ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
333 'Internal link (interwiki)',
334 ],
335 [
336 'https://',
337 [ null, false ],
338 [],
339 'Invalid link target',
340 ],
341 [
342 '<>',
343 [ null, false ],
344 [],
345 'Invalid link target',
346 ],
347 [
348 ' ',
349 [ null, false ],
350 [],
351 'Invalid link target',
352 ],
353 ];
354 }
355 }