Debian Codegraph @

tests/phpunit/includes/parser/ParserMethodsTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Parser;
4
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;
21
22 /**
23 * @group Database
24 * @covers \MediaWiki\Parser\Parser
25 * @covers \MediaWiki\Parser\BlockLevelPass
26 */
27 class ParserMethodsTest extends MediaWikiLangTestCase {
28 use MockTitleTrait;
29
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]]",
34 ],
35 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
36 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
37 ],
38 ];
39 }
40
41 /**
42 * @dataProvider providePreSaveTransform
43 */
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 );
51
52 $this->assertEquals( $expected, $text );
53 }
54
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." );
58
59 return [
60 [
61 "<p>Text.</p>",
62 "Text.",
63 ],
64 [
65 "<p class='foo'>Text.</p>",
66 "<p class='foo'>Text.</p>",
67 ],
68 [
69 "<p>Text.\n</p>\n",
70 "Text.",
71 ],
72 [
73 "<p>Text.</p><p>More text.</p>",
74 "<p>Text.</p><p>More text.</p>",
75 ],
76 [
77 $message->parse(),
78 "Message text.",
79 ],
80 ];
81 }
82
83 /**
84 * @dataProvider provideStripOuterParagraph
85 */
86 public function testStripOuterParagraph( $text, $expected ) {
87 $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
88 }
89
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>',
95 ],
96 "Main namespace (ignores the separator)" => [
97 [ '', ':', 'Hello' ],
98 '<span class="mw-page-title-main">Hello</span>',
99 ],
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>',
103 ],
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>',
107 ],
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>',
111 ],
112 ];
113 }
114
115 /**
116 * @dataProvider provideFormatPageTitle
117 */
118 public function testFormatPageTitle( $args, $expected ) {
119 $this->assertEquals( $expected, Parser::formatPageTitle( ...$args ) );
120 }
121
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?"
130 );
131 $parser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
132 }
133
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';
139 }
140
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
149 );
150 $frame = $parser->getPreprocessor()->newFrame();
151 $ret = $parser->callParserFunction( $frame, '#tag',
152 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
153 );
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}}' );
159 }
160
161 /**
162 * @covers \MediaWiki\Parser\Parser
163 * @covers \MediaWiki\Parser\ParserOutput::getSections
164 */
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()
172 );
173 $this->assertSame( [
174 [
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',
184 ],
185 [
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',
195 ],
196 [
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',
206 ],
207 [
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',
217 ]
218 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
219 }
220
221 /**
222 * @dataProvider provideNormalizeLinkUrl
223 */
224 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
225 $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
226 }
227
228 public static function provideNormalizeLinkUrl() {
229 return [
230 [
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',
234 ],
235 [
236 'Case normalization of percent-encoded characters',
237 'http://example.org/%ab%cD%Ef%FF',
238 'http://example.org/%AB%CD%EF%FF',
239 ],
240 [
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',
244 ],
245 [
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/?&=+;',
249 ],
250 [
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',
254 ],
255 [
256 'IPv6 links aren\'t escaped',
257 'http://[::1]/foobar',
258 'http://[::1]/foobar',
259 ],
260 [
261 'non-IPv6 links aren\'t unescaped',
262 'http://%5B::1%5D/foobar',
263 'http://%5B::1%5D/foobar',
264 ],
265 ];
266 }
267
268 public function provideRevisionAccess() {
269 $title = $this->makeMockTitle( 'ParserRevisionAccessTest', [
270 'language' => MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' )
271 ] );
272
273 $frank = new UserIdentityValue( 5, 'Frank' );
274
275 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
276 $po = new ParserOptions( $frank );
277
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;' ];
281
282 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
283 $po = new ParserOptions( $frank );
284
285 yield 'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
286
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' ) );
292
293 $po = new ParserOptions( $frank );
294 $po->setCurrentRevisionRecordCallback( static function () use ( $oldRevision ) {
295 return $oldRevision;
296 } );
297
298 yield 'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
299
300 $text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
301
302 $po = new ParserOptions( $frank );
303 $po->setIsPreview( true );
304
305 yield 'preview without override, using context' => [
306 $text,
307 $po,
308 null,
309 'user:Frank;',
310 'user-subst:Frank;',
311 ];
312
313 $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
314 . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
315
316 $newRevision = new MutableRevisionRecord( $title );
317 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor' ) );
318 $newRevision->setTimestamp( '20180808000000' );
319 $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'NEW' ) );
320
321 $po = new ParserOptions( $frank );
322 $po->setIsPreview( true );
323 $po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
324 return $newRevision;
325 } );
326
327 yield 'preview' => [
328 $text,
329 $po,
330 null,
331 'user:NewAuthor;time:20180808000000;',
332 'user-subst:NewAuthor;time-subst:20180808000000;',
333 ];
334
335 $po = new ParserOptions( $frank );
336 $po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
337 return $newRevision;
338 } );
339
340 yield 'pre-save' => [
341 $text,
342 $po,
343 null,
344 'user:NewAuthor;time:20180808000000;',
345 'user-subst:NewAuthor;time-subst:20180808000000;',
346 ];
347
348 $text = "(ONE)<includeonly>(TWO)</includeonly>"
349 . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
350
351 $newRevision = new MutableRevisionRecord( $title );
352 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor' ) );
353 $newRevision->setTimestamp( '20180808000000' );
354 $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
355
356 $po = new ParserOptions( $frank );
357 $po->setIsPreview( true );
358 $po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
359 return $newRevision;
360 } );
361
362 yield 'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
363 }
364
365 /**
366 * @dataProvider provideRevisionAccess
367 */
368 public function testRevisionAccess(
369 $text,
370 ParserOptions $po,
371 $revId,
372 $expectedInHtml,
373 $expectedInPst = null
374 ) {
375 $title = $this->makeMockTitle( 'ParserRevisionAccessTest', [
376 'language' => $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' )
377 ] );
378
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' ) );
384
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' ) );
390
391 $revisionStore = $this->createMock( RevisionStore::class );
392
393 $revisionStore
394 ->method( 'getKnownCurrentRevision' )
395 ->willReturnMap( [
396 [ $title, 100, $oldRevision ],
397 [ $title, 200, $currentRevision ],
398 [ $title, 0, $currentRevision ],
399 ] );
400
401 $revisionStore
402 ->method( 'getRevisionById' )
403 ->willReturnMap( [
404 [ 100, 0, null, $oldRevision ],
405 [ 200, 0, null, $currentRevision ],
406 ] );
407
408 $this->setService( 'RevisionStore', $revisionStore );
409
410 $parser = $this->getServiceContainer()->getParser();
411 $parser->parse( $text, $title, $po, true, true, $revId );
412 $html = $parser->getOutput()->getRawText();
413
414 $this->assertStringContainsString( $expectedInHtml, $html, 'In HTML' );
415
416 if ( $expectedInPst !== null ) {
417 $pst = $parser->preSaveTransform( $text, $title, $po->getUserIdentity(), $po );
418 $this->assertStringContainsString( $expectedInPst, $pst, 'After Pre-Safe Transform' );
419 }
420 }
421
422 public static function provideGuessSectionNameFromWikiText() {
423 return [
424 [ '1/2', 'html5', '#1/2' ],
425 [ '1/2', 'legacy', '#1.2F2' ],
426 ];
427 }
428
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 );
435 }
436
437 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
438 // replaceSection(), getPreloadText()
439 }