Merge "Add tablesUsed to RevisionStoreDbTest"
[lhc/web/wiklou.git] / tests / phpunit / includes / LinkerTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * @group Database
7 */
8 class LinkerTest extends MediaWikiLangTestCase {
9
10 /**
11 * @dataProvider provideCasesForUserLink
12 * @covers Linker::userLink
13 */
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
92 /**
93 * @dataProvider provideCasesForFormatComment
94 * @covers Linker::formatComment
95 * @covers Linker::formatAutocomments
96 * @covers Linker::formatLinksInComment
97 */
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
132 public function provideCasesForFormatComment() {
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
262 /**
263 * @covers Linker::formatLinksInComment
264 * @dataProvider provideCasesForFormatLinksInComment
265 */
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
368 /**
369 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
370 * @dataProvider provideLinkBeginHook
371 */
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
415 /**
416 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
417 * @dataProvider provideLinkEndHook
418 */
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
431 /**
432 * @covers Linker::getLinkColour
433 */
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 }