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',
19 Linker
::userLink( $userId, $userName, $altUserName ),
24 public static function provideCasesForUserLink() {
29 # - optional altUserName
33 # ## ANONYMOUS USER ########################################
35 '<a href="/wiki/Special:Contributions/JohnDoe" '
36 . 'class="mw-userlink mw-anonuserlink" '
37 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
41 '<a href="/wiki/Special:Contributions/::1" '
42 . 'class="mw-userlink mw-anonuserlink" '
43 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
45 'Anonymous with pretty IPv6'
48 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
49 . 'class="mw-userlink mw-anonuserlink" '
50 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
51 0, '0:0:0:0:0:0:0:1', false,
52 'Anonymous with almost pretty IPv6'
55 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
56 . 'class="mw-userlink mw-anonuserlink" '
57 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
58 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
59 'Anonymous with full IPv6'
62 '<a href="/wiki/Special:Contributions/::1" '
63 . 'class="mw-userlink mw-anonuserlink" '
64 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
65 0, '::1', 'AlternativeUsername',
66 'Anonymous with pretty IPv6 and an alternative username'
71 '<a href="/wiki/Special:Contributions/127.0.0.1" '
72 . 'class="mw-userlink mw-anonuserlink" '
73 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
74 0, '127.0.0.1', false,
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>AlternativeUsername</bdi></a>',
81 0, '127.0.0.1', 'AlternativeUsername',
82 'Anonymous with IPv4 and an alternative username'
85 # ## Regular user ##########################################
91 * @dataProvider provideCasesForFormatComment
92 * @covers Linker::formatComment
93 * @covers Linker::formatAutocomments
94 * @covers Linker::formatLinksInComment
96 public function testFormatComment(
97 $expected, $comment, $title = false, $local = false, $wikiId = null
99 $conf = new SiteConfiguration();
102 'enwiki' => '//en.example.org',
103 'dewiki' => '//de.example.org',
110 $conf->suffixes
= [ 'wiki' ];
112 $this->setMwGlobals( [
113 'wgScript' => '/wiki/index.php',
114 'wgArticlePath' => '/wiki/$1',
115 'wgCapitalLinks' => true,
119 if ( $title === false ) {
120 // We need a page title that exists
121 $title = Title
::newFromText( 'Special:BlankPage' );
126 Linker
::formatComment( $comment, $title, $local, $wikiId )
130 public function provideCasesForFormatComment() {
131 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
133 // phpcs:disable Generic.Files.LineLength
135 // Linker::formatComment
145 "'''not bolded'''",
149 "try <script>evil</scipt> things",
150 "try <script>evil</scipt> things",
152 // Linker::formatAutocomments
154 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a></span></span>',
158 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→[[linkie?]]</a></span></span>',
162 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a>: </span> post</span>',
163 "/* autocomment */ post",
166 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a></span></span>',
167 "pre /* autocomment */",
170 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a>: </span> post</span>',
171 "pre /* autocomment */ post",
174 '<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>',
175 "/* autocomment */ multiple? /* autocomment2 */",
178 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→autocomment containing /*</a>: </span> T70361</span>',
179 "/* autocomment containing /* */ T70361"
182 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→autocomment containing "quotes"</a></span></span>',
183 "/* autocomment containing \"quotes\" */"
186 '<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>',
187 "/* autocomment containing <script>tags</script> */"
190 '<span dir="auto"><span class="autocomment"><a href="#autocomment">→autocomment</a></span></span>',
195 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
200 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a></span></span>',
205 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→autocomment</a></span></span>',
207 false, false, $wikiId
209 // Linker::formatLinksInComment
211 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
215 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def',
216 "abc [[link|text]] def",
219 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
220 "abc [[Special:BlankPage|]] def",
223 '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',
224 "abc [[%C4%85%C5%9B%C5%BC]] def",
227 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
228 "abc [[#section]] def",
231 'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
232 "abc [[/subpage]] def",
235 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
236 "abc [[\"evil!\"]] def",
239 'abc [[<script>very evil</script>]] def',
240 "abc [[<script>very evil</script>]] def",
247 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
252 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
254 false, false, $wikiId
261 * @covers Linker::formatLinksInComment
262 * @dataProvider provideCasesForFormatLinksInComment
264 public function testFormatLinksInComment( $expected, $input, $wiki ) {
265 $conf = new SiteConfiguration();
268 'enwiki' => '//en.example.org'
274 $conf->suffixes
= [ 'wiki' ];
275 $this->setMwGlobals( [
276 'wgScript' => '/wiki/index.php',
277 'wgArticlePath' => '/wiki/$1',
278 'wgCapitalLinks' => true,
284 Linker
::formatLinksInComment( $input, Title
::newFromText( 'Special:BlankPage' ), false, $wiki )
288 public static function provideCasesForFormatLinksInComment() {
289 // phpcs:disable Generic.Files.LineLength
292 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
293 'foo bar [[Special:BlankPage]]',
297 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
298 '[[ :Special:BlankPage]]',
302 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
307 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
308 'foo bar [[Special:BlankPage]]',
312 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
313 'foo bar [[Image:Example]]',
320 public static function provideLinkBeginHook() {
321 // phpcs:disable Generic.Files.LineLength
325 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
328 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
332 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
333 $attribs['bar'] = 'baz';
335 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
339 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
340 $query['bar'] = 'baz';
342 '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
344 // Force HTTP $options
346 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
347 $options = [ 'http' ];
349 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
351 // Force 'forcearticlepath' in $options
353 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
354 $options = [ 'forcearticlepath' ];
355 $query['foo'] = 'bar';
357 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
361 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
372 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
373 * @dataProvider provideLinkBeginHook
375 public function testLinkBeginHook( $callback, $expected ) {
376 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
377 $this->setMwGlobals( [
378 'wgArticlePath' => '/wiki/$1',
379 'wgServer' => '//example.org',
380 'wgCanonicalServer' => 'http://example.org',
381 'wgScriptPath' => '/w',
382 'wgScript' => '/w/index.php',
385 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
386 $title = SpecialPage
::getTitleFor( 'Blankpage' );
387 $out = Linker
::link( $title );
388 $this->assertEquals( $expected, $out );
391 public static function provideLinkEndHook() {
395 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
398 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
402 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
403 $attribs['bar'] = 'baz';
405 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
407 // Fully override return value and abort hook
409 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
410 $ret = 'blahblahblah';
420 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
421 * @dataProvider provideLinkEndHook
423 public function testLinkEndHook( $callback, $expected ) {
424 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
425 $this->setMwGlobals( [
426 'wgArticlePath' => '/wiki/$1',
429 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
431 $title = SpecialPage
::getTitleFor( 'Blankpage' );
432 $out = Linker
::link( $title );
433 $this->assertEquals( $expected, $out );