7 class LinkerTest
extends MediaWikiLangTestCase
{
10 * @dataProvider provideCasesForUserLink
11 * @covers Linker::userLink
13 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
14 $this->setMwGlobals( [
15 'wgArticlePath' => '/wiki/$1',
20 Linker
::userLink( $userId, $userName, $altUserName ),
25 public static function provideCasesForUserLink() {
30 # - optional altUserName
34 # ## ANONYMOUS USER ########################################
36 '<a href="/wiki/Special:Contributions/JohnDoe" '
37 . 'class="mw-userlink mw-anonuserlink" '
38 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
42 '<a href="/wiki/Special:Contributions/::1" '
43 . 'class="mw-userlink mw-anonuserlink" '
44 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
46 'Anonymous with pretty IPv6'
49 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
50 . 'class="mw-userlink mw-anonuserlink" '
51 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
52 0, '0:0:0:0:0:0:0:1', false,
53 'Anonymous with almost pretty IPv6'
56 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
57 . 'class="mw-userlink mw-anonuserlink" '
58 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
59 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
60 'Anonymous with full IPv6'
63 '<a href="/wiki/Special:Contributions/::1" '
64 . 'class="mw-userlink mw-anonuserlink" '
65 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
66 0, '::1', 'AlternativeUsername',
67 'Anonymous with pretty IPv6 and an alternative username'
72 '<a href="/wiki/Special:Contributions/127.0.0.1" '
73 . 'class="mw-userlink mw-anonuserlink" '
74 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
75 0, '127.0.0.1', false,
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>AlternativeUsername</bdi></a>',
82 0, '127.0.0.1', 'AlternativeUsername',
83 'Anonymous with IPv4 and an alternative username'
86 # ## Regular user ##########################################
92 * @dataProvider provideCasesForFormatComment
93 * @covers Linker::formatComment
94 * @covers Linker::formatAutocomments
95 * @covers Linker::formatLinksInComment
97 public function testFormatComment(
98 $expected, $comment, $title = false, $local = false, $wikiId = null
100 $conf = new SiteConfiguration();
103 'enwiki' => '//en.example.org',
104 'dewiki' => '//de.example.org',
111 $conf->suffixes
= [ 'wiki' ];
113 $this->setMwGlobals( [
114 'wgScript' => '/wiki/index.php',
115 'wgArticlePath' => '/wiki/$1',
116 'wgCapitalLinks' => true,
120 if ( $title === false ) {
121 // We need a page title that exists
122 $title = Title
::newFromText( 'Special:BlankPage' );
127 Linker
::formatComment( $comment, $title, $local, $wikiId )
131 public function provideCasesForFormatComment() {
132 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
134 // phpcs:disable Generic.Files.LineLength
136 // Linker::formatComment
146 "'''not bolded'''",
150 "try <script>evil</scipt> things",
151 "try <script>evil</scipt> things",
153 // Linker::formatAutocomments
155 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a>',
159 '<a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→<a href="/wiki/index.php?title=Linkie%3F&action=edit&redlink=1" class="new" title="Linkie? (page does not exist)">linkie?</a></a>',
163 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a><span dir="auto"><span class="autocomment">: </span> post</span>',
164 "/* autocomment */ post",
167 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a>',
168 "pre /* autocomment */",
171 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a><span dir="auto"><span class="autocomment">: </span> post</span>',
172 "pre /* autocomment */ post",
175 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a><span dir="auto"><span class="autocomment">: </span> multiple? <a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→autocomment2</a></span>',
176 "/* autocomment */ multiple? /* autocomment2 */",
179 '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→autocomment containing /*</a><span dir="auto"><span class="autocomment">: </span> T70361</span>',
180 "/* autocomment containing /* */ T70361"
183 '<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→autocomment containing "quotes"</a>',
184 "/* autocomment containing \"quotes\" */"
187 '<a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→autocomment containing <script>tags</script></a>',
188 "/* autocomment containing <script>tags</script> */"
191 '<a href="#autocomment">→autocomment</a>',
196 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
201 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→autocomment</a>',
206 '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→autocomment</a>',
208 false, false, $wikiId
210 // Linker::formatLinksInComment
212 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
216 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def',
217 "abc [[link|text]] def",
220 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
221 "abc [[Special:BlankPage|]] def",
224 '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',
225 "abc [[%C4%85%C5%9B%C5%BC]] def",
228 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
229 "abc [[#section]] def",
232 'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
233 "abc [[/subpage]] def",
236 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
237 "abc [[\"evil!\"]] def",
240 'abc [[<script>very evil</script>]] def',
241 "abc [[<script>very evil</script>]] def",
248 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
253 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
255 false, false, $wikiId
262 * @covers Linker::formatLinksInComment
263 * @dataProvider provideCasesForFormatLinksInComment
265 public function testFormatLinksInComment( $expected, $input, $wiki ) {
266 $conf = new SiteConfiguration();
269 'enwiki' => '//en.example.org'
275 $conf->suffixes
= [ 'wiki' ];
276 $this->setMwGlobals( [
277 'wgScript' => '/wiki/index.php',
278 'wgArticlePath' => '/wiki/$1',
279 'wgCapitalLinks' => true,
285 Linker
::formatLinksInComment( $input, Title
::newFromText( 'Special:BlankPage' ), false, $wiki )
289 public static function provideCasesForFormatLinksInComment() {
290 // phpcs:disable Generic.Files.LineLength
293 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
294 'foo bar [[Special:BlankPage]]',
298 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
299 '[[ :Special:BlankPage]]',
303 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
308 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
309 'foo bar [[Special:BlankPage]]',
313 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
314 'foo bar [[Image:Example]]',
321 public static function provideLinkBeginHook() {
322 // phpcs:disable Generic.Files.LineLength
326 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
329 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
333 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
334 $attribs['bar'] = 'baz';
336 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
340 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
341 $query['bar'] = 'baz';
343 '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
345 // Force HTTP $options
347 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
348 $options = [ 'http' ];
350 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
352 // Force 'forcearticlepath' in $options
354 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
355 $options = [ 'forcearticlepath' ];
356 $query['foo'] = 'bar';
358 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
362 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
373 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
374 * @dataProvider provideLinkBeginHook
376 public function testLinkBeginHook( $callback, $expected ) {
377 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
378 $this->setMwGlobals( [
379 'wgArticlePath' => '/wiki/$1',
380 'wgServer' => '//example.org',
381 'wgCanonicalServer' => 'http://example.org',
382 'wgScriptPath' => '/w',
383 'wgScript' => '/w/index.php',
386 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
387 $title = SpecialPage
::getTitleFor( 'Blankpage' );
388 $out = Linker
::link( $title );
389 $this->assertEquals( $expected, $out );
392 public static function provideLinkEndHook() {
396 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
399 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
403 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
404 $attribs['bar'] = 'baz';
406 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
408 // Fully override return value and abort hook
410 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
411 $ret = 'blahblahblah';
421 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
422 * @dataProvider provideLinkEndHook
424 public function testLinkEndHook( $callback, $expected ) {
425 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
426 $this->setMwGlobals( [
427 'wgArticlePath' => '/wiki/$1',
430 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
432 $title = SpecialPage
::getTitleFor( 'Blankpage' );
433 $out = Linker
::link( $title );
434 $this->assertEquals( $expected, $out );