MediaWiki REL1_31
LinkerTest.php
Go to the documentation of this file.
1<?php
2
4
9
14 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
15 $this->setMwGlobals( [
16 'wgArticlePath' => '/wiki/$1',
17 ] );
18
19 $this->assertEquals(
20 $expected,
21 Linker::userLink( $userId, $userName, $altUserName ),
22 $msg
23 );
24 }
25
26 public static function provideCasesForUserLink() {
27 # Format:
28 # - expected
29 # - userid
30 # - username
31 # - optional altUserName
32 # - optional message
33 return [
34
35 # ## ANONYMOUS USER ########################################
36 [
37 '<a href="/wiki/Special:Contributions/JohnDoe" '
38 . 'class="mw-userlink mw-anonuserlink" '
39 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
40 0, 'JohnDoe', false,
41 ],
42 [
43 '<a href="/wiki/Special:Contributions/::1" '
44 . 'class="mw-userlink mw-anonuserlink" '
45 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
46 0, '::1', false,
47 'Anonymous with pretty IPv6'
48 ],
49 [
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'
55 ],
56 [
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'
62 ],
63 [
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'
69 ],
70
71 # IPV4
72 [
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,
77 'Anonymous with IPv4'
78 ],
79 [
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'
85 ],
86
87 # ## Regular user ##########################################
88 # TODO!
89 ];
90 }
91
98 public function testFormatComment(
99 $expected, $comment, $title = false, $local = false, $wikiId = null
100 ) {
101 $conf = new SiteConfiguration();
102 $conf->settings = [
103 'wgServer' => [
104 'enwiki' => '//en.example.org',
105 'dewiki' => '//de.example.org',
106 ],
107 'wgArticlePath' => [
108 'enwiki' => '/w/$1',
109 'dewiki' => '/w/$1',
110 ],
111 ];
112 $conf->suffixes = [ 'wiki' ];
113
114 $this->setMwGlobals( [
115 'wgScript' => '/wiki/index.php',
116 'wgArticlePath' => '/wiki/$1',
117 'wgCapitalLinks' => true,
118 'wgConf' => $conf,
119 ] );
120
121 if ( $title === false ) {
122 // We need a page title that exists
123 $title = Title::newFromText( 'Special:BlankPage' );
124 }
125
126 $this->assertEquals(
127 $expected,
128 Linker::formatComment( $comment, $title, $local, $wikiId )
129 );
130 }
131
133 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
134
135 // phpcs:disable Generic.Files.LineLength
136 return [
137 // Linker::formatComment
138 [
139 'a&lt;script&gt;b',
140 'a<script>b',
141 ],
142 [
143 'a—b',
144 'a&mdash;b',
145 ],
146 [
147 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
148 "'''not bolded'''",
149 ],
150 [
151 "try &lt;script&gt;evil&lt;/scipt&gt; things",
152 "try <script>evil</scipt> things",
153 ],
154 // Linker::formatAutocomments
155 [
156 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
157 "/* autocomment */",
158 ],
159 [
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&amp;action=edit&amp;redlink=1" class="new" title="Linkie? (page does not exist)">linkie?</a></span></span>',
161 "/* [[linkie?]] */",
162 ],
163 [
164 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
165 "/* autocomment */ post",
166 ],
167 [
168 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
169 "pre /* autocomment */",
170 ],
171 [
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",
174 ],
175 [
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 */ ",
178 ],
179 [
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"
182 ],
183 [
184 '<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing &quot;quotes&quot;</span></span>',
185 "/* autocomment containing \"quotes\" */"
186 ],
187 [
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 &lt;script&gt;tags&lt;/script&gt;</span></span>',
189 "/* autocomment containing <script>tags</script> */"
190 ],
191 [
192 '<a href="#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
193 "/* autocomment */",
194 false, true
195 ],
196 [
197 '‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
198 "/* autocomment */",
199 null
200 ],
201 [
202 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
203 "/* autocomment */",
204 false, false
205 ],
206 [
207 '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
208 "/* autocomment */",
209 false, false, $wikiId
210 ],
211 // Linker::formatLinksInComment
212 [
213 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
214 "abc [[link]] def",
215 ],
216 [
217 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">text</a> def',
218 "abc [[link|text]] def",
219 ],
220 [
221 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
222 "abc [[Special:BlankPage|]] def",
223 ],
224 [
225 'abc <a href="/wiki/index.php?title=%C4%84%C5%9B%C5%BC&amp;action=edit&amp;redlink=1" class="new" title="Ąśż (page does not exist)">ąśż</a> def',
226 "abc [[%C4%85%C5%9B%C5%BC]] def",
227 ],
228 [
229 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
230 "abc [[#section]] def",
231 ],
232 [
233 'abc <a href="/wiki/index.php?title=/subpage&amp;action=edit&amp;redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
234 "abc [[/subpage]] def",
235 ],
236 [
237 'abc <a href="/wiki/index.php?title=%22evil!%22&amp;action=edit&amp;redlink=1" class="new" title="&quot;evil!&quot; (page does not exist)">&quot;evil!&quot;</a> def',
238 "abc [[\"evil!\"]] def",
239 ],
240 [
241 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
242 "abc [[<script>very evil</script>]] def",
243 ],
244 [
245 'abc [[|]] def',
246 "abc [[|]] def",
247 ],
248 [
249 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
250 "abc [[link]] def",
251 false, false
252 ],
253 [
254 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
255 "abc [[link]] def",
256 false, false, $wikiId
257 ],
258 ];
259 // phpcs:enable
260 }
261
266 public function testFormatLinksInComment( $expected, $input, $wiki ) {
267 $conf = new SiteConfiguration();
268 $conf->settings = [
269 'wgServer' => [
270 'enwiki' => '//en.example.org'
271 ],
272 'wgArticlePath' => [
273 'enwiki' => '/w/$1',
274 ],
275 ];
276 $conf->suffixes = [ 'wiki' ];
277 $this->setMwGlobals( [
278 'wgScript' => '/wiki/index.php',
279 'wgArticlePath' => '/wiki/$1',
280 'wgCapitalLinks' => true,
281 'wgConf' => $conf,
282 ] );
283
284 $this->assertEquals(
285 $expected,
286 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
287 );
288 }
289
290 public static function provideCasesForFormatLinksInComment() {
291 // phpcs:disable Generic.Files.LineLength
292 return [
293 [
294 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
295 'foo bar [[Special:BlankPage]]',
296 null,
297 ],
298 [
299 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
300 "[[Foo'bar]]",
301 'enwiki',
302 ],
303 [
304 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
305 'foo bar [[Special:BlankPage]]',
306 'enwiki',
307 ],
308 [
309 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
310 'foo bar [[Image:Example]]',
311 'enwiki',
312 ],
313 ];
314 // phpcs:enable
315 }
316
317 public static function provideLinkBeginHook() {
318 // phpcs:disable Generic.Files.LineLength
319 return [
320 // Modify $html
321 [
322 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
323 $html = 'foobar';
324 },
325 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
326 ],
327 // Modify $attribs
328 [
329 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
330 $attribs['bar'] = 'baz';
331 },
332 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
333 ],
334 // Modify $query
335 [
336 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
337 $query['bar'] = 'baz';
338 },
339 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
340 ],
341 // Force HTTP $options
342 [
343 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
344 $options = [ 'http' ];
345 },
346 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
347 ],
348 // Force 'forcearticlepath' in $options
349 [
350 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
351 $options = [ 'forcearticlepath' ];
352 $query['foo'] = 'bar';
353 },
354 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
355 ],
356 // Abort early
357 [
358 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
359 $ret = 'foobar';
360 return false;
361 },
362 'foobar'
363 ],
364 ];
365 // phpcs:enable
366 }
367
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',
379 ] );
380
381 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
382 $title = SpecialPage::getTitleFor( 'Blankpage' );
383 $out = Linker::link( $title );
384 $this->assertEquals( $expected, $out );
385 }
386
387 public static function provideLinkEndHook() {
388 return [
389 // Override $html
390 [
391 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
392 $html = 'foobar';
393 },
394 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
395 ],
396 // Modify $attribs
397 [
398 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
399 $attribs['bar'] = 'baz';
400 },
401 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
402 ],
403 // Fully override return value and abort hook
404 [
405 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
406 $ret = 'blahblahblah';
407 return false;
408 },
409 'blahblahblah'
410 ],
411
412 ];
413 }
414
419 public function testLinkEndHook( $callback, $expected ) {
420 $this->setMwGlobals( [
421 'wgArticlePath' => '/wiki/$1',
422 ] );
423
424 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
425
426 $title = SpecialPage::getTitleFor( 'Blankpage' );
427 $out = Linker::link( $title );
428 $this->assertEquals( $expected, $out );
429 }
430
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(
441 1, // id
442 $foobarTitle,
443 10, // len
444 0 // redir
445 );
446 $linkCache->addGoodLinkObj(
447 2, // id
448 $redirectTitle,
449 10, // len
450 1 // redir
451 );
452
453 $linkCache->addGoodLinkObj(
454 3, // id
455 $userTitle,
456 10, // len
457 0 // redir
458 );
459
460 $this->assertEquals(
461 '',
462 Linker::getLinkColour( $foobarTitle, 0 )
463 );
464
465 $this->assertEquals(
466 'stub',
467 Linker::getLinkColour( $foobarTitle, 20 )
468 );
469
470 $this->assertEquals(
471 'mw-redirect',
472 Linker::getLinkColour( $redirectTitle, 0 )
473 );
474
475 $this->assertEquals(
476 '',
477 Linker::getLinkColour( $userTitle, 20 )
478 );
479 }
480}
Database.
Definition LinkerTest.php:8
testUserLink( $expected, $userId, $userName, $altUserName=false, $msg='')
provideCasesForUserLink Linker::userLink
testLinkEndHook( $callback, $expected)
MediaWiki\Linker\LinkRenderer::buildAElement provideLinkEndHook.
testGetLinkColour()
Linker::getLinkColour.
provideCasesForFormatComment()
testLinkBeginHook( $callback, $expected)
MediaWiki\Linker\LinkRenderer::runLegacyBeginHook provideLinkBeginHook.
static provideCasesForFormatLinksInComment()
testFormatComment( $expected, $comment, $title=false, $local=false, $wikiId=null)
provideCasesForFormatComment Linker::formatComment Linker::formatAutocomments Linker::formatLinksInCo...
testFormatLinksInComment( $expected, $input, $wiki)
Linker::formatLinksInComment provideCasesForFormatLinksInComment.
static provideLinkEndHook()
static provideLinkBeginHook()
static provideCasesForUserLink()
static link( $target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition Linker.php:107
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:893
static getLinkColour(LinkTarget $t, $threshold)
Return the CSS colour of a known link.
Definition Linker.php:51
static formatLinksInComment( $comment, $title=null, $local=false, $wikiId=null)
Formats wiki links and media links in text; all other wiki formatting is ignored.
Definition Linker.php:1234
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition Linker.php:1109
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
MediaWikiServices is the service locator for the application scope of MediaWiki.
This is a class for holding configuration settings, particularly for multi-wiki sites.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2005
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2013
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2014
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1620
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
const NS_MAIN
Definition Defines.php:74
if(is_array($mode)) switch( $mode) $input