aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/parser/ExtraParserTest.php
blob: 7817744c27f0e1f7b1c20d660bb5a250b3c5b99f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php

namespace MediaWiki\Tests\Parser;

use MediaWiki\Context\RequestContext;
use MediaWiki\Interwiki\ClassicInterwikiLookup;
use MediaWiki\MainConfigNames;
use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWikiIntegrationTestCase;
use Wikimedia\TestingAccessWrapper;

/**
 * Parser-related tests that don't suit for parserTests.txt
 *
 * @group Database
 */
class ExtraParserTest extends MediaWikiIntegrationTestCase {

	/** @var ParserOptions */
	protected $options;
	/** @var Parser */
	protected $parser;

	protected function setUp(): void {
		parent::setUp();

		$this->setUserLang( 'en' );
		$this->overrideConfigValues( [
			MainConfigNames::ShowExceptionDetails => true,
			MainConfigNames::CleanSignatures => true,
			MainConfigNames::LanguageCode => 'en',
		] );

		$services = $this->getServiceContainer();
		$contLang = $services->getContentLanguage();

		// FIXME: This test should pass without setting global content language
		$this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
		$this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );

		$this->parser = $services->getParserFactory()->create();
	}

	/**
	 * @see T10689
	 * @covers \MediaWiki\Parser\Parser::parse
	 */
	public function testLongNumericLinesDontKillTheParser() {
		$longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";

		$title = Title::makeTitle( NS_MAIN, 'Unit test' );
		$options = ParserOptions::newFromUser( new User() );
		$this->assertEquals( "<p>$longLine</p>",
			$this->parser->parse( $longLine, $title, $options )->getRawText() );
	}

	/**
	 * @covers \MediaWiki\Parser\Parser::braceSubstitution
	 * @covers \MediaWiki\SpecialPage\SpecialPageFactory::capturePath
	 */
	public function testSpecialPageTransclusionRestoresGlobalState() {
		$text = "{{Special:ApiHelp/help}}";
		$title = Title::makeTitle( NS_MAIN, 'TestSpecialPageTransclusionRestoresGlobalState' );
		$options = ParserOptions::newFromUser( new User() );

		RequestContext::getMain()->setTitle( $title );

		$parsed = $this->parser->parse( $text, $title, $options )->getRawText();
		$this->assertStringContainsString( 'apihelp-header', $parsed );
	}

	/**
	 * Test the parser entry points
	 * @covers \MediaWiki\Parser\Parser::parse
	 */
	public function testParse() {
		$title = Title::newFromText( __FUNCTION__ );
		$parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
		$this->assertEquals(
			"<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
			$parserOutput->getRawText()
		);
	}

	/**
	 * @covers \MediaWiki\Parser\Parser::preSaveTransform
	 */
	public function testPreSaveTransform() {
		$title = Title::newFromText( __FUNCTION__ );
		$outputText = $this->parser->preSaveTransform(
			"Test\r\n{{subst:Foo}}\n{{Bar}}",
			$title,
			new User(),
			$this->options
		);
		$this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );

		$outputText = $this->parser->preSaveTransform(
			"hello\n\n{{subst:ns:0}}",
			$title,
			new User(),
			$this->options
		);
		$this->assertEquals( "hello", $outputText,
			"Pre-save transform removes trailing whitespace after substituting templates" );
	}

	/**
	 * @covers \MediaWiki\Parser\Parser::preprocess
	 */
	public function testPreprocess() {
		$title = Title::newFromText( __FUNCTION__ );
		$outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );

		$this->assertEquals(
			"Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
			$outputText
		);
	}

	/**
	 * cleanSig() makes all templates substs and removes tildes
	 * @covers \MediaWiki\Parser\Parser::cleanSig
	 */
	public function testCleanSig() {
		$outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );

		$this->assertEquals( "{{SUBST:Foo}} ", $outputText );
	}

	/**
	 * cleanSig() should do nothing if disabled
	 * @covers \MediaWiki\Parser\Parser::cleanSig
	 */
	public function testCleanSigDisabled() {
		$this->overrideConfigValue( MainConfigNames::CleanSignatures, false );

		$outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );

		$this->assertEquals( "{{Foo}} ~~~~", $outputText );
	}

	/**
	 * cleanSigInSig() just removes tildes
	 * @dataProvider provideStringsForCleanSigInSig
	 * @covers \MediaWiki\Parser\Parser::cleanSigInSig
	 */
	public function testCleanSigInSig( $in, $out ) {
		$this->assertEquals( Parser::cleanSigInSig( $in ), $out );
	}

	public static function provideStringsForCleanSigInSig() {
		return [
			[ "{{Foo}} ~~~~", "{{Foo}} " ],
			[ "~~~", "" ],
			[ "~~~~~", "" ],
		];
	}

	/**
	 * @covers \MediaWiki\Parser\Parser::getSection
	 */
	public function testGetSection() {
		$outputText2 = $this->parser->getSection(
			"Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
				. "Section 2\n== Heading 3 ==\nSection 3\n",
			2
		);
		$outputText1 = $this->parser->getSection(
			"Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
				. "Section 2\n== Heading 3 ==\nSection 3\n",
			1
		);

		$this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
		$this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
	}

	/**
	 * @covers \MediaWiki\Parser\Parser::replaceSection
	 */
	public function testReplaceSection() {
		$outputText = $this->parser->replaceSection(
			"Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
				. "Section 2\n== Heading 3 ==\nSection 3\n",
			1,
			"New section 1"
		);

		$this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
	}

	/**
	 * Templates and comments are not affected, but noinclude/onlyinclude is.
	 * @covers \MediaWiki\Parser\Parser::getPreloadText
	 */
	public function testGetPreloadText() {
		$title = Title::newFromText( __FUNCTION__ );
		$outputText = $this->parser->getPreloadText(
			"{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
			$title,
			$this->options
		);

		$this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
	}

	/**
	 * @param Title $title
	 * @param bool $parser
	 *
	 * @return array
	 */
	public static function statelessFetchTemplate( $title, $parser = false ) {
		$text = "Content of ''" . $title->getFullText() . "''";
		$deps = [];

		return [
			'text' => $text,
			'finalTitle' => $title,
			'deps' => $deps ];
	}

	/**
	 * @covers \MediaWiki\Parser\Parser::parse
	 */
	public function testTrackingCategory() {
		$title = Title::newFromText( __FUNCTION__ );
		$catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
		$cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
		$expected = [ $cat->getDBkey() ];
		$parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
		$result = $parserOutput->getCategoryNames();
		$this->assertEquals( $expected, $result );
	}

	/**
	 * @covers \MediaWiki\Parser\Parser::parse
	 */
	public function testTrackingCategorySpecial() {
		// Special pages shouldn't have tracking cats.
		$title = SpecialPage::getTitleFor( 'Contributions' );
		$parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
		$result = $parserOutput->getCategoryNames();
		$this->assertSame( [], $result );
	}

	/**
	 * @covers \MediaWiki\Parser\Parser::parseLinkParameter
	 * @dataProvider provideParseLinkParameter
	 */
	public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
		static $testInterwikis = [
			[
				'iw_prefix' => 'local',
				'iw_url' => 'http://doesnt.matter.invalid/$1',
				'iw_api' => '',
				'iw_wikiid' => '',
				'iw_local' => 0
			],
			[
				'iw_prefix' => 'mw',
				'iw_url' => 'https://www.mediawiki.org/wiki/$1',
				'iw_api' => 'https://www.mediawiki.org/w/api.php',
				'iw_wikiid' => '',
				'iw_local' => 0
			]
		];
		$this->overrideConfigValue(
			MainConfigNames::InterwikiCache,
			ClassicInterwikiLookup::buildCdbHash( $testInterwikis )
		);
		Title::clearCaches();
		$this->parser->startExternalParse(
			Title::newFromText( __FUNCTION__ ),
			$this->options,
			Parser::OT_HTML
		);
		$output = TestingAccessWrapper::newFromObject( $this->parser )
			->parseLinkParameter( $input );

		$this->assertEquals( $expected[0], $output[0], "$desc (type)" );

		if ( $expected[0] === 'link-title' ) {
			$this->assertTrue(
				$output[1]->equals( Title::newFromText( $expected[1] ) ),
				"$desc (target); link list title instance matches new title instance"
			);
		} else {
			$this->assertEquals( $expected[1], $output[1], "$desc (target)" );
		}

		foreach ( $expectedLinks as $func => $expected ) {
			$output = $this->parser->getOutput()->$func();
			$this->assertEquals( $expected, $output, "$desc ($func)" );
		}
	}

	public static function provideParseLinkParameter() {
		return [
			[
				'',
				[ 'no-link', false ],
				[],
				'Return no link when requested',
			],
			[
				'https://example.com/',
				[ 'link-url', 'https://example.com/' ],
				[ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
				'External link',
			],
			[
				'//example.com/',
				[ 'link-url', '//example.com/' ],
				[ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
				'External link',
			],
			[
				'Test',
				[ 'link-title', 'Test' ],
				[ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
				'Internal link',
			],
			[
				'mw:Test',
				[ 'link-title', 'mw:Test' ],
				[ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
				'Internal link (interwiki)',
			],
			[
				'https://',
				[ null, false ],
				[],
				'Invalid link target',
			],
			[
				'<>',
				[ null, false ],
				[],
				'Invalid link target',
			],
			[
				' ',
				[ null, false ],
				[],
				'Invalid link target',
			],
		];
	}
}