3 use MediaWiki\MediaWikiServices
;
8 class LinkerTest
extends MediaWikiLangTestCase
{
11 * @dataProvider provideCasesForUserLink
12 * @covers Linker::userLink
14 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
15 $this->setMwGlobals( [
16 'wgArticlePath' => '/wiki/$1',
21 Linker
::userLink( $userId, $userName, $altUserName ),
26 public static function provideCasesForUserLink() {
31 # - optional altUserName
35 # ## ANONYMOUS USER ########################################
37 '<a href="/wiki/Special:Contributions/JohnDoe" '
38 . 'class="mw-userlink mw-anonuserlink" '
39 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
43 '<a href="/wiki/Special:Contributions/::1" '
44 . 'class="mw-userlink mw-anonuserlink" '
45 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
47 'Anonymous with pretty IPv6'
50 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
51 . 'class="mw-userlink mw-anonuserlink" '
52 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
53 0, '0:0:0:0:0:0:0:1', false,
54 'Anonymous with almost pretty IPv6'
57 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
58 . 'class="mw-userlink mw-anonuserlink" '
59 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
60 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
61 'Anonymous with full IPv6'
64 '<a href="/wiki/Special:Contributions/::1" '
65 . 'class="mw-userlink mw-anonuserlink" '
66 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
67 0, '::1', 'AlternativeUsername',
68 'Anonymous with pretty IPv6 and an alternative username'
73 '<a href="/wiki/Special:Contributions/127.0.0.1" '
74 . 'class="mw-userlink mw-anonuserlink" '
75 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
76 0, '127.0.0.1', false,
80 '<a href="/wiki/Special:Contributions/127.0.0.1" '
81 . 'class="mw-userlink mw-anonuserlink" '
82 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
83 0, '127.0.0.1', 'AlternativeUsername',
84 'Anonymous with IPv4 and an alternative username'
87 # ## Regular user ##########################################
93 * @dataProvider provideCasesForFormatComment
94 * @covers Linker::formatComment
95 * @covers Linker::formatAutocomments
96 * @covers Linker::formatLinksInComment
98 public function testFormatComment(
99 $expected, $comment, $title = false, $local = false, $wikiId = null
101 $conf = new SiteConfiguration();
104 'enwiki' => '//en.example.org',
105 'dewiki' => '//de.example.org',
112 $conf->suffixes
= [ 'wiki' ];
114 $this->setMwGlobals( [
115 'wgScript' => '/wiki/index.php',
116 'wgArticlePath' => '/wiki/$1',
117 'wgCapitalLinks' => true,
121 if ( $title === false ) {
122 // We need a page title that exists
123 $title = Title
::newFromText( 'Special:BlankPage' );
128 Linker
::formatComment( $comment, $title, $local, $wikiId )
132 public function provideCasesForFormatComment() {
133 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
135 // phpcs:disable Generic.Files.LineLength
137 // Linker::formatComment
147 "'''not bolded'''",
151 "try <script>evil</scipt> things",
152 "try <script>evil</scipt> things",
154 // Linker::formatAutocomments
156 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
160 '<a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment"><a href="/wiki/index.php?title=Linkie%3F&action=edit&redlink=1" class="new" title="Linkie? (page does not exist)">linkie?</a></span></span>',
164 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
165 "/* autocomment */ post",
168 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
169 "pre /* autocomment */",
172 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
173 "pre /* autocomment */ post",
176 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> multiple? <a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment2: </span> </span></span>',
177 "/* autocomment */ multiple? /* autocomment2 */ ",
180 '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
181 "/* autocomment containing /* */ T70361"
184 '<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing "quotes"</span></span>',
185 "/* autocomment containing \"quotes\" */"
188 '<a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing <script>tags</script></span></span>',
189 "/* autocomment containing <script>tags</script> */"
192 '<a href="#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
197 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
202 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
207 '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
209 false, false, $wikiId
211 // Linker::formatLinksInComment
213 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
217 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def',
218 "abc [[link|text]] def",
221 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
222 "abc [[Special:BlankPage|]] def",
225 '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',
226 "abc [[%C4%85%C5%9B%C5%BC]] def",
229 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
230 "abc [[#section]] def",
233 'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
234 "abc [[/subpage]] def",
237 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
238 "abc [[\"evil!\"]] def",
241 'abc [[<script>very evil</script>]] def',
242 "abc [[<script>very evil</script>]] def",
249 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
254 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
256 false, false, $wikiId
263 * @covers Linker::formatLinksInComment
264 * @dataProvider provideCasesForFormatLinksInComment
266 public function testFormatLinksInComment( $expected, $input, $wiki ) {
267 $conf = new SiteConfiguration();
270 'enwiki' => '//en.example.org'
276 $conf->suffixes
= [ 'wiki' ];
277 $this->setMwGlobals( [
278 'wgScript' => '/wiki/index.php',
279 'wgArticlePath' => '/wiki/$1',
280 'wgCapitalLinks' => true,
286 Linker
::formatLinksInComment( $input, Title
::newFromText( 'Special:BlankPage' ), false, $wiki )
290 public static function provideCasesForFormatLinksInComment() {
291 // phpcs:disable Generic.Files.LineLength
294 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
295 'foo bar [[Special:BlankPage]]',
299 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
304 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
305 'foo bar [[Special:BlankPage]]',
309 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
310 'foo bar [[Image:Example]]',
317 public static function provideLinkBeginHook() {
318 // phpcs:disable Generic.Files.LineLength
322 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
325 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
329 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
330 $attribs['bar'] = 'baz';
332 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
336 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
337 $query['bar'] = 'baz';
339 '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
341 // Force HTTP $options
343 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
344 $options = [ 'http' ];
346 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
348 // Force 'forcearticlepath' in $options
350 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
351 $options = [ 'forcearticlepath' ];
352 $query['foo'] = 'bar';
354 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
358 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
369 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
370 * @dataProvider provideLinkBeginHook
372 public function testLinkBeginHook( $callback, $expected ) {
373 $this->setMwGlobals( [
374 'wgArticlePath' => '/wiki/$1',
375 'wgServer' => '//example.org',
376 'wgCanonicalServer' => 'http://example.org',
377 'wgScriptPath' => '/w',
378 'wgScript' => '/w/index.php',
381 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
382 $title = SpecialPage
::getTitleFor( 'Blankpage' );
383 $out = Linker
::link( $title );
384 $this->assertEquals( $expected, $out );
387 public static function provideLinkEndHook() {
391 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
394 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
398 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
399 $attribs['bar'] = 'baz';
401 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
403 // Fully override return value and abort hook
405 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
406 $ret = 'blahblahblah';
416 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
417 * @dataProvider provideLinkEndHook
419 public function testLinkEndHook( $callback, $expected ) {
420 $this->setMwGlobals( [
421 'wgArticlePath' => '/wiki/$1',
424 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
426 $title = SpecialPage
::getTitleFor( 'Blankpage' );
427 $out = Linker
::link( $title );
428 $this->assertEquals( $expected, $out );
432 * @covers Linker::getLinkColour
434 public function testGetLinkColour() {
435 $this->hideDeprecated( 'Linker::getLinkColour' );
436 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
437 $foobarTitle = Title
::makeTitle( NS_MAIN
, 'FooBar' );
438 $redirectTitle = Title
::makeTitle( NS_MAIN
, 'Redirect' );
439 $userTitle = Title
::makeTitle( NS_USER
, 'Someuser' );
440 $linkCache->addGoodLinkObj(
446 $linkCache->addGoodLinkObj(
453 $linkCache->addGoodLinkObj(
462 Linker
::getLinkColour( $foobarTitle, 0 )
467 Linker
::getLinkColour( $foobarTitle, 20 )
472 Linker
::getLinkColour( $redirectTitle, 0 )
477 Linker
::getLinkColour( $userTitle, 20 )