X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FLinkerTest.php;h=3edf99f2e249ef4ac75eaf25cc2e4949d7619791;hb=b182d9693e900c29f806d71081a746ed82bce6f2;hp=a3efbb8da0e5d3ae94ec43a10ed69f61c23f586c;hpb=d9859b32cddd76ab7c2e9c17bcd951aa2f6b7fd9;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/LinkerTest.php b/tests/phpunit/includes/LinkerTest.php index a3efbb8da0..3edf99f2e2 100644 --- a/tests/phpunit/includes/LinkerTest.php +++ b/tests/phpunit/includes/LinkerTest.php @@ -1,5 +1,7 @@ setMwGlobals( array( + $this->setMwGlobals( [ 'wgArticlePath' => '/wiki/$1', - 'wgWellFormedXml' => true, - ) ); + ] ); - $this->assertEquals( $expected, - Linker::userLink( $userId, $userName, $altUserName, $msg ) + $this->assertEquals( + $expected, + Linker::userLink( $userId, $userName, $altUserName ), + $msg ); } @@ -28,63 +31,63 @@ class LinkerTest extends MediaWikiLangTestCase { # - username # - optional altUserName # - optional message - return array( + return [ - ### ANONYMOUS USER ######################################## - array( + # ## ANONYMOUS USER ######################################## + [ 'JohnDoe', + . 'class="mw-userlink mw-anonuserlink" ' + . 'title="Special:Contributions/JohnDoe">JohnDoe', 0, 'JohnDoe', false, - ), - array( + ], + [ '::1', + . 'class="mw-userlink mw-anonuserlink" ' + . 'title="Special:Contributions/::1">::1', 0, '::1', false, 'Anonymous with pretty IPv6' - ), - array( + ], + [ '::1', + . 'class="mw-userlink mw-anonuserlink" ' + . 'title="Special:Contributions/0:0:0:0:0:0:0:1">::1', 0, '0:0:0:0:0:0:0:1', false, 'Anonymous with almost pretty IPv6' - ), - array( + ], + [ '::1', + . 'class="mw-userlink mw-anonuserlink" ' + . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001">::1', 0, '0000:0000:0000:0000:0000:0000:0000:0001', false, 'Anonymous with full IPv6' - ), - array( + ], + [ 'AlternativeUsername', + . 'class="mw-userlink mw-anonuserlink" ' + . 'title="Special:Contributions/::1">AlternativeUsername', 0, '::1', 'AlternativeUsername', 'Anonymous with pretty IPv6 and an alternative username' - ), + ], # IPV4 - array( + [ '127.0.0.1', + . 'class="mw-userlink mw-anonuserlink" ' + . 'title="Special:Contributions/127.0.0.1">127.0.0.1', 0, '127.0.0.1', false, 'Anonymous with IPv4' - ), - array( + ], + [ 'AlternativeUsername', + . 'class="mw-userlink mw-anonuserlink" ' + . 'title="Special:Contributions/127.0.0.1">AlternativeUsername', 0, '127.0.0.1', 'AlternativeUsername', 'Anonymous with IPv4 and an alternative username' - ), + ], - ### Regular user ########################################## + # ## Regular user ########################################## # TODO! - ); + ]; } /** @@ -93,27 +96,28 @@ class LinkerTest extends MediaWikiLangTestCase { * @covers Linker::formatAutocomments * @covers Linker::formatLinksInComment */ - public function testFormatComment( $expected, $comment, $title = false, $local = false, $wikiId = null ) { + public function testFormatComment( + $expected, $comment, $title = false, $local = false, $wikiId = null + ) { $conf = new SiteConfiguration(); - $conf->settings = array( - 'wgServer' => array( + $conf->settings = [ + 'wgServer' => [ 'enwiki' => '//en.example.org', 'dewiki' => '//de.example.org', - ), - 'wgArticlePath' => array( + ], + 'wgArticlePath' => [ 'enwiki' => '/w/$1', 'dewiki' => '/w/$1', - ), - ); - $conf->suffixes = array( 'wiki' ); + ], + ]; + $conf->suffixes = [ 'wiki' ]; - $this->setMwGlobals( array( + $this->setMwGlobals( [ 'wgScript' => '/wiki/index.php', 'wgArticlePath' => '/wiki/$1', - 'wgWellFormedXml' => true, 'wgCapitalLinks' => true, 'wgConf' => $conf, - ) ); + ] ); if ( $title === false ) { // We need a page title that exists @@ -129,129 +133,131 @@ class LinkerTest extends MediaWikiLangTestCase { public function provideCasesForFormatComment() { $wikiId = 'enwiki'; // $wgConf has a fake entry for this - return array( + // @codingStandardsIgnoreStart Generic.Files.LineLength + return [ // Linker::formatComment - array( + [ 'a<script>b', 'a */" - ), - array( + ], + [ '→‎autocomment', "/* autocomment */", false, true - ), - array( + ], + [ '‎autocomment', "/* autocomment */", null - ), - array( + ], + [ '→‎autocomment', "/* autocomment */", false, false - ), - array( + ], + [ '→‎autocomment', "/* autocomment */", false, false, $wikiId - ), + ], // Linker::formatLinksInComment - array( + [ 'abc link def', "abc [[link]] def", - ), - array( + ], + [ 'abc text def', "abc [[link|text]] def", - ), - array( + ], + [ 'abc Special:BlankPage def', "abc [[Special:BlankPage|]] def", - ), - array( + ], + [ 'abc ąśż def', "abc [[%C4%85%C5%9B%C5%BC]] def", - ), - array( + ], + [ 'abc #section def', "abc [[#section]] def", - ), - array( + ], + [ 'abc /subpage def', "abc [[/subpage]] def", - ), - array( + ], + [ 'abc "evil!" def', "abc [[\"evil!\"]] def", - ), - array( + ], + [ 'abc [[<script>very evil</script>]] def', "abc [[]] def", - ), - array( + ], + [ 'abc [[|]] def', "abc [[|]] def", - ), - array( + ], + [ 'abc link def', "abc [[link]] def", false, false - ), - array( + ], + [ 'abc link def', "abc [[link]] def", false, false, $wikiId - ) - ); + ], + ]; + // @codingStandardsIgnoreEnd } /** @@ -261,22 +267,21 @@ class LinkerTest extends MediaWikiLangTestCase { public function testFormatLinksInComment( $expected, $input, $wiki ) { $conf = new SiteConfiguration(); - $conf->settings = array( - 'wgServer' => array( + $conf->settings = [ + 'wgServer' => [ 'enwiki' => '//en.example.org' - ), - 'wgArticlePath' => array( + ], + 'wgArticlePath' => [ 'enwiki' => '/w/$1', - ), - ); - $conf->suffixes = array( 'wiki' ); - $this->setMwGlobals( array( + ], + ]; + $conf->suffixes = [ 'wiki' ]; + $this->setMwGlobals( [ 'wgScript' => '/wiki/index.php', 'wgArticlePath' => '/wiki/$1', - 'wgWellFormedXml' => true, 'wgCapitalLinks' => true, 'wgConf' => $conf, - ) ); + ] ); $this->assertEquals( $expected, @@ -285,22 +290,188 @@ class LinkerTest extends MediaWikiLangTestCase { } public static function provideCasesForFormatLinksInComment() { - return array( - array( + // @codingStandardsIgnoreStart Generic.Files.LineLength + return [ + [ 'foo bar Special:BlankPage', 'foo bar [[Special:BlankPage]]', null, - ), - array( + ], + [ 'Foo\'bar', "[[Foo'bar]]", 'enwiki', - ), - array( + ], + [ 'foo bar Special:BlankPage', 'foo bar [[Special:BlankPage]]', 'enwiki', - ), + ], + ]; + // @codingStandardsIgnoreEnd + } + + public static function provideLinkBeginHook() { + // @codingStandardsIgnoreStart 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' + ], + ]; + // @codingStandardsIgnoreEnd + } + + /** + * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook + * @dataProvider provideLinkBeginHook + */ + public function testLinkBeginHook( $callback, $expected ) { + $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->setMwGlobals( [ + 'wgArticlePath' => '/wiki/$1', + ] ); + + $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] ); + + $title = SpecialPage::getTitleFor( 'Blankpage' ); + $out = Linker::link( $title ); + $this->assertEquals( $expected, $out ); + } + + /** + * @covers Linker::getLinkColour + */ + public function testGetLinkColour() { + $this->hideDeprecated( 'Linker::getLinkColour' ); + $linkCache = MediaWikiServices::getInstance()->getLinkCache(); + $foobarTitle = Title::makeTitle( NS_MAIN, 'FooBar' ); + $redirectTitle = Title::makeTitle( NS_MAIN, 'Redirect' ); + $userTitle = Title::makeTitle( NS_USER, 'Someuser' ); + $linkCache->addGoodLinkObj( + 1, // id + $foobarTitle, + 10, // len + 0 // redir + ); + $linkCache->addGoodLinkObj( + 2, // id + $redirectTitle, + 10, // len + 1 // redir + ); + + $linkCache->addGoodLinkObj( + 3, // id + $userTitle, + 10, // len + 0 // redir + ); + + $this->assertEquals( + '', + Linker::getLinkColour( $foobarTitle, 0 ) + ); + + $this->assertEquals( + 'stub', + Linker::getLinkColour( $foobarTitle, 20 ) + ); + + $this->assertEquals( + 'mw-redirect', + Linker::getLinkColour( $redirectTitle, 0 ) + ); + + $this->assertEquals( + '', + Linker::getLinkColour( $userTitle, 20 ) ); } }