jenkins-bot has submitted this change and it was merged.

Change subject: Improve tests for EntityView.
......................................................................


Improve tests for EntityView.

Minor refactoring to improve testability.
This is ground work for tracking more types of links.

Change-Id: I00f7e2a9cfe02024642bdaf2d99b8d57e5b2e9df
---
M lib/tests/phpunit/ReferencedEntitiesFinderTest.php
M repo/includes/EntityView.php
M repo/includes/actions/ViewEntityAction.php
M repo/includes/content/EntityContent.php
M repo/tests/phpunit/includes/EntityViewTest.php
M repo/tests/phpunit/includes/ItemViewTest.php
6 files changed, 217 insertions(+), 50 deletions(-)

Approvals:
  Addshore: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php 
b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
index caf87d3..c4ab41f 100644
--- a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
+++ b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
@@ -55,11 +55,11 @@
  */
 class ReferencedEntitiesFinderTest extends \MediaWikiTestCase {
 
-       public function claimsProvider() {
+       public function snaksProvider() {
                $argLists = array();
 
                $p11 = new EntityId( Property::ENTITY_TYPE, 11 );
-               $p42 = new EntityId( Property::ENTITY_TYPE, 42 );
+               $p27 = new EntityId( Property::ENTITY_TYPE, 27 );
                $p44 = new EntityId( Property::ENTITY_TYPE, 44 );
 
                $q23 = new EntityId( Item::ENTITY_TYPE, 23 );
@@ -71,41 +71,41 @@
                        "empty" );
 
                $argLists[] = array(
-                       array( new PropertyNoValueSnak( $p42 ) ),
-                       array( $p42 ),
+                       array( new PropertyNoValueSnak( $p27 ) ),
+                       array( $p27 ),
                        "Property" );
 
                $argLists[] = array(
-                       array( new PropertySomeValueSnak( $p42 ) ),
-                       array( $p42 ),
+                       array( new PropertySomeValueSnak( $p27 ) ),
+                       array( $p27 ),
                        "PropertySomeValueSnak" );
 
                $argLists[] = array(
-                       array( new PropertyValueSnak( $p42, new StringValue( 
'onoez' )  ) ),
-                       array( $p42 ),
+                       array( new PropertyValueSnak( $p27, new StringValue( 
'onoez' )  ) ),
+                       array( $p27 ),
                        "PropertyValueSnak with string value" );
 
                $argLists[] = array(
-                       array( new PropertyValueSnak( $p42, $q23 ) ),
-                       array( $p42, $q23 ),
+                       array( new PropertyValueSnak( $p27, $q23 ) ),
+                       array( $p27, $q23 ),
                        "PropertyValueSnak with EntityId" );
 
                $argLists[] = array(
                        array(
                                new PropertyValueSnak( $p11, $q23 ),
-                               new PropertyNoValueSnak( $p42 ),
+                               new PropertyNoValueSnak( $p27 ),
                                new PropertySomeValueSnak( $p44 ),
                                new PropertyValueSnak( $p44, new StringValue( 
'onoez' ) ),
                                new PropertyValueSnak( $p44, $q24 ),
                        ),
-                       array( $p11, $q23, $p42, $p44, $q24 ),
+                       array( $p11, $q23, $p27, $p44, $q24 ),
                        "PropertyValueSnak with EntityId" );
 
                return $argLists;
        }
 
        /**
-        * @dataProvider claimsProvider
+        * @dataProvider snaksProvider
         *
         * @param Snak[]     $snaks
         * @param EntityId[] $expected
diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php
index ba68bac..89c0b85 100644
--- a/repo/includes/EntityView.php
+++ b/repo/includes/EntityView.php
@@ -13,6 +13,7 @@
 use MWException;
 use FormatJson;
 use Wikibase\Lib\EntityIdFormatter;
+use Wikibase\Lib\PropertyDataTypeLookup;
 use Wikibase\Lib\Serializers\EntitySerializationOptions;
 use Wikibase\Lib\Serializers\SerializerFactory;
 use ValueFormatters\ValueFormatterFactory;
@@ -85,16 +86,25 @@
        /**
         * Constructor.
         *
-        * @todo think about using IContextSource here. Parser for example uses 
parser options (which also can be generated
-        *       from an IContextSource) but this seems sufficient for now.
+        * @todo  think about using IContextSource here. Parser for example 
uses parser options (which also can be generated
+        *        from an IContextSource) but this seems sufficient for now.
         *
         * @since 0.1
         *
-        * @param ValueFormatterFactory $valueFormatters
-        * @param IContextSource|null $context
+        * @param ValueFormatterFactory      $valueFormatters
+        * @param Lib\PropertyDataTypeLookup $dataTypeLookup
+        * @param EntityLookup               $entityLoader
+        * @param IContextSource|null        $context
         */
-       public function __construct( ValueFormatterFactory $valueFormatters, 
IContextSource $context = null ) {
+       public function __construct(
+               ValueFormatterFactory $valueFormatters,
+               PropertyDataTypeLookup $dataTypeLookup,
+               EntityLookup $entityLoader,
+               IContextSource $context = null ) {
+
                $this->valueFormatters = $valueFormatters;
+               $this->dataTypeLookup = $dataTypeLookup;
+               $this->entityLoader = $entityLoader;
 
                if ( !$context ) {
                        $context = \RequestContext::getMain();
@@ -242,24 +252,26 @@
                // fresh parser output with entity markup
                $pout = new ParserOutput();
 
-               $entityLoader = StoreFactory::getStore()->getEntityLookup();
+               $allSnaks = $entity->getEntity()->getAllSnaks();
 
-               $refFinder = new ReferencedEntitiesFinder( $entityLoader );
+               // treat referenced entities as page links ------
+               $refFinder = new ReferencedEntitiesFinder();
+               $usedEntityIds = $refFinder->findSnakLinks( $allSnaks );
+
                $contentFactory = EntityContentFactory::singleton();
-
-               $usedEntityIds = $refFinder->findSnakLinks( 
$entity->getEntity()->getAllSnaks() );
 
                foreach ( $usedEntityIds as $entityId ) {
                        $pout->addLink( $contentFactory->getTitleForId( 
$entityId ) );
                }
 
+               //@todo put externallinks (from URL values) into ParserOutput 
as externallinks.
+               //@todo would be nice to also put imagelinks (from CommonsMedia 
values)...
+               //@todo:...as well as iwlinks (from sitelinks) into the 
ParserOutput.
+
                if ( $generateHtml ) {
                        $html = $this->getHtml( $entity, $langCode, $editable );
                        $pout->setText( $html );
                }
-
-               //@todo (phase 2) would be nice to put pagelinks (entity 
references) and categorylinks (from special properties)...
-               //@todo:          ...as well as languagelinks/sisterlinks into 
the ParserOutput.
 
                // make css available for JavaScript-less browsers
                $pout->addModuleStyles( array(
@@ -838,11 +850,10 @@
                );
 
                // make information about other entities used in this entity 
available in JavaScript view:
-               $entityLoader = StoreFactory::getStore()->getEntityLookup();
-               $refFinder = new ReferencedEntitiesFinder( $entityLoader );
+               $refFinder = new ReferencedEntitiesFinder();
 
                $usedEntityIds = $refFinder->findSnakLinks( 
$entity->getAllSnaks() );
-               $basicEntityInfo = static::getBasicEntityInfo( $entityLoader, 
$usedEntityIds, $langCode );
+               $basicEntityInfo = $this->getBasicEntityInfo( $usedEntityIds, 
$langCode );
 
                $out->addJsConfigVars(
                        'wbUsedEntities',
@@ -857,16 +868,15 @@
         * set of entity IDs.
         * @since 0.4
         *
-        * @param EntityLookup $entityLoader
         * @param EntityId[] $entityIds
         * @param string $langCode For the entity labels which will be included 
in one language only.
         * @return array
         */
-       protected static function getBasicEntityInfo( EntityLookup 
$entityLoader, array $entityIds, $langCode ) {
+       protected function getBasicEntityInfo( array $entityIds, $langCode ) {
                wfProfileIn( __METHOD__ );
 
                $entityContentFactory = EntityContentFactory::singleton();
-               $entities = $entityLoader->getEntities( $entityIds );
+               $entities = $this->entityLoader->getEntities( $entityIds );
                $entityInfo = array();
 
                $serializerFactory = new SerializerFactory();
@@ -900,16 +910,20 @@
         *
         * @since 0.2
         *
-        * @param EntityContent $entity
-        * @param ValueFormatterFactory $valueFormatters
-        * @param IContextSource|null $context
+        * @param EntityContent              $entity
+        * @param ValueFormatterFactory      $valueFormatters
+        * @param Lib\PropertyDataTypeLookup $dataTypeLookup
+        * @param EntityLookup               $entityLoader
+        * @param IContextSource|null        $context
         *
+        * @throws \MWException
         * @return EntityView
-        * @throws MWException
         */
        public static function newForEntityContent(
                EntityContent $entity,
                ValueFormatterFactory $valueFormatters,
+               PropertyDataTypeLookup $dataTypeLookup,
+               EntityLookup $entityLoader,
                IContextSource $context = null
        ) {
                $type = $entity->getEntity()->getType();
@@ -918,7 +932,11 @@
                        throw new MWException( "No entity view known for 
handling entities of type '$type'" );
                }
 
-               $instance = new self::$typeMap[ $type ]( $valueFormatters, 
$context );
+               $instance = new self::$typeMap[ $type ](
+                       $valueFormatters,
+                       $dataTypeLookup,
+                       $entityLoader,
+                       $context );
                return $instance;
        }
 }
diff --git a/repo/includes/actions/ViewEntityAction.php 
b/repo/includes/actions/ViewEntityAction.php
index 790aee7..1e54ebc 100644
--- a/repo/includes/actions/ViewEntityAction.php
+++ b/repo/includes/actions/ViewEntityAction.php
@@ -156,9 +156,12 @@
                        $this->displayEntityContent( $content );
 
                        $valueFormatters = new ValueFormatterFactory( 
$GLOBALS['wgValueFormatters'] );
+                       $dataTypeLookup = 
WikibaseRepo::getDefaultInstance()->getPropertyDataTypeLookup();
+                       $entityLoader = 
WikibaseRepo::getDefaultInstance()->getStore()->getEntityLookup();
+
                        $isEditableView = $this->isPlainView();
 
-                       $view = EntityView::newForEntityContent( $content, 
$valueFormatters );
+                       $view = EntityView::newForEntityContent( $content, 
$valueFormatters, $dataTypeLookup, $entityLoader );
                        $view->registerJsConfigVars(
                                $this->getOutput(),
                                $content,
diff --git a/repo/includes/content/EntityContent.php 
b/repo/includes/content/EntityContent.php
index fb1e028..7952e32 100644
--- a/repo/includes/content/EntityContent.php
+++ b/repo/includes/content/EntityContent.php
@@ -2,8 +2,10 @@
 
 namespace Wikibase;
 
+use ParserOutput;
 use WikiPage, Title, User, Status, ParserOptions;
 use \ValueFormatters\ValueFormatterFactory;
+use Wikibase\Repo\WikibaseRepo;
 
 /**
  * Abstract content object for articles representing Wikibase entities.
@@ -139,8 +141,10 @@
         */
        public function getParserOutput( Title $title, $revId = null, 
ParserOptions $options = null, $generateHtml = true )  {
                $valueFormatters = new ValueFormatterFactory( 
$GLOBALS['wgValueFormatters'] );
+               $dataTypeLookup = 
WikibaseRepo::getDefaultInstance()->getPropertyDataTypeLookup();
+               $entityLoader = 
WikibaseRepo::getDefaultInstance()->getStore()->getEntityLookup();
 
-               $entityView = EntityView::newForEntityContent( $this, 
$valueFormatters );;
+               $entityView = EntityView::newForEntityContent( $this, 
$valueFormatters, $dataTypeLookup, $entityLoader );
                return $entityView->getParserOutput( $this, $options, 
$generateHtml );
        }
 
diff --git a/repo/tests/phpunit/includes/EntityViewTest.php 
b/repo/tests/phpunit/includes/EntityViewTest.php
index 1c5293d..263998c 100644
--- a/repo/tests/phpunit/includes/EntityViewTest.php
+++ b/repo/tests/phpunit/includes/EntityViewTest.php
@@ -1,8 +1,21 @@
 <?php
 
 namespace Wikibase\Test;
+use DataValues\StringValue;
+use Wikibase\Claim;
+use Wikibase\EntityContent;
+use Wikibase\EntityContentFactory;
+use Wikibase\EntityId;
+use Wikibase\EntityView;
+use Wikibase\Item;
 use Wikibase\ItemContent;
 use \ValueFormatters\ValueFormatterFactory;
+use Wikibase\Lib\InMemoryDataTypeLookup;
+use Wikibase\Property;
+use Wikibase\PropertyContent;
+use Wikibase\PropertyNoValueSnak;
+use Wikibase\PropertySomeValueSnak;
+use Wikibase\PropertyValueSnak;
 
 /**
  * @covers Wikibase\EntityView
@@ -33,8 +46,46 @@
  *
  * @licence GNU GPL v2+
  * @author H. Snater < [email protected] >
+ * @author Daniel Kinzler
  */
 class EntityViewTest extends \PHPUnit_Framework_TestCase {
+
+       protected function newEntityView( EntityContent $entityContent ) {
+               $valueFormatters = new ValueFormatterFactory( array() );
+               $entityLoader = new MockRepository();
+
+               $p11 = new EntityId( Property::ENTITY_TYPE, 11 );
+               $p23 = new EntityId( Property::ENTITY_TYPE, 23 );
+               $p42 = new EntityId( Property::ENTITY_TYPE, 42 );
+               $p44 = new EntityId( Property::ENTITY_TYPE, 44 );
+
+               $dataTypeLookup = new InMemoryDataTypeLookup();
+               $dataTypeLookup->setDataTypeForProperty( $p23, 'string' );
+               $dataTypeLookup->setDataTypeForProperty( $p42, 'url' );
+
+               $dataTypeLookup->setDataTypeForProperty( $p11, 'wikibase-item' 
);
+               $dataTypeLookup->setDataTypeForProperty( $p44, 'wikibase-item' 
);
+
+               $entityView = EntityView::newForEntityContent(
+                       $entityContent,
+                       $valueFormatters,
+                       $dataTypeLookup,
+                       $entityLoader
+               );
+
+               return $entityView;
+       }
+
+       protected function newEntityContentForClaims( $claims ) {
+               $entity = Item::newEmpty();
+
+               foreach ( $claims as $claim ) {
+                       $entity->addClaim( $claim );
+               }
+
+               $content = EntityContentFactory::singleton()->newFromEntity( 
$entity );
+               return $content;
+       }
 
        /**
         * @return array
@@ -44,9 +95,9 @@
 
                $itemContent = ItemContent::newEmpty();
                $itemContent->getEntity()->addClaim(
-                       new \Wikibase\Claim(
-                               new \Wikibase\PropertyNoValueSnak(
-                                       new \Wikibase\EntityId( 
\Wikibase\Property::ENTITY_TYPE, 24 )
+                       new Claim(
+                               new PropertyNoValueSnak(
+                                       new EntityId( Property::ENTITY_TYPE, 24 
)
                                )
                        )
                );
@@ -59,14 +110,10 @@
        /**
         * @dataProvider getHtmlForClaimsProvider
         *
-        * @param \Wikibase\EntityContent $entityContent
+        * @param EntityContent $entityContent
         */
-       public function testGetHtmlForClaims( \Wikibase\EntityContent 
$entityContent ) {
-
-               $entityView = \Wikibase\EntityView::newForEntityContent(
-                       $entityContent,
-                       new ValueFormatterFactory( 
$GLOBALS['wgValueFormatters'] )
-               );
+       public function testGetHtmlForClaims( EntityContent $entityContent ) {
+               $entityView = $this->newEntityView( $entityContent );
 
                // Using a DOM document to parse HTML output:
                $doc = new \DOMDocument();
@@ -90,4 +137,95 @@
                libxml_use_internal_errors();
        }
 
+       /**
+        * @dataProvider getParserOutputLinksProvider
+        *
+        * @param Claim[] $claims
+        * @param EntityId[] $expectedLinks
+        */
+       public function testParserOutputLinks( array $claims, $expectedLinks ) {
+               $entityContent = $this->newEntityContentForClaims( $claims );
+               $entityView = $this->newEntityView( $entityContent );
+
+               $out = $entityView->getParserOutput( $entityContent, null, 
false );
+               $links = $out->getLinks();
+
+               // convert expected links to link structure
+               $contentFactory = EntityContentFactory::singleton();
+
+               foreach ( $expectedLinks as $entityId ) {
+                       $title = $contentFactory->getTitleForId( $entityId );
+                       $ns = $title->getNamespace();
+                       $dbk = $title->getDBkey();
+
+                       $this->assertArrayHasKey( $ns, $links, "sub-array for 
namespace" );
+                       $this->assertArrayHasKey( $dbk, $links[$ns], "entry for 
database key" );
+               }
+       }
+
+       public function getParserOutputLinksProvider() {
+               $argLists = array();
+
+               $p11 = new EntityId( Property::ENTITY_TYPE, 11 );
+               $p27 = new EntityId( Property::ENTITY_TYPE, 42 );
+               $p44 = new EntityId( Property::ENTITY_TYPE, 44 );
+
+               $q23 = new EntityId( Item::ENTITY_TYPE, 23 );
+               $q24 = new EntityId( Item::ENTITY_TYPE, 24 );
+
+               $argLists["empty"] = array(
+                       array(),
+                       array() );
+
+               $argLists["Property"] = array(
+                       array( new Claim( new PropertyNoValueSnak( $p27 ) ) ),
+                       array( $p27 ) );
+
+               $argLists["PropertySomeValueSnak"] = array(
+                       array( new Claim( new PropertySomeValueSnak( $p27 ) ) ),
+                       array( $p27 ) );
+
+               $argLists["PropertyValueSnak with string value"] = array(
+                       array( new Claim( new PropertyValueSnak( $p27, new 
StringValue( 'onoez' ) ) ) ),
+                       array( $p27 ) );
+
+               $argLists["PropertyValueSnak with EntityId"] = array(
+                       array( new Claim( new PropertyValueSnak( $p27, $q23 ) ) 
),
+                       array( $p27, $q23 ) );
+
+               $argLists["Mixed Snaks"] = array(
+                       array(
+                               new Claim( new PropertyValueSnak( $p11, $q23 ) 
),
+                               new Claim( new PropertyNoValueSnak( $p27 ) ),
+                               new Claim( new PropertySomeValueSnak( $p44 ) ),
+                               new Claim( new PropertyValueSnak( $p44, new 
StringValue( 'onoez' ) ) ),
+                               new Claim( new PropertyValueSnak( $p44, $q24 ) 
),
+                       ),
+                       array( $p11, $q23, $p27, $p44, $q24 ) );
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider providerNewForEntityContent
+        */
+       public function testNewForEntityContent( EntityContent $entityContent ) 
{
+               $valueFormatters = new ValueFormatterFactory( array() );
+               $dataTypeLookup = new InMemoryDataTypeLookup( array() );
+               $entityLoader = new MockRepository();
+
+               // test whether we get the right EntityView from an 
EntityContent
+               $view = EntityView::newForEntityContent( $entityContent, 
$valueFormatters, $dataTypeLookup, $entityLoader );
+               $this->assertInstanceOf(
+                       EntityView::$typeMap[ 
$entityContent->getEntity()->getType() ],
+                       $view
+               );
+       }
+
+       public static function providerNewForEntityContent() {
+               return array(
+                       array( ItemContent::newEmpty() ),
+                       array( PropertyContent::newEmpty() )
+               );
+       }
 }
diff --git a/repo/tests/phpunit/includes/ItemViewTest.php 
b/repo/tests/phpunit/includes/ItemViewTest.php
index 0ce9838..d65a49c 100644
--- a/repo/tests/phpunit/includes/ItemViewTest.php
+++ b/repo/tests/phpunit/includes/ItemViewTest.php
@@ -4,6 +4,7 @@
 
 use Wikibase\ItemContent;
 use Wikibase\Item;
+use Wikibase\Lib\InMemoryDataTypeLookup;
 use Wikibase\Utils;
 use Wikibase\ItemView;
 use ValueFormatters\ValueFormatterFactory;
@@ -53,10 +54,13 @@
         * @dataProvider providerNewForEntityContent
         */
        public function testNewForEntityContent( $entityContent ) {
-               $valueFormatters = new ValueFormatterFactory( 
$GLOBALS['wgValueFormatters'] );
+               $valueFormatters = new ValueFormatterFactory( array() );
+               $entityLoader = new MockRepository();
+               $dataTypeLookup = new InMemoryDataTypeLookup();
 
                // test whether we get the right EntityView from an 
EntityContent
-               $view = ItemView::newForEntityContent( $entityContent, 
$valueFormatters );
+               $view = ItemView::newForEntityContent( $entityContent, 
$valueFormatters, $dataTypeLookup, $entityLoader );
+
                $this->assertType(
                        ItemView::$typeMap[ 
$entityContent->getEntity()->getType() ],
                        $view

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I00f7e2a9cfe02024642bdaf2d99b8d57e5b2e9df
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to