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

Change subject: Have labels shown in variants for {{#property: }}
......................................................................


Have labels shown in variants for {{#property: }}

Change-Id: I5a461251c5c4b0ff6c5abd701707c5c04910fadf
---
M client/WikibaseClient.php
M client/includes/parserhooks/PropertyParserFunction.php
A client/includes/parserhooks/PropertyParserFunctionRenderer.php
A 
client/tests/phpunit/includes/parserhooks/PropertyParserFunctionRendererTest.php
M client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
5 files changed, 462 insertions(+), 222 deletions(-)

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



diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index 8081f22..0b5e76c 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -103,6 +103,7 @@
        $wgAutoloadClasses['Wikibase\NoLangLinkHandler']    = $dir . 
'includes/parserhooks/NoLangLinkHandler.php';
        $wgAutoloadClasses['Wikibase\ParserErrorMessageFormatter']      = $dir 
. 'includes/parserhooks/ParserErrorMessageFormatter.php';
        $wgAutoloadClasses['Wikibase\PropertyParserFunction'] = $dir . 
'includes/parserhooks/PropertyParserFunction.php';
+       $wgAutoloadClasses['Wikibase\PropertyParserFunctionRenderer'] = $dir . 
'includes/parserhooks/PropertyParserFunctionRenderer.php';
 
        // includes/recentchanges
        $wgAutoloadClasses['Wikibase\ExternalChangesLine']      = $dir . 
'includes/recentchanges/ExternalChangesLine.php';
diff --git a/client/includes/parserhooks/PropertyParserFunction.php 
b/client/includes/parserhooks/PropertyParserFunction.php
index 04286e6..2efdf84 100644
--- a/client/includes/parserhooks/PropertyParserFunction.php
+++ b/client/includes/parserhooks/PropertyParserFunction.php
@@ -10,23 +10,6 @@
 /**
  * Handler of the {{#property}} parser function.
  *
- * TODO: cleanup injection of dependencies
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
  * @since 0.4
  *
  * @file
@@ -36,137 +19,174 @@
  * @author Katie Filbert < [email protected] >
  * @author Jeroen De Dauw < [email protected] >
  * @author Daniel Kinzler
+ * @author Liangent < [email protected] >
  */
 class PropertyParserFunction {
 
-       /* @var \Language */
-       protected $language;
+       /**
+        * @var \Parser
+        */
+       protected $parser;
 
-       /* @var EntityLookup */
+       /**
+        * @var EntityId
+        */
+       protected $entityId;
+
+       /**
+        * @var EntityLookup
+        */
        protected $entityLookup;
 
-       /* @var PropertyLabelResolver */
+       /**
+        * @var PropertyLabelResolver
+        */
        protected $propertyLabelResolver;
 
-       /* @var ParserErrorMessageFormatter */
-       protected $errorFormatter;
-
-       /* @var SnakFormatter */
-       protected $snaksFormatter;
-
        /**
-        * @since    0.4
+        * Constructor.
         *
-        * @param \Language                   $language
+        * @param \Parser $parser
+        * @param EntityId $entityId
         * @param EntityLookup                $entityLookup
         * @param PropertyLabelResolver       $propertyLabelResolver
-        * @param ParserErrorMessageFormatter $errorFormatter
-        * @param SnakFormatter               $snaksFormatter
         */
-       public function __construct( \Language $language,
-               EntityLookup $entityLookup, PropertyLabelResolver 
$propertyLabelResolver,
-               ParserErrorMessageFormatter $errorFormatter, SnakFormatter 
$snaksFormatter ) {
-               $this->language = $language;
+       public function __construct( \Parser $parser, EntityId $entityId,
+               EntityLookup $entityLookup, PropertyLabelResolver 
$propertyLabelResolver
+       ) {
+               $this->parser = $parser;
+               $this->entityId = $entityId;
                $this->entityLookup = $entityLookup;
                $this->propertyLabelResolver = $propertyLabelResolver;
-               $this->errorFormatter = $errorFormatter;
-               $this->snaksFormatter = $snaksFormatter;
        }
 
        /**
-        * Returns such Claims from $entity that have a main Snak for the 
property that
-        * is specified by $propertyLabel.
+        * Check whether variants are used in this parser run.
         *
-        * @param Entity $entity The Entity from which to get the clams
-        * @param string $propertyLabel A property label (in the wiki's content 
language) or a prefixed property ID.
-        *
-        * @return Claims The claims for the given property.
+        * @param \Parser $parser
+        * @return bool
         */
-       private function getClaimsForProperty( Entity $entity, $propertyLabel ) 
{
-               $propertyIdToFind = EntityId::newFromPrefixedId( $propertyLabel 
);
+       public function isParserUsingVariants() {
+               $parserOptions = $this->parser->getOptions();
+               return $this->parser->OutputType() === \Parser::OT_HTML && 
!$parserOptions->getInterfaceMessage()
+                       && !$parserOptions->getDisableContentConversion();
+       }
 
-               if ( $propertyIdToFind === null ) {
-                       //XXX: It might become useful to give the 
PropertyLabelResolver a hint as to which
-                       //     properties may become relevant during the 
present request, namely the ones
-                       //     used by the Item linked to the current page. 
This could be done with
-                       //     something like this:
-                       //
-                       //     $this->propertyLabelResolver->preloadLabelsFor( 
$propertiesUsedByItem );
-
-                       $propertyIds = 
$this->propertyLabelResolver->getPropertyIdsForLabels( array( $propertyLabel ) 
);
-
-                       if ( empty( $propertyIds ) ) {
-                               return new Claims();
-                       } else {
-                               $propertyIdToFind = 
$propertyIds[$propertyLabel];
-                       }
+       /**
+        * Post-process rendered array (variant text) into wikitext to be used 
in pages.
+        *
+        * @param array $textArray
+        * @return string
+        */
+       public function processRenderedArray( $textArray ) {
+               // We got arrays, so they must have already checked that 
variants are being used.
+               $text = '-{';
+               foreach ( $textArray as $variantCode => $variantText ) {
+                       $text .= "$variantCode:$variantText;";
                }
+               $text .= '}-';
 
-               $allClaims = new Claims( $entity->getClaims() );
-               $claims = $allClaims->getClaimsForProperty( 
$propertyIdToFind->getNumericId() );
-
-               return $claims;
+               return $text;
        }
 
        /**
-        * @since 0.4
+        * Build a PropertyParserFunctionRenderer object for a given language.
         *
-        * @param Snak[] $snaks
-        *
-        * @return string - wikitext format
+        * @param \Language $language
+        * @return PropertyParserFunctionRenderer
         */
-       private function formatSnakList( $snaks ) {
-               $formattedValues = $this->formatSnaks( $snaks );
-               return $this->language->commaList( $formattedValues );
-       }
-
-       private function formatSnaks( $snaks ) {
-               $strings = array();
-
-               foreach ( $snaks as $snak ) {
-                       $strings[] = $this->snaksFormatter->formatSnak( $snak );
-               }
-
-               return $strings;
-       }
-
-       /**
-        * @since 0.4
-        *
-        * @param EntityId $entityId
-        * @param string   $propertyLabel
-        *
-        * @return \Status a status object wrapping a wikitext string
-        */
-       public function renderForEntityId( EntityId $entityId, $propertyLabel ) 
{
+       public function getRenderer( \Language $language ) {
                wfProfileIn( __METHOD__ );
 
-               try {
-                       $entity = $this->entityLookup->getEntity( $entityId );
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
+               $errorFormatter = new ParserErrorMessageFormatter( $language );
 
-                       if ( !$entity ) {
-                               wfProfileOut( __METHOD__ );
-                               return \Status::newGood( '' );
+               $languageFallbackChainFactory = 
$wikibaseClient->getLanguageFallbackChainFactory();
+               $languageFallbackChain = 
$languageFallbackChainFactory->newFromLanguage( $language,
+                       LanguageFallbackChainFactory::FALLBACK_SELF | 
LanguageFallbackChainFactory::FALLBACK_VARIANTS
+               );
+
+               $options = new FormatterOptions( array(
+                       'languages' => $languageFallbackChain,
+                       // ...more options...
+               ) );
+
+               $snaksFormatter = $wikibaseClient->newSnakFormatter( 
SnakFormatter::FORMAT_WIKI, $options );
+
+               $instance = new PropertyParserFunctionRenderer( $language,
+                       $this->entityLookup, $this->propertyLabelResolver,
+                       $errorFormatter, $snaksFormatter );
+
+               wfProfileIn( __METHOD__ );
+               return $instance;
+       }
+
+       /**
+        * @param string $propertyLabel property label or ID (pXXX)
+        * @param \Language $language
+        *
+        * @return string
+        */
+       public function renderInLanguage( $propertyLabel, \Language $language ) 
{
+
+               $renderer = $this->getRenderer( $language );
+
+               $status = $renderer->renderForEntityId( $this->entityId, 
$propertyLabel );
+
+               if ( !$status->isGood() ) {
+                       // stuff the error messages into the ParserOutput, so 
we can render them later somewhere
+
+                       $errors = $this->parser->getOutput()->getExtensionData( 
'wikibase-property-render-errors' );
+                       if ( $errors === null ) {
+                               $errors = array();
                        }
 
-                       $claims = $this->getClaimsForProperty( $entity, 
$propertyLabel );
+                       //XXX: if Status sucked less, we'd could get an array 
of Message objects
+                       $errors[] = $status->getWikiText();
 
-                       if ( $claims->isEmpty() ) {
-                               wfProfileOut( __METHOD__ );
-                               return \Status::newGood( '' );
-                       }
+                       $this->parser->getOutput()->setExtensionData( 
'wikibase-property-render-errors', $errors );
+               }
 
-                       $snakList = $claims->getMainSnaks();
-                       $text = $this->formatSnakList( $snakList, 
$propertyLabel );
-                       $status = \Status::newGood( $text );
-               } catch ( \Exception $ex ) {
-                       wfDebugLog( __CLASS__, __FUNCTION__ . ': ' . 
$ex->getMessage() );
+               return $status->isOK() ? $status->getValue() : '';
+       }
 
-                       $status = \Status::newFatal( 
'wikibase-property-render-error', $propertyLabel, $ex->getMessage() );
+       /**
+        * @param string $propertyLabel property label or ID (pXXX)
+        * @param string[] $variants Variant codes
+        *
+        * @return string[], key by variant codes
+        */
+       public function renderInVariants( $propertyLabel, array $variants ) {
+               $textArray = array();
+
+               foreach ( $variants as $variantCode ) {
+                       $variantLanguage = \Language::factory( $variantCode );
+                       $textArray[$variantCode] = $this->renderInLanguage( 
$propertyLabel, $variantLanguage );
+               }
+
+               return $textArray;
+       }
+
+       /**
+        * @param string $propertyLabel property label or ID (pXXX)
+        *
+        * @return string Wikitext
+        */
+       public function doRender( $propertyLabel ) {
+               wfProfileIn( __METHOD__ );
+
+               $targetLanguage = $this->parser->getTargetLanguage();
+
+               if ( $this->isParserUsingVariants() && 
$this->parser->getConverterLanguage()->hasVariants() ) {
+                       $text = $this->processRenderedArray( 
$this->renderInVariants(
+                               $propertyLabel, 
$this->parser->getConverterLanguage()->getVariants()
+                       ) );
+               } else {
+                       $text = $this->renderInLanguage( $propertyLabel, 
$targetLanguage );
                }
 
                wfProfileOut( __METHOD__ );
-               return $status;
+               return $text;
        }
 
        /**
@@ -179,6 +199,7 @@
         */
        public static function render( \Parser $parser, $propertyLabel ) {
                wfProfileIn( __METHOD__ );
+
                $siteId = Settings::get( 'siteGlobalID' );
 
                $siteLinkLookup = 
WikibaseClient::getDefaultInstance()->getStore()->getSiteLinkTable();
@@ -192,50 +213,16 @@
                        return '';
                }
 
-               $targetLanguage = $parser->getTargetLanguage();
-               $errorFormatter = new ParserErrorMessageFormatter( 
$targetLanguage );
-
                $wikibaseClient = WikibaseClient::getDefaultInstance();
 
                $entityLookup = $wikibaseClient->getStore()->getEntityLookup();
                $propertyLabelResolver = 
$wikibaseClient->getStore()->getPropertyLabelResolver();
 
-               $languageFallbackChainFactory = 
WikibaseClient::getDefaultInstance()->getLanguageFallbackChainFactory();
-               $languageFallbackChain = 
$languageFallbackChainFactory->newFromLanguage( $targetLanguage,
-                       LanguageFallbackChainFactory::FALLBACK_SELF | 
LanguageFallbackChainFactory::FALLBACK_VARIANTS
-               );
-
-               $options = new FormatterOptions( array(
-                       'languages' => $languageFallbackChain,
-                       // ...more options...
-               ) );
-
+               $instance = new self( $parser, $entityId, $entityLookup, 
$propertyLabelResolver );
                $formatter = $wikibaseClient->newSnakFormatter( 
SnakFormatter::FORMAT_WIKI, $options );
 
-               $instance = new self( $targetLanguage,
-                       $entityLookup, $propertyLabelResolver,
-                       $errorFormatter, $formatter );
-
-               $status = $instance->renderForEntityId( $entityId, 
$propertyLabel );
-
-               if ( !$status->isGood() ) {
-                       // stuff the error messages into the ParserOutput, so 
we can render them later somewhere
-
-                       $errors = $parser->getOutput()->getExtensionData( 
'wikibase-property-render-errors' );
-                       if ( $errors === null ) {
-                               $errors = array();
-                       }
-
-                       //XXX: if Status sucked less, we'd could get an array 
of Message objects
-                       $errors[] = $status->getWikiText();
-
-                       $parser->getOutput()->setExtensionData( 
'wikibase-property-render-errors', $errors );
-               }
-
-               $text = $status->isOK() ? $status->getValue() : '';
-
                $result = array(
-                       $text,
+                       $instance->doRender( $propertyLabel ),
                        'noparse' => false, // parse wikitext
                        'nowiki' => false,  // formatters take care of escaping 
as needed
                );
diff --git a/client/includes/parserhooks/PropertyParserFunctionRenderer.php 
b/client/includes/parserhooks/PropertyParserFunctionRenderer.php
new file mode 100644
index 0000000..7accf9d
--- /dev/null
+++ b/client/includes/parserhooks/PropertyParserFunctionRenderer.php
@@ -0,0 +1,148 @@
+<?php
+
+namespace Wikibase;
+
+use Wikibase\Client\WikibaseClient;
+use Wikibase\Lib\SnakFormatter;
+
+/**
+ * Renderer of the {{#property}} parser function.
+ *
+ * @since 0.5
+ *
+ * @file
+ * @ingroup WikibaseClient
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < [email protected] >
+ * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
+ * @author Liangent < [email protected] >
+ */
+class PropertyParserFunctionRenderer {
+
+       /* @var \Language */
+       protected $language;
+
+       /* @var EntityLookup */
+       protected $entityLookup;
+
+       /* @var PropertyLabelResolver */
+       protected $propertyLabelResolver;
+
+       /* @var ParserErrorMessageFormatter */
+       protected $errorFormatter;
+
+       /* @var SnakFormatter */
+       protected $snaksFormatter;
+
+       /**
+        * @param \Language                   $language
+        * @param EntityLookup                $entityLookup
+        * @param PropertyLabelResolver       $propertyLabelResolver
+        * @param ParserErrorMessageFormatter $errorFormatter
+        * @param Lib\SnakFormatter           $snaksFormatter
+        */
+       public function __construct( \Language $language,
+               EntityLookup $entityLookup, PropertyLabelResolver 
$propertyLabelResolver,
+               ParserErrorMessageFormatter $errorFormatter, SnakFormatter 
$snaksFormatter ) {
+               $this->language = $language;
+               $this->entityLookup = $entityLookup;
+               $this->propertyLabelResolver = $propertyLabelResolver;
+               $this->errorFormatter = $errorFormatter;
+               $this->snaksFormatter = $snaksFormatter;
+       }
+
+       /**
+        * Returns such Claims from $entity that have a main Snak for the 
property that
+        * is specified by $propertyLabel.
+        *
+        * @param Entity $entity The Entity from which to get the clams
+        * @param string $propertyLabel A property label (in the wiki's content 
language) or a prefixed property ID.
+        *
+        * @return Claims The claims for the given property.
+        */
+       private function getClaimsForProperty( Entity $entity, $propertyLabel ) 
{
+               $propertyIdToFind = EntityId::newFromPrefixedId( $propertyLabel 
);
+
+               if ( $propertyIdToFind === null ) {
+                       //XXX: It might become useful to give the 
PropertyLabelResolver a hint as to which
+                       //     properties may become relevant during the 
present request, namely the ones
+                       //     used by the Item linked to the current page. 
This could be done with
+                       //     something like this:
+                       //
+                       //     $this->propertyLabelResolver->preloadLabelsFor( 
$propertiesUsedByItem );
+
+                       $propertyIds = 
$this->propertyLabelResolver->getPropertyIdsForLabels( array( $propertyLabel ) 
);
+
+                       if ( empty( $propertyIds ) ) {
+                               return new Claims();
+                       } else {
+                               $propertyIdToFind = 
$propertyIds[$propertyLabel];
+                       }
+               }
+
+               $allClaims = new Claims( $entity->getClaims() );
+               $claims = $allClaims->getClaimsForProperty( 
$propertyIdToFind->getNumericId() );
+
+               return $claims;
+       }
+
+       /**
+        * @param Snak[] $snaks
+        *
+        * @return string - wikitext format
+        */
+       private function formatSnakList( $snaks ) {
+               $formattedValues = $this->formatSnaks( $snaks );
+               return $this->language->commaList( $formattedValues );
+       }
+
+       private function formatSnaks( $snaks ) {
+               $strings = array();
+
+               foreach ( $snaks as $snak ) {
+                       $strings[] = $this->snaksFormatter->formatSnak( $snak );
+               }
+
+               return $strings;
+       }
+
+       /**
+        * @param EntityId $entityId
+        * @param string   $propertyLabel
+        *
+        * @return \Status a status object wrapping a wikitext string
+        */
+       public function renderForEntityId( EntityId $entityId, $propertyLabel ) 
{
+               wfProfileIn( __METHOD__ );
+
+               try {
+                       $entity = $this->entityLookup->getEntity( $entityId );
+
+                       if ( !$entity ) {
+                               wfProfileOut( __METHOD__ );
+                               return \Status::newGood( '' );
+                       }
+
+                       $claims = $this->getClaimsForProperty( $entity, 
$propertyLabel );
+
+                       if ( $claims->isEmpty() ) {
+                               wfProfileOut( __METHOD__ );
+                               return \Status::newGood( '' );
+                       }
+
+                       $snakList = $claims->getMainSnaks();
+                       $text = $this->formatSnakList( $snakList, 
$propertyLabel );
+                       $status = \Status::newGood( $text );
+               } catch ( \Exception $ex ) {
+                       wfDebugLog( __CLASS__, __FUNCTION__ . ': ' . 
$ex->getMessage() );
+
+                       $status = \Status::newFatal( 
'wikibase-property-render-error', $propertyLabel, $ex->getMessage() );
+               }
+
+               wfProfileOut( __METHOD__ );
+               return $status;
+       }
+
+}
diff --git 
a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionRendererTest.php
 
b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionRendererTest.php
new file mode 100644
index 0000000..ba78be0
--- /dev/null
+++ 
b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionRendererTest.php
@@ -0,0 +1,123 @@
+<?php
+
+namespace Wikibase\Test;
+
+use DataValues\StringValue;
+use Wikibase\Claim;
+use Wikibase\Client\WikibaseClient;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\Item;
+use Wikibase\ParserErrorMessageFormatter;
+use Wikibase\Property;
+use Wikibase\PropertyParserFunctionRenderer;
+use Wikibase\PropertyValueSnak;
+
+/**
+ * @covers Wikibase\PropertyParserFunctionRenderer
+ *
+ * @file
+ * @since 0.4
+ *
+ * @ingroup WikibaseClient
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseClient
+ * @group PropertyParserFunctionTest
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class PropertyParserFunctionRendererTest extends \PHPUnit_Framework_TestCase {
+
+       private function getDefaultInstance() {
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
+
+               $targetLanguage = \Language::factory( 'en' );
+               $errorFormatter = new ParserErrorMessageFormatter( 
$targetLanguage );
+               $dataTypeFactory = $wikibaseClient->getDataTypeFactory();
+               $mockRepo = $this->newMockRepository();
+               $mockResolver = new MockPropertyLabelResolver( 
$targetLanguage->getCode(), $mockRepo );
+
+               $formatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' );
+               $formatter->expects( $this->any() )
+                       ->method( 'formatSnak' )
+                       ->will( $this->returnValue( '(a kitten)' ) );
+
+               return new PropertyParserFunctionRenderer(
+                       $targetLanguage,
+                       $mockRepo,
+                       $mockResolver,
+                       $errorFormatter,
+                       $formatter
+               );
+       }
+
+       private function newMockRepository() {
+               $propertyId = new PropertyId( 'P1337' );
+
+               $entityLookup = new MockRepository();
+
+               $item = Item::newEmpty();
+               $item->setId( 42 );
+               $item->addClaim( new Claim( new PropertyValueSnak(
+                       $propertyId,
+                       new StringValue( 'Please write tests before merging 
your code' )
+               ) ) );
+               $item->addClaim( new Claim( new PropertyValueSnak(
+                       $propertyId,
+                       new StringValue( 'or kittens will die' )
+               ) ) );
+
+               $property = Property::newEmpty();
+               $property->setId( $propertyId );
+
+               $property->setDataTypeId( 'string' );
+               $property->setLabel( 'en', 'kitten' );
+
+               $entityLookup->putEntity( $item );
+               $entityLookup->putEntity( $property );
+
+               return $entityLookup;
+       }
+
+       public static function provideRenderForEntityId() {
+               return array(
+                       array(
+                               'p1337',
+                               '(a kitten), (a kitten)',
+                               'Congratulations, you just killed a kitten'
+                       ),
+                       array(
+                               'kitten',
+                               '(a kitten), (a kitten)',
+                               'Congratulations, you just killed a kitten'
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider provideRenderForEntityId
+        */
+       public function testRenderForEntityId( $name, $expected, $info ) {
+               $parserFunction = $this->getDefaultInstance();
+
+               $status = $parserFunction->renderForEntityId(
+                       new ItemId( 'Q42' ),
+                       $name
+               );
+
+               $this->assertTrue( $status->isOK() );
+
+               $text = $status->getValue();
+               $this->assertInternalType( 'string', $text );
+
+               $this->assertEquals(
+                       $expected,
+                       $text,
+                       $info
+               );
+       }
+
+}
diff --git 
a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php 
b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
index 7077346..6b49bbd 100644
--- a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
+++ b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php
@@ -2,16 +2,8 @@
 
 namespace Wikibase\Test;
 
-use DataValues\StringValue;
-use Wikibase\Claim;
-use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\Entity\ItemId;
-use Wikibase\DataModel\Entity\PropertyId;
-use Wikibase\Item;
-use Wikibase\ParserErrorMessageFormatter;
-use Wikibase\Property;
 use Wikibase\PropertyParserFunction;
-use Wikibase\PropertyValueSnak;
 
 /**
  * @covers Wikibase\PropertyParserFunction
@@ -31,89 +23,78 @@
  */
 class PropertyParserFunctionTest extends \PHPUnit_Framework_TestCase {
 
-       private function getDefaultInstance() {
-               $targetLanguage = \Language::factory( 'en' );
-               $errorFormatter = new ParserErrorMessageFormatter( 
$targetLanguage );
-               $mockRepo = $this->newMockRepository();
-               $mockResolver = new MockPropertyLabelResolver( 
$targetLanguage->getCode(), $mockRepo );
-
-               $formatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' );
-               $formatter->expects( $this->any() )
-                       ->method( 'formatSnak' )
-                       ->will( $this->returnValue( '(a kitten)' ) );
-
-               return new PropertyParserFunction(
-                       $targetLanguage,
-                       $mockRepo,
-                       $mockResolver,
-                       $errorFormatter,
-                       $formatter
-               );
-       }
-
-       private function newMockRepository() {
-               $propertyId = new PropertyId( 'P1337' );
-
+       public function getPropertyParserFunction( $parser, $entityId ) {
                $entityLookup = new MockRepository();
+               $propertyLabelResolver = new MockPropertyLabelResolver( 
$parser->getTargetLanguage(), $entityLookup );
 
-               $item = Item::newEmpty();
-               $item->setId( 42 );
-               $item->addClaim( new Claim( new PropertyValueSnak(
-                       $propertyId,
-                       new StringValue( 'Please write tests before merging 
your code' )
-               ) ) );
-               $item->addClaim( new Claim( new PropertyValueSnak(
-                       $propertyId,
-                       new StringValue( 'or kittens will die' )
-               ) ) );
-
-               $property = Property::newEmpty();
-               $property->setId( $propertyId );
-
-               $property->setDataTypeId( 'string' );
-               $property->setLabel( 'en', 'kitten' );
-
-               $entityLookup->putEntity( $item );
-               $entityLookup->putEntity( $property );
-
-               return $entityLookup;
+               return new PropertyParserFunction( $parser, $entityId, 
$entityLookup,
+                       $propertyLabelResolver );
        }
 
-       public static function provideRenderForEntityId() {
+       /**
+        * @dataProvider provideGetRenderer
+        */
+       public function testGetRenderer( $languageCode, $outputType ) {
+               $parser = new \Parser();
+               $parserOptions = new \ParserOptions();
+               $parser->startExternalParse( null, $parserOptions, $outputType 
);
+               $instance = $this->getPropertyParserFunction( $parser, new 
ItemId( 'q42' ) );
+               $renderer = $instance->getRenderer( \Language::factory( 
$languageCode ) );
+               $this->assertInstanceOf( 
'Wikibase\PropertyParserFunctionRenderer', $renderer );
+       }
+
+       public function provideGetRenderer() {
                return array(
-                       array(
-                               'p1337',
-                               '(a kitten), (a kitten)',
-                               'Congratulations, you just killed a kitten'
-                       ),
-                       array(
-                               'kitten',
-                               '(a kitten), (a kitten)',
-                               'Congratulations, you just killed a kitten'
-                       ),
+                       array( 'en', \Parser::OT_HTML ),
+                       array( 'zh', \Parser::OT_WIKI ),
                );
        }
 
        /**
-        * @dataProvider provideRenderForEntityId
+        * @dataProvider provideIsParserUsingVariants
         */
-       public function testRenderForEntityId( $name, $expected, $info ) {
-               $parserFunction = $this->getDefaultInstance();
+       public function testIsParserUsingVariants(
+               $outputType, $interfaceMessage, $disableContentConversion, 
$disableTitleConversion, $expected
+       ) {
+               $parser = new \Parser();
+               $parserOptions = new \ParserOptions();
+               $parserOptions->setInterfaceMessage( $interfaceMessage );
+               $parserOptions->disableContentConversion( 
$disableContentConversion );
+               $parserOptions->disableTitleConversion( $disableTitleConversion 
);
+               $parser->startExternalParse( null, $parserOptions, $outputType 
);
+               $instance = $this->getPropertyParserFunction( $parser, new 
ItemId( 'q42' ) );
+               $this->assertEquals( $expected, 
$instance->isParserUsingVariants() );
+       }
 
-               $status = $parserFunction->renderForEntityId(
-                       new ItemId( 'Q42' ),
-                       $name
+       public function provideIsParserUsingVariants() {
+               return array(
+                       array( \Parser::OT_HTML, false, false, false, true ),
+                       array( \Parser::OT_WIKI, false, false, false, false ),
+                       array( \Parser::OT_PREPROCESS, false, false, false, 
false ),
+                       array( \Parser::OT_PLAIN, false, false, false, false ),
+                       array( \Parser::OT_HTML, true, false, false, false ),
+                       array( \Parser::OT_HTML, false, true, false, false ),
+                       array( \Parser::OT_HTML, false, false, true, true ),
                );
+       }
 
-               $this->assertTrue( $status->isOK() );
+       /**
+        * @dataProvider provideProcessRenderedArray
+        */
+       public function testProcessRenderedArray( $outputType, $textArray, 
$expected ) {
+               $parser = new \Parser();
+               $parserOptions = new \ParserOptions();
+               $parser->startExternalParse( null, $parserOptions, $outputType 
);
+               $instance = $this->getPropertyParserFunction( $parser, new 
ItemId( 'q42' ) );
+               $this->assertEquals( $expected, 
$instance->processRenderedArray( $textArray ) );
+       }
 
-               $text = $status->getValue();
-               $this->assertInternalType( 'string', $text );
-
-               $this->assertEquals(
-                       $expected,
-                       $text,
-                       $info
+       public function provideProcessRenderedArray() {
+               return array(
+                       array( \Parser::OT_HTML, array(
+                               'zh-cn' => 'fo&#60;ob&#62;ar',
+                               'zh-tw' => 'FO&#60;OB&#62;AR',
+                       ), '-{zh-cn:fo&#60;ob&#62;ar;zh-tw:FO&#60;OB&#62;AR;}-' 
),
                );
        }
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I5a461251c5c4b0ff6c5abd701707c5c04910fadf
Gerrit-PatchSet: 33
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Liangent <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Denny Vrandecic <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Liangent <[email protected]>
Gerrit-Reviewer: SPQRobin <[email protected]>
Gerrit-Reviewer: Tim Starling <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to