Merge "rdbms: clean up and refactor ResultWrapper classes"
[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">: </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 '',
325 "/* */",
326 false, true
327 ],
328 [
329 '',
330 "/* */",
331 null
332 ],
333 [
334 '<span dir="auto"><span class="autocomment">[[</span></span>',
335 "/* [[ */",
336 false, true
337 ],
338 [
339 '<span dir="auto"><span class="autocomment">[[</span></span>',
340 "/* [[ */",
341 null
342 ],
343 [
344 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#.23\">→‎&#91;[#_\t_]]</a></span></span>",
345 "foo /* [[#_\t_]] */",
346 false, true
347 ],
348 [
349 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#_.09\">#_\t_</a></span></span>",
350 "foo /* [[#_\t_]] */",
351 null
352 ],
353 [
354 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
355 "/* autocomment */",
356 false, false
357 ],
358 [
359 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→‎autocomment</a></span></span>',
360 "/* autocomment */",
361 false, false, $wikiId
362 ],
363 // Linker::formatLinksInComment
364 [
365 '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',
366 "abc [[link]] def",
367 ],
368 [
369 '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',
370 "abc [[link|text]] def",
371 ],
372 [
373 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
374 "abc [[Special:BlankPage|]] def",
375 ],
376 [
377 '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',
378 "abc [[%C4%85%C5%9B%C5%BC]] def",
379 ],
380 [
381 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
382 "abc [[#section]] def",
383 ],
384 [
385 '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',
386 "abc [[/subpage]] def",
387 ],
388 [
389 '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',
390 "abc [[\"evil!\"]] def",
391 ],
392 [
393 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
394 "abc [[<script>very evil</script>]] def",
395 ],
396 [
397 'abc [[|]] def',
398 "abc [[|]] def",
399 ],
400 [
401 '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',
402 "abc [[link]] def",
403 false, false
404 ],
405 [
406 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
407 "abc [[link]] def",
408 false, false, $wikiId
409 ],
410 ];
411 // phpcs:enable
412 }
413
414 /**
415 * @covers Linker::formatLinksInComment
416 * @dataProvider provideCasesForFormatLinksInComment
417 */
418 public function testFormatLinksInComment( $expected, $input, $wiki ) {
419 $conf = new SiteConfiguration();
420 $conf->settings = [
421 'wgServer' => [
422 'enwiki' => '//en.example.org'
423 ],
424 'wgArticlePath' => [
425 'enwiki' => '/w/$1',
426 ],
427 ];
428 $conf->suffixes = [ 'wiki' ];
429 $this->setMwGlobals( [
430 'wgScript' => '/wiki/index.php',
431 'wgArticlePath' => '/wiki/$1',
432 'wgCapitalLinks' => true,
433 'wgConf' => $conf,
434 ] );
435
436 $this->assertEquals(
437 $expected,
438 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
439 );
440 }
441
442 /**
443 * @covers Linker::generateRollback
444 * @dataProvider provideCasesForRollbackGeneration
445 */
446 public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) {
447 $this->markTestSkippedIfDbType( 'postgres' );
448
449 $context = RequestContext::getMain();
450 $user = $context->getUser();
451 $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
452
453 $this->assertEquals( 0, Title::newFromText( $title )->getArticleID() );
454 $pageData = $this->insertPage( $title );
455 $page = WikiPage::factory( $pageData['title'] );
456
457 $updater = $page->newPageUpdater( $user );
458 $updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN,
459 new TextContent( 'Technical Wishes 123!' )
460 );
461 $summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
462 $updater->saveRevision( $summary );
463
464 $rollbackOutput = Linker::generateRollback( $page->getRevision(), $context );
465 $modules = $context->getOutput()->getModules();
466 $currentRev = $page->getRevision();
467 $oldestRev = $page->getOldestRevision();
468
469 $this->assertEquals( $expectedModules, $modules );
470 $this->assertEquals( $user->getName(), $currentRev->getUserText() );
471 $this->assertEquals(
472 static::getTestSysop()->getUser(),
473 $oldestRev->getUserText()
474 );
475
476 $ids = [];
477 $r = $oldestRev;
478 while ( $r ) {
479 $ids[] = $r->getId();
480 $r = $r->getNext();
481 }
482 $this->assertEquals( [ $oldestRev->getId(), $currentRev->getId() ], $ids );
483
484 $this->assertContains( 'rollback 1 edit', $rollbackOutput );
485 }
486
487 public static function provideCasesForRollbackGeneration() {
488 return [
489 [
490 true,
491 [ 'mediawiki.page.rollback.confirmation' ],
492 'Rollback_Test_Page'
493 ],
494 [
495 false,
496 [],
497 'Rollback_Test_Page2'
498 ]
499 ];
500 }
501
502 public static function provideCasesForFormatLinksInComment() {
503 // phpcs:disable Generic.Files.LineLength
504 return [
505 [
506 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
507 'foo bar [[Special:BlankPage]]',
508 null,
509 ],
510 [
511 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
512 '[[ :Special:BlankPage]]',
513 null,
514 ],
515 [
516 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
517 "[[Foo'bar]]",
518 'enwiki',
519 ],
520 [
521 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
522 'foo bar [[Special:BlankPage]]',
523 'enwiki',
524 ],
525 [
526 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
527 'foo bar [[Image:Example]]',
528 'enwiki',
529 ],
530 ];
531 // phpcs:enable
532 }
533
534 public static function provideLinkBeginHook() {
535 // phpcs:disable Generic.Files.LineLength
536 return [
537 // Modify $html
538 [
539 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
540 $html = 'foobar';
541 },
542 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
543 ],
544 // Modify $attribs
545 [
546 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
547 $attribs['bar'] = 'baz';
548 },
549 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
550 ],
551 // Modify $query
552 [
553 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
554 $query['bar'] = 'baz';
555 },
556 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
557 ],
558 // Force HTTP $options
559 [
560 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
561 $options = [ 'http' ];
562 },
563 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
564 ],
565 // Force 'forcearticlepath' in $options
566 [
567 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
568 $options = [ 'forcearticlepath' ];
569 $query['foo'] = 'bar';
570 },
571 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
572 ],
573 // Abort early
574 [
575 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
576 $ret = 'foobar';
577 return false;
578 },
579 'foobar'
580 ],
581 ];
582 // phpcs:enable
583 }
584
585 /**
586 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
587 * @dataProvider provideLinkBeginHook
588 */
589 public function testLinkBeginHook( $callback, $expected ) {
590 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
591 $this->setMwGlobals( [
592 'wgArticlePath' => '/wiki/$1',
593 'wgServer' => '//example.org',
594 'wgCanonicalServer' => 'http://example.org',
595 'wgScriptPath' => '/w',
596 'wgScript' => '/w/index.php',
597 ] );
598
599 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
600 $title = SpecialPage::getTitleFor( 'Blankpage' );
601 $out = Linker::link( $title );
602 $this->assertEquals( $expected, $out );
603 }
604
605 public static function provideLinkEndHook() {
606 return [
607 // Override $html
608 [
609 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
610 $html = 'foobar';
611 },
612 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
613 ],
614 // Modify $attribs
615 [
616 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
617 $attribs['bar'] = 'baz';
618 },
619 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
620 ],
621 // Fully override return value and abort hook
622 [
623 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
624 $ret = 'blahblahblah';
625 return false;
626 },
627 'blahblahblah'
628 ],
629
630 ];
631 }
632
633 /**
634 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
635 * @dataProvider provideLinkEndHook
636 */
637 public function testLinkEndHook( $callback, $expected ) {
638 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
639 $this->setMwGlobals( [
640 'wgArticlePath' => '/wiki/$1',
641 ] );
642
643 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
644
645 $title = SpecialPage::getTitleFor( 'Blankpage' );
646 $out = Linker::link( $title );
647 $this->assertEquals( $expected, $out );
648 }
649 }