setMwGlobals( [ 'wgArticlePath' => '/wiki/$1', ] ); $this->assertEquals( $expected, Linker::userLink( $userId, $userName, $altUserName ), $msg ); } public static function provideCasesForUserLink() { # Format: # - expected # - userid # - username # - optional altUserName # - optional message return [ # ## ANONYMOUS USER ######################################## [ 'JohnDoe', 0, 'JohnDoe', false, ], [ '::1', 0, '::1', false, 'Anonymous with pretty IPv6' ], [ '::1', 0, '0:0:0:0:0:0:0:1', false, 'Anonymous with almost pretty IPv6' ], [ '::1', 0, '0000:0000:0000:0000:0000:0000:0000:0001', false, 'Anonymous with full IPv6' ], [ 'AlternativeUsername', 0, '::1', 'AlternativeUsername', 'Anonymous with pretty IPv6 and an alternative username' ], # IPV4 [ '127.0.0.1', 0, '127.0.0.1', false, 'Anonymous with IPv4' ], [ 'AlternativeUsername', 0, '127.0.0.1', 'AlternativeUsername', 'Anonymous with IPv4 and an alternative username' ], # ## Regular user ########################################## # TODO! ]; } /** * @dataProvider provideCasesForFormatComment * @covers Linker::formatComment * @covers Linker::formatAutocomments * @covers Linker::formatLinksInComment */ public function testFormatComment( $expected, $comment, $title = false, $local = false, $wikiId = null ) { $conf = new SiteConfiguration(); $conf->settings = [ 'wgServer' => [ 'enwiki' => '//en.example.org', 'dewiki' => '//de.example.org', ], 'wgArticlePath' => [ 'enwiki' => '/w/$1', 'dewiki' => '/w/$1', ], ]; $conf->suffixes = [ 'wiki' ]; $this->setMwGlobals( [ 'wgScript' => '/wiki/index.php', 'wgArticlePath' => '/wiki/$1', 'wgCapitalLinks' => true, 'wgConf' => $conf, ] ); if ( $title === false ) { // We need a page title that exists $title = Title::newFromText( 'Special:BlankPage' ); } $this->assertEquals( $expected, Linker::formatComment( $comment, $title, $local, $wikiId ) ); } public function provideCasesForFormatComment() { $wikiId = 'enwiki'; // $wgConf has a fake entry for this // phpcs:disable Generic.Files.LineLength return [ // Linker::formatComment [ 'a<script>b', 'a */" ], [ 'autocomment', "/* autocomment */", false, true ], [ '‎autocomment', "/* autocomment */", null ], [ 'autocomment', "/* autocomment */", false, false ], [ 'autocomment', "/* autocomment */", false, false, $wikiId ], // Linker::formatLinksInComment [ 'abc link def', "abc [[link]] def", ], [ 'abc text def', "abc [[link|text]] def", ], [ 'abc Special:BlankPage def', "abc [[Special:BlankPage|]] def", ], [ 'abc ąśż def', "abc [[%C4%85%C5%9B%C5%BC]] def", ], [ 'abc #section def', "abc [[#section]] def", ], [ 'abc /subpage def', "abc [[/subpage]] def", ], [ 'abc "evil!" def', "abc [[\"evil!\"]] def", ], [ 'abc [[<script>very evil</script>]] def', "abc [[]] def", ], [ 'abc [[|]] def', "abc [[|]] def", ], [ 'abc link def', "abc [[link]] def", false, false ], [ 'abc link def', "abc [[link]] def", false, false, $wikiId ], ]; // phpcs:enable } /** * @covers Linker::formatLinksInComment * @dataProvider provideCasesForFormatLinksInComment */ public function testFormatLinksInComment( $expected, $input, $wiki ) { $conf = new SiteConfiguration(); $conf->settings = [ 'wgServer' => [ 'enwiki' => '//en.example.org' ], 'wgArticlePath' => [ 'enwiki' => '/w/$1', ], ]; $conf->suffixes = [ 'wiki' ]; $this->setMwGlobals( [ 'wgScript' => '/wiki/index.php', 'wgArticlePath' => '/wiki/$1', 'wgCapitalLinks' => true, 'wgConf' => $conf, ] ); $this->assertEquals( $expected, Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki ) ); } public static function provideCasesForFormatLinksInComment() { // phpcs:disable Generic.Files.LineLength return [ [ 'foo bar Special:BlankPage', 'foo bar [[Special:BlankPage]]', null, ], [ 'Special:BlankPage', '[[ :Special:BlankPage]]', null, ], [ 'Foo\'bar', "[[Foo'bar]]", 'enwiki', ], [ 'foo bar Special:BlankPage', 'foo bar [[Special:BlankPage]]', 'enwiki', ], [ 'foo bar Image:Example', 'foo bar [[Image:Example]]', 'enwiki', ], ]; // phpcs:enable } public static function provideLinkBeginHook() { // phpcs:disable Generic.Files.LineLength return [ // Modify $html [ function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { $html = 'foobar'; }, 'foobar' ], // Modify $attribs [ function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { $attribs['bar'] = 'baz'; }, 'Special:BlankPage' ], // Modify $query [ function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { $query['bar'] = 'baz'; }, 'Special:BlankPage' ], // Force HTTP $options [ function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { $options = [ 'http' ]; }, 'Special:BlankPage' ], // Force 'forcearticlepath' in $options [ function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { $options = [ 'forcearticlepath' ]; $query['foo'] = 'bar'; }, 'Special:BlankPage' ], // Abort early [ function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) { $ret = 'foobar'; return false; }, 'foobar' ], ]; // phpcs:enable } /** * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook * @dataProvider provideLinkBeginHook */ public function testLinkBeginHook( $callback, $expected ) { $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' ); $this->setMwGlobals( [ 'wgArticlePath' => '/wiki/$1', 'wgServer' => '//example.org', 'wgCanonicalServer' => 'http://example.org', 'wgScriptPath' => '/w', 'wgScript' => '/w/index.php', ] ); $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] ); $title = SpecialPage::getTitleFor( 'Blankpage' ); $out = Linker::link( $title ); $this->assertEquals( $expected, $out ); } public static function provideLinkEndHook() { return [ // Override $html [ function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) { $html = 'foobar'; }, 'foobar' ], // Modify $attribs [ function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) { $attribs['bar'] = 'baz'; }, 'Special:BlankPage' ], // Fully override return value and abort hook [ function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) { $ret = 'blahblahblah'; return false; }, 'blahblahblah' ], ]; } /** * @covers MediaWiki\Linker\LinkRenderer::buildAElement * @dataProvider provideLinkEndHook */ public function testLinkEndHook( $callback, $expected ) { $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' ); $this->setMwGlobals( [ 'wgArticlePath' => '/wiki/$1', ] ); $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] ); $title = SpecialPage::getTitleFor( 'Blankpage' ); $out = Linker::link( $title ); $this->assertEquals( $expected, $out ); } }