2 use MediaWiki\Storage\MutableRevisionRecord
;
3 use MediaWiki\Storage\RevisionStore
;
4 use MediaWiki\User\UserIdentityValue
;
9 * @covers BlockLevelPass
11 class ParserMethodsTest
extends MediaWikiLangTestCase
{
13 public static function providePreSaveTransform() {
15 [ 'hello this is ~~~',
16 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
18 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
19 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
25 * @dataProvider providePreSaveTransform
27 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 = $wgParser->preSaveTransform( $text, $title, $user, $popts );
36 $this->assertEquals( $expected, $text );
39 public static function provideStripOuterParagraph() {
40 // This mimics the most common use case (stripping paragraphs generated by the parser).
41 $message = new RawMessage( "Message text." );
49 "<p class='foo'>Text.</p>",
50 "<p class='foo'>Text.</p>",
57 "<p>Text.</p><p>More text.</p>",
58 "<p>Text.</p><p>More text.</p>",
68 * @dataProvider provideStripOuterParagraph
70 public function testStripOuterParagraph( $text, $expected ) {
71 $this->assertEquals( $expected, Parser
::stripOuterParagraph( $text ) );
75 * @expectedException MWException
76 * @expectedExceptionMessage Parser state cleared while parsing.
77 * Did you call Parser::parse recursively?
79 public function testRecursiveParse() {
81 $title = Title
::newFromText( 'foo' );
82 $po = new ParserOptions
;
83 $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
84 $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
87 public function helperParserFunc( $input, $args, $parser ) {
88 $title = Title
::newFromText( 'foo' );
89 $po = new ParserOptions
;
90 $parser->parse( $input, $title, $po );
94 public function testCallParserFunction() {
97 // Normal parses test passing PPNodes. Test passing an array.
98 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
99 $wgParser->startExternalParse( $title, new ParserOptions(), Parser
::OT_HTML
);
100 $frame = $wgParser->getPreprocessor()->newFrame();
101 $ret = $wgParser->callParserFunction( $frame, '#tag',
102 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
104 $ret['text'] = $wgParser->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() {
118 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
119 $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
127 'fromtitle' => $title->getPrefixedDBkey(),
137 'fromtitle' => false,
138 'byteoffset' => null,
147 'fromtitle' => $title->getPrefixedDBkey(),
151 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
155 * @dataProvider provideNormalizeLinkUrl
157 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
158 $this->assertEquals( $expected, Parser
::normalizeLinkUrl( $url ), $explanation );
161 public static function provideNormalizeLinkUrl() {
164 'Escaping of unsafe characters',
165 'http://example.org/foo bar?param[]="value"¶m[]=valüe',
166 'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%5B%5D=val%C3%BCe',
169 'Case normalization of percent-encoded characters',
170 'http://example.org/%ab%cD%Ef%FF',
171 'http://example.org/%AB%CD%EF%FF',
174 'Unescaping of safe characters',
175 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
176 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
179 'Context-sensitive replacement of sometimes-safe characters',
180 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
181 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
186 public function testWrapOutput() {
188 $title = Title
::newFromText( 'foo' );
189 $po = new ParserOptions();
190 $wgParser->parse( 'Hello World', $title, $po );
191 $text = $wgParser->getOutput()->getText();
193 $this->assertContains( 'Hello World', $text );
194 $this->assertContains( '<div', $text );
195 $this->assertContains( 'class="mw-parser-output"', $text );
199 * @param string $name
202 private function getMockTitle( $name ) {
203 $title = $this->getMock( Title
::class );
204 $title->method( 'getPrefixedDBkey' )->willReturn( $name );
205 $title->method( 'getPrefixedText' )->willReturn( $name );
206 $title->method( 'getDBkey' )->willReturn( $name );
207 $title->method( 'getText' )->willReturn( $name );
208 $title->method( 'getNamespace' )->willReturn( 0 );
209 $title->method( 'getPageLanguage' )->willReturn( Language
::factory( 'en' ) );
214 public function provideRevisionAccess() {
215 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
217 $frank = $this->getMockBuilder( User
::class )
218 ->disableOriginalConstructor()
221 $frank->method( 'getName' )->willReturn( 'Frank' );
223 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
224 $po = new ParserOptions( $frank );
226 yield
'current' => [ $text, $po, 0, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
227 yield
'current with ID' => [ $text, $po, 200, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
229 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
230 $po = new ParserOptions( $frank );
232 yield
'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
234 $oldRevision = new MutableRevisionRecord( $title );
235 $oldRevision->setId( 100 );
236 $oldRevision->setUser( new UserIdentityValue( 7, 'FauxAuthor', 0 ) );
237 $oldRevision->setTimestamp( '20141111111111' );
238 $oldRevision->setContent( 'main', new WikitextContent( 'FAUX' ) );
240 $po = new ParserOptions( $frank );
241 $po->setCurrentRevisionCallback( function () use ( $oldRevision ) {
242 return new Revision( $oldRevision );
245 yield
'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
247 $text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
249 $po = new ParserOptions( $frank );
250 $po->setIsPreview( true );
252 yield
'preview without override, using context' => [
260 $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
261 . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
263 $newRevision = new MutableRevisionRecord( $title );
264 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
265 $newRevision->setTimestamp( '20180808000000' );
266 $newRevision->setContent( 'main', new WikitextContent( 'NEW' ) );
268 $po = new ParserOptions( $frank );
269 $po->setIsPreview( true );
270 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
271 return new Revision( $newRevision );
278 'user:NewAuthor;time:20180808000000;',
279 'user-subst:NewAuthor;time-subst:20180808000000;',
282 $po = new ParserOptions( $frank );
283 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
284 return new Revision( $newRevision );
287 yield
'pre-save' => [
291 'user:NewAuthor;time:20180808000000;',
292 'user-subst:NewAuthor;time-subst:20180808000000;',
295 $text = "(ONE)<includeonly>(TWO)</includeonly>"
296 . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
298 $newRevision = new MutableRevisionRecord( $title );
299 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
300 $newRevision->setTimestamp( '20180808000000' );
301 $newRevision->setContent( 'main', new WikitextContent( $text ) );
303 $po = new ParserOptions( $frank );
304 $po->setIsPreview( true );
305 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
306 return new Revision( $newRevision );
309 yield
'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
313 * @dataProvider provideRevisionAccess
315 public function testRevisionAccess(
320 $expectedInPst = null
324 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
326 $po->enableLimitReport( false );
328 $oldRevision = new MutableRevisionRecord( $title );
329 $oldRevision->setId( 100 );
330 $oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor', 0 ) );
331 $oldRevision->setTimestamp( '20140404000000' );
332 $oldRevision->setContent( 'main', new WikitextContent( 'OLD' ) );
334 $currentRevision = new MutableRevisionRecord( $title );
335 $currentRevision->setId( 200 );
336 $currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor', 0 ) );
337 $currentRevision->setTimestamp( '20160606000000' );
338 $currentRevision->setContent( 'main', new WikitextContent( 'CURRENT' ) );
340 $revisionStore = $this->getMockBuilder( RevisionStore
::class )
341 ->disableOriginalConstructor()
345 ->method( 'getKnownCurrentRevision' )
347 [ $title, 100, $oldRevision ],
348 [ $title, 200, $currentRevision ],
349 [ $title, 0, $currentRevision ],
353 ->method( 'getRevisionById' )
355 [ 100, 0, $oldRevision ],
356 [ 200, 0, $currentRevision ],
359 $this->setService( 'RevisionStore', $revisionStore );
361 $wgParser->parse( $text, $title, $po, true, true, $revId );
362 $html = $wgParser->getOutput()->getText();
364 $this->assertContains( $expectedInHtml, $html, 'In HTML' );
366 if ( $expectedInPst !== null ) {
367 $pst = $wgParser->preSaveTransform( $text, $title, $po->getUser(), $po );
368 $this->assertContains( $expectedInPst, $pst, 'After Pre-Safe Transform' );
372 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
373 // replaceSection(), getPreloadText()