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
9 class LinkerTest extends MediaWikiLangTestCase {
10
11 /**
12 * @dataProvider provideCasesForUserLink
13 * @covers Linker::userLink
14 */
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
93 /**
94 * @dataProvider provideCasesForFormatComment
95 * @covers Linker::formatComment
96 * @covers Linker::formatAutocomments
97 * @covers Linker::formatLinksInComment
98 */
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 // phpcs:disable 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 // phpcs:enable
261 }
262
263 /**
264 * @covers Linker::formatLinksInComment
265 * @dataProvider provideCasesForFormatLinksInComment
266 */
267 public function testFormatLinksInComment( $expected, $input, $wiki ) {
268 $conf = new SiteConfiguration();
269 $conf->settings = [
270 'wgServer' => [
271 'enwiki' => '//en.example.org'
272 ],
273 'wgArticlePath' => [
274 'enwiki' => '/w/$1',
275 ],
276 ];
277 $conf->suffixes = [ 'wiki' ];
278 $this->setMwGlobals( [
279 'wgScript' => '/wiki/index.php',
280 'wgArticlePath' => '/wiki/$1',
281 'wgCapitalLinks' => true,
282 'wgConf' => $conf,
283 ] );
284
285 $this->assertEquals(
286 $expected,
287 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
288 );
289 }
290
291 public static function provideCasesForFormatLinksInComment() {
292 // phpcs:disable Generic.Files.LineLength
293 return [
294 [
295 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
296 'foo bar [[Special:BlankPage]]',
297 null,
298 ],
299 [
300 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
301 "[[Foo'bar]]",
302 'enwiki',
303 ],
304 [
305 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
306 'foo bar [[Special:BlankPage]]',
307 'enwiki',
308 ],
309 [
310 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
311 'foo bar [[Image:Example]]',
312 'enwiki',
313 ],
314 ];
315 // phpcs:enable
316 }
317
318 public static function provideLinkBeginHook() {
319 // phpcs:disable Generic.Files.LineLength
320 return [
321 // Modify $html
322 [
323 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
324 $html = 'foobar';
325 },
326 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
327 ],
328 // Modify $attribs
329 [
330 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
331 $attribs['bar'] = 'baz';
332 },
333 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
334 ],
335 // Modify $query
336 [
337 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
338 $query['bar'] = 'baz';
339 },
340 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
341 ],
342 // Force HTTP $options
343 [
344 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
345 $options = [ 'http' ];
346 },
347 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
348 ],
349 // Force 'forcearticlepath' in $options
350 [
351 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
352 $options = [ 'forcearticlepath' ];
353 $query['foo'] = 'bar';
354 },
355 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
356 ],
357 // Abort early
358 [
359 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
360 $ret = 'foobar';
361 return false;
362 },
363 'foobar'
364 ],
365 ];
366 // phpcs:enable
367 }
368
369 /**
370 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
371 * @dataProvider provideLinkBeginHook
372 */
373 public function testLinkBeginHook( $callback, $expected ) {
374 $this->setMwGlobals( [
375 'wgArticlePath' => '/wiki/$1',
376 'wgServer' => '//example.org',
377 'wgCanonicalServer' => 'http://example.org',
378 'wgScriptPath' => '/w',
379 'wgScript' => '/w/index.php',
380 ] );
381
382 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
383 $title = SpecialPage::getTitleFor( 'Blankpage' );
384 $out = Linker::link( $title );
385 $this->assertEquals( $expected, $out );
386 }
387
388 public static function provideLinkEndHook() {
389 return [
390 // Override $html
391 [
392 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
393 $html = 'foobar';
394 },
395 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
396 ],
397 // Modify $attribs
398 [
399 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
400 $attribs['bar'] = 'baz';
401 },
402 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
403 ],
404 // Fully override return value and abort hook
405 [
406 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
407 $ret = 'blahblahblah';
408 return false;
409 },
410 'blahblahblah'
411 ],
412
413 ];
414 }
415
416 /**
417 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
418 * @dataProvider provideLinkEndHook
419 */
420 public function testLinkEndHook( $callback, $expected ) {
421 $this->setMwGlobals( [
422 'wgArticlePath' => '/wiki/$1',
423 ] );
424
425 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
426
427 $title = SpecialPage::getTitleFor( 'Blankpage' );
428 $out = Linker::link( $title );
429 $this->assertEquals( $expected, $out );
430 }
431
432 /**
433 * @covers Linker::getLinkColour
434 */
435 public function testGetLinkColour() {
436 $this->hideDeprecated( 'Linker::getLinkColour' );
437 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
438 $foobarTitle = Title::makeTitle( NS_MAIN, 'FooBar' );
439 $redirectTitle = Title::makeTitle( NS_MAIN, 'Redirect' );
440 $userTitle = Title::makeTitle( NS_USER, 'Someuser' );
441 $linkCache->addGoodLinkObj(
442 1, // id
443 $foobarTitle,
444 10, // len
445 0 // redir
446 );
447 $linkCache->addGoodLinkObj(
448 2, // id
449 $redirectTitle,
450 10, // len
451 1 // redir
452 );
453
454 $linkCache->addGoodLinkObj(
455 3, // id
456 $userTitle,
457 10, // len
458 0 // redir
459 );
460
461 $this->assertEquals(
462 '',
463 Linker::getLinkColour( $foobarTitle, 0 )
464 );
465
466 $this->assertEquals(
467 'stub',
468 Linker::getLinkColour( $foobarTitle, 20 )
469 );
470
471 $this->assertEquals(
472 'mw-redirect',
473 Linker::getLinkColour( $redirectTitle, 0 )
474 );
475
476 $this->assertEquals(
477 '',
478 Linker::getLinkColour( $userTitle, 20 )
479 );
480 }
481 }