Tpt has uploaded a new change for review. https://gerrit.wikimedia.org/r/213340
Change subject: [WIP] Display badges on the other project sidebar ...................................................................... [WIP] Display badges on the other project sidebar Bug: T73887 Change-Id: I7a92b54c7b24fb23171d4eec0bd136b19fa8fdb9 --- M client/includes/Hooks/LanguageLinkBadgeDisplay.php M client/includes/Hooks/OtherProjectsSidebarGenerator.php M client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php M client/includes/Hooks/SidebarHookHandlers.php A client/includes/Hooks/SidebarLinkBadgeDisplay.php M client/includes/WikibaseClient.php M client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php M client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php M client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php M client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php M client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php A client/tests/phpunit/includes/Hooks/SidebarLinkBadgeDisplayTest.php 12 files changed, 353 insertions(+), 124 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/40/213340/1 diff --git a/client/includes/Hooks/LanguageLinkBadgeDisplay.php b/client/includes/Hooks/LanguageLinkBadgeDisplay.php index e4189e5..a2d7778 100644 --- a/client/includes/Hooks/LanguageLinkBadgeDisplay.php +++ b/client/includes/Hooks/LanguageLinkBadgeDisplay.php @@ -5,11 +5,8 @@ use Language; use OutputPage; use ParserOutput; -use Sanitizer; use Title; -use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\SiteLink; -use Wikibase\Lib\Store\EntityLookup; /** * Provides access to the badges of the current page's sitelinks @@ -24,29 +21,15 @@ class LanguageLinkBadgeDisplay { /** - * @var EntityLookup + * @var SidebarLinkBadgeDisplay */ - protected $entityLookup; + private $sidebarLinkBadgeDisplay; /** - * @var array + * @param SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay */ - protected $badgeClassNames; - - /** - * @var Language - */ - protected $language; - - /** - * @param EntityLookup $entityLookup - * @param array $badgeClassNames - * @param Language $language - */ - public function __construct( EntityLookup $entityLookup, array $badgeClassNames, Language $language ) { - $this->entityLookup = $entityLookup; - $this->badgeClassNames = $badgeClassNames; - $this->language = $language; + public function __construct( SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay ) { + $this->sidebarLinkBadgeDisplay = $sidebarLinkBadgeDisplay; } /** @@ -65,7 +48,7 @@ $badges = $link->getBadges(); if ( !empty( $badges ) ) { - $badgeInfoForAllLinks[$key] = $this->getBadgeInfo( $badges ); + $badgeInfoForAllLinks[$key] = $this->sidebarLinkBadgeDisplay->getBadgeInfo( $badges ); } } @@ -98,82 +81,6 @@ return; } - /** @var array $linksBadgeInfo an associative array with the keys 'class', and 'itemtitle'. */ - $linksBadgeInfo = $badges[$navId]; - - if ( isset( $languageLink['class'] ) ) { - $languageLink['class'] .= ' ' . $linksBadgeInfo['class']; - } else { - $languageLink['class'] = $linksBadgeInfo['class']; - } - - $languageLink['itemtitle'] = $linksBadgeInfo['label']; + $this->sidebarLinkBadgeDisplay->applyBadgeToLink( $languageLink, $badges[$navId] ); } - - /** - * Builds badge information for the given badges. - * CSS classes are derived from the given list of badges, and any extra badge class - * names specified in the badgeClassNames setting are added. - * For badges that have a such an extra class name assigned, this also - * adds a title according to the items' labels. Other badges do not have labels - * added to the link's title attribute, so the can be effectively ignored - * on this client wiki. - * - * @param ItemId[] $badges - * - * @return array An associative array with the keys 'class' and 'itemtitle' with assigned - * string values. These fields correspond to the fields in the description array for language - * links used by the SkinTemplateGetLanguageLink hook and expected by the applyBadges() - * function. - */ - private function getBadgeInfo( array $badges ) { - $classes = array(); - $labels = array(); - - foreach ( $badges as $badge ) { - $badgeSerialization = $badge->getSerialization(); - $classes[] = 'badge-' . Sanitizer::escapeClass( $badgeSerialization ); - - // nicer classes for well known badges - if ( isset( $this->badgeClassNames[$badgeSerialization] ) ) { - // add class name - $classes[] = Sanitizer::escapeClass( $this->badgeClassNames[$badgeSerialization] ); - - // add label (but only if this badge is well known on this wiki) - $label = $this->getLabel( $badge ); - - if ( $label !== null ) { - $labels[] = $label; - } - } - } - - $info = array( - 'class' => implode( ' ', $classes ), - 'label' => $this->language->commaList( $labels ), - ); - - return $info; - } - - /** - * Returns the label for the given badge. - * - * @param ItemId $badge - * - * @return string|null - */ - private function getLabel( ItemId $badge ) { - $entity = $this->entityLookup->getEntity( $badge ); - if ( !$entity ) { - return null; - } - - $title = $entity->getLabel( $this->language->getCode() ); - if ( !$title ) { - return null; - } - return $title; - } - } diff --git a/client/includes/Hooks/OtherProjectsSidebarGenerator.php b/client/includes/Hooks/OtherProjectsSidebarGenerator.php index 7402428..c1f928f 100644 --- a/client/includes/Hooks/OtherProjectsSidebarGenerator.php +++ b/client/includes/Hooks/OtherProjectsSidebarGenerator.php @@ -40,21 +40,29 @@ private $siteIdsToOutput; /** + * @var SidebarLinkBadgeDisplay + */ + private $sidebarLinkBadgeDisplay; + + /** * @param string $localSiteId * @param SiteLinkLookup $siteLinkLookup * @param SiteStore $siteStore * @param string[] $siteIdsToOutput + * @param SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay */ public function __construct( $localSiteId, SiteLinkLookup $siteLinkLookup, SiteStore $siteStore, - array $siteIdsToOutput + array $siteIdsToOutput, + SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay ) { $this->localSiteId = $localSiteId; $this->siteLinkLookup = $siteLinkLookup; $this->siteStore = $siteStore; $this->siteIdsToOutput = $siteIdsToOutput; + $this->sidebarLinkBadgeDisplay = $sidebarLinkBadgeDisplay; } /** @@ -159,6 +167,11 @@ $attributes['hreflang'] = $siteLanguageCode; } + $this->sidebarLinkBadgeDisplay->applyBadgeToLink( + $attributes, + $this->sidebarLinkBadgeDisplay->getBadgeInfo( $siteLink->getBadges() ) + ); + return $attributes; } diff --git a/client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php b/client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php index 567e59d..eec7b15 100644 --- a/client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php +++ b/client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php @@ -35,18 +35,26 @@ private $siteStore; /** + * @var SidebarLinkBadgeDisplay + */ + private $sidebarLinkBadgeDisplay; + + /** * @param SettingsArray $settings * @param SiteLinkLookup $siteLinkLookup * @param SiteStore $siteStore + * @param SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay */ public function __construct( SettingsArray $settings, SiteLinkLookup $siteLinkLookup, - SiteStore $siteStore + SiteStore $siteStore, + SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay ) { $this->settings = $settings; $this->siteLinkLookup = $siteLinkLookup; $this->siteStore = $siteStore; + $this->sidebarLinkBadgeDisplay = $sidebarLinkBadgeDisplay; } /** @@ -68,7 +76,8 @@ $this->settings->getSetting( 'siteGlobalID' ), $this->siteLinkLookup, $this->siteStore, - $this->settings->getSetting( 'otherProjectsLinks' ) + $this->settings->getSetting( 'otherProjectsLinks' ), + $this->sidebarLinkBadgeDisplay ); } diff --git a/client/includes/Hooks/SidebarHookHandlers.php b/client/includes/Hooks/SidebarHookHandlers.php index 7e82a8d..209d0fd 100644 --- a/client/includes/Hooks/SidebarHookHandlers.php +++ b/client/includes/Hooks/SidebarHookHandlers.php @@ -77,9 +77,11 @@ $badgeClassNames = $settings->getSetting( 'badgeClassNames' ); $badgeDisplay = new LanguageLinkBadgeDisplay( - $entityLookup, - is_array( $badgeClassNames ) ? $badgeClassNames : array(), - $wgLang + new SidebarLinkBadgeDisplay( + $entityLookup, + is_array( $badgeClassNames ) ? $badgeClassNames : array(), + $wgLang + ) ); return new SidebarHookHandlers( diff --git a/client/includes/Hooks/SidebarLinkBadgeDisplay.php b/client/includes/Hooks/SidebarLinkBadgeDisplay.php new file mode 100644 index 0000000..ad5225e --- /dev/null +++ b/client/includes/Hooks/SidebarLinkBadgeDisplay.php @@ -0,0 +1,138 @@ +<?php + +namespace Wikibase\Client\Hooks; + +use Language; +use Sanitizer; +use Wikibase\DataModel\Entity\ItemId; +use Wikibase\Lib\Store\EntityLookup; + +/** + * Basic display logic to output badges in the sidebar + * + * @since 0.5 + * + * @licence GNU GPL v2+ + * @author Bene* < benestar.wikime...@gmail.com > + * @author Daniel Kinzler + * @author Thomas Pellissier Tanon + */ +class SidebarLinkBadgeDisplay { + + /** + * @var EntityLookup + */ + protected $entityLookup; + + /** + * @var array + */ + protected $badgeClassNames; + + /** + * @var Language + */ + protected $language; + + /** + * @param EntityLookup $entityLookup + * @param array $badgeClassNames + * @param Language $language + */ + public function __construct( EntityLookup $entityLookup, array $badgeClassNames, Language $language ) { + $this->entityLookup = $entityLookup; + $this->badgeClassNames = $badgeClassNames; + $this->language = $language; + } + + /** + * Applies the badges described in the wikibase_badges property of $output to + * the language link to $languageLinkTitle. The badge info for this linked is + * looked up in the wikibase_badges data using the key returned by + * $languageLinkTitle->getInterwiki(). + * + * This is generally called in the context of generating skin output. + * + * @since 0.5 + * + * @param array $sidebarLink + * @param array $badgeInfo An associative array with the keys 'class' and 'itemtitle' with assigned + * string values. These fields are the one outputted by the getBadgeInfo() function. + */ + public function applyBadgeToLink( array &$sidebarLink, array $badgeInfo ) { + if ( isset( $sidebarLink['class'] ) ) { + $sidebarLink['class'] .= ' ' . $badgeInfo['class']; + } else { + $sidebarLink['class'] = $badgeInfo['class']; + } + + $sidebarLink['itemtitle'] = $badgeInfo['label']; + } + + /** + * Builds badge information for the given badges. + * CSS classes are derived from the given list of badges, and any extra badge class + * names specified in the badgeClassNames setting are added. + * For badges that have a such an extra class name assigned, this also + * adds a title according to the items' labels. Other badges do not have labels + * added to the link's title attribute, so the can be effectively ignored + * on this client wiki. + * + * @param ItemId[] $badges + * + * @return array An associative array with the keys 'class' and 'itemtitle' with assigned + * string values. These fields correspond to the fields in the description array for language + * links used by the SkinTemplateGetLanguageLink hook and expected by the applyBadgeToLink() + * function. + */ + public function getBadgeInfo( array $badges ) { + $classes = array(); + $labels = array(); + + foreach ( $badges as $badge ) { + $badgeSerialization = $badge->getSerialization(); + $classes[] = 'badge-' . Sanitizer::escapeClass( $badgeSerialization ); + + // nicer classes for well known badges + if ( isset( $this->badgeClassNames[$badgeSerialization] ) ) { + // add class name + $classes[] = Sanitizer::escapeClass( $this->badgeClassNames[$badgeSerialization] ); + + // add label (but only if this badge is well known on this wiki) + $label = $this->getLabel( $badge ); + + if ( $label !== null ) { + $labels[] = $label; + } + } + } + + $info = array( + 'class' => implode( ' ', $classes ), + 'label' => $this->language->commaList( $labels ), + ); + + return $info; + } + + /** + * Returns the label for the given badge. + * + * @param ItemId $badge + * + * @return string|null + */ + private function getLabel( ItemId $badge ) { + $entity = $this->entityLookup->getEntity( $badge ); + if ( !$entity ) { + return null; + } + + $title = $entity->getLabel( $this->language->getCode() ); + if ( !$title ) { + return null; + } + return $title; + } + +} diff --git a/client/includes/WikibaseClient.php b/client/includes/WikibaseClient.php index 1268c67..a86b4ab 100644 --- a/client/includes/WikibaseClient.php +++ b/client/includes/WikibaseClient.php @@ -21,7 +21,7 @@ use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay; use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory; use Wikibase\Client\Hooks\ParserFunctionRegistrant; -use Wikibase\Client\ParserOutputDataUpdater; +use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay; use Wikibase\Client\Store\TitleFactory; use Wikibase\ClientStore; use Wikibase\DataAccess\PropertyIdResolver; @@ -578,12 +578,21 @@ * @return LanguageLinkBadgeDisplay */ public function getLanguageLinkBadgeDisplay() { + return new LanguageLinkBadgeDisplay( + $this->getSidebarLinkBadgeDisplay() + ); + } + + /** + * @return SidebarLinkBadgeDisplay + */ + private function getSidebarLinkBadgeDisplay() { global $wgLang; StubObject::unstub( $wgLang ); $badgeClassNames = $this->settings->getSetting( 'badgeClassNames' ); - return new LanguageLinkBadgeDisplay( + return new SidebarLinkBadgeDisplay( $this->getEntityLookup(), is_array( $badgeClassNames ) ? $badgeClassNames : array(), $wgLang @@ -682,7 +691,8 @@ return new OtherProjectsSidebarGeneratorFactory( $this->settings, $this->getStore()->getSiteLinkLookup(), - $this->getSiteStore() + $this->getSiteStore(), + $this->getSidebarLinkBadgeDisplay() ); } diff --git a/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php b/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php index 6ff4632..803bda2 100644 --- a/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php +++ b/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php @@ -8,6 +8,7 @@ use RequestContext; use Title; use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay; +use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\SiteLink; @@ -69,9 +70,11 @@ $badgeClassNames = array( 'Q4' => 'foo', 'Q3' => 'bar' ); return new LanguageLinkBadgeDisplay( - $entityLookup, - $badgeClassNames, - Language::factory( 'de' ) + new SidebarLinkBadgeDisplay( + $entityLookup, + $badgeClassNames, + Language::factory( 'de' ) + ) ); } diff --git a/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php b/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php index 69e102f..c6e5768 100644 --- a/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php +++ b/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php @@ -32,7 +32,8 @@ $factory = new OtherProjectsSidebarGeneratorFactory( $settings, $siteLinkLookup, - $siteStore + $siteStore, + $this->getMock( 'Wikibase\Client\Hooks\SidebarLinkBadgeDisplay' ) ); $otherProjectSidebarGenerator = $factory->getOtherProjectsSidebarGenerator(); diff --git a/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php b/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php index 4835054..2c0c369 100644 --- a/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php +++ b/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php @@ -33,7 +33,8 @@ 'enwiki', $this->getSiteLinkLookup(), $this->getSiteStore(), - $siteIdsToOutput + $siteIdsToOutput, + $this->getSidebarLinkBadgeDisplay() ); $this->assertEquals( @@ -115,10 +116,31 @@ ->with( $Q123 ) ->will( $this->returnValue( array( new SiteLink( 'enwikiquote', 'Nyan Cat' ), - new SiteLink( 'enwiki', 'Nyan Cat' ), + new SiteLink( 'enwiki', 'Nyan Cat', array( new ItemId( 'Q4242' ) ) ), new SiteLink( 'enwiktionary', 'Nyan Cat' ) ) ) ); return $lookup; } + + private function getSidebarLinkBadgeDisplay() { + $sidebarLinkBadgeDisplay = $this->getMock( 'Wikibase\Client\Hooks\SidebarLinkBadgeDisplay' ); + $sidebarLinkBadgeDisplay->expects( $this->any() ) + ->method( 'getBadgeInfo' ) + ->with( $this->equalsTo( array( new ItemId( 'Q4242' ) ) ) ) + ->will( $this->returnValue( $this->equalsTo( array( 'data' ) ) ) ); + $sidebarLinkBadgeDisplay->expects( $this->any() ) + ->method( 'applyBadgeToLink' ) + ->with( + $this->equalsTo( array( + 'msg' => 'wikibase-otherprojects-wikipedia', + 'class' => 'wb-otherproject-link wb-otherproject-wikipedia', + 'href' => 'https://en.wikipedia.org/wiki/Nyan_Cat', + 'hreflang' => 'en' + ) ), + $this->equalsTo( array( 'data' ) ) + ); + + return $sidebarLinkBadgeDisplay; + } } diff --git a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php index 6997607..bd3e894 100644 --- a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php +++ b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php @@ -15,6 +15,7 @@ use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay; use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory; use Wikibase\Client\Hooks\ParserOutputUpdateHookHandlers; +use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay; use Wikibase\Client\ParserOutputDataUpdater; use Wikibase\Client\Usage\EntityUsage; use Wikibase\Client\WikibaseClient; @@ -165,20 +166,21 @@ $mockRepo = $this->getMockRepo( $links ); $mockRepo->putEntity( $this->getBadgeItem() ); - $badgeDisplay = new LanguageLinkBadgeDisplay( + $sidebardBadgeDisplay = new SidebarLinkBadgeDisplay( $mockRepo, array( 'Q17' => 'featured' ), Language::factory( 'en' ) ); + $languageBadgeDisplay = new LanguageLinkBadgeDisplay( $sidebardBadgeDisplay ); $parserOutputDataUpdater = new ParserOutputDataUpdater( - $this->getOtherProjectsSidebarGeneratorFactory( $settings, $mockRepo ), + $this->getOtherProjectsSidebarGeneratorFactory( $settings, $mockRepo, $sidebardBadgeDisplay ), $mockRepo, $settings->getSetting( 'siteGlobalID' ) ); $langLinkHandler = new LangLinkHandler( - $badgeDisplay, + $languageBadgeDisplay, $namespaceChecker, $mockRepo, $mockRepo, @@ -204,12 +206,14 @@ private function getOtherProjectsSidebarGeneratorFactory( SettingsArray $settings, - SiteLinkLookup $siteLinkLookup + SiteLinkLookup $siteLinkLookup, + SidebarLinkBadgeDisplay $badgeDisplay ) { - return new OtherProjectsSidebarGeneratorFactory( + return new OtherProjectsSidebarGeneratorFactory( $settings, $siteLinkLookup, - $this->getSiteStore() + $this->getSiteStore(), + $badgeDisplay ); } diff --git a/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php b/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php index 4274425..cb1cb70 100644 --- a/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php +++ b/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php @@ -13,6 +13,7 @@ use Wikibase\Client\Hooks\OtherProjectsSidebarGenerator; use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory; use Wikibase\Client\Hooks\SidebarHookHandlers; +use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay; use Wikibase\Client\WikibaseClient; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; @@ -165,9 +166,11 @@ $entityLookup = $this->getEntityLookup( $siteLinksPerItem ); $badgeDisplay = new LanguageLinkBadgeDisplay( - $entityLookup, - array( 'Q17' => 'featured' ), - $en + new SidebarLinkBadgeDisplay( + $entityLookup, + array( 'Q17' => 'featured' ), + $en + ) ); return new SidebarHookHandlers( diff --git a/client/tests/phpunit/includes/Hooks/SidebarLinkBadgeDisplayTest.php b/client/tests/phpunit/includes/Hooks/SidebarLinkBadgeDisplayTest.php new file mode 100644 index 0000000..f94f471 --- /dev/null +++ b/client/tests/phpunit/includes/Hooks/SidebarLinkBadgeDisplayTest.php @@ -0,0 +1,117 @@ +<?php + +namespace Wikibase\Client\Tests\Hooks; + +use Language; +use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay; +use Wikibase\DataModel\Entity\Item; +use Wikibase\DataModel\Entity\ItemId; +use Wikibase\Test\MockRepository; + +/** + * @covers Wikibase\Client\Hooks\SidebarLinkBadgeDisplay + * + * @since 0.5 + * + * @group WikibaseClient + * @group Wikibase + * @group Database + * + * @licence GNU GPL v2+ + * @author Bene* < benestar.wikime...@gmail.com > + * @author Thomas Pellissier Tanon + */ +class SidebarLinkBadgeDisplayTest extends \MediaWikiTestCase { + + private function getItems() { + $items = array(); + + $item = new Item( new ItemId( 'Q3' ) ); + $item->setLabel( 'en', 'Good article' ); + $item->setLabel( 'de', 'Lesenswerter Artikel' ); + $items[] = $item; + + $item = new Item( new ItemId( 'Q4' ) ); + $item->setLabel( 'en', 'Featured article' ); + $item->setLabel( 'de', 'Exzellenter Artikel' ); + $items[] = $item; + + return $items; + } + + /** + * @return SidebarLinkBadgeDisplay + */ + private function getSidebarLinkBadgeDisplay() { + $entityLookup = new MockRepository(); + + foreach ( $this->getItems() as $item ) { + $entityLookup->putEntity( $item ); + } + + $badgeClassNames = array( 'Q4' => 'foo', 'Q3' => 'bar' ); + + return new SidebarLinkBadgeDisplay( + $entityLookup, + $badgeClassNames, + Language::factory( 'de' ) + ); + } + + /** + * @dataProvider getBadgeInfoProvider + */ + public function testGetBadgeInfo( $expected, $badges ) { + $sidebarLinkBadgeDisplay = $this->getSidebarLinkBadgeDisplay(); + + $this->assertEquals( $expected, $sidebarLinkBadgeDisplay->getBadgeInfo( $badges ) ); + } + + public function getBadgeInfoProvider() { + $q2 = new ItemId( 'Q2' ); + $q3 = new ItemId( 'Q3' ); + $q4 = new ItemId( 'Q4' ); + + return array( + array( array(), array() ), + array( + array( + 'class' => 'badge-Q3 bar badge-Q2', + 'label' => 'Lesenswerter Artikel' + ), + array( $q3, $q2 ) + ), + array( + array( + 'class' => 'badge-Q3 bar badge-Q4 foo', + 'label' => 'Lesenswerter Artikel, Exzellenter Artikel' + ), + array( $q3, $q4 ) + ), + ); + } + + public function testApplyBadges() { + $badgeInfo = array( + 'class' => 'badge-Q3', + 'label' => 'Lesenswerter Artikel', + ); + + $link = array( + 'href' => 'http://acme.com', + 'class' => 'foo', + ); + + $expected = array( + 'href' => 'http://acme.com', + 'class' => 'foo badge-Q3', + 'itemtitle' => 'Lesenswerter Artikel', + ); + + $sidebarLinkBadgeDisplay = $this->getSidebarLinkBadgeDisplay(); + $sidebarLinkBadgeDisplay->applyBadgeToLink( $link, $badgeInfo ); + + $this->assertEquals( $expected, $link ); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/213340 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7a92b54c7b24fb23171d4eec0bd136b19fa8fdb9 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Tpt <thoma...@hotmail.fr> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits