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.
17 if ( $userName === '' ) {
18 Wikimedia\
suppressWarnings();
20 $actual = Linker
::userLink( $userId, $userName, $altUserName );
21 if ( $userName === '' ) {
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 # ## ANONYMOUS USER ########################################
42 '<a href="/wiki/Special:Contributions/JohnDoe" '
43 . 'class="mw-userlink mw-anonuserlink" '
44 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
48 '<a href="/wiki/Special:Contributions/::1" '
49 . 'class="mw-userlink mw-anonuserlink" '
50 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
52 'Anonymous with pretty IPv6'
55 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
56 . 'class="mw-userlink mw-anonuserlink" '
57 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
58 0, '0:0:0:0:0:0:0:1', false,
59 'Anonymous with almost pretty IPv6'
62 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
63 . 'class="mw-userlink mw-anonuserlink" '
64 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
65 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
66 'Anonymous with full IPv6'
69 '<a href="/wiki/Special:Contributions/::1" '
70 . 'class="mw-userlink mw-anonuserlink" '
71 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
72 0, '::1', 'AlternativeUsername',
73 'Anonymous with pretty IPv6 and an alternative username'
78 '<a href="/wiki/Special:Contributions/127.0.0.1" '
79 . 'class="mw-userlink mw-anonuserlink" '
80 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
81 0, '127.0.0.1', false,
85 '<a href="/wiki/Special:Contributions/127.0.0.1" '
86 . 'class="mw-userlink mw-anonuserlink" '
87 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
88 0, '127.0.0.1', 'AlternativeUsername',
89 'Anonymous with IPv4 and an alternative username'
92 # ## Regular user ##########################################
98 * @dataProvider provideUserToolLinks
99 * @covers Linker::userToolLinks
100 * @param string $expected
102 * @param string $userText
104 public function testUserToolLinks( $expected, $userId, $userText ) {
105 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
106 if ( $userText === '' ) {
107 Wikimedia\
suppressWarnings();
109 $actual = Linker
::userToolLinks( $userId, $userText );
110 if ( $userText === '' ) {
111 Wikimedia\restoreWarnings
();
114 $this->assertSame( $expected, $actual );
117 public static function provideUserToolLinks() {
119 // Empty name (T222529)
120 'Empty username, userid 0' => [ ' (no username available)', 0, '' ],
121 'Empty username, userid > 0' => [ ' (no username available)', 73, '' ],
126 * @dataProvider provideUserTalkLink
127 * @covers Linker::userTalkLink
128 * @param string $expected
130 * @param string $userText
132 public function testUserTalkLink( $expected, $userId, $userText ) {
133 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
134 if ( $userText === '' ) {
135 Wikimedia\
suppressWarnings();
137 $actual = Linker
::userTalkLink( $userId, $userText );
138 if ( $userText === '' ) {
139 Wikimedia\restoreWarnings
();
142 $this->assertSame( $expected, $actual );
145 public static function provideUserTalkLink() {
147 // Empty name (T222529)
148 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
149 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
154 * @dataProvider provideBlockLink
155 * @covers Linker::blockLink
156 * @param string $expected
158 * @param string $userText
160 public function testBlockLink( $expected, $userId, $userText ) {
161 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
162 if ( $userText === '' ) {
163 Wikimedia\
suppressWarnings();
165 $actual = Linker
::blockLink( $userId, $userText );
166 if ( $userText === '' ) {
167 Wikimedia\restoreWarnings
();
170 $this->assertSame( $expected, $actual );
173 public static function provideBlockLink() {
175 // Empty name (T222529)
176 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
177 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
182 * @dataProvider provideEmailLink
183 * @covers Linker::emailLink
184 * @param string $expected
186 * @param string $userText
188 public function testEmailLink( $expected, $userId, $userText ) {
189 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
190 if ( $userText === '' ) {
191 Wikimedia\
suppressWarnings();
193 $actual = Linker
::emailLink( $userId, $userText );
194 if ( $userText === '' ) {
195 Wikimedia\restoreWarnings
();
198 $this->assertSame( $expected, $actual );
201 public static function provideEmailLink() {
203 // Empty name (T222529)
204 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
205 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
210 * @dataProvider provideCasesForFormatComment
211 * @covers Linker::formatComment
212 * @covers Linker::formatAutocomments
213 * @covers Linker::formatLinksInComment
215 public function testFormatComment(
216 $expected, $comment, $title = false, $local = false, $wikiId = null
218 $conf = new SiteConfiguration();
221 'enwiki' => '//en.example.org',
222 'dewiki' => '//de.example.org',
229 $conf->suffixes
= [ 'wiki' ];
231 $this->setMwGlobals( [
232 'wgScript' => '/wiki/index.php',
233 'wgArticlePath' => '/wiki/$1',
234 'wgCapitalLinks' => true,
238 if ( $title === false ) {
239 // We need a page title that exists
240 $title = Title
::newFromText( 'Special:BlankPage' );
245 Linker
::formatComment( $comment, $title, $local, $wikiId )
249 public function provideCasesForFormatComment() {
250 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
252 // phpcs:disable Generic.Files.LineLength
254 // Linker::formatComment
264 "'''not bolded'''",
268 "try <script>evil</scipt> things",
269 "try <script>evil</scipt> things",
271 // Linker::formatAutocomments
273 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
277 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→‎[[linkie?]]</a></span></span>',
281 '<span dir="auto"><span class="autocomment">: </span> // Edit via via</span>',
282 // Regression test for T222857
283 "/* */ // Edit via via",
286 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
287 "/* autocomment */ post",
290 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
291 "pre /* autocomment */",
294 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
295 "pre /* autocomment */ post",
298 '<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>',
299 "/* autocomment */ multiple? /* autocomment2 */",
302 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→‎autocomment containing /*</a>: </span> T70361</span>',
303 "/* autocomment containing /* */ T70361"
306 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→‎autocomment containing "quotes"</a></span></span>',
307 "/* autocomment containing \"quotes\" */"
310 '<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>',
311 "/* autocomment containing <script>tags</script> */"
314 '<span dir="auto"><span class="autocomment"><a href="#autocomment">→‎autocomment</a></span></span>',
319 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
334 '<span dir="auto"><span class="autocomment">[[</span></span>',
339 '<span dir="auto"><span class="autocomment">[[</span></span>',
344 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#.23\">→‎[[#_\t_]]</a></span></span>",
345 "foo /* [[#_\t_]] */",
349 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#_.09\">#_\t_</a></span></span>",
350 "foo /* [[#_\t_]] */",
354 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
359 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→‎autocomment</a></span></span>',
361 false, false, $wikiId
363 // Linker::formatLinksInComment
365 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
369 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def',
370 "abc [[link|text]] def",
373 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
374 "abc [[Special:BlankPage|]] def",
377 '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',
378 "abc [[%C4%85%C5%9B%C5%BC]] def",
381 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
382 "abc [[#section]] def",
385 'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
386 "abc [[/subpage]] def",
389 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
390 "abc [[\"evil!\"]] def",
393 'abc [[<script>very evil</script>]] def',
394 "abc [[<script>very evil</script>]] def",
401 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
406 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
408 false, false, $wikiId
415 * @covers Linker::formatLinksInComment
416 * @dataProvider provideCasesForFormatLinksInComment
418 public function testFormatLinksInComment( $expected, $input, $wiki ) {
419 $conf = new SiteConfiguration();
422 'enwiki' => '//en.example.org'
428 $conf->suffixes
= [ 'wiki' ];
429 $this->setMwGlobals( [
430 'wgScript' => '/wiki/index.php',
431 'wgArticlePath' => '/wiki/$1',
432 'wgCapitalLinks' => true,
438 Linker
::formatLinksInComment( $input, Title
::newFromText( 'Special:BlankPage' ), false, $wiki )
443 * @covers Linker::generateRollback
444 * @dataProvider provideCasesForRollbackGeneration
446 public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) {
447 $this->markTestSkippedIfDbType( 'postgres' );
449 $context = RequestContext
::getMain();
450 $user = $context->getUser();
451 $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
453 $this->assertEquals( 0, Title
::newFromText( $title )->getArticleID() );
454 $pageData = $this->insertPage( $title );
455 $page = WikiPage
::factory( $pageData['title'] );
457 $updater = $page->newPageUpdater( $user );
458 $updater->setContent( \MediaWiki\Revision\SlotRecord
::MAIN
,
459 new TextContent( 'Technical Wishes 123!' )
461 $summary = CommentStoreComment
::newUnsavedComment( 'Some comment!' );
462 $updater->saveRevision( $summary );
464 $rollbackOutput = Linker
::generateRollback( $page->getRevision(), $context );
465 $modules = $context->getOutput()->getModules();
466 $currentRev = $page->getRevision();
467 $oldestRev = $page->getOldestRevision();
469 $this->assertEquals( $expectedModules, $modules );
470 $this->assertEquals( $user->getName(), $currentRev->getUserText() );
472 static::getTestSysop()->getUser(),
473 $oldestRev->getUserText()
479 $ids[] = $r->getId();
482 $this->assertEquals( [ $oldestRev->getId(), $currentRev->getId() ], $ids );
484 $this->assertContains( 'rollback 1 edit', $rollbackOutput );
487 public static function provideCasesForRollbackGeneration() {
491 [ 'mediawiki.page.rollback.confirmation' ],
497 'Rollback_Test_Page2'
502 public static function provideCasesForFormatLinksInComment() {
503 // phpcs:disable Generic.Files.LineLength
506 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
507 'foo bar [[Special:BlankPage]]',
511 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
512 '[[ :Special:BlankPage]]',
516 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
521 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
522 'foo bar [[Special:BlankPage]]',
526 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
527 'foo bar [[Image:Example]]',
534 public static function provideLinkBeginHook() {
535 // phpcs:disable Generic.Files.LineLength
539 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
542 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
546 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
547 $attribs['bar'] = 'baz';
549 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
553 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
554 $query['bar'] = 'baz';
556 '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
558 // Force HTTP $options
560 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
561 $options = [ 'http' ];
563 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
565 // Force 'forcearticlepath' in $options
567 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
568 $options = [ 'forcearticlepath' ];
569 $query['foo'] = 'bar';
571 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
575 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
586 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
587 * @dataProvider provideLinkBeginHook
589 public function testLinkBeginHook( $callback, $expected ) {
590 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
591 $this->setMwGlobals( [
592 'wgArticlePath' => '/wiki/$1',
593 'wgServer' => '//example.org',
594 'wgCanonicalServer' => 'http://example.org',
595 'wgScriptPath' => '/w',
596 'wgScript' => '/w/index.php',
599 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
600 $title = SpecialPage
::getTitleFor( 'Blankpage' );
601 $out = Linker
::link( $title );
602 $this->assertEquals( $expected, $out );
605 public static function provideLinkEndHook() {
609 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
612 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
616 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
617 $attribs['bar'] = 'baz';
619 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
621 // Fully override return value and abort hook
623 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
624 $ret = 'blahblahblah';
634 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
635 * @dataProvider provideLinkEndHook
637 public function testLinkEndHook( $callback, $expected ) {
638 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
639 $this->setMwGlobals( [
640 'wgArticlePath' => '/wiki/$1',
643 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
645 $title = SpecialPage
::getTitleFor( 'Blankpage' );
646 $out = Linker
::link( $title );
647 $this->assertEquals( $expected, $out );