Cleanup LinkerTest leakage between provider entries
[lhc/web/wiklou.git] / tests / phpunit / includes / LinkerTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class LinkerTest extends MediaWikiLangTestCase {
7 /**
8 * @dataProvider provideCasesForUserLink
9 * @covers Linker::userLink
10 */
11 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
12 $this->setMwGlobals( [
13 'wgArticlePath' => '/wiki/$1',
14 ] );
15
16 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
17 if ( $userName === '' ) {
18 Wikimedia\suppressWarnings();
19 }
20 $actual = Linker::userLink( $userId, $userName, $altUserName );
21 if ( $userName === '' ) {
22 Wikimedia\restoreWarnings();
23 }
24
25 $this->assertEquals( $expected, $actual, $msg );
26 }
27
28 public static function provideCasesForUserLink() {
29 # Format:
30 # - expected
31 # - userid
32 # - username
33 # - optional altUserName
34 # - optional message
35 return [
36 # Empty name (T222529)
37 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
38 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
39
40 # ## ANONYMOUS USER ########################################
41 [
42 '<a href="/wiki/Special:Contributions/JohnDoe" '
43 . 'class="mw-userlink mw-anonuserlink" '
44 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
45 0, 'JohnDoe', false,
46 ],
47 [
48 '<a href="/wiki/Special:Contributions/::1" '
49 . 'class="mw-userlink mw-anonuserlink" '
50 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
51 0, '::1', false,
52 'Anonymous with pretty IPv6'
53 ],
54 [
55 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
56 . 'class="mw-userlink mw-anonuserlink" '
57 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
58 0, '0:0:0:0:0:0:0:1', false,
59 'Anonymous with almost pretty IPv6'
60 ],
61 [
62 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
63 . 'class="mw-userlink mw-anonuserlink" '
64 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
65 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
66 'Anonymous with full IPv6'
67 ],
68 [
69 '<a href="/wiki/Special:Contributions/::1" '
70 . 'class="mw-userlink mw-anonuserlink" '
71 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
72 0, '::1', 'AlternativeUsername',
73 'Anonymous with pretty IPv6 and an alternative username'
74 ],
75
76 # IPV4
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>127.0.0.1</bdi></a>',
81 0, '127.0.0.1', false,
82 'Anonymous with IPv4'
83 ],
84 [
85 '<a href="/wiki/Special:Contributions/127.0.0.1" '
86 . 'class="mw-userlink mw-anonuserlink" '
87 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
88 0, '127.0.0.1', 'AlternativeUsername',
89 'Anonymous with IPv4 and an alternative username'
90 ],
91
92 # ## Regular user ##########################################
93 # TODO!
94 ];
95 }
96
97 /**
98 * @dataProvider provideUserToolLinks
99 * @covers Linker::userToolLinks
100 * @param string $expected
101 * @param int $userId
102 * @param string $userText
103 */
104 public function testUserToolLinks( $expected, $userId, $userText ) {
105 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
106 if ( $userText === '' ) {
107 Wikimedia\suppressWarnings();
108 }
109 $actual = Linker::userToolLinks( $userId, $userText );
110 if ( $userText === '' ) {
111 Wikimedia\restoreWarnings();
112 }
113
114 $this->assertSame( $expected, $actual );
115 }
116
117 public static function provideUserToolLinks() {
118 return [
119 // Empty name (T222529)
120 'Empty username, userid 0' => [ ' (no username available)', 0, '' ],
121 'Empty username, userid > 0' => [ ' (no username available)', 73, '' ],
122 ];
123 }
124
125 /**
126 * @dataProvider provideUserTalkLink
127 * @covers Linker::userTalkLink
128 * @param string $expected
129 * @param int $userId
130 * @param string $userText
131 */
132 public function testUserTalkLink( $expected, $userId, $userText ) {
133 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
134 if ( $userText === '' ) {
135 Wikimedia\suppressWarnings();
136 }
137 $actual = Linker::userTalkLink( $userId, $userText );
138 if ( $userText === '' ) {
139 Wikimedia\restoreWarnings();
140 }
141
142 $this->assertSame( $expected, $actual );
143 }
144
145 public static function provideUserTalkLink() {
146 return [
147 // Empty name (T222529)
148 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
149 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
150 ];
151 }
152
153 /**
154 * @dataProvider provideBlockLink
155 * @covers Linker::blockLink
156 * @param string $expected
157 * @param int $userId
158 * @param string $userText
159 */
160 public function testBlockLink( $expected, $userId, $userText ) {
161 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
162 if ( $userText === '' ) {
163 Wikimedia\suppressWarnings();
164 }
165 $actual = Linker::blockLink( $userId, $userText );
166 if ( $userText === '' ) {
167 Wikimedia\restoreWarnings();
168 }
169
170 $this->assertSame( $expected, $actual );
171 }
172
173 public static function provideBlockLink() {
174 return [
175 // Empty name (T222529)
176 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
177 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
178 ];
179 }
180
181 /**
182 * @dataProvider provideEmailLink
183 * @covers Linker::emailLink
184 * @param string $expected
185 * @param int $userId
186 * @param string $userText
187 */
188 public function testEmailLink( $expected, $userId, $userText ) {
189 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
190 if ( $userText === '' ) {
191 Wikimedia\suppressWarnings();
192 }
193 $actual = Linker::emailLink( $userId, $userText );
194 if ( $userText === '' ) {
195 Wikimedia\restoreWarnings();
196 }
197
198 $this->assertSame( $expected, $actual );
199 }
200
201 public static function provideEmailLink() {
202 return [
203 // Empty name (T222529)
204 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
205 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
206 ];
207 }
208
209 /**
210 * @dataProvider provideCasesForFormatComment
211 * @covers Linker::formatComment
212 * @covers Linker::formatAutocomments
213 * @covers Linker::formatLinksInComment
214 */
215 public function testFormatComment(
216 $expected, $comment, $title = false, $local = false, $wikiId = null
217 ) {
218 $conf = new SiteConfiguration();
219 $conf->settings = [
220 'wgServer' => [
221 'enwiki' => '//en.example.org',
222 'dewiki' => '//de.example.org',
223 ],
224 'wgArticlePath' => [
225 'enwiki' => '/w/$1',
226 'dewiki' => '/w/$1',
227 ],
228 ];
229 $conf->suffixes = [ 'wiki' ];
230
231 $this->setMwGlobals( [
232 'wgScript' => '/wiki/index.php',
233 'wgArticlePath' => '/wiki/$1',
234 'wgCapitalLinks' => true,
235 'wgConf' => $conf,
236 ] );
237
238 if ( $title === false ) {
239 // We need a page title that exists
240 $title = Title::newFromText( 'Special:BlankPage' );
241 }
242
243 $this->assertEquals(
244 $expected,
245 Linker::formatComment( $comment, $title, $local, $wikiId )
246 );
247 }
248
249 public function provideCasesForFormatComment() {
250 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
251
252 // phpcs:disable Generic.Files.LineLength
253 return [
254 // Linker::formatComment
255 [
256 'a&lt;script&gt;b',
257 'a<script>b',
258 ],
259 [
260 'a—b',
261 'a&mdash;b',
262 ],
263 [
264 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
265 "'''not bolded'''",
266 ],
267 [
268 "try &lt;script&gt;evil&lt;/scipt&gt; things",
269 "try <script>evil</scipt> things",
270 ],
271 // Linker::formatAutocomments
272 [
273 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
274 "/* autocomment */",
275 ],
276 [
277 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→‎&#91;[linkie?]]</a></span></span>',
278 "/* [[linkie?]] */",
279 ],
280 [
281 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage" title="Special:BlankPage">→‎</a>: </span> // Edit via via</span>',
282 // Regression test for T222857
283 "/* */ // Edit via via",
284 ],
285 [
286 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
287 "/* autocomment */ post",
288 ],
289 [
290 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
291 "pre /* autocomment */",
292 ],
293 [
294 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
295 "pre /* autocomment */ post",
296 ],
297 [
298 '<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>',
299 "/* autocomment */ multiple? /* autocomment2 */",
300 ],
301 [
302 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→‎autocomment containing /*</a>: </span> T70361</span>',
303 "/* autocomment containing /* */ T70361"
304 ],
305 [
306 '<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>',
307 "/* autocomment containing \"quotes\" */"
308 ],
309 [
310 '<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>',
311 "/* autocomment containing <script>tags</script> */"
312 ],
313 [
314 '<span dir="auto"><span class="autocomment"><a href="#autocomment">→‎autocomment</a></span></span>',
315 "/* autocomment */",
316 false, true
317 ],
318 [
319 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
320 "/* autocomment */",
321 null
322 ],
323 [
324 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
325 "/* autocomment */",
326 false, false
327 ],
328 [
329 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→‎autocomment</a></span></span>',
330 "/* autocomment */",
331 false, false, $wikiId
332 ],
333 // Linker::formatLinksInComment
334 [
335 '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',
336 "abc [[link]] def",
337 ],
338 [
339 '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',
340 "abc [[link|text]] def",
341 ],
342 [
343 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
344 "abc [[Special:BlankPage|]] def",
345 ],
346 [
347 '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',
348 "abc [[%C4%85%C5%9B%C5%BC]] def",
349 ],
350 [
351 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
352 "abc [[#section]] def",
353 ],
354 [
355 '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',
356 "abc [[/subpage]] def",
357 ],
358 [
359 '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',
360 "abc [[\"evil!\"]] def",
361 ],
362 [
363 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
364 "abc [[<script>very evil</script>]] def",
365 ],
366 [
367 'abc [[|]] def',
368 "abc [[|]] def",
369 ],
370 [
371 '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',
372 "abc [[link]] def",
373 false, false
374 ],
375 [
376 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
377 "abc [[link]] def",
378 false, false, $wikiId
379 ],
380 ];
381 // phpcs:enable
382 }
383
384 /**
385 * @covers Linker::formatLinksInComment
386 * @dataProvider provideCasesForFormatLinksInComment
387 */
388 public function testFormatLinksInComment( $expected, $input, $wiki ) {
389 $conf = new SiteConfiguration();
390 $conf->settings = [
391 'wgServer' => [
392 'enwiki' => '//en.example.org'
393 ],
394 'wgArticlePath' => [
395 'enwiki' => '/w/$1',
396 ],
397 ];
398 $conf->suffixes = [ 'wiki' ];
399 $this->setMwGlobals( [
400 'wgScript' => '/wiki/index.php',
401 'wgArticlePath' => '/wiki/$1',
402 'wgCapitalLinks' => true,
403 'wgConf' => $conf,
404 ] );
405
406 $this->assertEquals(
407 $expected,
408 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
409 );
410 }
411
412 /**
413 * @covers Linker::generateRollback
414 * @dataProvider provideCasesForRollbackGeneration
415 */
416 public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) {
417 $this->markTestSkippedIfDbType( 'postgres' );
418
419 $context = RequestContext::getMain();
420 $user = $context->getUser();
421 $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
422
423 $this->assertEquals( 0, Title::newFromText( $title )->getArticleID() );
424 $pageData = $this->insertPage( $title );
425 $page = WikiPage::factory( $pageData['title'] );
426
427 $updater = $page->newPageUpdater( $user );
428 $updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN,
429 new TextContent( 'Technical Wishes 123!' )
430 );
431 $summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
432 $updater->saveRevision( $summary );
433
434 $rollbackOutput = Linker::generateRollback( $page->getRevision(), $context );
435 $modules = $context->getOutput()->getModules();
436 $currentRev = $page->getRevision();
437 $oldestRev = $page->getOldestRevision();
438
439 $this->assertEquals( $expectedModules, $modules );
440 $this->assertEquals( $user->getName(), $currentRev->getUserText() );
441 $this->assertEquals(
442 static::getTestSysop()->getUser(),
443 $oldestRev->getUserText()
444 );
445
446 $ids = [];
447 $r = $oldestRev;
448 while ( $r ) {
449 $ids[] = $r->getId();
450 $r = $r->getNext();
451 }
452 $this->assertEquals( [ $oldestRev->getId(), $currentRev->getId() ], $ids );
453
454 $this->assertContains( 'rollback 1 edit', $rollbackOutput );
455 }
456
457 public static function provideCasesForRollbackGeneration() {
458 return [
459 [
460 true,
461 [ 'mediawiki.page.rollback.confirmation' ],
462 'Rollback_Test_Page'
463 ],
464 [
465 false,
466 [],
467 'Rollback_Test_Page2'
468 ]
469 ];
470 }
471
472 public static function provideCasesForFormatLinksInComment() {
473 // phpcs:disable Generic.Files.LineLength
474 return [
475 [
476 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
477 'foo bar [[Special:BlankPage]]',
478 null,
479 ],
480 [
481 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
482 '[[ :Special:BlankPage]]',
483 null,
484 ],
485 [
486 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
487 "[[Foo'bar]]",
488 'enwiki',
489 ],
490 [
491 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
492 'foo bar [[Special:BlankPage]]',
493 'enwiki',
494 ],
495 [
496 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
497 'foo bar [[Image:Example]]',
498 'enwiki',
499 ],
500 ];
501 // phpcs:enable
502 }
503
504 public static function provideLinkBeginHook() {
505 // phpcs:disable Generic.Files.LineLength
506 return [
507 // Modify $html
508 [
509 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
510 $html = 'foobar';
511 },
512 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
513 ],
514 // Modify $attribs
515 [
516 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
517 $attribs['bar'] = 'baz';
518 },
519 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
520 ],
521 // Modify $query
522 [
523 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
524 $query['bar'] = 'baz';
525 },
526 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
527 ],
528 // Force HTTP $options
529 [
530 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
531 $options = [ 'http' ];
532 },
533 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
534 ],
535 // Force 'forcearticlepath' in $options
536 [
537 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
538 $options = [ 'forcearticlepath' ];
539 $query['foo'] = 'bar';
540 },
541 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
542 ],
543 // Abort early
544 [
545 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
546 $ret = 'foobar';
547 return false;
548 },
549 'foobar'
550 ],
551 ];
552 // phpcs:enable
553 }
554
555 /**
556 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
557 * @dataProvider provideLinkBeginHook
558 */
559 public function testLinkBeginHook( $callback, $expected ) {
560 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
561 $this->setMwGlobals( [
562 'wgArticlePath' => '/wiki/$1',
563 'wgServer' => '//example.org',
564 'wgCanonicalServer' => 'http://example.org',
565 'wgScriptPath' => '/w',
566 'wgScript' => '/w/index.php',
567 ] );
568
569 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
570 $title = SpecialPage::getTitleFor( 'Blankpage' );
571 $out = Linker::link( $title );
572 $this->assertEquals( $expected, $out );
573 }
574
575 public static function provideLinkEndHook() {
576 return [
577 // Override $html
578 [
579 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
580 $html = 'foobar';
581 },
582 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
583 ],
584 // Modify $attribs
585 [
586 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
587 $attribs['bar'] = 'baz';
588 },
589 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
590 ],
591 // Fully override return value and abort hook
592 [
593 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
594 $ret = 'blahblahblah';
595 return false;
596 },
597 'blahblahblah'
598 ],
599
600 ];
601 }
602
603 /**
604 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
605 * @dataProvider provideLinkEndHook
606 */
607 public function testLinkEndHook( $callback, $expected ) {
608 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
609 $this->setMwGlobals( [
610 'wgArticlePath' => '/wiki/$1',
611 ] );
612
613 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
614
615 $title = SpecialPage::getTitleFor( 'Blankpage' );
616 $out = Linker::link( $title );
617 $this->assertEquals( $expected, $out );
618 }
619 }