Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / parser / ParserMethodsTest.php
blob267976aff5f24a74900ab1e561be1843550b37a6
1 <?php
3 namespace MediaWiki\Tests\Parser;
5 use HtmlArmor;
6 use LogicException;
7 use MediaWiki\Content\WikitextContent;
8 use MediaWiki\Language\RawMessage;
9 use MediaWiki\MainConfigNames;
10 use MediaWiki\MediaWikiServices;
11 use MediaWiki\Parser\Parser;
12 use MediaWiki\Parser\ParserOptions;
13 use MediaWiki\Revision\MutableRevisionRecord;
14 use MediaWiki\Revision\RevisionStore;
15 use MediaWiki\Revision\SlotRecord;
16 use MediaWiki\Title\Title;
17 use MediaWiki\User\User;
18 use MediaWiki\User\UserIdentityValue;
19 use MediaWikiLangTestCase;
20 use MockTitleTrait;
22 /**
23 * @group Database
24 * @covers \MediaWiki\Parser\Parser
25 * @covers \MediaWiki\Parser\BlockLevelPass
27 class ParserMethodsTest extends MediaWikiLangTestCase {
28 use MockTitleTrait;
30 public static function providePreSaveTransform() {
31 return [
32 [ 'hello this is ~~~',
33 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
35 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
36 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
41 /**
42 * @dataProvider providePreSaveTransform
44 public function testPreSaveTransform( $text, $expected ) {
45 $title = Title::makeTitle( NS_MAIN, 'TestPreSaveTransform' );
46 $user = new User();
47 $user->setName( "127.0.0.1" );
48 $popts = ParserOptions::newFromUser( $user );
49 $text = $this->getServiceContainer()->getParser()
50 ->preSaveTransform( $text, $title, $user, $popts );
52 $this->assertEquals( $expected, $text );
55 public static function provideStripOuterParagraph() {
56 // This mimics the most common use case (stripping paragraphs generated by the parser).
57 $message = new RawMessage( "Message text." );
59 return [
61 "<p>Text.</p>",
62 "Text.",
65 "<p class='foo'>Text.</p>",
66 "<p class='foo'>Text.</p>",
69 "<p>Text.\n</p>\n",
70 "Text.",
73 "<p>Text.</p><p>More text.</p>",
74 "<p>Text.</p><p>More text.</p>",
77 $message->parse(),
78 "Message text.",
83 /**
84 * @dataProvider provideStripOuterParagraph
86 public function testStripOuterParagraph( $text, $expected ) {
87 $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
90 public static function provideFormatPageTitle() {
91 return [
92 "Non-main namespace" => [
93 [ 'Talk', ':', 'Hello' ],
94 '<span class="mw-page-title-namespace">Talk</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">Hello</span>',
96 "Main namespace (ignores the separator)" => [
97 [ '', ':', 'Hello' ],
98 '<span class="mw-page-title-main">Hello</span>',
100 "Pieces are HTML-escaped" => [
101 [ 'Ta&lk', ':', 'He&llo' ],
102 '<span class="mw-page-title-namespace">Ta&amp;lk</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">He&amp;llo</span>',
104 "In the future, the colon separator could be localized" => [
105 [ 'Talk', ' : ', 'Hello' ],
106 '<span class="mw-page-title-namespace">Talk</span><span class="mw-page-title-separator"> : </span><span class="mw-page-title-main">Hello</span>',
108 "In the future, displaytitle could be customized separately from the namespace" => [
109 [ 'Talk', ':', new HtmlArmor( '<span class="whatever">Hello</span>' ) ],
110 '<span class="mw-page-title-namespace">Talk</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main"><span class="whatever">Hello</span></span>',
116 * @dataProvider provideFormatPageTitle
118 public function testFormatPageTitle( $args, $expected ) {
119 $this->assertEquals( $expected, Parser::formatPageTitle( ...$args ) );
122 public function testRecursiveParse() {
123 $title = Title::makeTitle( NS_MAIN, 'Foo' );
124 $parser = $this->getServiceContainer()->getParser();
125 $po = ParserOptions::newFromAnon();
126 $parser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
127 $this->expectException( LogicException::class );
128 $this->expectExceptionMessage(
129 "Parser state cleared while parsing. Did you call Parser::parse recursively?"
131 $parser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
134 public function helperParserFunc( $input, $args, $parser ) {
135 $title = Title::makeTitle( NS_MAIN, 'Foo' );
136 $po = ParserOptions::newFromAnon();
137 $parser->parse( $input, $title, $po );
138 return 'bar';
141 public function testCallParserFunction() {
142 // Normal parses test passing PPNodes. Test passing an array.
143 $title = Title::makeTitle( NS_MAIN, 'TestCallParserFunction' );
144 $parser = $this->getServiceContainer()->getParser();
145 $parser->startExternalParse(
146 $title,
147 ParserOptions::newFromAnon(),
148 Parser::OT_HTML
150 $frame = $parser->getPreprocessor()->newFrame();
151 $ret = $parser->callParserFunction( $frame, '#tag',
152 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
154 $ret['text'] = $parser->getStripState()->unstripBoth( $ret['text'] );
155 $this->assertSame( [
156 'found' => true,
157 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
158 ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
162 * @covers \MediaWiki\Parser\Parser
163 * @covers \MediaWiki\Parser\ParserOutput::getSections
165 public function testGetSections() {
166 $this->overrideConfigValue( MainConfigNames::FragmentMode, [ 'html5' ] );
167 $title = Title::makeTitle( NS_MAIN, 'TestGetSections' );
168 $out = $this->getServiceContainer()->getParser()->parse(
169 "==foo==\n<h2>bar</h2>\n==baz==\n== Romeo+Juliet %A Ó %20 ==\ntest",
170 $title,
171 ParserOptions::newFromAnon()
173 $this->assertSame( [
175 'toclevel' => 1,
176 'level' => '2',
177 'line' => 'foo',
178 'number' => '1',
179 'index' => '1',
180 'fromtitle' => $title->getPrefixedDBkey(),
181 'byteoffset' => 0,
182 'anchor' => 'foo',
183 'linkAnchor' => 'foo',
186 'toclevel' => 1,
187 'level' => '2',
188 'line' => 'bar',
189 'number' => '2',
190 'index' => '',
191 'fromtitle' => false,
192 'byteoffset' => null,
193 'anchor' => 'bar',
194 'linkAnchor' => 'bar',
197 'toclevel' => 1,
198 'level' => '2',
199 'line' => 'baz',
200 'number' => '3',
201 'index' => '2',
202 'fromtitle' => $title->getPrefixedDBkey(),
203 'byteoffset' => 21,
204 'anchor' => 'baz',
205 'linkAnchor' => 'baz',
208 'toclevel' => 1,
209 'level' => '2',
210 'line' => 'Romeo+Juliet %A Ó %20',
211 'number' => '4',
212 'index' => '3',
213 'fromtitle' => $title->getPrefixedDBkey(),
214 'byteoffset' => 29,
215 'anchor' => 'Romeo+Juliet_%A_Ó_%20',
216 'linkAnchor' => 'Romeo+Juliet_%A_Ó_%2520',
218 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
222 * @dataProvider provideNormalizeLinkUrl
224 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
225 $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
228 public static function provideNormalizeLinkUrl() {
229 return [
231 'Escaping of unsafe characters',
232 'http://example.org/foo bar?param[]="value"&param[]=valüe',
233 'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
236 'Case normalization of percent-encoded characters',
237 'http://example.org/%ab%cD%Ef%FF',
238 'http://example.org/%AB%CD%EF%FF',
241 'Unescaping of safe characters',
242 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
243 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
246 'Context-sensitive replacement of sometimes-safe characters',
247 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
248 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
251 'Removing dot segments in the path part only',
252 'http://example.org/foo/../bar?param=foo/../bar#foo/../bar',
253 'http://example.org/bar?param=foo/../bar#foo/../bar',
256 'IPv6 links aren\'t escaped',
257 'http://[::1]/foobar',
258 'http://[::1]/foobar',
261 'non-IPv6 links aren\'t unescaped',
262 'http://%5B::1%5D/foobar',
263 'http://%5B::1%5D/foobar',
268 public function provideRevisionAccess() {
269 $title = $this->makeMockTitle( 'ParserRevisionAccessTest', [
270 'language' => MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' )
271 ] );
273 $frank = new UserIdentityValue( 5, 'Frank' );
275 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
276 $po = new ParserOptions( $frank );
278 yield 'current' => [ $text, $po, 0, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
279 yield 'anonymous' => [ $text, $po, null, 'user:;id:;time:' ];
280 yield 'current with ID' => [ $text, $po, 200, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
282 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
283 $po = new ParserOptions( $frank );
285 yield 'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
287 $oldRevision = new MutableRevisionRecord( $title );
288 $oldRevision->setId( 100 );
289 $oldRevision->setUser( new UserIdentityValue( 7, 'FauxAuthor' ) );
290 $oldRevision->setTimestamp( '20141111111111' );
291 $oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'FAUX' ) );
293 $po = new ParserOptions( $frank );
294 $po->setCurrentRevisionRecordCallback( static function () use ( $oldRevision ) {
295 return $oldRevision;
296 } );
298 yield 'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
300 $text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
302 $po = new ParserOptions( $frank );
303 $po->setIsPreview( true );
305 yield 'preview without override, using context' => [
306 $text,
307 $po,
308 null,
309 'user:Frank;',
310 'user-subst:Frank;',
313 $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
314 . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
316 $newRevision = new MutableRevisionRecord( $title );
317 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor' ) );
318 $newRevision->setTimestamp( '20180808000000' );
319 $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'NEW' ) );
321 $po = new ParserOptions( $frank );
322 $po->setIsPreview( true );
323 $po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
324 return $newRevision;
325 } );
327 yield 'preview' => [
328 $text,
329 $po,
330 null,
331 'user:NewAuthor;time:20180808000000;',
332 'user-subst:NewAuthor;time-subst:20180808000000;',
335 $po = new ParserOptions( $frank );
336 $po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
337 return $newRevision;
338 } );
340 yield 'pre-save' => [
341 $text,
342 $po,
343 null,
344 'user:NewAuthor;time:20180808000000;',
345 'user-subst:NewAuthor;time-subst:20180808000000;',
348 $text = "(ONE)<includeonly>(TWO)</includeonly>"
349 . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
351 $newRevision = new MutableRevisionRecord( $title );
352 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor' ) );
353 $newRevision->setTimestamp( '20180808000000' );
354 $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
356 $po = new ParserOptions( $frank );
357 $po->setIsPreview( true );
358 $po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
359 return $newRevision;
360 } );
362 yield 'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
366 * @dataProvider provideRevisionAccess
368 public function testRevisionAccess(
369 $text,
370 ParserOptions $po,
371 $revId,
372 $expectedInHtml,
373 $expectedInPst = null
375 $title = $this->makeMockTitle( 'ParserRevisionAccessTest', [
376 'language' => $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' )
377 ] );
379 $oldRevision = new MutableRevisionRecord( $title );
380 $oldRevision->setId( 100 );
381 $oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor' ) );
382 $oldRevision->setTimestamp( '20140404000000' );
383 $oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'OLD' ) );
385 $currentRevision = new MutableRevisionRecord( $title );
386 $currentRevision->setId( 200 );
387 $currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor' ) );
388 $currentRevision->setTimestamp( '20160606000000' );
389 $currentRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'CURRENT' ) );
391 $revisionStore = $this->createMock( RevisionStore::class );
393 $revisionStore
394 ->method( 'getKnownCurrentRevision' )
395 ->willReturnMap( [
396 [ $title, 100, $oldRevision ],
397 [ $title, 200, $currentRevision ],
398 [ $title, 0, $currentRevision ],
399 ] );
401 $revisionStore
402 ->method( 'getRevisionById' )
403 ->willReturnMap( [
404 [ 100, 0, null, $oldRevision ],
405 [ 200, 0, null, $currentRevision ],
406 ] );
408 $this->setService( 'RevisionStore', $revisionStore );
410 $parser = $this->getServiceContainer()->getParser();
411 $parser->parse( $text, $title, $po, true, true, $revId );
412 $html = $parser->getOutput()->getRawText();
414 $this->assertStringContainsString( $expectedInHtml, $html, 'In HTML' );
416 if ( $expectedInPst !== null ) {
417 $pst = $parser->preSaveTransform( $text, $title, $po->getUserIdentity(), $po );
418 $this->assertStringContainsString( $expectedInPst, $pst, 'After Pre-Safe Transform' );
422 public static function provideGuessSectionNameFromWikiText() {
423 return [
424 [ '1/2', 'html5', '#1/2' ],
425 [ '1/2', 'legacy', '#1.2F2' ],
429 /** @dataProvider provideGuessSectionNameFromWikiText */
430 public function testGuessSectionNameFromWikiText( $input, $mode, $expected ) {
431 $this->overrideConfigValue( MainConfigNames::FragmentMode, [ $mode ] );
432 $result = $this->getServiceContainer()->getParser()
433 ->guessSectionNameFromWikiText( $input );
434 $this->assertEquals( $expected, $result );
437 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
438 // replaceSection(), getPreloadText()