MediaWiki REL1_33
LinkerTest.php
Go to the documentation of this file.
1<?php
2
7
12 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
13 $this->setMwGlobals( [
14 'wgArticlePath' => '/wiki/$1',
15 ] );
16
17 $this->assertEquals(
18 $expected,
19 Linker::userLink( $userId, $userName, $altUserName ),
20 $msg
21 );
22 }
23
24 public static function provideCasesForUserLink() {
25 # Format:
26 # - expected
27 # - userid
28 # - username
29 # - optional altUserName
30 # - optional message
31 return [
32
33 # ## ANONYMOUS USER ########################################
34 [
35 '<a href="/wiki/Special:Contributions/JohnDoe" '
36 . 'class="mw-userlink mw-anonuserlink" '
37 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
38 0, 'JohnDoe', false,
39 ],
40 [
41 '<a href="/wiki/Special:Contributions/::1" '
42 . 'class="mw-userlink mw-anonuserlink" '
43 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
44 0, '::1', false,
45 'Anonymous with pretty IPv6'
46 ],
47 [
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'
53 ],
54 [
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'
60 ],
61 [
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'
67 ],
68
69 # IPV4
70 [
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,
75 'Anonymous with IPv4'
76 ],
77 [
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'
83 ],
84
85 # ## Regular user ##########################################
86 # TODO!
87 ];
88 }
89
96 public function testFormatComment(
97 $expected, $comment, $title = false, $local = false, $wikiId = null
98 ) {
99 $conf = new SiteConfiguration();
100 $conf->settings = [
101 'wgServer' => [
102 'enwiki' => '//en.example.org',
103 'dewiki' => '//de.example.org',
104 ],
105 'wgArticlePath' => [
106 'enwiki' => '/w/$1',
107 'dewiki' => '/w/$1',
108 ],
109 ];
110 $conf->suffixes = [ 'wiki' ];
111
112 $this->setMwGlobals( [
113 'wgScript' => '/wiki/index.php',
114 'wgArticlePath' => '/wiki/$1',
115 'wgCapitalLinks' => true,
116 'wgConf' => $conf,
117 ] );
118
119 if ( $title === false ) {
120 // We need a page title that exists
121 $title = Title::newFromText( 'Special:BlankPage' );
122 }
123
124 $this->assertEquals(
125 $expected,
126 Linker::formatComment( $comment, $title, $local, $wikiId )
127 );
128 }
129
131 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
132
133 // phpcs:disable Generic.Files.LineLength
134 return [
135 // Linker::formatComment
136 [
137 'a&lt;script&gt;b',
138 'a<script>b',
139 ],
140 [
141 'a—b',
142 'a&mdash;b',
143 ],
144 [
145 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
146 "'''not bolded'''",
147 ],
148 [
149 "try &lt;script&gt;evil&lt;/scipt&gt; things",
150 "try <script>evil</scipt> things",
151 ],
152 // Linker::formatAutocomments
153 [
154 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
155 "/* autocomment */",
156 ],
157 [
158 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→‎&#91;[linkie?]]</a></span></span>',
159 "/* [[linkie?]] */",
160 ],
161 [
162 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
163 "/* autocomment */ post",
164 ],
165 [
166 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
167 "pre /* autocomment */",
168 ],
169 [
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",
172 ],
173 [
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 */",
176 ],
177 [
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"
180 ],
181 [
182 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→‎autocomment containing &quot;quotes&quot;</a></span></span>',
183 "/* autocomment containing \"quotes\" */"
184 ],
185 [
186 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→‎autocomment containing &lt;script&gt;tags&lt;/script&gt;</a></span></span>',
187 "/* autocomment containing <script>tags</script> */"
188 ],
189 [
190 '<span dir="auto"><span class="autocomment"><a href="#autocomment">→‎autocomment</a></span></span>',
191 "/* autocomment */",
192 false, true
193 ],
194 [
195 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
196 "/* autocomment */",
197 null
198 ],
199 [
200 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
201 "/* autocomment */",
202 false, false
203 ],
204 [
205 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→‎autocomment</a></span></span>',
206 "/* autocomment */",
207 false, false, $wikiId
208 ],
209 // Linker::formatLinksInComment
210 [
211 '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',
212 "abc [[link]] def",
213 ],
214 [
215 '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',
216 "abc [[link|text]] def",
217 ],
218 [
219 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
220 "abc [[Special:BlankPage|]] def",
221 ],
222 [
223 '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',
224 "abc [[%C4%85%C5%9B%C5%BC]] def",
225 ],
226 [
227 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
228 "abc [[#section]] def",
229 ],
230 [
231 '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',
232 "abc [[/subpage]] def",
233 ],
234 [
235 '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',
236 "abc [[\"evil!\"]] def",
237 ],
238 [
239 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
240 "abc [[<script>very evil</script>]] def",
241 ],
242 [
243 'abc [[|]] def',
244 "abc [[|]] def",
245 ],
246 [
247 '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',
248 "abc [[link]] def",
249 false, false
250 ],
251 [
252 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
253 "abc [[link]] def",
254 false, false, $wikiId
255 ],
256 ];
257 // phpcs:enable
258 }
259
264 public function testFormatLinksInComment( $expected, $input, $wiki ) {
265 $conf = new SiteConfiguration();
266 $conf->settings = [
267 'wgServer' => [
268 'enwiki' => '//en.example.org'
269 ],
270 'wgArticlePath' => [
271 'enwiki' => '/w/$1',
272 ],
273 ];
274 $conf->suffixes = [ 'wiki' ];
275 $this->setMwGlobals( [
276 'wgScript' => '/wiki/index.php',
277 'wgArticlePath' => '/wiki/$1',
278 'wgCapitalLinks' => true,
279 'wgConf' => $conf,
280 ] );
281
282 $this->assertEquals(
283 $expected,
284 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
285 );
286 }
287
292 public function testGenerateRollback( $rollbackEnabled, $expectedModules ) {
293 $this->markTestSkippedIfDbType( 'postgres' );
294
295 $context = RequestContext::getMain();
296 $user = $context->getUser();
297 $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
298
299 $pageData = $this->insertPage( 'Rollback_Test_Page' );
300 $page = WikiPage::factory( $pageData['title'] );
301
302 $updater = $page->newPageUpdater( $user );
303 $updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN,
304 new TextContent( 'Technical Wishes 123!' )
305 );
306 $summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
307 $updater->saveRevision( $summary );
308
309 $rollbackOutput = Linker::generateRollback( $page->getRevision(), $context );
310 $modules = $context->getOutput()->getModules();
311
312 $this->assertEquals( $expectedModules, $modules );
313 $this->assertContains( 'rollback 1 edit', $rollbackOutput );
314 }
315
316 public static function provideCasesForRollbackGeneration() {
317 return [
318 [
319 true,
320 [ 'mediawiki.page.rollback.confirmation' ]
321
322 ],
323 [
324 false,
325 []
326 ]
327 ];
328 }
329
330 public static function provideCasesForFormatLinksInComment() {
331 // phpcs:disable Generic.Files.LineLength
332 return [
333 [
334 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
335 'foo bar [[Special:BlankPage]]',
336 null,
337 ],
338 [
339 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
340 '[[ :Special:BlankPage]]',
341 null,
342 ],
343 [
344 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
345 "[[Foo'bar]]",
346 'enwiki',
347 ],
348 [
349 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
350 'foo bar [[Special:BlankPage]]',
351 'enwiki',
352 ],
353 [
354 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
355 'foo bar [[Image:Example]]',
356 'enwiki',
357 ],
358 ];
359 // phpcs:enable
360 }
361
362 public static function provideLinkBeginHook() {
363 // phpcs:disable Generic.Files.LineLength
364 return [
365 // Modify $html
366 [
367 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
368 $html = 'foobar';
369 },
370 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
371 ],
372 // Modify $attribs
373 [
374 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
375 $attribs['bar'] = 'baz';
376 },
377 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
378 ],
379 // Modify $query
380 [
381 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
382 $query['bar'] = 'baz';
383 },
384 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
385 ],
386 // Force HTTP $options
387 [
388 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
389 $options = [ 'http' ];
390 },
391 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
392 ],
393 // Force 'forcearticlepath' in $options
394 [
395 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
396 $options = [ 'forcearticlepath' ];
397 $query['foo'] = 'bar';
398 },
399 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
400 ],
401 // Abort early
402 [
403 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
404 $ret = 'foobar';
405 return false;
406 },
407 'foobar'
408 ],
409 ];
410 // phpcs:enable
411 }
412
417 public function testLinkBeginHook( $callback, $expected ) {
418 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
419 $this->setMwGlobals( [
420 'wgArticlePath' => '/wiki/$1',
421 'wgServer' => '//example.org',
422 'wgCanonicalServer' => 'http://example.org',
423 'wgScriptPath' => '/w',
424 'wgScript' => '/w/index.php',
425 ] );
426
427 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
428 $title = SpecialPage::getTitleFor( 'Blankpage' );
430 $this->assertEquals( $expected, $out );
431 }
432
433 public static function provideLinkEndHook() {
434 return [
435 // Override $html
436 [
437 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
438 $html = 'foobar';
439 },
440 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
441 ],
442 // Modify $attribs
443 [
444 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
445 $attribs['bar'] = 'baz';
446 },
447 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
448 ],
449 // Fully override return value and abort hook
450 [
451 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
452 $ret = 'blahblahblah';
453 return false;
454 },
455 'blahblahblah'
456 ],
457
458 ];
459 }
460
465 public function testLinkEndHook( $callback, $expected ) {
466 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
467 $this->setMwGlobals( [
468 'wgArticlePath' => '/wiki/$1',
469 ] );
470
471 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
472
473 $title = SpecialPage::getTitleFor( 'Blankpage' );
475 $this->assertEquals( $expected, $out );
476 }
477}
Database.
Definition LinkerTest.php:6
testUserLink( $expected, $userId, $userName, $altUserName=false, $msg='')
provideCasesForUserLink Linker::userLink
testLinkEndHook( $callback, $expected)
MediaWiki\Linker\LinkRenderer::buildAElement provideLinkEndHook.
testGenerateRollback( $rollbackEnabled, $expectedModules)
Linker::generateRollback provideCasesForRollbackGeneration.
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 provideCasesForRollbackGeneration()
static provideLinkBeginHook()
static provideCasesForUserLink()
static generateRollback( $rev, IContextSource $context=null, $options=[ 'verify'])
Generate a rollback link for a given revision.
Definition Linker.php:1750
static link( $target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition Linker.php:84
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:892
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:1253
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:1122
Base class that store and restore the Language objects.
markTestSkippedIfDbType( $type)
Skip the test if using the specified database type.
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.
insertPage( $pageName, $text='Sample page for unit test.', $namespace=null, User $user=null)
Insert a new page.
This is a class for holding configuration settings, particularly for multi-wiki sites.
Content object implementation for representing flat text.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:855
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:1999
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2848
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition hooks.txt:783
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:2004
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:2003
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:2011
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:2012
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:1617
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
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:37
A helper class for throttling authentication attempts.
$page->newPageUpdater($user) $updater
if(is_array($mode)) switch( $mode) $input