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

Change subject: Implement MediaInfoView
......................................................................


Implement MediaInfoView

Bug: T128472
Change-Id: I6f168c59ae19f1a64f273a52647057bb7b973e53
---
A src/View/MediaInfoView.php
A tests/phpunit/mediawiki/View/MediaInfoViewTest.php
2 files changed, 432 insertions(+), 0 deletions(-)

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



diff --git a/src/View/MediaInfoView.php b/src/View/MediaInfoView.php
new file mode 100644
index 0000000..1c6e9da
--- /dev/null
+++ b/src/View/MediaInfoView.php
@@ -0,0 +1,127 @@
+<?php
+
+namespace Wikibase\MediaInfo\View;
+
+use InvalidArgumentException;
+use Language;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\Fingerprint;
+use Wikibase\EntityRevision;
+use Wikibase\MediaInfo\DataModel\MediaInfo;
+use Wikibase\MediaInfo\DataModel\MediaInfoId;
+use Wikibase\View\EntityView;
+use Wikibase\View\EntityTermsView;
+use Wikibase\View\StatementSectionsView;
+use Wikibase\View\Template\TemplateFactory;
+use Wikibase\View\TextInjector;
+
+/**
+ * @license GPL-2.0+
+ * @author Adrian Heine <[email protected]>
+ */
+class MediaInfoView extends EntityView {
+
+       /**
+        * @var EntityTermsView
+        */
+       private $entityTermsView;
+
+       /**
+        * @var TextInjector
+        */
+       private $textInjector;
+
+       /**
+        * @var StatementSectionsView
+        */
+       private $statementSectionsView;
+
+       /**
+        * @param TemplateFactory $templateFactory
+        * @param EntityTermsView $entityTermsView
+        * @param StatementSectionsView $statementSectionsView
+        * @param Language $uiLanguage
+        */
+       public function __construct(
+               TemplateFactory $templateFactory,
+               EntityTermsView $entityTermsView,
+               StatementSectionsView $statementSectionsView,
+               Language $uiLanguage
+       ) {
+               parent::__construct( $templateFactory, $entityTermsView, 
$uiLanguage );
+               $this->entityTermsView = $entityTermsView;
+               $this->statementSectionsView = $statementSectionsView;
+               $this->textInjector = new TextInjector();
+       }
+
+       /**
+        * Construct a Fingerprint holding the MediaInfo's terms for 
compatibility with EntityTermsView
+        *
+        * @param MediaInfo $entity
+        */
+       private function getFingerprint( MediaInfo $entity ) {
+               return new Fingerprint( $entity->getLabels(), 
$entity->getDescriptions() );
+       }
+
+       /**
+        * @see EntityView::getMainHtml
+        */
+       protected function getMainHtml( EntityRevision $entityRevision ) {
+               $entity = $entityRevision->getEntity();
+               if ( !( $entity instanceof MediaInfo ) ) {
+                       throw new InvalidArgumentException( '$entityRevision 
must contain a MediaInfo entity.' );
+               }
+               $id = $entity->getId();
+               $fingerprint = $this->getFingerprint( $entity );
+               return $this->entityTermsView->getHtml(
+                               $fingerprint,
+                               $id,
+                               
$this->entityTermsView->getEntityTermsForLanguageListView(
+                                       $fingerprint,
+                                       [ $this->language->getCode() ]
+                               ), // FIXME should be $this->getHtmlForTermBox( 
$id, $entityRevision->getRevisionId() ),
+                               $this->textInjector
+                       ) .
+                       $this->statementSectionsView->getHtml( 
$entity->getStatements() );
+       }
+
+       /**
+        * Returns the placeholder map build while generating HTML.
+        * The map returned here may be used with TextInjector.
+        *
+        * @return array[] string -> array
+        */
+       public function getPlaceholders() {
+               return array_merge(
+                       parent::getPlaceHolders(),
+                       $this->textInjector->getMarkers()
+               );
+       }
+
+       /**
+        * @see EntityView::getSideHtml
+        */
+       protected function getSideHtml( EntityDocument $entity ) {
+               return '';
+       }
+
+       /**
+        * Returns the html used for the title of the page.
+        * @see ParserOutput::setDisplayTitle
+        *
+        * @param EntityRevision $entityRevision
+        *
+        * @return string HTML
+        */
+       public function getTitleHtml( EntityRevision $entityRevision ) {
+               $entity = $entityRevision->getEntity();
+               if ( !( $entity instanceof MediaInfo ) ) {
+                       throw new InvalidArgumentException( '$entityRevision 
must contain a MediaInfo entity.' );
+               }
+               return $this->entityTermsView->getTitleHtml(
+                       $this->getFingerprint( $entity ),
+                       $entity->getId()
+               );
+       }
+
+}
diff --git a/tests/phpunit/mediawiki/View/MediaInfoViewTest.php 
b/tests/phpunit/mediawiki/View/MediaInfoViewTest.php
new file mode 100644
index 0000000..6aaabee
--- /dev/null
+++ b/tests/phpunit/mediawiki/View/MediaInfoViewTest.php
@@ -0,0 +1,305 @@
+<?php
+
+namespace Wikibase\MediaInfo\Tests\MediaWiki\View;
+
+use Language;
+use PHPUnit_Framework_TestCase;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Statement\Statement;
+use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\DataModel\Term\Fingerprint;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
+use Wikibase\EntityRevision;
+use Wikibase\MediaInfo\DataModel\MediaInfo;
+use Wikibase\MediaInfo\DataModel\MediaInfoId;
+use Wikibase\MediaInfo\View\MediaInfoView;
+use Wikibase\View\EntityTermsView;
+use Wikibase\View\EntityView;
+use Wikibase\View\EntityViewPlaceholderExpander;
+use Wikibase\View\StatementSectionsView;
+use Wikibase\View\Template\TemplateFactory;
+use Wikibase\View\TextInjector;
+
+/**
+ * @covers Wikibase\MediaInfo\View\MediaInfoView
+ *
+ * @license GPL-2.0+
+ * @author Adrian Heine <[email protected]>
+ */
+class MediaInfoViewTest extends PHPUnit_Framework_TestCase {
+
+       private function newStatementSectionsViewMock() {
+               return $this->getMockBuilder( StatementSectionsView::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+       }
+
+       private function newEntityTermsViewMock() {
+               return $this->getMockBuilder( EntityTermsView::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+       }
+
+       private function newMediaInfoView(
+               $contentLanguageCode = 'en',
+               EntityTermsView $entityTermsView = null,
+               StatementSectionsView $statementSectionsView = null
+       ) {
+               $templateFactory = TemplateFactory::getDefaultInstance();
+               if ( !$entityTermsView ) {
+                       $entityTermsView = $this->newEntityTermsViewMock();
+               }
+               if ( !$statementSectionsView ) {
+                       $statementSectionsView = 
$this->newStatementSectionsViewMock();
+               }
+               $view = new MediaInfoView(
+                       $templateFactory,
+                       $entityTermsView,
+                       $statementSectionsView,
+                       $this->getMock( Language::class ),
+                       $contentLanguageCode
+               );
+               return $view;
+       }
+
+       private function newEntityRevision( EntityDocument $entity ) {
+               $revId = 0;
+               $timestamp = wfTimestamp( TS_MW );
+               return new EntityRevision( $entity, $revId, $timestamp );
+       }
+
+       public function testInstantiate() {
+               $view = $this->newMediaInfoView();
+               $this->assertInstanceOf( MediaInfoView::class, $view );
+               $this->assertInstanceOf( EntityView::class, $view );
+       }
+
+       /**
+        * @expectedException InvalidArgumentException
+        */
+       public function testGetHtml_invalidEntityType() {
+               $view = $this->newMediaInfoView();
+
+               $entity = $this->getMock( EntityDocument::class );
+               $revision = $this->newEntityRevision( $entity );
+
+               $view->getHtml( $revision );
+       }
+
+       /**
+        * @dataProvider provideTestGetHtml
+        */
+       public function testGetHtml(
+               MediaInfo $entity,
+               MediaInfoId $entityId = null,
+               TermList $descriptions = null,
+               $contentLanguageCode = 'en',
+               StatementList $statements = null
+       ) {
+
+               $entityTermsView = $this->newEntityTermsViewMock();
+               $entityTermsView->expects( $this->once() )
+                       ->method( 'getHtml' )
+                       ->will( $this->returnValue( 'entityTermsView->getHtml' 
) )
+                       ->with(
+                               $this->callback( function( Fingerprint 
$fingerprint ) use ( $descriptions ) {
+                                       if ( $descriptions ) {
+                                               return 
$fingerprint->getDescriptions() === $descriptions;
+                                       } else {
+                                               return 
$fingerprint->getDescriptions()->isEmpty();
+                                       }
+                               } ),
+                               $entityId,
+                               $this->isType( 'string' ),
+                               $this->isInstanceOf( TextInjector::class )
+                       );
+
+               // FIXME Shouldn't be called
+               $entityTermsView->expects( $this->once() )
+                       ->method( 'getEntityTermsForLanguageListView' )
+                       ->will( $this->returnValue( 
'entityTermsView->getEntityTermsForLanguageListView' ) );
+
+               $statementSectionsView = $this->newStatementSectionsViewMock();
+               $statementSectionsView->expects( $this->once() )
+                       ->method( 'getHtml' )
+                       ->with(
+                               $this->callback( function( StatementList 
$statementList ) use ( $statements ) {
+                                       return $statements ? $statementList === 
$statements : $statementList->isEmpty();
+                               } )
+                       )
+                       ->will( $this->returnValue( 
'statementSectionsView->getHtml' ) );
+
+               $view = $this->newMediaInfoView(
+                       $contentLanguageCode,
+                       $entityTermsView,
+                       $statementSectionsView
+               );
+
+               $revision = $this->newEntityRevision( $entity );
+
+               $result = $view->getHtml( $revision );
+               $this->assertInternalType( 'string', $result );
+               $this->assertContains( 'wb-mediainfo', $result );
+               $this->assertContains( 'entityTermsView->getHtml', $result );
+       }
+
+       public function provideTestGetHtml() {
+               $mediaInfoId = new MediaInfoId( 'M1' );
+               $descriptions = new TermList( [ new Term( 'en', 
'EN_DESCRIPTION' ) ] );
+               $statements = new StatementList( [
+                       new Statement( new PropertyNoValueSnak( new PropertyId( 
'P1' ) ) )
+               ] );
+
+               return [
+                       [
+                               new MediaInfo()
+                       ],
+                       [
+                               new MediaInfo(
+                                       $mediaInfoId
+                               ),
+                               $mediaInfoId
+                       ],
+                       [
+                               new MediaInfo(
+                                       $mediaInfoId,
+                                       null,
+                                       $descriptions,
+                                       $statements
+                               ),
+                               $mediaInfoId,
+                               $descriptions,
+                               'en',
+                               $statements
+                       ],
+                       [
+                               new MediaInfo(
+                                       $mediaInfoId,
+                                       null,
+                                       $descriptions
+                               ),
+                               $mediaInfoId,
+                               $descriptions,
+                               'lkt'
+                       ],
+                       [
+                               new MediaInfo(
+                                       $mediaInfoId,
+                                       null,
+                                       $descriptions,
+                                       $statements
+                               ),
+                               $mediaInfoId,
+                               $descriptions,
+                               'lkt',
+                               $statements
+                       ],
+               ];
+       }
+
+       /**
+        * @expectedException InvalidArgumentException
+        */
+       public function testGetTitleHtml_invalidEntityType() {
+               $view = $this->newMediaInfoView();
+
+               $entity = $this->getMock( EntityDocument::class );
+               $revision = $this->newEntityRevision( $entity );
+
+               $view->getTitleHtml( $revision );
+       }
+
+       /**
+        * @dataProvider provideTestGetTitleHtml
+        */
+       public function testGetTitleHtml(
+               MediaInfo $entity,
+               TermList $labels = null,
+               MediaInfoId $entityId = null,
+               $contentLanguageCode = 'en'
+       ) {
+               $entityTermsView = $this->newEntityTermsViewMock();
+               $entityTermsView->expects( $this->once() )
+                       ->method( 'getTitleHtml' )
+                       ->will( $this->returnValue( 
'entityTermsView->getTitleHtml' ) )
+                       ->with(
+                               $this->callback( function( Fingerprint 
$fingerprint ) use ( $labels ) {
+                                       return $labels ? 
$fingerprint->getLabels() === $labels : $fingerprint->getLabels()->isEmpty();
+                               } ),
+                               $entityId
+                       );
+               $view = $this->newMediaInfoView( $contentLanguageCode, 
$entityTermsView );
+
+               $revision = $this->newEntityRevision( $entity );
+
+               $result = $view->getTitleHtml( $revision );
+               $this->assertInternalType( 'string', $result );
+               $this->assertEquals( 'entityTermsView->getTitleHtml', $result );
+       }
+
+       public function provideTestGetTitleHtml() {
+               $mediaInfoId = new MediaInfoId( 'M1' );
+               $labels = new TermList( [ new Term( 'en', 'EN_LABEL' ) ] );
+               return [
+                       [
+                               new MediaInfo()
+                       ],
+                       [
+                               new MediaInfo(
+                                       $mediaInfoId
+                               ),
+                               null,
+                               $mediaInfoId
+                       ],
+                       [
+                               new MediaInfo(
+                                       $mediaInfoId,
+                                       $labels
+                               ),
+                               $labels,
+                               $mediaInfoId
+                       ],
+                       [
+                               new MediaInfo(
+                                       $mediaInfoId,
+                                       $labels
+                               ),
+                               $labels,
+                               $mediaInfoId,
+                               'lkt'
+                       ],
+               ];
+       }
+
+       public function testPlaceholderIntegration() {
+               $entityRevision = $this->newEntityRevision( new MediaInfo( new 
MediaInfoId( 'M1' ) ) );
+
+               $entityTermsView = $this->newEntityTermsViewMock();
+               $entityTermsView->expects( $this->once() )
+                       ->method( 'getHtml' )
+                       ->will( $this->returnCallback(
+                               function(
+                                       Fingerprint $fingerprint,
+                                       MediaInfoId $entityId,
+                                       $termBoxHtml,
+                                       TextInjector $textInjector
+                               ) {
+                                       return $textInjector->newMarker(
+                                               
'entityViewPlaceholder-entitytermsview-entitytermsforlanguagelistview-class'
+                                       );
+                               }
+                       ) );
+
+               $view = $this->newMediaInfoView( 'en', $entityTermsView );
+               $html = $view->getHtml( $entityRevision );
+               $placeholders = $view->getPlaceholders();
+
+               // FIXME: EntityViewPlaceholderExpander only supports entities 
with fingerprints
+               // Otherwise this would be 2.
+               $this->assertEquals( 1, count( $placeholders ) );
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I6f168c59ae19f1a64f273a52647057bb7b973e53
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/WikibaseMediaInfo
Gerrit-Branch: master
Gerrit-Owner: Adrian Heine <[email protected]>
Gerrit-Reviewer: Adrian Heine <[email protected]>
Gerrit-Reviewer: Bene <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to