Debian Codegraph @

tests/phpunit/integration/includes/CommentFormatter/CommentParserTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Integration\CommentFormatter;
4
5 use LinkCacheTestTrait;
6 use MediaWiki\Cache\LinkBatchFactory;
7 use MediaWiki\CommentFormatter\CommentFormatter;
8 use MediaWiki\CommentFormatter\CommentParser;
9 use MediaWiki\CommentFormatter\CommentParserFactory;
10 use MediaWiki\CommentStore\CommentStoreComment;
11 use MediaWiki\Config\SiteConfiguration;
12 use MediaWiki\Context\RequestContext;
13 use MediaWiki\Logger\LoggerFactory;
14 use MediaWiki\MainConfigNames;
15 use MediaWiki\Revision\MutableRevisionRecord;
16 use MediaWiki\Revision\RevisionRecord;
17 use MediaWiki\Tests\Unit\DummyServicesTrait;
18 use MediaWiki\Title\Title;
19 use RepoGroup;
20
21 /**
22 * @group Database
23 * @covers \MediaWiki\CommentFormatter\CommentParser
24 * @group Database
25 */
26 class CommentParserTest extends \MediaWikiIntegrationTestCase {
27 use DummyServicesTrait;
28 use LinkCacheTestTrait;
29
30 /**
31 * @return RepoGroup
32 */
33 private function getRepoGroup() {
34 $repoGroup = $this->createNoOpMock( RepoGroup::class, [ 'findFiles' ] );
35 $repoGroup->method( 'findFiles' )->willReturn( [] );
36 return $repoGroup;
37 }
38
39 private function getParser() {
40 $services = $this->getServiceContainer();
41 return new CommentParser(
42 $services->getLinkRenderer(),
43 $services->getLinkBatchFactory(),
44 $services->getLinkCache(),
45 $this->getRepoGroup(),
46 $services->getContentLanguage(),
47 $services->getContentLanguage(),
48 $services->getTitleParser(),
49 $services->getNamespaceInfo(),
50 $services->getHookContainer()
51 );
52 }
53
54 private function getFormatter() {
55 $parserFactory = $this->createNoOpMock( CommentParserFactory::class, [ 'create' ] );
56 $parserFactory->method( 'create' )->willReturnCallback( function () {
57 return $this->getParser();
58 } );
59 return new CommentFormatter( $parserFactory );
60 }
61
62 /**
63 * @before
64 */
65 public function interwikiSetUp() {
66 $this->setService( 'InterwikiLookup', function () {
67 return $this->getDummyInterwikiLookup( [
68 'interwiki' => [
69 'iw_prefix' => 'interwiki',
70 'iw_url' => 'https://interwiki/$1',
71 ]
72 ] );
73 } );
74 }
75
76 /**
77 * @before
78 */
79 public function configSetUp() {
80 $conf = new SiteConfiguration();
81 $conf->settings = [
82 'wgServer' => [
83 'foowiki' => '//foo.example.org'
84 ],
85 'wgArticlePath' => [
86 'foowiki' => '/foo/$1',
87 ],
88 ];
89 $conf->suffixes = [ 'wiki' ];
90 $this->setMwGlobals( 'wgConf', $conf );
91 $this->overrideConfigValues( [
92 MainConfigNames::Script => '/w/index.php',
93 MainConfigNames::ArticlePath => '/wiki/$1',
94 MainConfigNames::CapitalLinks => true,
95 MainConfigNames::LanguageCode => 'en',
96 ] );
97 }
98
99 public static function provideFormatComment() {
100 return [
101 // MediaWiki\CommentFormatter\CommentFormatter::format
102 [
103 'a&lt;script&gt;b',
104 'a<script>b',
105 ],
106 [
107 'a—b',
108 'a&mdash;b',
109 ],
110 [
111 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
112 "'''not bolded'''",
113 ],
114 [
115 "try &lt;script&gt;evil&lt;/scipt&gt; things",
116 "try <script>evil</scipt> things",
117 ],
118 // MediaWiki\CommentFormatter\CommentParser::doSectionLinks
119 [
120 '<span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→<bdi dir="ltr">autocomment</bdi></a></span>',
121 "/* autocomment */",
122 ],
123 [
124 '<span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→<bdi dir="ltr">&#91;&#91;linkie?&#93;&#93;</bdi></a></span>',
125 "/* [[linkie?]] */",
126 ],
127 [
128 '<span class="autocomment">: </span> // Edit via via',
129 // Regression test for T222857
130 "/* */ // Edit via via",
131 ],
132 [
133 '<span class="autocomment">: </span> foobar',
134 // Regression test for T222857
135 "/**/ foobar",
136 ],
137 [
138 '<span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→<bdi dir="ltr">autocomment</bdi></a>: </span> post',
139 "/* autocomment */ post",
140 ],
141 [
142 'pre <span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→<bdi dir="ltr">autocomment</bdi></a></span>',
143 "pre /* autocomment */",
144 ],
145 [
146 'pre <span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→<bdi dir="ltr">autocomment</bdi></a>: </span> post',
147 "pre /* autocomment */ post",
148 ],
149 [
150 '<span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→<bdi dir="ltr">autocomment</bdi></a>: </span> multiple? <span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→<bdi dir="ltr">autocomment2</bdi></a></span>',
151 "/* autocomment */ multiple? /* autocomment2 */",
152 ],
153 [
154 '<span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→<bdi dir="ltr">autocomment containing /*</bdi></a>: </span> T70361',
155 "/* autocomment containing /* */ T70361"
156 ],
157 [
158 '<span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→<bdi dir="ltr">autocomment containing &quot;quotes&quot;</bdi></a></span>',
159 "/* autocomment containing \"quotes\" */"
160 ],
161 [
162 '<span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→<bdi dir="ltr">autocomment containing &lt;script&gt;tags&lt;/script&gt;</bdi></a></span>',
163 "/* autocomment containing <script>tags</script> */"
164 ],
165 [
166 '<span class="autocomment"><a href="#autocomment">→<bdi dir="ltr">autocomment</bdi></a></span>',
167 "/* autocomment */",
168 false, true
169 ],
170 [
171 '<span class="autocomment">autocomment</span>',
172 "/* autocomment */",
173 null
174 ],
175 [
176 '',
177 "/* */",
178 false, true
179 ],
180 [
181 '',
182 "/* */",
183 null
184 ],
185 [
186 '<span class="autocomment">&#91;&#91;</span>',
187 "/* [[ */",
188 false, true
189 ],
190 [
191 '<span class="autocomment">&#91;&#91;</span>',
192 "/* [[ */",
193 null
194 ],
195 [
196 "foo <span class=\"autocomment\"><a href=\"#.23\">→<bdi dir=\"ltr\">&#91;&#91;#_\t_&#93;&#93;</bdi></a></span>",
197 "foo /* [[#_\t_]] */",
198 false, true
199 ],
200 [
201 "foo <span class=\"autocomment\">&#91;&#91;#_\t_&#93;&#93;</span>",
202 "foo /* [[#_\t_]] */",
203 null
204 ],
205 [
206 '<span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→<bdi dir="ltr">autocomment</bdi></a></span>',
207 "/* autocomment */",
208 false, false
209 ],
210 [
211 '<span class="autocomment"><a class="external" rel="nofollow" href="//foo.example.org/foo/Special:BlankPage#autocomment">→<bdi dir="ltr">autocomment</bdi></a></span>',
212 "/* autocomment */",
213 false, false, 'foowiki'
214 ],
215 // MediaWiki\CommentFormatter\CommentParser::doWikiLinks
216 [
217 'abc <a href="/w/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
218 "abc [[link]] def",
219 ],
220 [
221 'abc <a href="/w/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">text</a> def',
222 "abc [[link|text]] def",
223 ],
224 [
225 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
226 "abc [[Special:BlankPage|]] def",
227 ],
228 [
229 'abc <a href="/w/index.php?title=%C4%84%C5%9B%C5%BC&amp;action=edit&amp;redlink=1" class="new" title="Ąśż (page does not exist)">ąśż</a> def',
230 "abc [[%C4%85%C5%9B%C5%BC]] def",
231 ],
232 [
233 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
234 "abc [[#section]] def",
235 ],
236 [
237 'abc <a href="/w/index.php?title=/subpage&amp;action=edit&amp;redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
238 "abc [[/subpage]] def",
239 ],
240 [
241 'abc <a href="/w/index.php?title=%22evil!%22&amp;action=edit&amp;redlink=1" class="new" title="&quot;evil!&quot; (page does not exist)">&quot;evil!&quot;</a> def',
242 "abc [[\"evil!\"]] def",
243 ],
244 [
245 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
246 "abc [[<script>very evil</script>]] def",
247 ],
248 [
249 'abc [[|]] def',
250 "abc [[|]] def",
251 ],
252 [
253 'abc <a href="/w/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
254 "abc [[link]] def",
255 false, false
256 ],
257 [
258 'abc <a class="external" rel="nofollow" href="//foo.example.org/foo/Link">link</a> def',
259 "abc [[link]] def",
260 false, false, 'foowiki'
261 ],
262 [
263 '<a href="/w/index.php?title=Special:Upload&amp;wpDestFile=LinkerTest.jpg" class="new" title="LinkerTest.jpg">Media:LinkerTest.jpg</a>',
264 '[[Media:LinkerTest.jpg]]'
265 ],
266 [
267 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
268 '[[:Special:BlankPage]]'
269 ],
270 [
271 '<a href="/w/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">linktrail</a>...',
272 '[[link]]trail...'
273 ],
274 [
275 '<a href="/wiki/Present" title="Present">Present</a>',
276 '[[Present]]',
277 ],
278 [
279 '<a href="https://interwiki/Some_page" class="extiw" title="interwiki:Some page">interwiki:Some page</a>',
280 '[[interwiki:Some page]]',
281 ],
282 [
283 '<a href="https://interwiki/Present" class="extiw" title="interwiki:Present">interwiki:Present</a> <a href="/wiki/Present" title="Present">Present</a>',
284 '[[interwiki:Present]] [[Present]]'
285 ]
286 ];
287 // phpcs:enable
288 }
289
290 /**
291 * @dataProvider provideFormatComment
292 */
293 public function testFormatComment(
294 $expected, $comment, $title = false, $local = false, $wikiId = null
295 ) {
296 $conf = new SiteConfiguration();
297 $conf->settings = [
298 'wgServer' => [
299 'foowiki' => '//foo.example.org',
300 ],
301 'wgArticlePath' => [
302 'foowiki' => '/foo/$1',
303 ],
304 ];
305 $conf->suffixes = [ 'wiki' ];
306
307 $this->setMwGlobals( 'wgConf', $conf );
308 $this->overrideConfigValues( [
309 MainConfigNames::Script => '/w/index.php',
310 MainConfigNames::ArticlePath => '/wiki/$1',
311 MainConfigNames::CapitalLinks => true,
312 // TODO: update tests when the default changes
313 MainConfigNames::FragmentMode => [ 'legacy' ],
314 MainConfigNames::LanguageCode => 'en',
315 ] );
316
317 $this->addGoodLinkObject( 1, Title::makeTitle( NS_MAIN, 'Present' ) );
318
319 if ( $title === false ) {
320 // We need a page title that exists
321 $title = Title::makeTitle( NS_SPECIAL, 'BlankPage' );
322 }
323
324 $parser = $this->getParser();
325 $result = $parser->finalize(
326 $parser->preprocess(
327 $comment,
328 $title,
329 $local,
330 $wikiId
331 )
332 );
333
334 $this->assertEquals( $expected, $result );
335 }
336
337 public static function provideFormatLinksInComment() {
338 return [
339 [
340 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
341 'foo bar [[Special:BlankPage]]',
342 null,
343 ],
344 [
345 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
346 '[[ :Special:BlankPage]]',
347 null,
348 ],
349 [
350 '[[Foo<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
351 '[[Foo[[Special:BlankPage]]',
352 null,
353 ],
354 [
355 '<a class="external" rel="nofollow" href="//foo.example.org/foo/Foo%27bar">Foo&#039;bar</a>',
356 "[[Foo'bar]]",
357 'foowiki',
358 ],
359 [
360 '<a class="external" rel="nofollow" href="//foo.example.org/foo/Foo$100bar">Foo$100bar</a>',
361 '[[Foo$100bar]]',
362 'foowiki',
363 ],
364 [
365 'foo bar <a class="external" rel="nofollow" href="//foo.example.org/foo/Special:BlankPage">Special:BlankPage</a>',
366 'foo bar [[Special:BlankPage]]',
367 'foowiki',
368 ],
369 [
370 'foo bar <a class="external" rel="nofollow" href="//foo.example.org/foo/File:Example">Image:Example</a>',
371 'foo bar [[Image:Example]]',
372 'foowiki',
373 ],
374 ];
375 // phpcs:enable
376 }
377
378 /**
379 * @covers \MediaWiki\CommentFormatter\CommentFormatter
380 * @covers \MediaWiki\CommentFormatter\CommentParser
381 * @dataProvider provideCommentBlock
382 */
383 public function testCommentBlock(
384 $expected, $comment, $title = null, $local = false, $wikiId = null, $useParentheses = true
385 ) {
386 $conf = new SiteConfiguration();
387 $conf->settings = [
388 'wgServer' => [
389 'foowiki' => '//foo.example.org'
390 ],
391 'wgArticlePath' => [
392 'foowiki' => '/foo/$1',
393 ],
394 ];
395 $conf->suffixes = [ 'wiki' ];
396 $this->setMwGlobals( 'wgConf', $conf );
397 $this->overrideConfigValues( [
398 MainConfigNames::Script => '/w/index.php',
399 MainConfigNames::ArticlePath => '/wiki/$1',
400 MainConfigNames::CapitalLinks => true,
401 ] );
402
403 $formatter = $this->getFormatter();
404 $this->assertEquals(
405 $expected,
406 $formatter->formatBlock( $comment, $title, $local, $wikiId, $useParentheses )
407 );
408 }
409
410 public static function provideCommentBlock() {
411 return [
412 [
413 ' <span class="comment">(Test)</span>',
414 'Test'
415 ],
416 'Empty comment' => [ '', '' ],
417 'Backwards compatibility empty comment' => [ '', '*' ],
418 'No parenthesis' => [
419 ' <span class="comment comment--without-parentheses">Test</span>',
420 'Test',
421 null, false, null,
422 false
423 ],
424 'Page exist link' => [
425 ' <span class="comment">(<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>)</span>',
426 '[[Special:BlankPage]]'
427 ],
428 'Page does not exist link' => [
429 ' <span class="comment">(<a href="/w/index.php?title=Test&amp;action=edit&amp;redlink=1" class="new" title="Test (page does not exist)">Test</a>)</span>',
430 '[[Test]]'
431 ],
432 'Link to other page section' => [
433 ' <span class="comment">(<a href="/wiki/Special:BlankPage#Test" title="Special:BlankPage">#Test</a>)</span>',
434 '[[#Test]]',
435 Title::makeTitle( NS_SPECIAL, 'BlankPage' )
436 ],
437 '$local is true' => [
438 ' <span class="comment">(<a href="#Test">#Test</a>)</span>',
439 '[[#Test]]',
440 Title::makeTitle( NS_SPECIAL, 'BlankPage' ),
441 true
442 ],
443 'Given wikiId' => [
444 ' <span class="comment">(<a class="external" rel="nofollow" href="//foo.example.org/foo/Test">Test</a>)</span>',
445 '[[Test]]',
446 null, false,
447 'foowiki'
448 ],
449 'Section link to external wiki page' => [
450 ' <span class="comment">(<a class="external" rel="nofollow" href="//foo.example.org/foo/Special:BlankPage#Test">#Test</a>)</span>',
451 '[[#Test]]',
452 Title::makeTitle( NS_SPECIAL, 'BlankPage' ),
453 false,
454 'foowiki'
455 ],
456 ];
457 }
458
459 /**
460 * Note that we test the new HTML escaping variant.
461 *
462 * @dataProvider provideFormatLinksInComment
463 */
464 public function testFormatLinksInComment( $expected, $input, $wiki ) {
465 $parser = $this->getParser();
466 $title = Title::makeTitle( NS_SPECIAL, 'BlankPage' );
467 $result = $parser->finalize(
468 $parser->preprocess(
469 $input, $title, false, $wiki, false
470 )
471 );
472
473 $this->assertEquals( $expected, $result );
474 }
475
476 public function testLinkCacheInteraction() {
477 $services = $this->getServiceContainer();
478 $present = $this->getExistingTestPage( 'Present' )->getTitle();
479 $absent = $this->getNonexistingTestPage( 'Absent' )->getTitle();
480
481 $parser = $this->getParser();
482 $linkCache = $services->getLinkCache();
483 $result = $parser->finalize( [
484 $parser->preprocess( "[[$present]]" ),
485 $parser->preprocess( "[[$absent]]" )
486 ] );
487 $expected = [
488 '<a href="/wiki/Present" title="Present">Present</a>',
489 '<a href="/w/index.php?title=Absent&amp;action=edit&amp;redlink=1" class="new" title="Absent (page does not exist)">Absent</a>'
490 ];
491 $this->assertSame( $expected, $result );
492 $this->assertGreaterThan( 0, $linkCache->getGoodLinkID( $present ) );
493 $this->assertTrue( $linkCache->isBadLink( $absent ) );
494
495 // Run the comment batch again and confirm that LinkBatch does not need
496 // to execute a query. This is a CommentParser responsibility since
497 // LinkBatch does not provide a transparent read-through cache.
498 // TODO: Generic $this->assertQueryCount() would do the job.
499 $dbProvider = $services->getConnectionProvider();
500 $linkBatchFactory = new LinkBatchFactory(
501 $services->getLinkCache(),
502 $services->getTitleFormatter(),
503 $services->getContentLanguage(),
504 $services->getGenderCache(),
505 $dbProvider,
506 $services->getLinksMigration(),
507 LoggerFactory::getInstance( 'LinkBatch' )
508 );
509 $parser = new CommentParser(
510 $services->getLinkRenderer(),
511 $linkBatchFactory,
512 $linkCache,
513 $this->getRepoGroup(),
514 $services->getContentLanguage(),
515 $services->getContentLanguage(),
516 $services->getTitleParser(),
517 $services->getNamespaceInfo(),
518 $services->getHookContainer()
519 );
520 $result = $parser->finalize( [
521 $parser->preprocess( "[[$present]]" ),
522 $parser->preprocess( "[[$absent]]" )
523 ] );
524 $this->assertSame( $expected, $result );
525 }
526
527 /**
528 * Regression test for T300311
529 */
530 public function testInterwikiLinkCachePollution() {
531 $present = $this->getExistingTestPage( 'Template:Present' )->getTitle();
532
533 $this->getServiceContainer()->getLinkCache()->clear();
534 $parser = $this->getParser();
535 $result = $parser->finalize(
536 $parser->preprocess( "[[interwiki:$present]] [[$present]]" )
537 );
538 $this->assertSame(
539 // phpcs:ignore Generic.Files.LineLength
540 "<a href=\"https://interwiki/$present\" class=\"extiw\" title=\"interwiki:$present\">interwiki:$present</a> <a href=\"/wiki/$present\" title=\"$present\">$present</a>",
541 $result
542 );
543 }
544
545 /**
546 * Regression test for T293665
547 */
548 public function testAlwaysKnownPages() {
549 $this->setTemporaryHook( 'TitleIsAlwaysKnown',
550 static function ( $target, &$isKnown ) {
551 $isKnown = $target->getText() == 'AlwaysKnownFoo';
552 }
553 );
554
555 $title = Title::makeTitle( NS_USER, 'AlwaysKnownFoo' );
556 $this->assertFalse( $title->exists() );
557
558 $parser = $this->getParser();
559 $result = $parser->finalize( $parser->preprocess( 'test [[User:AlwaysKnownFoo]]' ) );
560
561 $this->assertSame(
562 'test <a href="/wiki/User:AlwaysKnownFoo" title="User:AlwaysKnownFoo">User:AlwaysKnownFoo</a>',
563 $result
564 );
565 }
566
567 /**
568 * @dataProvider provideRevComment
569 */
570 public function testRevComment(
571 string $expected,
572 bool $isSysop = false,
573 int $visibility = 0,
574 bool $local = false,
575 bool $isPublic = false,
576 bool $useParentheses = true,
577 ?string $comment = 'Some comment!'
578 ) {
579 $pageData = $this->insertPage( 'RevCommentTestPage' );
580 $revisionRecord = new MutableRevisionRecord( $pageData['title'] );
581 if ( $comment ) {
582 $revisionRecord->setComment( CommentStoreComment::newUnsavedComment( $comment ) );
583 }
584 $revisionRecord->setVisibility( $visibility );
585
586 $context = RequestContext::getMain();
587 $user = $isSysop ? $this->getTestSysop()->getUser() : $this->getTestUser()->getUser();
588 $context->setUser( $user );
589
590 $formatter = $this->getFormatter();
591 $authority = RequestContext::getMain()->getAuthority();
592 $this->assertEquals( $expected, $formatter->formatRevision( $revisionRecord, $authority, $local, $isPublic, $useParentheses ) );
593 }
594
595 public static function provideRevComment() {
596 return [
597 'Should be visible' => [
598 ' <span class="comment">(Some comment!)</span>'
599 ],
600 'Should not have parenthesis' => [
601 ' <span class="comment comment--without-parentheses">Some comment!</span>',
602 false, 0, false, false,
603 false
604 ],
605 'Should be empty' => [
606 '',
607 false, 0, false, false, true,
608 null
609 ],
610 'Deleted comment should not be visible to normal users' => [
611 ' <span class="history-deleted comment"> <span class="comment">(edit summary removed)</span></span>',
612 false,
613 RevisionRecord::DELETED_COMMENT
614 ],
615 'Deleted comment should not be visible to normal users even if public' => [
616 ' <span class="history-deleted comment"> <span class="comment">(edit summary removed)</span></span>',
617 false,
618 RevisionRecord::DELETED_COMMENT,
619 false,
620 true
621 ],
622 'Deleted comment should be visible to sysops' => [
623 ' <span class="history-deleted comment"> <span class="comment">(Some comment!)</span></span>',
624 true,
625 RevisionRecord::DELETED_COMMENT
626 ],
627 ];
628 }
629
630 }