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

Change subject: (bug 54216) Implement entity deserialization
......................................................................


(bug 54216) Implement entity deserialization

Change-Id: If5a9fb5415552b4e8fc259ebfa1a3f6693d2ac7b
---
M lib/includes/serializers/AliasSerializer.php
M lib/includes/serializers/DescriptionSerializer.php
M lib/includes/serializers/EntitySerializer.php
M lib/includes/serializers/ItemSerializer.php
M lib/includes/serializers/LabelSerializer.php
M lib/includes/serializers/PropertySerializer.php
M lib/includes/serializers/SerializerFactory.php
M lib/includes/serializers/SiteLinkSerializer.php
M lib/tests/phpunit/serializers/AliasSerializerTest.php
M lib/tests/phpunit/serializers/DescriptionSerializerTest.php
M lib/tests/phpunit/serializers/EntitySerializerBaseTest.php
M lib/tests/phpunit/serializers/LabelSerializerTest.php
M lib/tests/phpunit/serializers/SiteLinkSerializerTest.php
13 files changed, 521 insertions(+), 125 deletions(-)

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



diff --git a/lib/includes/serializers/AliasSerializer.php 
b/lib/includes/serializers/AliasSerializer.php
index 6652ab4..bfaddeb 100644
--- a/lib/includes/serializers/AliasSerializer.php
+++ b/lib/includes/serializers/AliasSerializer.php
@@ -7,30 +7,13 @@
 /**
  * Serializer for aliases.
  *
- * 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
- * @ingroup WikibaseLib
  *
  * @licence GNU GPL v2+
  * @author Tobias Gritschacher < [email protected] >
+ * @author Katie Filbert < [email protected] >
  */
-class AliasSerializer extends SerializerObject {
+class AliasSerializer extends SerializerObject implements Unserializer {
 
        /**
         * @see ApiSerializerObject::$options
@@ -65,7 +48,7 @@
         * @return array
         * @throws InvalidArgumentException
         */
-       public final function getSerialized( $aliases ) {
+       final public function getSerialized( $aliases ) {
                if ( !is_array( $aliases ) ) {
                        throw new InvalidArgumentException( 'AliasSerializer 
can only serialize an array of aliases' );
                }
@@ -107,4 +90,56 @@
 
                return $value;
        }
+
+       /**
+        * @see Unserializer::newFromSerialization
+        *
+        * @since 0.4
+        *
+        * @param array $data
+        *
+        * @return array
+        */
+       public function newFromSerialization( array $data ) {
+               $aliases = array();
+
+               foreach( $data as $key => $aliasSet ) {
+                       if ( $key === '_element' ) {
+                               continue;
+                       }
+
+                       if ( !is_array( $aliasSet ) ) {
+                               throw new InvalidArgumentException( 'Alias data 
is invalid.' );
+                       }
+
+                       $lang = array_key_exists( 'language', $aliasSet ) ? 
$aliasSet['language'] : $key;
+
+                       if ( array_key_exists( 'value', $aliasSet ) ) {
+                               $aliases[$lang][] = $aliasSet['value'];
+                       } else {
+                               $aliases[$lang] = $this->extractAliasValues( 
$aliasSet );
+                       }
+               }
+
+               return $aliases;
+       }
+
+       /**
+        * @param array
+        *
+        * @return string[]
+        */
+       protected function extractAliasValues( array $aliasSet ) {
+               $aliases = array();
+
+               foreach( $aliasSet as $alias ) {
+                       if ( is_array( $alias ) && array_key_exists( 'value', 
$alias ) && is_string( $alias['value'] ) ) {
+                               $aliases[] = $alias['value'];
+                       } else {
+                               throw new InvalidArgumentException( 'Alias 
value is invalid' );
+                       }
+               }
+
+               return $aliases;
+       }
 }
diff --git a/lib/includes/serializers/DescriptionSerializer.php 
b/lib/includes/serializers/DescriptionSerializer.php
index c2d859d..3aaf611 100644
--- a/lib/includes/serializers/DescriptionSerializer.php
+++ b/lib/includes/serializers/DescriptionSerializer.php
@@ -7,30 +7,13 @@
 /**
  * Serializer for descriptions.
  *
- * 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
- * @ingroup WikibaseLib
  *
  * @licence GNU GPL v2+
  * @author Tobias Gritschacher < [email protected] >
+ * @author Katie Filbert < [email protected] >
  */
-class DescriptionSerializer extends SerializerObject {
+class DescriptionSerializer extends SerializerObject implements Unserializer {
 
        /**
         * @see ApiSerializerObject::$options
@@ -77,7 +60,7 @@
         * @return array
         * @throws InvalidArgumentException
         */
-       public final function getSerialized( $descriptions ) {
+       final public function getSerialized( $descriptions ) {
                if ( !is_array( $descriptions ) ) {
                        throw new InvalidArgumentException( 
'DescriptionSerializer can only serialize an array of descriptions' );
                }
@@ -103,8 +86,37 @@
         * @return array
         * @throws InvalidArgumentException
         */
-       public final function getSerializedMultilingualValues( $descriptions ) {
+       final public function getSerializedMultilingualValues( $descriptions ) {
                $descriptions = 
$this->multilingualSerializer->filterPreferredMultilingualValues( $descriptions 
);
                return $this->getSerialized( $descriptions );
        }
+
+       /**
+        * @see Unserializer::newFromSerialization
+        *
+        * @since 0.4
+        *
+        * @param array $data
+        *
+        * @return array
+        */
+       public function newFromSerialization( array $data ) {
+               $descriptions = array();
+
+               foreach( $data as $key => $description ) {
+                       if ( $key === '_element' ) {
+                               continue;
+                       }
+
+                       if ( !is_array( $description ) || !array_key_exists( 
'language', $description )
+                               || !array_key_exists( 'value', $description ) ) 
{
+                               throw new InvalidArgumentException( 
'Description serialization is invalid.' );
+                       }
+
+                       $lang = $description['language'];
+                       $descriptions[$lang] = $description['value'];
+               }
+
+               return $descriptions;
+       }
 }
diff --git a/lib/includes/serializers/EntitySerializer.php 
b/lib/includes/serializers/EntitySerializer.php
index 1acb0b6..ec13ae5 100644
--- a/lib/includes/serializers/EntitySerializer.php
+++ b/lib/includes/serializers/EntitySerializer.php
@@ -3,8 +3,12 @@
 namespace Wikibase\Lib\Serializers;
 
 use ApiResult;
+use InvalidArgumentException;
 use MWException;
+use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\DataModel\Entity\EntityIdValue;
 use Wikibase\Entity;
+use Wikibase\EntityFactory;
 
 /**
  * Serializer for entities.
@@ -15,8 +19,9 @@
  * @author Jeroen De Dauw < [email protected] >
  * @author John Erling Blad < [email protected] >
  * @author Tobias Gritschacher < [email protected] >
+ * @author Katie Filbert < [email protected] >
  */
-class EntitySerializer extends SerializerObject {
+class EntitySerializer extends SerializerObject implements Unserializer {
 
        /**
         * @see ApiSerializerObject::$options
@@ -106,4 +111,64 @@
                // Stub, override expected
                return array();
        }
+
+       /**
+        * @see Unserializer::newFromSerialization
+        *
+        * @since 0.4
+        *
+        * @param array $data
+        *
+        * @return Entity
+        * @throws InvalidArgumentException
+        */
+       public function newFromSerialization( array $data ) {
+               // @todo inject EntityFactory
+               $entityFactory = new EntityFactory();
+               $validTypes = $entityFactory->getEntityTypes();
+
+               if ( !array_key_exists( 'type', $data ) || !in_array( 
$data['type'], $validTypes ) ) {
+                       throw new InvalidArgumentException( 'Invalid entity 
serialization' );
+               }
+
+               $entityType = $data['type'];
+               $entity = $entityFactory->newEmpty( $entityType );
+
+               if ( array_key_exists( 'id', $data ) ) {
+                       $idParser = new BasicEntityIdParser();
+                       $entityId = $idParser->parse( $data['id'] );
+
+                       if ( $entityId->getEntityType() !== $entityType ) {
+                               throw new InvalidArgumentException( 'Mismatched 
entity type and entity id in serialization.' );
+                       }
+
+                       $entity->setId( $entityId );
+               }
+
+               if ( array_key_exists( 'aliases', $data ) ) {
+                       $aliasSerializer = new AliasSerializer( $this->options 
);
+                       $aliases = $aliasSerializer->newFromSerialization( 
$data['aliases'] );
+                       $entity->setAllAliases( $aliases );
+               }
+
+               if ( array_key_exists( 'descriptions', $data ) ) {
+                       $descriptionSerializer = new DescriptionSerializer( 
$this->options );
+                       $descriptions = 
$descriptionSerializer->newFromSerialization( $data['descriptions'] );
+                       $entity->setDescriptions( $descriptions );
+               }
+
+               if ( array_key_exists( 'labels', $data ) ) {
+                       $labelSerializer = new LabelSerializer( $this->options 
);
+                       $labels = $labelSerializer->newFromSerialization( 
$data['labels'] );
+                       $entity->setLabels( $labels );
+               }
+
+               if ( array_key_exists( 'claims', $data ) ) {
+                       $claimsSerializer = new ClaimsSerializer( 
$this->options );
+                       $claims = $claimsSerializer->newFromSerialization( 
$data['claims'] );
+                       $entity->setClaims( $claims );
+               }
+
+               return $entity;
+       }
 }
diff --git a/lib/includes/serializers/ItemSerializer.php 
b/lib/includes/serializers/ItemSerializer.php
index e326357..1a9320d 100644
--- a/lib/includes/serializers/ItemSerializer.php
+++ b/lib/includes/serializers/ItemSerializer.php
@@ -1,39 +1,23 @@
 <?php
-
 namespace Wikibase\Lib\Serializers;
+
 use MWException;
+use Wikibase\DataModel\SimpleSiteLink;
 use Wikibase\Entity;
 use Wikibase\Item;
 
 /**
  * Serializer for items.
  *
- * 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.2
- *
- * @file
- * @ingroup WikibaseLib
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  * @author John Erling Blad < [email protected] >
  * @author Tobias Gritschacher < [email protected] >
+ * @author Katie Filbert < [email protected] >
  */
-class ItemSerializer extends EntitySerializer {
+class ItemSerializer extends EntitySerializer implements Unserializer {
 
        /**
         * @since 0.4
@@ -83,4 +67,28 @@
 
                return $serialization;
        }
+
+       /**
+        * @see Unserializer::newFromSerialization
+        *
+        * @since 0.5
+        *
+        * @param array $data
+        *
+        * @return Item
+        */
+       public function newFromSerialization( array $data ) {
+               $item = parent::newFromSerialization( $data );
+
+               if ( array_key_exists( 'sitelinks', $data ) ) {
+                       $siteLinkSerializer = new SiteLinkSerializer( 
$this->options );
+                       $siteLinks = $siteLinkSerializer->newFromSerialization( 
$data['sitelinks'] );
+
+                       foreach( $siteLinks as $siteLink ) {
+                               $item->addSimpleSiteLink( $siteLink );
+                       }
+               }
+
+               return $item;
+       }
 }
diff --git a/lib/includes/serializers/LabelSerializer.php 
b/lib/includes/serializers/LabelSerializer.php
index 3bfe10a..9e647db 100644
--- a/lib/includes/serializers/LabelSerializer.php
+++ b/lib/includes/serializers/LabelSerializer.php
@@ -7,21 +7,6 @@
 /**
  * Serializer for labels.
  *
- * 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
@@ -29,8 +14,9 @@
  *
  * @licence GNU GPL v2+
  * @author Tobias Gritschacher < [email protected] >
+ * @author Katie Filbert < [email protected] >
  */
-class LabelSerializer extends SerializerObject {
+class LabelSerializer extends SerializerObject implements Unserializer {
 
        /**
         * @see ApiSerializerObject::$options
@@ -77,7 +63,7 @@
         * @return array
         * @throws InvalidArgumentException
         */
-       public final function getSerialized( $labels ) {
+       final public function getSerialized( $labels ) {
                if ( !is_array( $labels ) ) {
                        throw new InvalidArgumentException( 'LabelSerializer 
can only serialize an array of labels' );
                }
@@ -103,8 +89,38 @@
         * @return array
         * @throws InvalidArgumentException
         */
-       public final function getSerializedMultilingualValues( $labels ) {
+       final public function getSerializedMultilingualValues( $labels ) {
                $labels = 
$this->multilingualSerializer->filterPreferredMultilingualValues( $labels );
                return $this->getSerialized( $labels );
        }
+
+       /**
+        * @see Unserializer::newFromSerialization
+        *
+        * @since 0.5
+        *
+        * @param array $data
+        *
+        * @return array $labels
+        */
+       public function newFromSerialization( array $data ) {
+               $labels = array();
+
+               foreach( $data as $key => $label ) {
+                       if ( $key === '_element' ) {
+                               continue;
+                       }
+
+                       if ( is_array( $label ) && array_key_exists( 
'language', $label )
+                               && array_key_exists( 'value', $label )
+                       ) {
+                               $lang = $label['language'];
+                               $labels[$lang] = $label['value'];
+                       } else {
+                               throw new InvalidArgumentException( 'label 
serialization is invalid' );
+                       }
+               }
+
+               return $labels;
+       }
 }
diff --git a/lib/includes/serializers/PropertySerializer.php 
b/lib/includes/serializers/PropertySerializer.php
index 5d9b3f2..0ac6900 100644
--- a/lib/includes/serializers/PropertySerializer.php
+++ b/lib/includes/serializers/PropertySerializer.php
@@ -2,27 +2,12 @@
 
 namespace Wikibase\Lib\Serializers;
 
-use MWException;
+use InvalidArgumentException;
 use Wikibase\Entity;
 use Wikibase\Property;
 
 /**
  * Serializer for properties.
- *
- * 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.3
  *
@@ -31,8 +16,9 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ * @author Katie Filbert < [email protected] >
  */
-class PropertySerializer extends EntitySerializer {
+class PropertySerializer extends EntitySerializer implements Unserializer {
 
        /**
         * @see EntitySerializer::getEntityTypeSpecificSerialization
@@ -42,11 +28,12 @@
         * @param Entity $property
         *
         * @return array
-        * @throws MWException
+        * @throws InvalidArgumentException
         */
        protected function getEntityTypeSpecificSerialization( Entity $property 
) {
                if ( !( $property instanceof Property ) ) {
-                       throw new MWException( 'PropertySerializer can only 
serialize Property implementing objects' );
+                       throw new InvalidArgumentException( 'PropertySerializer 
can only serialize '
+                               . 'Property implementing objects' );
                }
 
                $serialization = array();
@@ -58,4 +45,22 @@
                return $serialization;
        }
 
+       /**
+        * @param array $data
+        *
+        * $return Property
+        * @throws InvalidArgumentException
+        */
+       public function newFromSerialization( array $data ) {
+               $entity = parent::newFromSerialization( $data );
+
+               if ( !array_key_exists( 'datatype', $data ) ) {
+                       throw new InvalidArgumentException( 'Property data type 
missing in serialization.' );
+               }
+
+               $entity->setDataTypeId( $data['datatype'] );
+
+               return $entity;
+       }
+
 }
diff --git a/lib/includes/serializers/SerializerFactory.php 
b/lib/includes/serializers/SerializerFactory.php
index 2260cf7..6f222ea 100644
--- a/lib/includes/serializers/SerializerFactory.php
+++ b/lib/includes/serializers/SerializerFactory.php
@@ -80,6 +80,10 @@
                }
 
                switch ( ltrim( $className, '\\' ) ) {
+                       case 'Wikibase\Item':
+                               return new ItemSerializer( $options );
+                       case 'Wikibase\Property':
+                               return new PropertySerializer( $options );
                        case 'Wikibase\Snak':
                                return new SnakSerializer( $options );
                        case 'Wikibase\Reference':
@@ -93,4 +97,21 @@
                throw new OutOfBoundsException( '"' . $className . '" has no 
associated unserializer' );
        }
 
-}
\ No newline at end of file
+       /**
+        * @param string $entityType
+        *
+        * @throws OutOfBoundsException
+        * @return Unserializer
+        */
+       public function newUnserializerForEntity( $entityType, $options ) {
+               switch( $entityType ) {
+                       case 'wikibase-item':
+                               return new ItemSerializer( $options );
+                       case 'wikibase-property':
+                               return new PropertySerializer( $options );
+                       default:
+                               throw new InvalidArgumentException( 
'$entityType is invalid' );
+               }
+       }
+
+}
diff --git a/lib/includes/serializers/SiteLinkSerializer.php 
b/lib/includes/serializers/SiteLinkSerializer.php
index 0c83f02..f1d6069 100644
--- a/lib/includes/serializers/SiteLinkSerializer.php
+++ b/lib/includes/serializers/SiteLinkSerializer.php
@@ -4,36 +4,21 @@
 
 use InvalidArgumentException;
 use Wikibase\SiteLink;
+use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\SimpleSiteLink;
 
 /**
  * Serializer for sitelinks.
  *
- * 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
- * @ingroup WikibaseLib
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  * @author John Erling Blad < [email protected] >
  * @author Tobias Gritschacher < [email protected] >
  * @author Michał Łazowik
+ * @author Katie Filbert < [email protected] >
  */
 class SiteLinkSerializer extends SerializerObject {
 
@@ -77,7 +62,7 @@
         * @return array
         * @throws InvalidArgumentException
         */
-       public final function getSerialized( $siteLinks ) {
+       final public function getSerialized( $siteLinks ) {
                if ( !is_array( $siteLinks ) ) {
                        throw new InvalidArgumentException( 'SiteLinkSerializer 
can only serialize an array of sitelinks' );
                }
@@ -85,7 +70,7 @@
                $serialization = array();
 
                $includeUrls = in_array( 'sitelinks/urls', 
$this->options->getProps() );
-               $setRemoved = in_array( 'sitelinks/removed' , 
$this->options->getProps() );
+               $setRemoved = in_array( 'sitelinks/removed', 
$this->options->getProps() );
 
                foreach ( $this->sortSiteLinks( $siteLinks ) as $link ) {
                        $response = array(
@@ -110,7 +95,7 @@
                                }
 
                                if ( $this->options->shouldIndexTags() ) {
-                                       $this->setIndexedTagName( $badges , 
'badge' );
+                                       $this->setIndexedTagName( $badges, 
'badge' );
                                }
 
                                $response['badges'] = $badges;
@@ -173,4 +158,56 @@
 
                return $siteLinks;
        }
+
+       /**
+        * @see Unserializer::newFromSerialization
+        *
+        * @since 0.5
+        *
+        * @param array $data
+        *
+        * @return SimpleSiteLink[]
+        * @throws InvalidArgumentException
+        */
+       public function newFromSerialization( array $data ) {
+               $siteLinks = array();
+
+               foreach( $data as $siteLink ) {
+                       if ( !array_key_exists( 'site', $siteLink ) || 
!array_key_exists( 'title', $siteLink ) ) {
+                               throw new InvalidArgumentException( 'Site link 
serialization is invalid.' );
+                       }
+
+                       if ( array_key_exists( 'badges', $siteLink ) ) {
+                               $badges = $this->extractBadges( 
$siteLink['badges'] );
+                       }
+
+                       $siteLinks[] = new SimpleSiteLink( $siteLink['site'], 
$siteLink['title'], $badges );
+               }
+
+               return $siteLinks;
+       }
+
+       /**
+        * @param array $badges
+        *
+        * @return ItemId[]
+        */
+       protected function extractBadges( array $data ) {
+               $idParser = new BasicEntityIdParser();
+
+               $badges = array();
+
+               foreach( $data as $badge ) {
+                       $itemId = $idParser->parse( $badge );
+
+                       if ( ! $itemId instanceof ItemId ) {
+                               throw new InvalidArgumentException( 'Site link 
badges must be valid item ids.' );
+                       }
+
+                       $badges[] = $itemId;
+
+               }
+
+               return $badges;
+       }
 }
diff --git a/lib/tests/phpunit/serializers/AliasSerializerTest.php 
b/lib/tests/phpunit/serializers/AliasSerializerTest.php
index bd93ef5..eaa35b0 100644
--- a/lib/tests/phpunit/serializers/AliasSerializerTest.php
+++ b/lib/tests/phpunit/serializers/AliasSerializerTest.php
@@ -95,4 +95,40 @@
                $aliasSerializer = new AliasSerializer();
                $serializedAliases = $aliasSerializer->getSerialized( $aliases 
);
        }
+
+       /**
+        * @dataProvider newFromSerializationProvider
+        */
+       public function testNewFromSerialization( $expected, $serialized, 
$message ) {
+               $aliasSerializer = new AliasSerializer(
+                       new MultiLangSerializationOptions()
+               );
+
+               $deserializedAliases = $aliasSerializer->newFromSerialization( 
$serialized );
+               $this->assertEquals( $expected, $deserializedAliases, $message 
);
+       }
+
+       public function newFromSerializationProvider() {
+               $options = new MultiLangSerializationOptions();
+               $options->setIndexTags( true );
+               $aliases = array(
+                       "en" => array( "Roma", "Rome, Italy", "The Eternal 
City" ),
+                       "de" => array( "Die ewige Stadt" ),
+                       "it" => array( "Urbe", "Città eterna" ),
+               );
+
+               $aliasSerializer = new AliasSerializer( $options );
+               $serialized = $aliasSerializer->getSerialized( $aliases );
+
+               $options->setIndexTags( false );
+
+               $aliasSerializer = new AliasSerializer( $options );
+               $serialized2 = $aliasSerializer->getSerialized( $aliases );
+
+               $data = array();
+               $data[] = array( $aliases, $serialized, 'serialization with 
index tags' );
+               $data[] = array( $aliases, $serialized2, 'serialization without 
index tags' );
+
+               return $data;
+       }
 }
diff --git a/lib/tests/phpunit/serializers/DescriptionSerializerTest.php 
b/lib/tests/phpunit/serializers/DescriptionSerializerTest.php
index 78c4c24..b3be587 100644
--- a/lib/tests/phpunit/serializers/DescriptionSerializerTest.php
+++ b/lib/tests/phpunit/serializers/DescriptionSerializerTest.php
@@ -246,7 +246,7 @@
                        'key-fr' => 
$languageFallbackChainFactory->newFromLanguageCode( 'fr' ),
                        'sr-ec' => 
$languageFallbackChainFactory->newFromLanguageCode( 'zh-cn', 
LanguageFallbackChainFactory::FALLBACK_SELF ),
                        'gan-hant' => 
$languageFallbackChainFactory->newFromLanguageCode( 'gan-hant' ),
-               ) );
+               ) );
                $values = array(
                        "en" => "capital city of Italy",
                        "de" => "Hauptstadt von Italien",
@@ -258,4 +258,40 @@
 
                return $validArgs;
        }
+
+       /**
+        * @dataProvider newFromSerializationProvider
+        */
+       public function testNewFromSerialization( $expected, $serialized, 
$message ) {
+               $descriptionSerializer = new DescriptionSerializer(
+                       new MultiLangSerializationOptions()
+               );
+
+               $descriptions = $descriptionSerializer->newFromSerialization( 
$serialized );
+               $this->assertEquals( $expected, $descriptions, $message );
+       }
+
+       public function newFromSerializationProvider() {
+               $options = new MultiLangSerializationOptions();
+               $options->setIndexTags( true );
+
+               $descriptionSerializer = new DescriptionSerializer( $options );
+
+               $descriptions = array(
+                       "en" => "capital city of Italy",
+                       "de" => "Hauptstadt von Italien",
+                       "fi" => "kunta Italiassa"
+               );
+
+               $serialized = $descriptionSerializer->getSerialized( 
$descriptions );
+
+               $options->setIndexTags( false );
+               $descriptionSerializer = new DescriptionSerializer( $options );
+               $serialized2 = $descriptionSerializer->getSerialized( 
$descriptions );
+
+               return array(
+                       array( $descriptions, $serialized, 'serialization with 
index tags' ),
+                       array( $descriptions, $serialized2, 'serialization 
without index tags' )
+               );
+       }
 }
diff --git a/lib/tests/phpunit/serializers/EntitySerializerBaseTest.php 
b/lib/tests/phpunit/serializers/EntitySerializerBaseTest.php
index 95883d3..3a4c9dc 100644
--- a/lib/tests/phpunit/serializers/EntitySerializerBaseTest.php
+++ b/lib/tests/phpunit/serializers/EntitySerializerBaseTest.php
@@ -3,7 +3,12 @@
 namespace Wikibase\Test;
 
 use ValueFormatters\FormatterOptions;
+use DataValues\StringValue;
+use Wikibase\Claim;
 use Wikibase\Entity;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\PropertyValueSnak;
+use Wikibase\Lib\ClaimGuidGenerator;
 use Wikibase\Lib\EntityIdFormatter;
 use Wikibase\Lib\Serializers\EntitySerializationOptions;
 
@@ -26,7 +31,7 @@
         *
         * @return Entity
         */
-       protected abstract function getEntityInstance();
+       abstract protected function getEntityInstance();
 
        protected function getInstance() {
                $class = $this->getClass();
@@ -80,7 +85,7 @@
                                                        'language' => 'de',
                                                ),
                                        ),
-                               ),
+                               )
                        ),
                        $options
                );
@@ -123,6 +128,53 @@
                        $options
                );
 
+               $entity2 = $this->getEntityInstance();
+               $options->setProps( array( 'descriptions', 'labels', 'claims', 
'aliases' ) );
+
+               $claim = new Claim(
+                       new PropertyValueSnak(
+                               new PropertyId( 'P42' ),
+                               new StringValue( 'foobar!' )
+                       )
+               );
+
+               $claimGuidGenerator = new ClaimGuidGenerator( $entity2->getId() 
);
+               $claim->setGuid( $claimGuidGenerator->newGuid() );
+
+               $entity2->setLabel( 'en', 'foo' );
+               $entity2->addClaim( $claim );
+
+               $validArgs[] = array(
+                       $entity2,
+                       array(
+                               'id' => $this->getFormattedIdForEntity( 
$entity2 ),
+                               'type' => $entity2->getType(),
+                               'labels' => array(
+                                       'en' => array(
+                                               'language' => 'en',
+                                               'value' => 'foo',
+                                       )
+                               ),
+                               'claims' => array(
+                                       'P42' => array(
+                                               array(
+                                                       'id' => 
$claim->getGuid(),
+                                                       'mainsnak' => array(
+                                                               'snaktype' => 
'value',
+                                                               'property' => 
'P42',
+                                                               'datavalue' => 
array(
+                                                                       'value' 
=> 'foobar!',
+                                                                       'type' 
=> 'string'
+                                                               )
+                                                       ),
+                                                       'type' => 'claim'
+                                               )
+                                       )
+                               )
+                       ),
+                       $options
+               );
+
                return $validArgs;
        }
 
diff --git a/lib/tests/phpunit/serializers/LabelSerializerTest.php 
b/lib/tests/phpunit/serializers/LabelSerializerTest.php
index 8fb5b10..f65944e 100644
--- a/lib/tests/phpunit/serializers/LabelSerializerTest.php
+++ b/lib/tests/phpunit/serializers/LabelSerializerTest.php
@@ -246,7 +246,7 @@
                        'key-fr' => 
$languageFallbackChainFactory->newFromLanguageCode( 'fr' ),
                        'sr-ec' => 
$languageFallbackChainFactory->newFromLanguageCode( 'zh-cn', 
LanguageFallbackChainFactory::FALLBACK_SELF ),
                        'gan-hant' => 
$languageFallbackChainFactory->newFromLanguageCode( 'gan-hant' ),
-               ) );
+               ) );
                $values = array(
                        "en" => "capital city of Italy",
                        "de" => "Hauptstadt von Italien",
@@ -258,4 +258,40 @@
 
                return $validArgs;
        }
+
+       /**
+        * @dataProvider newFromSerializationProvider
+        */
+       public function testNewFromSerialization( $expected, $serialized, 
$message ) {
+               $labelSerializer = new LabelSerializer(
+                       new MultiLangSerializationOptions()
+               );
+
+               $labels = $labelSerializer->newFromSerialization( $serialized );
+               $this->assertEquals( $expected, $labels, $message );
+       }
+
+       public function newFromSerializationProvider() {
+               $options = new MultiLangSerializationOptions();
+               $options->setIndexTags( true );
+               $labelSerializer = new LabelSerializer( $options );
+
+               $labels = array(
+                       "en" => "Rome",
+                       "de" => "Rom",
+                       "it" => "Roma"
+               );
+
+               $serialized = $labelSerializer->getSerialized( $labels );
+
+               $options->setIndexTags( false );
+               $labelSerializer = new LabelSerializer( $options );
+
+               $serialized2 = $labelSerializer->getSerialized( $labels );
+
+               return array(
+                       array( $labels, $serialized, 'serialization with index 
tags' ),
+                       array( $labels, $serialized2, 'serialization without 
index tags' )
+               );
+       }
 }
diff --git a/lib/tests/phpunit/serializers/SiteLinkSerializerTest.php 
b/lib/tests/phpunit/serializers/SiteLinkSerializerTest.php
index e4e0624..a2d48ce 100644
--- a/lib/tests/phpunit/serializers/SiteLinkSerializerTest.php
+++ b/lib/tests/phpunit/serializers/SiteLinkSerializerTest.php
@@ -5,6 +5,7 @@
 use InvalidArgumentException;
 use SiteSQLStore;
 use Wikibase\Lib\Serializers\EntitySerializationOptions;
+use Wikibase\Lib\Serializers\MultiLangSerializationOptions;
 use Wikibase\Lib\Serializers\SiteLinkSerializer;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\SimpleSiteLink;
@@ -112,4 +113,40 @@
                $formatterOptions = new FormatterOptions();
                return new EntityIdFormatter( $formatterOptions );
        }
+
+       /**
+        * @dataProvider newFromSerializationProvider
+        */
+       public function testNewFromSerialization( $expected, $serialized ) {
+               // todo inject / mock
+               $siteStore = SiteSQLStore::newInstance();
+               $options = new EntitySerializationOptions();
+               $siteLinkSerializer = new SiteLinkSerializer( $options, 
$siteStore );
+
+               $simpleSiteLinks = $siteLinkSerializer->newFromSerialization( 
$serialized );
+               $this->assertEquals( $expected, $simpleSiteLinks );
+       }
+
+       public function newFromSerializationProvider() {
+               $siteLinks = array();
+
+               $badges = array(
+                       new ItemId( 'Q944' ),
+                       new ItemId( 'Q1004' )
+               );
+
+               $siteLinks[] = new SimpleSiteLink( 'enwiki', 'Cat' );
+               $siteLinks[] = new SimpleSiteLink( 'dewiki', 'Katze', $badges );
+
+               // todo inject / mock
+               $siteStore = SiteSQLStore::newInstance();
+               $options = new EntitySerializationOptions();
+
+               $siteLinkSerializer = new SiteLinkSerializer( $options, 
$siteStore );
+               $serialized = $siteLinkSerializer->getSerialized( $siteLinks );
+
+               return array(
+                       array( $siteLinks, $serialized )
+               );
+       }
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If5a9fb5415552b4e8fc259ebfa1a3f6693d2ac7b
Gerrit-PatchSet: 22
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>
Gerrit-Reviewer: Ataherivand <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Pragunbhutani <[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