MediaWiki  1.28.3
LinkerTest.php
Go to the documentation of this file.
1 <?php
2 
4 
10 
15  public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
16  $this->setMwGlobals( [
17  'wgArticlePath' => '/wiki/$1',
18  ] );
19 
20  $this->assertEquals(
21  $expected,
22  Linker::userLink( $userId, $userName, $altUserName ),
23  $msg
24  );
25  }
26 
27  public static function provideCasesForUserLink() {
28  # Format:
29  # - expected
30  # - userid
31  # - username
32  # - optional altUserName
33  # - optional message
34  return [
35 
36  # ## ANONYMOUS USER ########################################
37  [
38  '<a href="/wiki/Special:Contributions/JohnDoe" '
39  . 'class="mw-userlink mw-anonuserlink" '
40  . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
41  0, 'JohnDoe', false,
42  ],
43  [
44  '<a href="/wiki/Special:Contributions/::1" '
45  . 'class="mw-userlink mw-anonuserlink" '
46  . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
47  0, '::1', false,
48  'Anonymous with pretty IPv6'
49  ],
50  [
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'
56  ],
57  [
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'
63  ],
64  [
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'
70  ],
71 
72  # IPV4
73  [
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,
78  'Anonymous with IPv4'
79  ],
80  [
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'
86  ],
87 
88  # ## Regular user ##########################################
89  # TODO!
90  ];
91  }
92 
99  public function testFormatComment(
100  $expected, $comment, $title = false, $local = false, $wikiId = null
101  ) {
102  $conf = new SiteConfiguration();
103  $conf->settings = [
104  'wgServer' => [
105  'enwiki' => '//en.example.org',
106  'dewiki' => '//de.example.org',
107  ],
108  'wgArticlePath' => [
109  'enwiki' => '/w/$1',
110  'dewiki' => '/w/$1',
111  ],
112  ];
113  $conf->suffixes = [ 'wiki' ];
114 
115  $this->setMwGlobals( [
116  'wgScript' => '/wiki/index.php',
117  'wgArticlePath' => '/wiki/$1',
118  'wgCapitalLinks' => true,
119  'wgConf' => $conf,
120  ] );
121 
122  if ( $title === false ) {
123  // We need a page title that exists
124  $title = Title::newFromText( 'Special:BlankPage' );
125  }
126 
127  $this->assertEquals(
128  $expected,
129  Linker::formatComment( $comment, $title, $local, $wikiId )
130  );
131  }
132 
133  public function provideCasesForFormatComment() {
134  $wikiId = 'enwiki'; // $wgConf has a fake entry for this
135 
136  // @codingStandardsIgnoreStart Generic.Files.LineLength
137  return [
138  // Linker::formatComment
139  [
140  'a&lt;script&gt;b',
141  'a<script>b',
142  ],
143  [
144  'a—b',
145  'a&mdash;b',
146  ],
147  [
148  "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
149  "'''not bolded'''",
150  ],
151  [
152  "try &lt;script&gt;evil&lt;/scipt&gt; things",
153  "try <script>evil</scipt> things",
154  ],
155  // Linker::formatAutocomments
156  [
157  '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
158  "/* autocomment */",
159  ],
160  [
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&amp;action=edit&amp;redlink=1" class="new" title="Linkie? (page does not exist)">linkie?</a></span></span>',
162  "/* [[linkie?]] */",
163  ],
164  [
165  '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
166  "/* autocomment */ post",
167  ],
168  [
169  'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
170  "pre /* autocomment */",
171  ],
172  [
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",
175  ],
176  [
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 */ ",
179  ],
180  [
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"
183  ],
184  [
185  '<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>',
186  "/* autocomment containing \"quotes\" */"
187  ],
188  [
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 &lt;script&gt;tags&lt;/script&gt;</span></span>',
190  "/* autocomment containing <script>tags</script> */"
191  ],
192  [
193  '<a href="#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
194  "/* autocomment */",
195  false, true
196  ],
197  [
198  '‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
199  "/* autocomment */",
200  null
201  ],
202  [
203  '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
204  "/* autocomment */",
205  false, false
206  ],
207  [
208  '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
209  "/* autocomment */",
210  false, false, $wikiId
211  ],
212  // Linker::formatLinksInComment
213  [
214  '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',
215  "abc [[link]] def",
216  ],
217  [
218  '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',
219  "abc [[link|text]] def",
220  ],
221  [
222  'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
223  "abc [[Special:BlankPage|]] def",
224  ],
225  [
226  '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',
227  "abc [[%C4%85%C5%9B%C5%BC]] def",
228  ],
229  [
230  'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
231  "abc [[#section]] def",
232  ],
233  [
234  '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',
235  "abc [[/subpage]] def",
236  ],
237  [
238  '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',
239  "abc [[\"evil!\"]] def",
240  ],
241  [
242  'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
243  "abc [[<script>very evil</script>]] def",
244  ],
245  [
246  'abc [[|]] def',
247  "abc [[|]] def",
248  ],
249  [
250  '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',
251  "abc [[link]] def",
252  false, false
253  ],
254  [
255  'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
256  "abc [[link]] def",
257  false, false, $wikiId
258  ],
259  ];
260  // @codingStandardsIgnoreEnd
261  }
262 
267  public function testFormatLinksInComment( $expected, $input, $wiki ) {
268 
269  $conf = new SiteConfiguration();
270  $conf->settings = [
271  'wgServer' => [
272  'enwiki' => '//en.example.org'
273  ],
274  'wgArticlePath' => [
275  'enwiki' => '/w/$1',
276  ],
277  ];
278  $conf->suffixes = [ 'wiki' ];
279  $this->setMwGlobals( [
280  'wgScript' => '/wiki/index.php',
281  'wgArticlePath' => '/wiki/$1',
282  'wgCapitalLinks' => true,
283  'wgConf' => $conf,
284  ] );
285 
286  $this->assertEquals(
287  $expected,
288  Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
289  );
290  }
291 
292  public static function provideCasesForFormatLinksInComment() {
293  // @codingStandardsIgnoreStart Generic.Files.LineLength
294  return [
295  [
296  'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
297  'foo bar [[Special:BlankPage]]',
298  null,
299  ],
300  [
301  '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
302  "[[Foo'bar]]",
303  'enwiki',
304  ],
305  [
306  'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
307  'foo bar [[Special:BlankPage]]',
308  'enwiki',
309  ],
310  ];
311  // @codingStandardsIgnoreEnd
312  }
313 
314  public static function provideLinkBeginHook() {
315  // @codingStandardsIgnoreStart Generic.Files.LineLength
316  return [
317  // Modify $html
318  [
319  function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
320  $html = 'foobar';
321  },
322  '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
323  ],
324  // Modify $attribs
325  [
326  function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
327  $attribs['bar'] = 'baz';
328  },
329  '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
330  ],
331  // Modify $query
332  [
333  function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
334  $query['bar'] = 'baz';
335  },
336  '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
337  ],
338  // Force HTTP $options
339  [
340  function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
341  $options = [ 'http' ];
342  },
343  '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
344  ],
345  // Force 'forcearticlepath' in $options
346  [
347  function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
348  $options = [ 'forcearticlepath' ];
349  $query['foo'] = 'bar';
350  },
351  '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
352  ],
353  // Abort early
354  [
355  function( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
356  $ret = 'foobar';
357  return false;
358  },
359  'foobar'
360  ],
361  ];
362  // @codingStandardsIgnoreEnd
363  }
364 
369  public function testLinkBeginHook( $callback, $expected ) {
370  $this->setMwGlobals( [
371  'wgArticlePath' => '/wiki/$1',
372  'wgServer' => '//example.org',
373  'wgCanonicalServer' => 'http://example.org',
374  'wgScriptPath' => '/w',
375  'wgScript' => '/w/index.php',
376  ] );
377 
378  $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
379  $title = SpecialPage::getTitleFor( 'Blankpage' );
380  $out = Linker::link( $title );
381  $this->assertEquals( $expected, $out );
382  }
383 
384  public static function provideLinkEndHook() {
385  return [
386  // Override $html
387  [
388  function( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
389  $html = 'foobar';
390  },
391  '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
392  ],
393  // Modify $attribs
394  [
395  function( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
396  $attribs['bar'] = 'baz';
397  },
398  '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
399  ],
400  // Fully override return value and abort hook
401  [
402  function( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
403  $ret = 'blahblahblah';
404  return false;
405  },
406  'blahblahblah'
407  ],
408 
409  ];
410  }
411 
416  public function testLinkEndHook( $callback, $expected ) {
417  $this->setMwGlobals( [
418  'wgArticlePath' => '/wiki/$1',
419  ] );
420 
421  $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
422 
423  $title = SpecialPage::getTitleFor( 'Blankpage' );
424  $out = Linker::link( $title );
425  $this->assertEquals( $expected, $out );
426  }
427 
431  public function testGetLinkColour() {
432  $this->hideDeprecated( 'Linker::getLinkColour' );
433  $linkCache = MediaWikiServices::getInstance()->getLinkCache();
434  $foobarTitle = Title::makeTitle( NS_MAIN, 'FooBar' );
435  $redirectTitle = Title::makeTitle( NS_MAIN, 'Redirect' );
436  $userTitle = Title::makeTitle( NS_USER, 'Someuser' );
437  $linkCache->addGoodLinkObj(
438  1, // id
439  $foobarTitle,
440  10, // len
441  0 // redir
442  );
443  $linkCache->addGoodLinkObj(
444  2, // id
445  $redirectTitle,
446  10, // len
447  1 // redir
448  );
449 
450  $linkCache->addGoodLinkObj(
451  3, // id
452  $userTitle,
453  10, // len
454  0 // redir
455  );
456 
457  $this->assertEquals(
458  '',
459  Linker::getLinkColour( $foobarTitle, 0 )
460  );
461 
462  $this->assertEquals(
463  'stub',
464  Linker::getLinkColour( $foobarTitle, 20 )
465  );
466 
467  $this->assertEquals(
468  'mw-redirect',
469  Linker::getLinkColour( $redirectTitle, 0 )
470  );
471 
472  $this->assertEquals(
473  '',
474  Linker::getLinkColour( $userTitle, 20 )
475  );
476  }
477 }
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:1940
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:806
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:1559
testFormatLinksInComment($expected, $input, $wiki)
Linker::formatLinksInComment provideCasesForFormatLinksInComment.
Definition: LinkerTest.php:267
const NS_MAIN
Definition: Defines.php:56
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
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:1940
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static getTitleFor($name, $subpage=false, $fragment= '')
Get a localised Title object for a specified special page name If you don't need a full Title object...
Definition: SpecialPage.php:82
$comment
static getLinkColour(LinkTarget $t, $threshold)
Return the CSS colour of a known link.
Definition: Linker.php:147
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
testUserLink($expected, $userId, $userName, $altUserName=false, $msg= '')
provideCasesForUserLink Linker::userLink
Definition: LinkerTest.php:15
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
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:1940
testFormatComment($expected, $comment, $title=false, $local=false, $wikiId=null)
provideCasesForFormatComment Linker::formatComment Linker::formatAutocomments Linker::formatLinksInCo...
Definition: LinkerTest.php:99
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:1305
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1050
static provideCasesForFormatLinksInComment()
Definition: LinkerTest.php:292
Base class that store and restore the Language objects.
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:1940
static provideCasesForUserLink()
Definition: LinkerTest.php:27
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:957
hideDeprecated($function)
Don't throw a warning if $function is deprecated and called later.
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:1180
static link($target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition: Linker.php:203
provideCasesForFormatComment()
Definition: LinkerTest.php:133
static userLink($userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:984
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Database.
Definition: LinkerTest.php:9
static provideLinkEndHook()
Definition: LinkerTest.php:384
static provideLinkBeginHook()
Definition: LinkerTest.php:314
testLinkBeginHook($callback, $expected)
MediaWiki\Linker\LinkRenderer::runLegacyBeginHook provideLinkBeginHook.
Definition: LinkerTest.php:369
This is a class for holding configuration settings, particularly for multi-wiki sites.
testGetLinkColour()
Linker::getLinkColour.
Definition: LinkerTest.php:431
setMwGlobals($pairs, $value=null)
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
testLinkEndHook($callback, $expected)
MediaWiki\Linker\LinkRenderer::buildAElement provideLinkEndHook.
Definition: LinkerTest.php:416