Thiemo Mättig (WMDE) has submitted this change and it was merged.

Change subject: Refactor claims code out of EntityView
......................................................................


Refactor claims code out of EntityView

All code related to claims is moved into a separate ClaimsView which only
knows about a list of claims. This helps us getting rid of EntityView.

Note that lots of test code is copied from EntityViewTest which however
will be deleted once we remove EntityView so that there is no duplicate
code in the long term. Furthermore, the tests do not actually test
anything further than that the output is actually HTML. The ClaimsViewTest
can also be improved in another patch as this one would grow too big.

A problem at the moment is the ClaimHtmlGenerator which also accepts
statements which extend claims at the moment. However, when this will
change we may think of renaming this to StatementsView and only accept
statements. Or we find another way of using this to display both claims
and statements.

In future ClaimsView should perhaps only be created in ItemView and be
injected via the constructor. However, this can be done in another patch
as this one would get too big. To actually kill EntityView we have to
modify the Content classes (EntityContent, ItemContent and
PropertyContent) atm..

Change-Id: Iee15ec58f69abac9fb6a85c23d6196a309d1a651
---
M repo/includes/EntityView.php
M repo/includes/ItemView.php
M repo/includes/PropertyView.php
A repo/includes/View/ClaimsView.php
M repo/tests/phpunit/includes/EntityViewTest.php
A repo/tests/phpunit/includes/View/ClaimsViewTest.php
6 files changed, 465 insertions(+), 249 deletions(-)

Approvals:
  WikidataJenkins: Verified
  Thiemo Mättig (WMDE): Looks good to me, approved
  jenkins-bot: Checked



diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php
index dda755f..4d56a66 100644
--- a/repo/includes/EntityView.php
+++ b/repo/includes/EntityView.php
@@ -6,13 +6,13 @@
 use Html;
 use IContextSource;
 use InvalidArgumentException;
-use Linker;
 use ParserOutput;
 use Wikibase\DataModel\SiteLinkList;
 use Wikibase\Lib\PropertyDataTypeLookup;
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\SnakFormatter;
 use Wikibase\Lib\Store\EntityInfoBuilderFactory;
+use Wikibase\Repo\View\ClaimsView;
 use Wikibase\Repo\View\FingerprintView;
 use Wikibase\Repo\View\SectionEditLinkGenerator;
 use Wikibase\Repo\View\SnakHtmlGenerator;
@@ -66,9 +66,9 @@
        protected $configBuilder;
 
        /**
-        * @var ClaimHtmlGenerator
+        * @var ClaimsView
         */
-       protected $claimHtmlGenerator;
+       protected $claimsView;
 
        /**
         * @var FingerprintView
@@ -137,9 +137,17 @@
                        $entityTitleLookup
                );
 
-               $this->claimHtmlGenerator = new ClaimHtmlGenerator(
+               $claimHtmlGenerator = new ClaimHtmlGenerator(
                        $snakHtmlGenerator,
                        $entityTitleLookup
+               );
+
+               $this->claimsView =  new ClaimsView(
+                       $entityInfoBuilderFactory,
+                       $entityTitleLookup,
+                       $this->sectionEditLinkGenerator,
+                       $claimHtmlGenerator,
+                       $this->getLanguage()->getCode()
                );
 
                $this->fingerprintView = new FingerprintView(
@@ -246,7 +254,6 @@
                $html .= $this->getHtmlForFingerprint( $entity, $editable );
                $html .= $this->getHtmlForToc();
                $html .= $this->getHtmlForTermBox( $entityRevision, $editable );
-               $html .= $this->getHtmlForClaims( $entity, $editable );
 
                wfProfileOut( __METHOD__ );
                return $html;
@@ -261,6 +268,16 @@
         */
        protected function getHtmlForFingerprint( Entity $entity, $editable = 
true ) {
                return $this->fingerprintView->getHtml( 
$entity->getFingerprint(), $entity->getId(), $editable );
+       }
+
+       /**
+        * Builds and returns the HTML for the entity's claims.
+        *
+        * @param Enttiy $entity
+        * @return string
+        */
+       protected function getHtmlForClaims( Entity $entity ) {
+               return $this->claimsView->getHtml( $entity->getClaims(), 
'wikibase-claims' );
        }
 
        /**
@@ -414,161 +431,6 @@
                                $pout->addLink( 
$this->entityTitleLookup->getTitleForID( $badge ) );
                        }
                }
-       }
-
-       /**
-        * Returns the HTML for the heading of the claims section
-        *
-        * @since 0.5
-        *
-        * @param Entity $entity
-        * @param bool $editable
-        *
-        * @return string
-        */
-       protected function getHtmlForClaimsSectionHeading( Entity $entity, 
$editable = true ) {
-               $html = wfTemplate(
-                       'wb-section-heading',
-                       wfMessage( 'wikibase-claims' ),
-                       'claims' // ID - TODO: should not be added if output 
page is not the entity's page
-               );
-
-               return $html;
-       }
-
-       /**
-        * Builds and returns the HTML representing a WikibaseEntity's claims.
-        *
-        * @since 0.2
-        *
-        * @param Entity $entity the entity to render
-        * @param bool $editable whether editing is allowed (enabled edit links)
-        * @return string
-        */
-       public function getHtmlForClaims( Entity $entity, $editable = true ) {
-               wfProfileIn( __METHOD__ );
-
-               $claims = $entity->getClaims();
-
-               $html = $this->getHtmlForClaimsSectionHeading( $entity, 
$editable );
-
-               // aggregate claims by properties
-               $claimsByProperty = array();
-               foreach ( $claims as $claim ) {
-                       $propertyId = $claim->getMainSnak()->getPropertyId();
-                       $claimsByProperty[$propertyId->getNumericId()][] = 
$claim;
-               }
-
-               $entityInfo = $this->getEntityInfo( $entity, 
$this->getLanguage()->getCode() );
-
-               /**
-                * @var string $claimsHtml
-                * @var Claim[] $claims
-                */
-               $claimsHtml = '';
-
-               foreach ( $claimsByProperty as $claims ) {
-                       $propertyHtml = '';
-
-                       $propertyId = 
$claims[0]->getMainSnak()->getPropertyId();
-                       $key = $propertyId->getSerialization();
-                       $propertyLabel = $key;
-                       if ( isset( $entityInfo[$key] ) && !empty( 
$entityInfo[$key]['labels'] ) ) {
-                               $entityInfoLabel = reset( 
$entityInfo[$key]['labels'] );
-                               $propertyLabel = $entityInfoLabel['value'];
-                       }
-
-                       $propertyLink = Linker::link(
-                               $this->entityTitleLookup->getTitleForId( 
$propertyId ),
-                               htmlspecialchars( $propertyLabel )
-                       );
-
-                       $htmlForEditSection = $this->getHtmlForEditSection( '', 
array() ); // TODO: add link to SpecialPage
-
-                       foreach ( $claims as $claim ) {
-                               $propertyHtml .= 
$this->claimHtmlGenerator->getHtmlForClaim(
-                                       $claim,
-                                       $entityInfo,
-                                       $htmlForEditSection
-                               );
-                       }
-
-                       $toolbarHtml = wfTemplate( 'wikibase-toolbar',
-                               'wb-addtoolbar',
-                               // TODO: add link to SpecialPage
-                               $this->getHtmlForEditSection( '', array(), 
'add' )
-                       );
-
-                       $claimsHtml .= wfTemplate( 'wb-claimlistview',
-                               $propertyHtml,
-                               wfTemplate( 'wb-claimgrouplistview-groupname', 
$propertyLink ) . $toolbarHtml,
-                               $propertyId->getSerialization()
-                       );
-
-               }
-
-               $claimgrouplistviewHtml = wfTemplate( 'wb-claimgrouplistview', 
$claimsHtml, '' );
-
-               // TODO: Add link to SpecialPage that allows adding a new claim.
-               $html = $html . wfTemplate( 'wb-claimlistview', 
$claimgrouplistviewHtml, '', '' );
-
-               wfProfileOut( __METHOD__ );
-               return $html;
-       }
-
-       /**
-        * Returns a toolbar with an edit link for a single statement. 
Equivalent to edit toolbar in JavaScript but with
-        * an edit link pointing to a special page where the statement can be 
edited. In case JavaScript is available, this
-        * toolbar will be removed an replaced with the interactive JavaScript 
one.
-        *
-        * @param string $specialPage specifies the special page
-        * @param string[] $specialPageParams specifies additional params for 
the special page
-        * @param string $action by default 'edit', for aliases this could also 
be 'add'
-        *
-        * @return string
-        */
-       private function getHtmlForEditSection( $specialPage, array 
$specialPageParams, $action = 'edit' ) {
-               $key = $action === 'add' ? 'wikibase-add' : 'wikibase-edit';
-               $message = $this->getContext()->msg( $key );
-
-               return $this->sectionEditLinkGenerator->getHtmlForEditSection(
-                       $specialPage,
-                       $specialPageParams,
-                       $message
-               );
-       }
-
-       /**
-        * Fetches labels and descriptions for all entities used as properties 
in snaks in the given
-        * entity.
-        *
-        * @param Entity $entity
-        * @param string $languageCode the language code of the labels to fetch.
-        *
-        * @return array[] Entity info array that maps property IDs to labels 
and descriptions.
-        */
-       protected function getEntityInfo( Entity $entity, $languageCode ) {
-               wfProfileIn( __METHOD__ );
-               // TODO: Share cache with PropertyLabelResolver
-               // TODO: ... or share info with getBasicEntityInfo.
-
-               // TODO: Make a finder just for properties, so we don't have to 
filter.
-               $refFinder = new ReferencedEntitiesFinder();
-               $entityIds = $refFinder->findSnakLinks( $entity->getAllSnaks() 
);
-               $propertyIds = array_filter( $entityIds, function ( EntityId 
$id ) {
-                       return $id->getEntityType() === Property::ENTITY_TYPE;
-               } );
-
-               // NOTE: This is a bit hackish, it would be more appropriate to 
use a TermTable here.
-               $entityInfoBuilder = 
$this->entityInfoBuilderFactory->newEntityInfoBuilder( $propertyIds );
-               $entityInfoBuilder->removeMissing();
-               $entityInfoBuilder->collectTerms(
-                       array( 'label', 'description' ),
-                       array( $languageCode )
-               );
-
-               wfProfileOut( __METHOD__ );
-               return $entityInfoBuilder->getEntityInfo();
        }
 
 }
diff --git a/repo/includes/ItemView.php b/repo/includes/ItemView.php
index ec4f80d..f332e3b 100644
--- a/repo/includes/ItemView.php
+++ b/repo/includes/ItemView.php
@@ -2,8 +2,8 @@
 
 namespace Wikibase;
 
-use Wikibase\Repo\WikibaseRepo;
 use Wikibase\Repo\View\SiteLinksView;
+use Wikibase\Repo\WikibaseRepo;
 
 /**
  * Class for creating views for Wikibase\Item instances.
@@ -30,23 +30,10 @@
        }
 
        /**
-        * Returns the HTML for the heading of the claims section
-        *
-        * @since 0.5
-        *
-        * @param Entity $entity
-        * @param bool $editable
-        *
-        * @return string
+        * @see EntityView::getHtmlForClaims
         */
-       protected function getHtmlForClaimsSectionHeading( Entity $entity, 
$editable = true ) {
-               $html = wfTemplate(
-                       'wb-section-heading',
-                       wfMessage( 'wikibase-statements' ),
-                       'claims' // ID - TODO: should not be added if output 
page is not the entity's page
-               );
-
-               return $html;
+       protected function getHtmlForClaims( Entity $entity ) {
+               return $this->claimsView->getHtml( $entity->getClaims(), 
'wikibase-statements' );
        }
 
        /**
diff --git a/repo/includes/PropertyView.php b/repo/includes/PropertyView.php
index 8ceb012..7d0dfec 100644
--- a/repo/includes/PropertyView.php
+++ b/repo/includes/PropertyView.php
@@ -46,7 +46,7 @@
                $html .= $this->getHtmlForDataType( $this->getDataType( 
$property ) );
 
                if ( defined( 'WB_EXPERIMENTAL_FEATURES' ) && 
WB_EXPERIMENTAL_FEATURES ) {
-                       $html .= $this->getHtmlForClaims( $property, $editable 
);
+                       $html .= $this->getHtmlForClaims( $property );
                }
 
                $footer = $this->msg( 'wikibase-property-footer' );
@@ -60,23 +60,10 @@
        }
 
        /**
-        * Returns the HTML for the heading of the claims section
-        *
-        * @since 0.5
-        *
-        * @param Entity $entity
-        * @param bool $editable
-        *
-        * @return string
+        * @see EntityView::getHtmlForClaims
         */
-       protected function getHtmlForClaimsSectionHeading( Entity $entity, 
$editable = true ) {
-               $html = wfTemplate(
-                       'wb-section-heading',
-                       wfMessage( 'wikibase-attributes' )->escaped(),
-                       'claims' // ID - TODO: should not be added if output 
page is not the entity's page
-               );
-
-               return $html;
+       protected function getHtmlForClaims( Entity $entity ) {
+               return $this->claimsView->getHtml( $entity->getClaims(), 
'wikibase-attributes' );
        }
 
        private function getDataType( Property $property ) {
diff --git a/repo/includes/View/ClaimsView.php 
b/repo/includes/View/ClaimsView.php
new file mode 100644
index 0000000..18a1d92
--- /dev/null
+++ b/repo/includes/View/ClaimsView.php
@@ -0,0 +1,222 @@
+<?php
+
+namespace Wikibase\Repo\View;
+
+use Linker;
+use Wikibase\ClaimHtmlGenerator;
+use Wikibase\DataModel\Claim\Claim;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\Property;
+use Wikibase\DataModel\Snak\Snak;
+use Wikibase\EntityTitleLookup;
+use Wikibase\Lib\Store\EntityInfoBuilderFactory;
+use Wikibase\ReferencedEntitiesFinder;
+
+/**
+ * Generates HTML to display claims.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < benestar.wikime...@gmail.com >
+ */
+class ClaimsView {
+
+       /**
+        * @var EntityInfoBuilderFactory
+        */
+       private $entityInfoBuilderFactory;
+
+       /**
+        * @var EntityTitleLookup
+        */
+       private $entityTitleLookup;
+
+       /**
+        * @var SectionEditLinkGenerator
+        */
+       private $sectionEditLinkGenerator;
+
+       /**
+        * @var ClaimHtmlGenerator
+        */
+       private $claimHtmlGenerator;
+
+       /**
+        * @var string
+        */
+       private $languageCode;
+
+       /**
+        * @param EntityInfoBuilderFactory $entityInfoBuilderFactory
+        * @param EnttiyTitleLookup $entityTitleLookup
+        * @param SectionEditLinkGenerator $sectionEditLinkGenerator
+        * @param ClaimHtmlGenerator $claimHtmlGenerator
+        * @param string $languageCode
+        */
+       public function __construct(
+               EntityInfoBuilderFactory $entityInfoBuilderFactory,
+               EntityTitleLookup $entityTitleLookup,
+               SectionEditLinkGenerator $sectionEditLinkGenerator,
+               ClaimHtmlGenerator $claimHtmlGenerator,
+               $languageCode
+       ) {
+               $this->entityInfoBuilderFactory = $entityInfoBuilderFactory;
+               $this->entityTitleLookup = $entityTitleLookup;
+               $this->sectionEditLinkGenerator = $sectionEditLinkGenerator;
+               $this->claimHtmlGenerator = $claimHtmlGenerator;
+               $this->languageCode = $languageCode;
+       }
+
+       /**
+        * Builds and returns the HTML representing a WikibaseEntity's claims.
+        *
+        * @since 0.5
+        *
+        * @param Claim[] $claims the claims to render
+        * @param string $heading the message key of the heading
+        * @return string
+        */
+       public function getHtml( array $claims, $heading = 'wikibase-claims' ) {
+               // aggregate claims by properties
+               $claimsByProperty = $this->groupClaimsByProperties( $claims );
+               $entityInfo = $this->getEntityInfo( $claims, 
$this->languageCode );
+
+               $claimsHtml = '';
+               foreach ( $claimsByProperty as $claims ) {
+                       $claimsHtml .= $this->getHtmlForClaimGroup( $claims, 
$entityInfo );
+               }
+
+               $claimgrouplistviewHtml = wfTemplate( 'wb-claimgrouplistview', 
$claimsHtml, '' );
+
+               // TODO: Add link to SpecialPage that allows adding a new claim.
+               $sectionHeading = $this->getHtmlForSectionHeading( $heading );
+               $html = wfTemplate( 'wb-claimlistview', 
$claimgrouplistviewHtml, '', '' );
+               return $sectionHeading . $html;
+       }
+
+       /**
+        * Returns the HTML for the heading of the statements section
+        *
+        * @return string
+        */
+       private function getHtmlForSectionHeading( $heading ) {
+               $html = wfTemplate(
+                       'wb-section-heading',
+                       wfMessage( $heading )->escaped(),
+                       'claims' // ID - TODO: should not be added if output 
page is not the entity's page
+               );
+
+               return $html;
+       }
+
+       /**
+        * Groups claims by their properties.
+        *
+        * @param Claim[] $claims
+        * @return Claim[][]
+        */
+       private function groupClaimsByProperties( array $claims ) {
+               $claimsByProperty = array();
+               /** @var Claim $claim */
+               foreach ( $claims as $claim ) {
+                       $propertyId = $claim->getMainSnak()->getPropertyId();
+                       $claimsByProperty[$propertyId->getNumericId()][] = 
$claim;
+               }
+               return $claimsByProperty;
+       }
+
+       /**
+        * Fetches labels and descriptions for all entities used as properties 
in snaks in the given
+        * entity.
+        *
+        * @param Snak[] $claims
+        * @param string $languageCode the language code of the labels to fetch.
+        * @return array[] Entity info array that maps property IDs to labels 
and descriptions.
+        */
+       private function getEntityInfo( array $claims, $languageCode ) {
+               // TODO: Share cache with PropertyLabelResolver
+               // TODO: ... or share info with getBasicEntityInfo.
+
+               // TODO: Make a finder just for properties, so we don't have to 
filter.
+               $refFinder = new ReferencedEntitiesFinder();
+               $snaks = $this->getSnaksFromClaims( $claims );
+               $entityIds = $refFinder->findSnakLinks( $snaks );
+               $propertyIds = array_filter( $entityIds, function ( EntityId 
$id ) {
+                       return $id->getEntityType() === Property::ENTITY_TYPE;
+               } );
+
+               // NOTE: This is a bit hackish, it would be more appropriate to 
use a TermTable here.
+               $entityInfoBuilder = 
$this->entityInfoBuilderFactory->newEntityInfoBuilder( $propertyIds );
+               $entityInfoBuilder->removeMissing();
+               $entityInfoBuilder->collectTerms(
+                       array( 'label', 'description' ),
+                       array( $languageCode )
+               );
+
+               return $entityInfoBuilder->getEntityInfo();
+       }
+
+       /**
+        * Returns all snaks which are stored in this list of claims.
+        *
+        * @param Claim[] $claims
+        * @return Snak[]
+        */
+       private function getSnaksFromClaims( array $claims ) {
+               $snaks = array();
+               /** @var Claim $claim */
+               foreach ( $claims as $claim ) {
+                       $snaks = array_merge( $snaks, $claim->getAllSnaks() );
+               }
+               return $snaks;
+       }
+
+       /**
+        * Returns the HTML for a group of claims.
+        *
+        * @param Claim[] $claims
+        * @param array $entityInfo
+        * @return string
+        */
+       private function getHtmlForClaimGroup( array $claims, array $entityInfo 
) {
+               $propertyHtml = '';
+
+               $propertyId = $claims[0]->getMainSnak()->getPropertyId();
+               $key = $propertyId->getSerialization();
+               $propertyLabel = $key;
+               if ( isset( $entityInfo[$key] ) && !empty( 
$entityInfo[$key]['labels'] ) ) {
+                       $entityInfoLabel = reset( $entityInfo[$key]['labels'] );
+                       $propertyLabel = $entityInfoLabel['value'];
+               }
+
+               $propertyLink = Linker::link(
+                       $this->entityTitleLookup->getTitleForId( $propertyId ),
+                       htmlspecialchars( $propertyLabel )
+               );
+
+               // TODO: add link to SpecialPage
+               $htmlForEditSection = 
$this->sectionEditLinkGenerator->getHtmlForEditSection( '', array(), wfMessage( 
'wikibase-edit' ) );
+
+               foreach ( $claims as $claim ) {
+                       $propertyHtml .= 
$this->claimHtmlGenerator->getHtmlForClaim(
+                               $claim,
+                               $entityInfo,
+                               $htmlForEditSection
+                       );
+               }
+
+               $toolbarHtml = wfTemplate( 'wikibase-toolbar',
+                       'wb-addtoolbar',
+                       // TODO: add link to 
SpecialPage$this->sectionEditLinkGenerator
+                       $this->sectionEditLinkGenerator->getHtmlForEditSection( 
'', array(), wfMessage( 'wikibase-add' ) )
+               );
+
+               return wfTemplate( 'wb-claimlistview',
+                       $propertyHtml,
+                       wfTemplate( 'wb-claimgrouplistview-groupname', 
$propertyLink ) . $toolbarHtml,
+                       $propertyId->getSerialization()
+               );
+       }
+
+}
diff --git a/repo/tests/phpunit/includes/EntityViewTest.php 
b/repo/tests/phpunit/includes/EntityViewTest.php
index f275039..3ad2a5e 100644
--- a/repo/tests/phpunit/includes/EntityViewTest.php
+++ b/repo/tests/phpunit/includes/EntityViewTest.php
@@ -242,61 +242,6 @@
        }
 
        /**
-        * @return array
-        */
-       public function getHtmlForClaimsProvider() {
-               $item = $this->makeEntity( $this->makeEntityId( 33 ), array(
-                       $this->makeClaim( new PropertyNoValueSnak(
-                               new PropertyId( 'P11' )
-                       ) ),
-                       $this->makeClaim( new PropertyValueSnak(
-                               new PropertyId( 'P11' ),
-                               new EntityIdValue( new ItemId( 'Q22' ) )
-                       ) ),
-                       $this->makeClaim( new PropertyValueSnak(
-                               new PropertyId( 'P23' ),
-                               new StringValue( 'test' )
-                       ) ),
-               ) );
-
-               return array(
-                       array( $item )
-               );
-       }
-
-       /**
-        * @dataProvider getHtmlForClaimsProvider
-        *
-        * @param Entity $entity
-        */
-       public function testGetHtmlForClaims( Entity $entity ) {
-               $entityView = $this->newEntityView( $entity->getType() );
-
-               $lang = Language::factory( 'en' );
-
-               // Using a DOM document to parse HTML output:
-               $doc = new \DOMDocument();
-
-               // Disable default error handling in order to catch warnings 
caused by malformed markup:
-               libxml_use_internal_errors( true );
-
-               // Try loading the HTML:
-               $this->assertTrue( $doc->loadHTML( 
$entityView->getHtmlForClaims( $entity, $lang ) ) );
-
-               // Check if no warnings have been thrown:
-               $errorString = '';
-               foreach( libxml_get_errors() as $error ) {
-                       $errorString .= "\r\n" . $error->message;
-               }
-
-               $this->assertEmpty( $errorString, 'Malformed markup:' . 
$errorString );
-
-               // Clear error cache and re-enable default error handling:
-               libxml_clear_errors();
-               libxml_use_internal_errors();
-       }
-
-       /**
         * @dataProvider parserOutputExtensionDataProvider
         */
        public function testParserOutputExtensionData( EntityRevision $revision 
) {
diff --git a/repo/tests/phpunit/includes/View/ClaimsViewTest.php 
b/repo/tests/phpunit/includes/View/ClaimsViewTest.php
new file mode 100644
index 0000000..8a5121f
--- /dev/null
+++ b/repo/tests/phpunit/includes/View/ClaimsViewTest.php
@@ -0,0 +1,213 @@
+<?php
+
+namespace Wikibase\Test;
+
+use DataValues\StringValue;
+use DOMDocument;
+use Title;
+use ValueFormatters\FormatterOptions;
+use Wikibase\ClaimHtmlGenerator;
+use Wikibase\DataModel\Claim\Claim;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\Property;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Snak\Snak;
+use Wikibase\EntityTitleLookup;
+use Wikibase\Lib\SnakFormatter;
+use Wikibase\Repo\View\ClaimsView;
+use Wikibase\Repo\View\SectionEditLinkGenerator;
+use Wikibase\Repo\View\SnakHtmlGenerator;
+use Wikibase\Repo\WikibaseRepo;
+
+/**
+ * @covers Wikibase\Repo\View\ClaimsView
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ *
+ * @group Database
+ *             ^---- needed because we rely on Title objects internally
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < benestar.wikime...@gmail.com >
+ */
+class ClaimsViewTest extends \MediaWikiLangTestCase {
+
+       public function getTitleForId( EntityId $id ) {
+               $name = $id->getEntityType() . ':' . $id->getPrefixedId();
+               return Title::makeTitle( NS_MAIN, $name );
+       }
+
+       public function getHtmlProvider() {
+               $claims = array(
+                       $this->makeClaim( new PropertyNoValueSnak(
+                               new PropertyId( 'P11' )
+                       ) ),
+                       $this->makeClaim( new PropertyValueSnak(
+                               new PropertyId( 'P11' ),
+                               new EntityIdValue( new ItemId( 'Q22' ) )
+                       ) ),
+                       $this->makeClaim( new PropertyValueSnak(
+                               new PropertyId( 'P23' ),
+                               new StringValue( 'test' )
+                       ) ),
+               );
+
+               return array(
+                       array( $claims )
+               );
+       }
+
+       /**
+        * @dataProvider getHtmlProvider
+        *
+        * @param Claim[] $claims
+        */
+       public function testGetHtml( array $claims ) {
+               $claimsView = $this->newClaimsView();
+
+               // Using a DOM document to parse HTML output:
+               $doc = new DOMDocument();
+
+               // Disable default error handling in order to catch warnings 
caused by malformed markup:
+               libxml_use_internal_errors( true );
+
+               // Try loading the HTML:
+               $this->assertTrue( $doc->loadHTML( $claimsView->getHtml( 
$claims ) ) );
+
+               // Check if no warnings have been thrown:
+               $errorString = '';
+               foreach( libxml_get_errors() as $error ) {
+                       $errorString .= "\r\n" . $error->message;
+               }
+
+               $this->assertEmpty( $errorString, 'Malformed markup:' . 
$errorString );
+
+               // Clear error cache and re-enable default error handling:
+               libxml_clear_errors();
+               libxml_use_internal_errors();
+       }
+
+       /**
+        * @return ClaimsView
+        */
+       private function newClaimsView() {
+               $formatterOptions = new FormatterOptions();
+               $snakFormatter = 
WikibaseRepo::getDefaultInstance()->getSnakFormatterFactory()
+                       ->getSnakFormatter( SnakFormatter::FORMAT_HTML_WIDGET, 
$formatterOptions );
+
+               $entityTitleLookup = $this->getEntityTitleLookupMock();
+
+               $snakHtmlGenerator = new SnakHtmlGenerator(
+                       $snakFormatter,
+                       $entityTitleLookup
+               );
+
+               $claimHtmlGenerator = new ClaimHtmlGenerator(
+                       $snakHtmlGenerator,
+                       $entityTitleLookup
+               );
+
+               $mockRepo = $this->getMockRepo();
+
+               $sectionEditLinkGenerator = new SectionEditLinkGenerator();
+
+               return new ClaimsView(
+                       $mockRepo,
+                       $entityTitleLookup,
+                       $sectionEditLinkGenerator,
+                       $claimHtmlGenerator,
+                       'en'
+               );
+       }
+
+       /**
+        * @return EntityTitleLookup
+        */
+       private function getEntityTitleLookupMock() {
+               $lookup = $this->getMock( 'Wikibase\EntityTitleLookup' );
+               $lookup->expects( $this->any() )
+                       ->method( 'getTitleForId' )
+                       ->will( $this->returnCallback( array( $this, 
'getTitleForId' ) ) );
+
+               return $lookup;
+       }
+
+       /**
+        * @return MockRepository
+        */
+       private function getMockRepo() {
+               static $mockRepo;
+
+               if ( !isset( $mockRepo ) ) {
+                       $mockRepo = new MockRepository();
+
+                       $mockRepo->putEntity( $this->makeItem( 'Q33' ) );
+                       $mockRepo->putEntity( $this->makeItem( 'Q22' ) );
+                       $mockRepo->putEntity( $this->makeItem( 'Q23' ) );
+                       $mockRepo->putEntity( $this->makeItem( 'Q24' ) );
+
+                       $mockRepo->putEntity( $this->makeProperty( 'P11', 
'wikibase-item' ) );
+                       $mockRepo->putEntity( $this->makeProperty( 'P23', 
'string' ) );
+                       $mockRepo->putEntity( $this->makeProperty( 'P42', 'url' 
) );
+                       $mockRepo->putEntity( $this->makeProperty( 'P44', 
'wikibase-item' ) );
+               }
+
+               return $mockRepo;
+       }
+
+       private function makeItem( $id, $claims = array() ) {
+               if ( is_string( $id ) ) {
+                       $id = new ItemId( $id );
+               }
+
+               $item = Item::newEmpty();
+               $item->setId( $id );
+               $item->setLabel( 'en', "label:$id" );
+               $item->setDescription( 'en', "description:$id" );
+
+               foreach ( $claims as $claim ) {
+                       $item->addClaim( $claim );
+               }
+
+               return $item;
+       }
+
+       private function makeProperty( $id, $dataTypeId, $claims = array() ) {
+               if ( is_string( $id ) ) {
+                       $id = new PropertyId( $id );
+               }
+
+               $property = Property::newFromType( $dataTypeId );
+               $property->setId( $id );
+
+               $property->setLabel( 'en', "label:$id" );
+               $property->setDescription( 'en', "description:$id" );
+
+               foreach ( $claims as $claim ) {
+                       $property->addClaim( $claim );
+               }
+
+               return $property;
+       }
+
+       protected function makeClaim( Snak $mainSnak, $guid = null ) {
+               static $guidCounter = 0;
+
+               if ( $guid === null ) {
+                       $guidCounter++;
+                       $guid = 'EntityViewTest$' . $guidCounter;
+               }
+
+               $claim = new Claim( $mainSnak );
+               $claim->setGuid( $guid );
+
+               return $claim;
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/154322
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iee15ec58f69abac9fb6a85c23d6196a309d1a651
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <benestar.wikime...@gmail.com>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Bene <benestar.wikime...@gmail.com>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to