3 namespace MediaWiki\Tests\Parser
;
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
;
24 * @covers \MediaWiki\Parser\Parser
25 * @covers \MediaWiki\Parser\BlockLevelPass
27 class ParserMethodsTest
extends MediaWikiLangTestCase
{
30 public static function providePreSaveTransform() {
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>',
42 * @dataProvider providePreSaveTransform
44 public function testPreSaveTransform( $text, $expected ) {
45 $title = Title
::makeTitle( NS_MAIN
, 'TestPreSaveTransform' );
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." );
65 "<p class='foo'>Text.</p>",
66 "<p class='foo'>Text.</p>",
73 "<p>Text.</p><p>More text.</p>",
74 "<p>Text.</p><p>More text.</p>",
84 * @dataProvider provideStripOuterParagraph
86 public function testStripOuterParagraph( $text, $expected ) {
87 $this->assertEquals( $expected, Parser
::stripOuterParagraph( $text ) );
90 public static function provideFormatPageTitle() {
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)" => [
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&lk</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">He&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 );
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(
147 ParserOptions
::newFromAnon(),
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'] );
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",
171 ParserOptions
::newFromAnon()
180 'fromtitle' => $title->getPrefixedDBkey(),
183 'linkAnchor' => 'foo',
191 'fromtitle' => false,
192 'byteoffset' => null,
194 'linkAnchor' => 'bar',
202 'fromtitle' => $title->getPrefixedDBkey(),
205 'linkAnchor' => 'baz',
210 'line' => 'Romeo+Juliet %A Ó %20',
213 'fromtitle' => $title->getPrefixedDBkey(),
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() {
231 'Escaping of unsafe characters',
232 'http://example.org/foo bar?param[]="value"¶m[]=valüe',
233 'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%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' )
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 ) {
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' => [
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 ) {
331 'user:NewAuthor;time:20180808000000;',
332 'user-subst:NewAuthor;time-subst:20180808000000;',
335 $po = new ParserOptions( $frank );
336 $po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
340 yield
'pre-save' => [
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 ) {
362 yield
'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
366 * @dataProvider provideRevisionAccess
368 public function testRevisionAccess(
373 $expectedInPst = null
375 $title = $this->makeMockTitle( 'ParserRevisionAccessTest', [
376 'language' => $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' )
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 );
394 ->method( 'getKnownCurrentRevision' )
396 [ $title, 100, $oldRevision ],
397 [ $title, 200, $currentRevision ],
398 [ $title, 0, $currentRevision ],
402 ->method( 'getRevisionById' )
404 [ 100, 0, null, $oldRevision ],
405 [ 200, 0, null, $currentRevision ],
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() {
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()