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',
18 $this->assertEquals( $expected,
19 Linker
::userLink( $userId, $userName, $altUserName, $msg )
23 public static function provideCasesForUserLink() {
28 # - optional altUserName
32 # ## ANONYMOUS USER ########################################
34 '<a href="/wiki/Special:Contributions/JohnDoe" '
35 . 'title="Special:Contributions/JohnDoe" '
36 . 'class="mw-userlink mw-anonuserlink">JohnDoe</a>',
40 '<a href="/wiki/Special:Contributions/::1" '
41 . 'title="Special:Contributions/::1" '
42 . 'class="mw-userlink mw-anonuserlink">::1</a>',
44 'Anonymous with pretty IPv6'
47 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
48 . 'title="Special:Contributions/0:0:0:0:0:0:0:1" '
49 . 'class="mw-userlink mw-anonuserlink">::1</a>',
50 0, '0:0:0:0:0:0:0:1', false,
51 'Anonymous with almost pretty IPv6'
54 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
55 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
56 . 'class="mw-userlink mw-anonuserlink">::1</a>',
57 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
58 'Anonymous with full IPv6'
61 '<a href="/wiki/Special:Contributions/::1" '
62 . 'title="Special:Contributions/::1" '
63 . 'class="mw-userlink mw-anonuserlink">AlternativeUsername</a>',
64 0, '::1', 'AlternativeUsername',
65 'Anonymous with pretty IPv6 and an alternative username'
70 '<a href="/wiki/Special:Contributions/127.0.0.1" '
71 . 'title="Special:Contributions/127.0.0.1" '
72 . 'class="mw-userlink mw-anonuserlink">127.0.0.1</a>',
73 0, '127.0.0.1', false,
77 '<a href="/wiki/Special:Contributions/127.0.0.1" '
78 . 'title="Special:Contributions/127.0.0.1" '
79 . 'class="mw-userlink mw-anonuserlink">AlternativeUsername</a>',
80 0, '127.0.0.1', 'AlternativeUsername',
81 'Anonymous with IPv4 and an alternative username'
84 # ## Regular user ##########################################
90 * @dataProvider provideCasesForFormatComment
91 * @covers Linker::formatComment
92 * @covers Linker::formatAutocomments
93 * @covers Linker::formatLinksInComment
95 public function testFormatComment(
96 $expected, $comment, $title = false, $local = false, $wikiId = null
98 $conf = new SiteConfiguration();
101 'enwiki' => '//en.example.org',
102 'dewiki' => '//de.example.org',
109 $conf->suffixes
= [ 'wiki' ];
111 $this->setMwGlobals( [
112 'wgScript' => '/wiki/index.php',
113 'wgArticlePath' => '/wiki/$1',
114 'wgCapitalLinks' => true,
118 if ( $title === false ) {
119 // We need a page title that exists
120 $title = Title
::newFromText( 'Special:BlankPage' );
125 Linker
::formatComment( $comment, $title, $local, $wikiId )
129 public function provideCasesForFormatComment() {
130 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
132 // @codingStandardsIgnoreStart Generic.Files.LineLength
134 // Linker::formatComment
144 "'''not bolded'''",
148 "try <script>evil</scipt> things",
149 "try <script>evil</scipt> things",
151 // Linker::formatAutocomments
153 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
157 '<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>',
161 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
162 "/* autocomment */ post",
165 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
166 "pre /* autocomment */",
169 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
170 "pre /* autocomment */ post",
173 '<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>',
174 "/* autocomment */ multiple? /* autocomment2 */ ",
177 '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
178 "/* autocomment containing /* */ T70361"
181 '<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing "quotes"</span></span>',
182 "/* autocomment containing \"quotes\" */"
185 '<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>',
186 "/* autocomment containing <script>tags</script> */"
189 '<a href="#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
194 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
199 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
204 '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
206 false, false, $wikiId
208 // Linker::formatLinksInComment
210 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
214 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def',
215 "abc [[link|text]] def",
218 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
219 "abc [[Special:BlankPage|]] def",
222 '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',
223 "abc [[%C4%85%C5%9B%C5%BC]] def",
226 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
227 "abc [[#section]] def",
230 'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
231 "abc [[/subpage]] def",
234 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
235 "abc [[\"evil!\"]] def",
238 'abc [[<script>very evil</script>]] def',
239 "abc [[<script>very evil</script>]] def",
246 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
251 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
253 false, false, $wikiId
256 // @codingStandardsIgnoreEnd
260 * @covers Linker::formatLinksInComment
261 * @dataProvider provideCasesForFormatLinksInComment
263 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 // @codingStandardsIgnoreStart Generic.Files.LineLength
292 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
293 'foo bar [[Special:BlankPage]]',
297 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
302 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
303 'foo bar [[Special:BlankPage]]',
307 // @codingStandardsIgnoreEnd
310 public static function provideLinkBeginHook() {
311 // @codingStandardsIgnoreStart Generic.Files.LineLength
315 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
318 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
322 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
323 $attribs['bar'] = 'baz';
325 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
329 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
330 $query['bar'] = 'baz';
332 '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
334 // Force HTTP $options
336 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
337 $options = [ 'http' ];
339 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
341 // Force 'forcearticlepath' in $options
343 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
344 $options = [ 'forcearticlepath' ];
345 $query['foo'] = 'bar';
347 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
351 function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
358 // @codingStandardsIgnoreEnd
362 * @dataProvider provideLinkBeginHook
364 public function testLinkBeginHook( $callback, $expected ) {
365 $this->setMwGlobals( [
366 'wgArticlePath' => '/wiki/$1',
367 'wgServer' => '//example.org',
368 'wgCanonicalServer' => 'http://example.org',
369 'wgScriptPath' => '/w',
370 'wgScript' => '/w/index.php',
373 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
374 $title = SpecialPage
::getTitleFor( 'Blankpage' );
375 $out = Linker
::link( $title );
376 $this->assertEquals( $expected, $out );
379 public static function provideLinkEndHook() {
383 function( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
386 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
390 function( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
391 $attribs['bar'] = 'baz';
393 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
395 // Fully override return value and abort hook
397 function( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
398 $ret = 'blahblahblah';
408 * @dataProvider provideLinkEndHook
410 public function testLinkEndHook( $callback, $expected ) {
411 $this->setMwGlobals( [
412 'wgArticlePath' => '/wiki/$1',
415 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
417 $title = SpecialPage
::getTitleFor( 'Blankpage' );
418 $out = Linker
::link( $title );
419 $this->assertEquals( $expected, $out );