6 class LinkerTest
extends MediaWikiLangTestCase
{
9 * @dataProvider provideCasesForUserLink
10 * @covers Linker::userLink
12 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
13 $this->setMwGlobals( [
14 'wgArticlePath' => '/wiki/$1',
17 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
18 if ( $userName === '' ) {
19 Wikimedia\
suppressWarnings();
21 $actual = Linker
::userLink( $userId, $userName, $altUserName );
22 if ( $userName === '' ) {
23 Wikimedia\restoreWarnings
();
26 $this->assertEquals( $expected, $actual, $msg );
29 public static function provideCasesForUserLink() {
34 # - optional altUserName
37 # Empty name (T222529)
38 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
39 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
41 # ## ANONYMOUS USER ########################################
43 '<a href="/wiki/Special:Contributions/JohnDoe" '
44 . 'class="mw-userlink mw-anonuserlink" '
45 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
49 '<a href="/wiki/Special:Contributions/::1" '
50 . 'class="mw-userlink mw-anonuserlink" '
51 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
53 'Anonymous with pretty IPv6'
56 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
57 . 'class="mw-userlink mw-anonuserlink" '
58 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
59 0, '0:0:0:0:0:0:0:1', false,
60 'Anonymous with almost pretty IPv6'
63 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
64 . 'class="mw-userlink mw-anonuserlink" '
65 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
66 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
67 'Anonymous with full IPv6'
70 '<a href="/wiki/Special:Contributions/::1" '
71 . 'class="mw-userlink mw-anonuserlink" '
72 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
73 0, '::1', 'AlternativeUsername',
74 'Anonymous with pretty IPv6 and an alternative username'
79 '<a href="/wiki/Special:Contributions/127.0.0.1" '
80 . 'class="mw-userlink mw-anonuserlink" '
81 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
82 0, '127.0.0.1', false,
86 '<a href="/wiki/Special:Contributions/127.0.0.1" '
87 . 'class="mw-userlink mw-anonuserlink" '
88 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
89 0, '127.0.0.1', 'AlternativeUsername',
90 'Anonymous with IPv4 and an alternative username'
93 # ## Regular user ##########################################
99 * @dataProvider provideUserToolLinks
100 * @covers Linker::userToolLinks
101 * @param string $expected
103 * @param string $userText
105 public function testUserToolLinks( $expected, $userId, $userText ) {
106 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
107 if ( $userText === '' ) {
108 Wikimedia\
suppressWarnings();
110 $actual = Linker
::userToolLinks( $userId, $userText );
111 if ( $userText === '' ) {
112 Wikimedia\restoreWarnings
();
115 $this->assertSame( $expected, $actual );
118 public static function provideUserToolLinks() {
120 // Empty name (T222529)
121 'Empty username, userid 0' => [ ' (no username available)', 0, '' ],
122 'Empty username, userid > 0' => [ ' (no username available)', 73, '' ],
127 * @dataProvider provideUserTalkLink
128 * @covers Linker::userTalkLink
129 * @param string $expected
131 * @param string $userText
133 public function testUserTalkLink( $expected, $userId, $userText ) {
134 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
135 if ( $userText === '' ) {
136 Wikimedia\
suppressWarnings();
138 $actual = Linker
::userTalkLink( $userId, $userText );
139 if ( $userText === '' ) {
140 Wikimedia\restoreWarnings
();
143 $this->assertSame( $expected, $actual );
146 public static function provideUserTalkLink() {
148 // Empty name (T222529)
149 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
150 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
155 * @dataProvider provideBlockLink
156 * @covers Linker::blockLink
157 * @param string $expected
159 * @param string $userText
161 public function testBlockLink( $expected, $userId, $userText ) {
162 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
163 if ( $userText === '' ) {
164 Wikimedia\
suppressWarnings();
166 $actual = Linker
::blockLink( $userId, $userText );
167 if ( $userText === '' ) {
168 Wikimedia\restoreWarnings
();
171 $this->assertSame( $expected, $actual );
174 public static function provideBlockLink() {
176 // Empty name (T222529)
177 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
178 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
183 * @dataProvider provideEmailLink
184 * @covers Linker::emailLink
185 * @param string $expected
187 * @param string $userText
189 public function testEmailLink( $expected, $userId, $userText ) {
190 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
191 if ( $userText === '' ) {
192 Wikimedia\
suppressWarnings();
194 $actual = Linker
::emailLink( $userId, $userText );
195 if ( $userText === '' ) {
196 Wikimedia\restoreWarnings
();
199 $this->assertSame( $expected, $actual );
202 public static function provideEmailLink() {
204 // Empty name (T222529)
205 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
206 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
211 * @dataProvider provideCasesForFormatComment
212 * @covers Linker::formatComment
213 * @covers Linker::formatAutocomments
214 * @covers Linker::formatLinksInComment
216 public function testFormatComment(
217 $expected, $comment, $title = false, $local = false, $wikiId = null
219 $conf = new SiteConfiguration();
222 'enwiki' => '//en.example.org',
223 'dewiki' => '//de.example.org',
230 $conf->suffixes
= [ 'wiki' ];
232 $this->setMwGlobals( [
233 'wgScript' => '/wiki/index.php',
234 'wgArticlePath' => '/wiki/$1',
235 'wgCapitalLinks' => true,
239 if ( $title === false ) {
240 // We need a page title that exists
241 $title = Title
::newFromText( 'Special:BlankPage' );
246 Linker
::formatComment( $comment, $title, $local, $wikiId )
250 public function provideCasesForFormatComment() {
251 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
253 // phpcs:disable Generic.Files.LineLength
255 // Linker::formatComment
265 "'''not bolded'''",
269 "try <script>evil</scipt> things",
270 "try <script>evil</scipt> things",
272 // Linker::formatAutocomments
274 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
278 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→‎[[linkie?]]</a></span></span>',
282 '<span dir="auto"><span class="autocomment">: </span> // Edit via via</span>',
283 // Regression test for T222857
284 "/* */ // Edit via via",
287 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
288 "/* autocomment */ post",
291 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
292 "pre /* autocomment */",
295 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
296 "pre /* autocomment */ post",
299 '<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>',
300 "/* autocomment */ multiple? /* autocomment2 */",
303 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→‎autocomment containing /*</a>: </span> T70361</span>',
304 "/* autocomment containing /* */ T70361"
307 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→‎autocomment containing "quotes"</a></span></span>',
308 "/* autocomment containing \"quotes\" */"
311 '<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>',
312 "/* autocomment containing <script>tags</script> */"
315 '<span dir="auto"><span class="autocomment"><a href="#autocomment">→‎autocomment</a></span></span>',
320 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
335 '<span dir="auto"><span class="autocomment">[[</span></span>',
340 '<span dir="auto"><span class="autocomment">[[</span></span>',
345 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#.23\">→‎[[#_\t_]]</a></span></span>",
346 "foo /* [[#_\t_]] */",
350 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#_.09\">#_\t_</a></span></span>",
351 "foo /* [[#_\t_]] */",
355 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
360 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→‎autocomment</a></span></span>',
362 false, false, $wikiId
364 // Linker::formatLinksInComment
366 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
370 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def',
371 "abc [[link|text]] def",
374 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
375 "abc [[Special:BlankPage|]] def",
378 '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',
379 "abc [[%C4%85%C5%9B%C5%BC]] def",
382 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
383 "abc [[#section]] def",
386 'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
387 "abc [[/subpage]] def",
390 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
391 "abc [[\"evil!\"]] def",
394 'abc [[<script>very evil</script>]] def',
395 "abc [[<script>very evil</script>]] def",
402 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
407 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
409 false, false, $wikiId
416 * @covers Linker::formatLinksInComment
417 * @dataProvider provideCasesForFormatLinksInComment
419 public function testFormatLinksInComment( $expected, $input, $wiki ) {
420 $conf = new SiteConfiguration();
423 'enwiki' => '//en.example.org'
429 $conf->suffixes
= [ 'wiki' ];
430 $this->setMwGlobals( [
431 'wgScript' => '/wiki/index.php',
432 'wgArticlePath' => '/wiki/$1',
433 'wgCapitalLinks' => true,
439 Linker
::formatLinksInComment( $input, Title
::newFromText( 'Special:BlankPage' ), false, $wiki )
444 * @covers Linker::generateRollback
445 * @dataProvider provideCasesForRollbackGeneration
447 public function testGenerateRollback( $rollbackEnabled, $expectedModules ) {
448 $this->markTestSkippedIfDbType( 'postgres' );
450 $context = RequestContext
::getMain();
451 $user = $context->getUser();
452 $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
454 $pageData = $this->insertPage( 'Rollback_Test_Page' );
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();
467 $this->assertEquals( $expectedModules, $modules );
468 $this->assertContains( 'rollback 1 edit', $rollbackOutput );
471 public static function provideCasesForRollbackGeneration() {
475 [ 'mediawiki.page.rollback.confirmation' ]
485 public static function provideCasesForFormatLinksInComment() {
486 // phpcs:disable Generic.Files.LineLength
489 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
490 'foo bar [[Special:BlankPage]]',
494 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
495 '[[ :Special:BlankPage]]',
499 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
504 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
505 'foo bar [[Special:BlankPage]]',
509 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
510 'foo bar [[Image:Example]]',
517 public static function provideLinkBeginHook() {
518 // phpcs:disable Generic.Files.LineLength
522 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
525 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
529 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
530 $attribs['bar'] = 'baz';
532 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
536 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
537 $query['bar'] = 'baz';
539 '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
541 // Force HTTP $options
543 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
544 $options = [ 'http' ];
546 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
548 // Force 'forcearticlepath' in $options
550 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
551 $options = [ 'forcearticlepath' ];
552 $query['foo'] = 'bar';
554 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
558 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
569 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
570 * @dataProvider provideLinkBeginHook
572 public function testLinkBeginHook( $callback, $expected ) {
573 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
574 $this->setMwGlobals( [
575 'wgArticlePath' => '/wiki/$1',
576 'wgServer' => '//example.org',
577 'wgCanonicalServer' => 'http://example.org',
578 'wgScriptPath' => '/w',
579 'wgScript' => '/w/index.php',
582 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
583 $title = SpecialPage
::getTitleFor( 'Blankpage' );
584 $out = Linker
::link( $title );
585 $this->assertEquals( $expected, $out );
588 public static function provideLinkEndHook() {
592 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
595 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
599 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
600 $attribs['bar'] = 'baz';
602 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
604 // Fully override return value and abort hook
606 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
607 $ret = 'blahblahblah';
617 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
618 * @dataProvider provideLinkEndHook
620 public function testLinkEndHook( $callback, $expected ) {
621 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
622 $this->setMwGlobals( [
623 'wgArticlePath' => '/wiki/$1',
626 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
628 $title = SpecialPage
::getTitleFor( 'Blankpage' );
629 $out = Linker
::link( $title );
630 $this->assertEquals( $expected, $out );