6 class LinkerTest
extends MediaWikiLangTestCase
{
8 * @dataProvider provideCasesForUserLink
9 * @covers Linker::userLink
11 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
12 $this->setMwGlobals( [
13 'wgArticlePath' => '/wiki/$1',
16 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
18 Wikimedia\
suppressWarnings();
20 $actual = Linker
::userLink( $userId, $userName, $altUserName );
22 Wikimedia\restoreWarnings
();
25 $this->assertEquals( $expected, $actual, $msg );
28 public static function provideCasesForUserLink() {
33 # - optional altUserName
36 # Empty name (T222529)
37 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
38 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
40 'false instead of username' => [ '(no username available)', 73, false ],
41 'null instead of username' => [ '(no username available)', 0, null ],
43 # ## ANONYMOUS USER ########################################
45 '<a href="/wiki/Special:Contributions/JohnDoe" '
46 . 'class="mw-userlink mw-anonuserlink" '
47 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
51 '<a href="/wiki/Special:Contributions/::1" '
52 . 'class="mw-userlink mw-anonuserlink" '
53 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
55 'Anonymous with pretty IPv6'
58 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
59 . 'class="mw-userlink mw-anonuserlink" '
60 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
61 0, '0:0:0:0:0:0:0:1', false,
62 'Anonymous with almost pretty IPv6'
65 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
66 . 'class="mw-userlink mw-anonuserlink" '
67 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
68 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
69 'Anonymous with full IPv6'
72 '<a href="/wiki/Special:Contributions/::1" '
73 . 'class="mw-userlink mw-anonuserlink" '
74 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
75 0, '::1', 'AlternativeUsername',
76 'Anonymous with pretty IPv6 and an alternative username'
81 '<a href="/wiki/Special:Contributions/127.0.0.1" '
82 . 'class="mw-userlink mw-anonuserlink" '
83 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
84 0, '127.0.0.1', false,
88 '<a href="/wiki/Special:Contributions/127.0.0.1" '
89 . 'class="mw-userlink mw-anonuserlink" '
90 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
91 0, '127.0.0.1', 'AlternativeUsername',
92 'Anonymous with IPv4 and an alternative username'
95 # ## Regular user ##########################################
101 * @dataProvider provideUserToolLinks
102 * @covers Linker::userToolLinks
103 * @param string $expected
105 * @param string $userText
107 public function testUserToolLinks( $expected, $userId, $userText ) {
108 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
109 if ( $userText === '' ) {
110 Wikimedia\
suppressWarnings();
112 $actual = Linker
::userToolLinks( $userId, $userText );
113 if ( $userText === '' ) {
114 Wikimedia\restoreWarnings
();
117 $this->assertSame( $expected, $actual );
120 public static function provideUserToolLinks() {
122 // Empty name (T222529)
123 'Empty username, userid 0' => [ ' (no username available)', 0, '' ],
124 'Empty username, userid > 0' => [ ' (no username available)', 73, '' ],
129 * @dataProvider provideUserTalkLink
130 * @covers Linker::userTalkLink
131 * @param string $expected
133 * @param string $userText
135 public function testUserTalkLink( $expected, $userId, $userText ) {
136 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
137 if ( $userText === '' ) {
138 Wikimedia\
suppressWarnings();
140 $actual = Linker
::userTalkLink( $userId, $userText );
141 if ( $userText === '' ) {
142 Wikimedia\restoreWarnings
();
145 $this->assertSame( $expected, $actual );
148 public static function provideUserTalkLink() {
150 // Empty name (T222529)
151 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
152 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
157 * @dataProvider provideBlockLink
158 * @covers Linker::blockLink
159 * @param string $expected
161 * @param string $userText
163 public function testBlockLink( $expected, $userId, $userText ) {
164 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
165 if ( $userText === '' ) {
166 Wikimedia\
suppressWarnings();
168 $actual = Linker
::blockLink( $userId, $userText );
169 if ( $userText === '' ) {
170 Wikimedia\restoreWarnings
();
173 $this->assertSame( $expected, $actual );
176 public static function provideBlockLink() {
178 // Empty name (T222529)
179 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
180 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
185 * @dataProvider provideEmailLink
186 * @covers Linker::emailLink
187 * @param string $expected
189 * @param string $userText
191 public function testEmailLink( $expected, $userId, $userText ) {
192 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
193 if ( $userText === '' ) {
194 Wikimedia\
suppressWarnings();
196 $actual = Linker
::emailLink( $userId, $userText );
197 if ( $userText === '' ) {
198 Wikimedia\restoreWarnings
();
201 $this->assertSame( $expected, $actual );
204 public static function provideEmailLink() {
206 // Empty name (T222529)
207 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
208 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
213 * @dataProvider provideCasesForFormatComment
214 * @covers Linker::formatComment
215 * @covers Linker::formatAutocomments
216 * @covers Linker::formatLinksInComment
218 public function testFormatComment(
219 $expected, $comment, $title = false, $local = false, $wikiId = null
221 $conf = new SiteConfiguration();
224 'enwiki' => '//en.example.org',
225 'dewiki' => '//de.example.org',
232 $conf->suffixes
= [ 'wiki' ];
234 $this->setMwGlobals( [
235 'wgScript' => '/wiki/index.php',
236 'wgArticlePath' => '/wiki/$1',
237 'wgCapitalLinks' => true,
241 if ( $title === false ) {
242 // We need a page title that exists
243 $title = Title
::newFromText( 'Special:BlankPage' );
248 Linker
::formatComment( $comment, $title, $local, $wikiId )
252 public function provideCasesForFormatComment() {
253 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
255 // phpcs:disable Generic.Files.LineLength
257 // Linker::formatComment
267 "'''not bolded'''",
271 "try <script>evil</scipt> things",
272 "try <script>evil</scipt> things",
274 // Linker::formatAutocomments
276 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
280 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→‎[[linkie?]]</a></span></span>',
284 '<span dir="auto"><span class="autocomment">: </span> // Edit via via</span>',
285 // Regression test for T222857
286 "/* */ // Edit via via",
289 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
290 "/* autocomment */ post",
293 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
294 "pre /* autocomment */",
297 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
298 "pre /* autocomment */ post",
301 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> multiple? <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→‎autocomment2</a></span></span></span>',
302 "/* autocomment */ multiple? /* autocomment2 */",
305 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→‎autocomment containing /*</a>: </span> T70361</span>',
306 "/* autocomment containing /* */ T70361"
309 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→‎autocomment containing "quotes"</a></span></span>',
310 "/* autocomment containing \"quotes\" */"
313 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→‎autocomment containing <script>tags</script></a></span></span>',
314 "/* autocomment containing <script>tags</script> */"
317 '<span dir="auto"><span class="autocomment"><a href="#autocomment">→‎autocomment</a></span></span>',
322 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
337 '<span dir="auto"><span class="autocomment">[[</span></span>',
342 '<span dir="auto"><span class="autocomment">[[</span></span>',
347 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#.23\">→‎[[#_\t_]]</a></span></span>",
348 "foo /* [[#_\t_]] */",
352 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#_.09\">#_\t_</a></span></span>",
353 "foo /* [[#_\t_]] */",
357 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
362 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→‎autocomment</a></span></span>',
364 false, false, $wikiId
366 // Linker::formatLinksInComment
368 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
372 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def',
373 "abc [[link|text]] def",
376 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
377 "abc [[Special:BlankPage|]] def",
380 'abc <a href="/wiki/index.php?title=%C4%84%C5%9B%C5%BC&action=edit&redlink=1" class="new" title="Ąśż (page does not exist)">ąśż</a> def',
381 "abc [[%C4%85%C5%9B%C5%BC]] def",
384 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
385 "abc [[#section]] def",
388 'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
389 "abc [[/subpage]] def",
392 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
393 "abc [[\"evil!\"]] def",
396 'abc [[<script>very evil</script>]] def',
397 "abc [[<script>very evil</script>]] def",
404 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
409 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
411 false, false, $wikiId
418 * @covers Linker::formatLinksInComment
419 * @dataProvider provideCasesForFormatLinksInComment
421 public function testFormatLinksInComment( $expected, $input, $wiki ) {
422 $conf = new SiteConfiguration();
425 'enwiki' => '//en.example.org'
431 $conf->suffixes
= [ 'wiki' ];
432 $this->setMwGlobals( [
433 'wgScript' => '/wiki/index.php',
434 'wgArticlePath' => '/wiki/$1',
435 'wgCapitalLinks' => true,
441 Linker
::formatLinksInComment( $input, Title
::newFromText( 'Special:BlankPage' ), false, $wiki )
446 * @covers Linker::generateRollback
447 * @dataProvider provideCasesForRollbackGeneration
449 public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) {
450 $this->markTestSkippedIfDbType( 'postgres' );
452 $context = RequestContext
::getMain();
453 $user = $context->getUser();
454 $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
456 $this->assertEquals( 0, Title
::newFromText( $title )->getArticleID() );
457 $pageData = $this->insertPage( $title );
458 $page = WikiPage
::factory( $pageData['title'] );
460 $updater = $page->newPageUpdater( $user );
461 $updater->setContent( \MediaWiki\Revision\SlotRecord
::MAIN
,
462 new TextContent( 'Technical Wishes 123!' )
464 $summary = CommentStoreComment
::newUnsavedComment( 'Some comment!' );
465 $updater->saveRevision( $summary );
467 $rollbackOutput = Linker
::generateRollback( $page->getRevision(), $context );
468 $modules = $context->getOutput()->getModules();
469 $currentRev = $page->getRevision();
470 $oldestRev = $page->getOldestRevision();
472 $this->assertEquals( $expectedModules, $modules );
473 $this->assertEquals( $user->getName(), $currentRev->getUserText() );
475 static::getTestSysop()->getUser(),
476 $oldestRev->getUserText()
482 $ids[] = $r->getId();
485 $this->assertEquals( [ $oldestRev->getId(), $currentRev->getId() ], $ids );
487 $this->assertContains( 'rollback 1 edit', $rollbackOutput );
490 public static function provideCasesForRollbackGeneration() {
494 [ 'mediawiki.page.rollback.confirmation' ],
500 'Rollback_Test_Page2'
505 public static function provideCasesForFormatLinksInComment() {
506 // phpcs:disable Generic.Files.LineLength
509 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
510 'foo bar [[Special:BlankPage]]',
514 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
515 '[[ :Special:BlankPage]]',
519 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
524 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
525 'foo bar [[Special:BlankPage]]',
529 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
530 'foo bar [[Image:Example]]',
537 public static function provideLinkBeginHook() {
538 // phpcs:disable Generic.Files.LineLength
542 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
545 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
549 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
550 $attribs['bar'] = 'baz';
552 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
556 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
557 $query['bar'] = 'baz';
559 '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
561 // Force HTTP $options
563 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
564 $options = [ 'http' ];
566 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
568 // Force 'forcearticlepath' in $options
570 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
571 $options = [ 'forcearticlepath' ];
572 $query['foo'] = 'bar';
574 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
578 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
589 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
590 * @dataProvider provideLinkBeginHook
592 public function testLinkBeginHook( $callback, $expected ) {
593 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
594 $this->setMwGlobals( [
595 'wgArticlePath' => '/wiki/$1',
596 'wgServer' => '//example.org',
597 'wgCanonicalServer' => 'http://example.org',
598 'wgScriptPath' => '/w',
599 'wgScript' => '/w/index.php',
602 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
603 $title = SpecialPage
::getTitleFor( 'Blankpage' );
604 $out = Linker
::link( $title );
605 $this->assertEquals( $expected, $out );
608 public static function provideLinkEndHook() {
612 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
615 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
619 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
620 $attribs['bar'] = 'baz';
622 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
624 // Fully override return value and abort hook
626 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
627 $ret = 'blahblahblah';
637 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
638 * @dataProvider provideLinkEndHook
640 public function testLinkEndHook( $callback, $expected ) {
641 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
642 $this->setMwGlobals( [
643 'wgArticlePath' => '/wiki/$1',
646 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
648 $title = SpecialPage
::getTitleFor( 'Blankpage' );
649 $out = Linker
::link( $title );
650 $this->assertEquals( $expected, $out );