3 use MediaWiki\MediaWikiServices
;
9 class LinkerTest
extends MediaWikiLangTestCase
{
12 * @dataProvider provideCasesForUserLink
13 * @covers Linker::userLink
15 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
16 $this->setMwGlobals( [
17 'wgArticlePath' => '/wiki/$1',
22 Linker
::userLink( $userId, $userName, $altUserName ),
27 public static function provideCasesForUserLink() {
32 # - optional altUserName
36 # ## ANONYMOUS USER ########################################
38 '<a href="/wiki/Special:Contributions/JohnDoe" '
39 . 'class="mw-userlink mw-anonuserlink" '
40 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
44 '<a href="/wiki/Special:Contributions/::1" '
45 . 'class="mw-userlink mw-anonuserlink" '
46 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
48 'Anonymous with pretty IPv6'
51 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
52 . 'class="mw-userlink mw-anonuserlink" '
53 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
54 0, '0:0:0:0:0:0:0:1', false,
55 'Anonymous with almost pretty IPv6'
58 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
59 . 'class="mw-userlink mw-anonuserlink" '
60 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
61 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
62 'Anonymous with full IPv6'
65 '<a href="/wiki/Special:Contributions/::1" '
66 . 'class="mw-userlink mw-anonuserlink" '
67 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
68 0, '::1', 'AlternativeUsername',
69 'Anonymous with pretty IPv6 and an alternative username'
74 '<a href="/wiki/Special:Contributions/127.0.0.1" '
75 . 'class="mw-userlink mw-anonuserlink" '
76 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
77 0, '127.0.0.1', false,
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>AlternativeUsername</bdi></a>',
84 0, '127.0.0.1', 'AlternativeUsername',
85 'Anonymous with IPv4 and an alternative username'
88 # ## Regular user ##########################################
94 * @dataProvider provideCasesForFormatComment
95 * @covers Linker::formatComment
96 * @covers Linker::formatAutocomments
97 * @covers Linker::formatLinksInComment
99 public function testFormatComment(
100 $expected, $comment, $title = false, $local = false, $wikiId = null
102 $conf = new SiteConfiguration();
105 'enwiki' => '//en.example.org',
106 'dewiki' => '//de.example.org',
113 $conf->suffixes
= [ 'wiki' ];
115 $this->setMwGlobals( [
116 'wgScript' => '/wiki/index.php',
117 'wgArticlePath' => '/wiki/$1',
118 'wgCapitalLinks' => true,
122 if ( $title === false ) {
123 // We need a page title that exists
124 $title = Title
::newFromText( 'Special:BlankPage' );
129 Linker
::formatComment( $comment, $title, $local, $wikiId )
133 public function provideCasesForFormatComment() {
134 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
136 // @codingStandardsIgnoreStart Generic.Files.LineLength
138 // Linker::formatComment
148 "'''not bolded'''",
152 "try <script>evil</scipt> things",
153 "try <script>evil</scipt> things",
155 // Linker::formatAutocomments
157 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
161 '<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>',
165 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
166 "/* autocomment */ post",
169 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
170 "pre /* autocomment */",
173 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
174 "pre /* autocomment */ post",
177 '<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>',
178 "/* autocomment */ multiple? /* autocomment2 */ ",
181 '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
182 "/* autocomment containing /* */ T70361"
185 '<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing "quotes"</span></span>',
186 "/* autocomment containing \"quotes\" */"
189 '<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>',
190 "/* autocomment containing <script>tags</script> */"
193 '<a href="#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
198 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
203 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
208 '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
210 false, false, $wikiId
212 // Linker::formatLinksInComment
214 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
218 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def',
219 "abc [[link|text]] def",
222 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
223 "abc [[Special:BlankPage|]] def",
226 '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',
227 "abc [[%C4%85%C5%9B%C5%BC]] def",
230 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
231 "abc [[#section]] def",
234 'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
235 "abc [[/subpage]] def",
238 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
239 "abc [[\"evil!\"]] def",
242 'abc [[<script>very evil</script>]] def',
243 "abc [[<script>very evil</script>]] def",
250 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
255 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
257 false, false, $wikiId
260 // @codingStandardsIgnoreEnd
264 * @covers Linker::formatLinksInComment
265 * @dataProvider provideCasesForFormatLinksInComment
267 public function testFormatLinksInComment( $expected, $input, $wiki ) {
268 $conf = new SiteConfiguration();
271 'enwiki' => '//en.example.org'
277 $conf->suffixes
= [ 'wiki' ];
278 $this->setMwGlobals( [
279 'wgScript' => '/wiki/index.php',
280 'wgArticlePath' => '/wiki/$1',
281 'wgCapitalLinks' => true,
287 Linker
::formatLinksInComment( $input, Title
::newFromText( 'Special:BlankPage' ), false, $wiki )
291 public static function provideCasesForFormatLinksInComment() {
292 // @codingStandardsIgnoreStart Generic.Files.LineLength
295 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
296 'foo bar [[Special:BlankPage]]',
300 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
305 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
306 'foo bar [[Special:BlankPage]]',
310 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
311 'foo bar [[Image:Example]]',
315 // @codingStandardsIgnoreEnd
318 public static function provideLinkBeginHook() {
319 // @codingStandardsIgnoreStart Generic.Files.LineLength
323 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
326 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
330 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
331 $attribs['bar'] = 'baz';
333 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
337 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
338 $query['bar'] = 'baz';
340 '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
342 // Force HTTP $options
344 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
345 $options = [ 'http' ];
347 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
349 // Force 'forcearticlepath' in $options
351 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
352 $options = [ 'forcearticlepath' ];
353 $query['foo'] = 'bar';
355 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
359 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
366 // @codingStandardsIgnoreEnd
370 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
371 * @dataProvider provideLinkBeginHook
373 public function testLinkBeginHook( $callback, $expected ) {
374 $this->setMwGlobals( [
375 'wgArticlePath' => '/wiki/$1',
376 'wgServer' => '//example.org',
377 'wgCanonicalServer' => 'http://example.org',
378 'wgScriptPath' => '/w',
379 'wgScript' => '/w/index.php',
382 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
383 $title = SpecialPage
::getTitleFor( 'Blankpage' );
384 $out = Linker
::link( $title );
385 $this->assertEquals( $expected, $out );
388 public static function provideLinkEndHook() {
392 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
395 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
399 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
400 $attribs['bar'] = 'baz';
402 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
404 // Fully override return value and abort hook
406 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
407 $ret = 'blahblahblah';
417 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
418 * @dataProvider provideLinkEndHook
420 public function testLinkEndHook( $callback, $expected ) {
421 $this->setMwGlobals( [
422 'wgArticlePath' => '/wiki/$1',
425 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
427 $title = SpecialPage
::getTitleFor( 'Blankpage' );
428 $out = Linker
::link( $title );
429 $this->assertEquals( $expected, $out );
433 * @covers Linker::getLinkColour
435 public function testGetLinkColour() {
436 $this->hideDeprecated( 'Linker::getLinkColour' );
437 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
438 $foobarTitle = Title
::makeTitle( NS_MAIN
, 'FooBar' );
439 $redirectTitle = Title
::makeTitle( NS_MAIN
, 'Redirect' );
440 $userTitle = Title
::makeTitle( NS_USER
, 'Someuser' );
441 $linkCache->addGoodLinkObj(
447 $linkCache->addGoodLinkObj(
454 $linkCache->addGoodLinkObj(
463 Linker
::getLinkColour( $foobarTitle, 0 )
468 Linker
::getLinkColour( $foobarTitle, 20 )
473 Linker
::getLinkColour( $redirectTitle, 0 )
478 Linker
::getLinkColour( $userTitle, 20 )