2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Revision\MutableRevisionRecord
;
4 use MediaWiki\Revision\RevisionStore
;
5 use MediaWiki\Revision\SlotRecord
;
6 use MediaWiki\User\UserIdentityValue
;
11 * @covers BlockLevelPass
13 class ParserMethodsTest
extends MediaWikiLangTestCase
{
15 public static function providePreSaveTransform() {
17 [ 'hello this is ~~~',
18 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
20 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
21 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
27 * @dataProvider providePreSaveTransform
29 public function testPreSaveTransform( $text, $expected ) {
30 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
32 $user->setName( "127.0.0.1" );
33 $popts = ParserOptions
::newFromUser( $user );
34 $text = MediaWikiServices
::getInstance()->getParser()
35 ->preSaveTransform( $text, $title, $user, $popts );
37 $this->assertEquals( $expected, $text );
40 public static function provideStripOuterParagraph() {
41 // This mimics the most common use case (stripping paragraphs generated by the parser).
42 $message = new RawMessage( "Message text." );
50 "<p class='foo'>Text.</p>",
51 "<p class='foo'>Text.</p>",
58 "<p>Text.</p><p>More text.</p>",
59 "<p>Text.</p><p>More text.</p>",
69 * @dataProvider provideStripOuterParagraph
71 public function testStripOuterParagraph( $text, $expected ) {
72 $this->assertEquals( $expected, Parser
::stripOuterParagraph( $text ) );
76 * @expectedException MWException
77 * @expectedExceptionMessage Parser state cleared while parsing.
78 * Did you call Parser::parse recursively?
80 public function testRecursiveParse() {
81 $title = Title
::newFromText( 'foo' );
82 $parser = MediaWikiServices
::getInstance()->getParser();
83 $po = new ParserOptions
;
84 $parser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
85 $parser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
88 public function helperParserFunc( $input, $args, $parser ) {
89 $title = Title
::newFromText( 'foo' );
90 $po = new ParserOptions
;
91 $parser->parse( $input, $title, $po );
95 public function testCallParserFunction() {
96 // Normal parses test passing PPNodes. Test passing an array.
97 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
98 $parser = MediaWikiServices
::getInstance()->getParser();
99 $parser->startExternalParse( $title, new ParserOptions(), Parser
::OT_HTML
);
100 $frame = $parser->getPreprocessor()->newFrame();
101 $ret = $parser->callParserFunction( $frame, '#tag',
102 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
104 $ret['text'] = $parser->mStripState
->unstripBoth( $ret['text'] );
107 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
108 ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
113 * @covers ParserOutput::getSections
115 public function testGetSections() {
116 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
117 $out = MediaWikiServices
::getInstance()->getParser()
118 ->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
126 'fromtitle' => $title->getPrefixedDBkey(),
136 'fromtitle' => false,
137 'byteoffset' => null,
146 'fromtitle' => $title->getPrefixedDBkey(),
150 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
154 * @dataProvider provideNormalizeLinkUrl
156 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
157 $this->assertEquals( $expected, Parser
::normalizeLinkUrl( $url ), $explanation );
160 public static function provideNormalizeLinkUrl() {
163 'Escaping of unsafe characters',
164 'http://example.org/foo bar?param[]="value"¶m[]=valüe',
165 'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%5B%5D=val%C3%BCe',
168 'Case normalization of percent-encoded characters',
169 'http://example.org/%ab%cD%Ef%FF',
170 'http://example.org/%AB%CD%EF%FF',
173 'Unescaping of safe characters',
174 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
175 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
178 'Context-sensitive replacement of sometimes-safe characters',
179 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
180 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
183 'IPv6 links aren\'t escaped',
184 'http://[::1]/foobar',
185 'http://[::1]/foobar',
188 'non-IPv6 links aren\'t unescaped',
189 'http://%5B::1%5D/foobar',
190 'http://%5B::1%5D/foobar',
195 public function testWrapOutput() {
196 $title = Title
::newFromText( 'foo' );
197 $po = new ParserOptions();
198 $parser = MediaWikiServices
::getInstance()->getParser();
199 $parser->parse( 'Hello World', $title, $po );
200 $text = $parser->getOutput()->getText();
202 $this->assertContains( 'Hello World', $text );
203 $this->assertContains( '<div', $text );
204 $this->assertContains( 'class="mw-parser-output"', $text );
208 * @param string $name
211 private function getMockTitle( $name ) {
212 $title = $this->getMock( Title
::class );
213 $title->method( 'getPrefixedDBkey' )->willReturn( $name );
214 $title->method( 'getPrefixedText' )->willReturn( $name );
215 $title->method( 'getDBkey' )->willReturn( $name );
216 $title->method( 'getText' )->willReturn( $name );
217 $title->method( 'getNamespace' )->willReturn( 0 );
218 $title->method( 'getPageLanguage' )->willReturn( Language
::factory( 'en' ) );
223 public function provideRevisionAccess() {
224 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
226 $frank = $this->getMockBuilder( User
::class )
227 ->disableOriginalConstructor()
230 $frank->method( 'getName' )->willReturn( 'Frank' );
232 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
233 $po = new ParserOptions( $frank );
235 yield
'current' => [ $text, $po, 0, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
236 yield
'current with ID' => [ $text, $po, 200, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
238 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
239 $po = new ParserOptions( $frank );
241 yield
'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
243 $oldRevision = new MutableRevisionRecord( $title );
244 $oldRevision->setId( 100 );
245 $oldRevision->setUser( new UserIdentityValue( 7, 'FauxAuthor', 0 ) );
246 $oldRevision->setTimestamp( '20141111111111' );
247 $oldRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( 'FAUX' ) );
249 $po = new ParserOptions( $frank );
250 $po->setCurrentRevisionCallback( function () use ( $oldRevision ) {
251 return new Revision( $oldRevision );
254 yield
'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
256 $text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
258 $po = new ParserOptions( $frank );
259 $po->setIsPreview( true );
261 yield
'preview without override, using context' => [
269 $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
270 . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
272 $newRevision = new MutableRevisionRecord( $title );
273 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
274 $newRevision->setTimestamp( '20180808000000' );
275 $newRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( 'NEW' ) );
277 $po = new ParserOptions( $frank );
278 $po->setIsPreview( true );
279 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
280 return new Revision( $newRevision );
287 'user:NewAuthor;time:20180808000000;',
288 'user-subst:NewAuthor;time-subst:20180808000000;',
291 $po = new ParserOptions( $frank );
292 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
293 return new Revision( $newRevision );
296 yield
'pre-save' => [
300 'user:NewAuthor;time:20180808000000;',
301 'user-subst:NewAuthor;time-subst:20180808000000;',
304 $text = "(ONE)<includeonly>(TWO)</includeonly>"
305 . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
307 $newRevision = new MutableRevisionRecord( $title );
308 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
309 $newRevision->setTimestamp( '20180808000000' );
310 $newRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
312 $po = new ParserOptions( $frank );
313 $po->setIsPreview( true );
314 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
315 return new Revision( $newRevision );
318 yield
'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
322 * @dataProvider provideRevisionAccess
324 public function testRevisionAccess(
329 $expectedInPst = null
331 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
333 $po->enableLimitReport( false );
335 $oldRevision = new MutableRevisionRecord( $title );
336 $oldRevision->setId( 100 );
337 $oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor', 0 ) );
338 $oldRevision->setTimestamp( '20140404000000' );
339 $oldRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( 'OLD' ) );
341 $currentRevision = new MutableRevisionRecord( $title );
342 $currentRevision->setId( 200 );
343 $currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor', 0 ) );
344 $currentRevision->setTimestamp( '20160606000000' );
345 $currentRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( 'CURRENT' ) );
347 $revisionStore = $this->getMockBuilder( RevisionStore
::class )
348 ->disableOriginalConstructor()
352 ->method( 'getKnownCurrentRevision' )
354 [ $title, 100, $oldRevision ],
355 [ $title, 200, $currentRevision ],
356 [ $title, 0, $currentRevision ],
360 ->method( 'getRevisionById' )
362 [ 100, 0, $oldRevision ],
363 [ 200, 0, $currentRevision ],
366 $this->setService( 'RevisionStore', $revisionStore );
368 $parser = MediaWikiServices
::getInstance()->getParser();
369 $parser->parse( $text, $title, $po, true, true, $revId );
370 $html = $parser->getOutput()->getText();
372 $this->assertContains( $expectedInHtml, $html, 'In HTML' );
374 if ( $expectedInPst !== null ) {
375 $pst = $parser->preSaveTransform( $text, $title, $po->getUser(), $po );
376 $this->assertContains( $expectedInPst, $pst, 'After Pre-Safe Transform' );
380 public static function provideGuessSectionNameFromWikiText() {
382 [ '1/2', 'html5', '#1/2' ],
383 [ '1/2', 'legacy', '#1.2F2' ],
387 /** @dataProvider provideGuessSectionNameFromWikiText */
388 public function testGuessSectionNameFromWikiText( $input, $mode, $expected ) {
389 $this->setMwGlobals( [ 'wgFragmentMode' => [ $mode ] ] );
390 $result = MediaWikiServices
::getInstance()->getParser()
391 ->guessSectionNameFromWikiText( $input );
392 $this->assertEquals( $result, $expected );
395 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
396 // replaceSection(), getPreloadText()