Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663
This commit is contained in:
parent
eb34d130a8
commit
f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions
|
@ -18,8 +18,8 @@ function serialization_help($route_name, RouteMatchInterface $route_match) {
|
|||
$output .= '<p>' . t('The Serialization module provides a service for serializing and deserializing data to and from formats such as JSON and XML.') . '</p>';
|
||||
$output .= '<p>' . t('Serialization is the process of converting data structures like arrays and objects into a string. This allows the data to be represented in a way that is easy to exchange and store (for example, for transmission over the Internet or for storage in a local file system). These representations can then be deserialized to get back to the original data structures.') . '</p>';
|
||||
$output .= '<p>' . t('The serializer splits this process into two parts. Normalization converts an object to a normalized array structure. Encoding takes that array and converts it to a string.') . '</p>';
|
||||
$output .= '<p>' . t('This module does not have a user interface. It is used by other modules which need to serialize data, such as <a href="!rest">REST</a>.', array('!rest' => (\Drupal::moduleHandler()->moduleExists('rest')) ? \Drupal::url('help.page', array('name' => 'rest')) : '#')) . '</p>';
|
||||
$output .= '<p>' . t('For more information, see the <a href="!doc_url">online documentation for the Serialization module</a>.', array('!doc_url' => 'https://www.drupal.org/documentation/modules/serialization')) . '</p>';
|
||||
$output .= '<p>' . t('This module does not have a user interface. It is used by other modules which need to serialize data, such as <a href=":rest">REST</a>.', array(':rest' => (\Drupal::moduleHandler()->moduleExists('rest')) ? \Drupal::url('help.page', array('name' => 'rest')) : '#')) . '</p>';
|
||||
$output .= '<p>' . t('For more information, see the <a href=":doc_url">online documentation for the Serialization module</a>.', array(':doc_url' => 'https://www.drupal.org/documentation/modules/serialization')) . '</p>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,13 @@ services:
|
|||
class: Drupal\serialization\Normalizer\ComplexDataNormalizer
|
||||
tags:
|
||||
- { name: normalizer }
|
||||
serializer.normalizer.entity_reference_field_item:
|
||||
class: Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
|
||||
tags:
|
||||
# Set the priority lower than the hal entity reference field item
|
||||
# normalizer, so that we do not replace that for hal_json.
|
||||
# @todo Find a better way for this in https://www.drupal.org/node/2575761.
|
||||
- { name: normalizer, priority: 5 }
|
||||
serializer.normalizer.list:
|
||||
class: Drupal\serialization\Normalizer\ListNormalizer
|
||||
tags:
|
||||
|
@ -31,7 +38,7 @@ services:
|
|||
tags:
|
||||
- { name: normalizer, priority: 20 }
|
||||
serializer.normalizer.safe_string:
|
||||
class: Drupal\serialization\Normalizer\SafeStringNormalizer
|
||||
class: Drupal\serialization\Normalizer\MarkupNormalizer
|
||||
tags:
|
||||
- { name: normalizer }
|
||||
serializer.normalizer.typed_data:
|
||||
|
|
|
@ -34,7 +34,7 @@ class ComplexDataNormalizer extends NormalizerBase {
|
|||
public function normalize($object, $format = NULL, array $context = array()) {
|
||||
$attributes = array();
|
||||
foreach ($object as $name => $field) {
|
||||
$attributes[$name] = $this->serializer->normalize($field, $format);
|
||||
$attributes[$name] = $this->serializer->normalize($field, $format, $context);
|
||||
}
|
||||
return $attributes;
|
||||
}
|
||||
|
|
|
@ -43,22 +43,54 @@ class EntityNormalizer extends ComplexDataNormalizer implements DenormalizerInte
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = NULL, array $context = array()) {
|
||||
if (empty($context['entity_type'])) {
|
||||
throw new UnexpectedValueException('Entity type parameter must be included in context.');
|
||||
public function denormalize($data, $class, $format = NULL, array $context = []) {
|
||||
// Get the entity type ID while letting context override the $class param.
|
||||
$entity_type_id = !empty($context['entity_type']) ? $context['entity_type'] : $this->entityManager->getEntityTypeFromClass($class);
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type_definition */
|
||||
// Get the entity type definition.
|
||||
$entity_type_definition = $this->entityManager->getDefinition($entity_type_id, FALSE);
|
||||
|
||||
// Don't try to create an entity without an entity type id.
|
||||
if (!$entity_type_definition) {
|
||||
throw new UnexpectedValueException(sprintf('The specified entity type "%s" does not exist. A valid etnity type is required for denormalization', $entity_type_id));
|
||||
}
|
||||
|
||||
$entity_type = $this->entityManager->getDefinition($context['entity_type']);
|
||||
// The bundle property will be required to denormalize a bundleable entity.
|
||||
if ($entity_type_definition->hasKey('bundle')) {
|
||||
$bundle_key = $entity_type_definition->getKey('bundle');
|
||||
// Get the base field definitions for this entity type.
|
||||
$base_field_definitions = $this->entityManager->getBaseFieldDefinitions($entity_type_id);
|
||||
|
||||
// The bundle property behaves differently from other entity properties.
|
||||
// i.e. the nested structure with a 'value' key does not work.
|
||||
if ($entity_type->hasKey('bundle')) {
|
||||
$bundle_key = $entity_type->getKey('bundle');
|
||||
$type = $data[$bundle_key][0]['value'];
|
||||
$data[$bundle_key] = $type;
|
||||
// Get the ID key from the base field definition for the bundle key or
|
||||
// default to 'value'.
|
||||
$key_id = isset($base_field_definitions[$bundle_key]) ? $base_field_definitions[$bundle_key]->getFieldStorageDefinition()->getMainPropertyName() : 'value';
|
||||
|
||||
// Normalize the bundle if it is not explicitly set.
|
||||
$data[$bundle_key] = isset($data[$bundle_key][0][$key_id]) ? $data[$bundle_key][0][$key_id] : (isset($data[$bundle_key]) ? $data[$bundle_key] : NULL);
|
||||
|
||||
// Get the bundle entity type from the entity type definition.
|
||||
$bundle_type_id = $entity_type_definition->getBundleEntityType();
|
||||
$bundle_types = $bundle_type_id ? $this->entityManager->getStorage($bundle_type_id)->getQuery()->execute() : [];
|
||||
|
||||
// Make sure a bundle has been provided.
|
||||
if (!is_string($data[$bundle_key])) {
|
||||
throw new UnexpectedValueException('A string must be provided as a bundle value.');
|
||||
}
|
||||
|
||||
// Make sure the submitted bundle is a valid bundle for the entity type.
|
||||
if ($bundle_types && !in_array($data[$bundle_key], $bundle_types)) {
|
||||
throw new UnexpectedValueException(sprintf('"%s" is not a valid bundle type for denormalization.', $data[$bundle_key]));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->entityManager->getStorage($context['entity_type'])->create($data);
|
||||
// Create the entity from data.
|
||||
$entity = $this->entityManager->getStorage($entity_type_id)->create($data);
|
||||
|
||||
// Pass the names of the fields whose values can be merged.
|
||||
// @todo https://www.drupal.org/node/2456257 remove this.
|
||||
$entity->_restSubmittedFields = array_keys($data);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\serialization\Normalizer\FileFieldItemNormalizer.
|
||||
*/
|
||||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||
|
||||
/**
|
||||
* Adds the file URI to embedded file entities.
|
||||
*/
|
||||
class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = EntityReferenceItem::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field_item, $format = NULL, array $context = []) {
|
||||
$values = parent::normalize($field_item, $format, $context);
|
||||
|
||||
// Add a 'url' value if there is a reference and a canonical URL. Hard code
|
||||
// 'canonical' here as config entities override the default $rel parameter
|
||||
// value to 'edit-form.
|
||||
/** @var \Drupal\Core\Entity\EntityInterface $entity */
|
||||
if (($entity = $field_item->get('entity')->getValue()) && ($url = $entity->url('canonical'))) {
|
||||
$values['url'] = $url;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,22 +2,22 @@
|
|||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\serialization\Normalizer\SafeStringNormalizer.
|
||||
* Contains \Drupal\serialization\Normalizer\MarkupNormalizer.
|
||||
*/
|
||||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
/**
|
||||
* Normalizes SafeStringInterface objects into a string.
|
||||
* Normalizes MarkupInterface objects into a string.
|
||||
*/
|
||||
class SafeStringNormalizer extends NormalizerBase {
|
||||
class MarkupNormalizer extends NormalizerBase {
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = array('Drupal\Component\Utility\SafeStringInterface');
|
||||
protected $supportedInterfaceOrClass = array('Drupal\Component\Render\MarkupInterface');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
|
@ -20,7 +20,7 @@ class EntityResolverTest extends NormalizerTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('entity_reference', 'hal', 'rest');
|
||||
public static $modules = ['hal', 'rest'];
|
||||
|
||||
/**
|
||||
* The format being tested.
|
||||
|
|
|
@ -39,6 +39,13 @@ class EntitySerializationTest extends NormalizerTestBase {
|
|||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* The test user.
|
||||
*
|
||||
* @var \Drupal\user\Entity\User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* The serializer service.
|
||||
*
|
||||
|
@ -58,10 +65,19 @@ class EntitySerializationTest extends NormalizerTestBase {
|
|||
|
||||
// User create needs sequence table.
|
||||
$this->installSchema('system', array('sequences'));
|
||||
|
||||
// Create a test user to use as the entity owner.
|
||||
$this->user = \Drupal::entityManager()->getStorage('user')->create([
|
||||
'name' => 'serialization_test_user',
|
||||
'mail' => 'foo@example.com',
|
||||
'pass' => '123456',
|
||||
]);
|
||||
$this->user->save();
|
||||
|
||||
// Create a test entity to serialize.
|
||||
$this->values = array(
|
||||
'name' => $this->randomMachineName(),
|
||||
'user_id' => \Drupal::currentUser()->id(),
|
||||
'user_id' => $this->user->id(),
|
||||
'field_test_text' => array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'full_html',
|
||||
|
@ -99,7 +115,10 @@ class EntitySerializationTest extends NormalizerTestBase {
|
|||
array('value' => $this->entity->created->value),
|
||||
),
|
||||
'user_id' => array(
|
||||
array('target_id' => $this->values['user_id']),
|
||||
array(
|
||||
'target_id' => $this->user->id(),
|
||||
'url' => $this->user->url(),
|
||||
),
|
||||
),
|
||||
'revision_id' => array(
|
||||
array('value' => 1),
|
||||
|
@ -128,22 +147,15 @@ class EntitySerializationTest extends NormalizerTestBase {
|
|||
* override some default access controls.
|
||||
*/
|
||||
public function testUserNormalize() {
|
||||
$account = User::create([
|
||||
'name' => 'serialization_test_user',
|
||||
'mail' => 'foo@example.com',
|
||||
'pass' => '123456',
|
||||
]);
|
||||
$account->save();
|
||||
|
||||
// Test password isn't available.
|
||||
$normalized = $this->serializer->normalize($account);
|
||||
$normalized = $this->serializer->normalize($this->user);
|
||||
|
||||
$this->assertFalse(array_key_exists('pass', $normalized), '"pass" key does not exist in normalized user');
|
||||
$this->assertFalse(array_key_exists('mail', $normalized), '"mail" key does not exist in normalized user');
|
||||
|
||||
// Test again using our test user, so that our access control override will
|
||||
// allow password viewing.
|
||||
$normalized = $this->serializer->normalize($account, NULL, ['account' => $account]);
|
||||
$normalized = $this->serializer->normalize($this->user, NULL, ['account' => $this->user]);
|
||||
|
||||
// The key 'pass' will now exist, but the password value should be
|
||||
// normalized to NULL.
|
||||
|
@ -179,7 +191,7 @@ class EntitySerializationTest extends NormalizerTestBase {
|
|||
'name' => '<name><value>' . $this->values['name'] . '</value></name>',
|
||||
'type' => '<type><value>entity_test_mulrev</value></type>',
|
||||
'created' => '<created><value>' . $this->entity->created->value . '</value></created>',
|
||||
'user_id' => '<user_id><target_id>' . $this->values['user_id'] . '</target_id></user_id>',
|
||||
'user_id' => '<user_id><target_id>' . $this->user->id() . '</target_id><url>' . $this->user->url() . '</url></user_id>',
|
||||
'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
|
||||
'default_langcode' => '<default_langcode><value>1</value></default_langcode>',
|
||||
'field_test_text' => '<field_test_text><value>' . $this->values['field_test_text']['value'] . '</value><format>' . $this->values['field_test_text']['format'] . '</format></field_test_text>',
|
||||
|
|
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\serialization\Unit\Normalizer;
|
||||
|
||||
use Drupal\Core\TypedData\ComplexDataInterface;
|
||||
use Drupal\Core\TypedData\TraversableTypedDataInterface;
|
||||
use Drupal\serialization\Normalizer\ComplexDataNormalizer;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\serialization\Normalizer\ComplexDataNormalizer
|
||||
* @group serialization
|
||||
*/
|
||||
class ComplexDataNormalizerTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Test format string.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TEST_FORMAT = 'test_format';
|
||||
|
||||
/**
|
||||
* The Complex data normalizer under test.
|
||||
*
|
||||
* @var \Drupal\serialization\Normalizer\ComplexDataNormalizer
|
||||
*/
|
||||
protected $normalizer;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->normalizer = new ComplexDataNormalizer();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::supportsNormalization
|
||||
*/
|
||||
public function testSupportsNormalization() {
|
||||
$this->assertTrue($this->normalizer->supportsNormalization(new TestComplexData()));
|
||||
// Also test that an object not implementing ComplexDataInterface fails.
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::normalize
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$context = ['test' => 'test'];
|
||||
|
||||
$serializer_prophecy = $this->prophesize(Serializer::class);
|
||||
|
||||
$serializer_prophecy->normalize('A', static::TEST_FORMAT, $context)
|
||||
->shouldBeCalled();
|
||||
$serializer_prophecy->normalize('B', static::TEST_FORMAT, $context)
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->normalizer->setSerializer($serializer_prophecy->reveal());
|
||||
|
||||
$complex_data = new TestComplexData(['a' => 'A', 'b' => 'B']);
|
||||
$this->normalizer->normalize($complex_data, static::TEST_FORMAT, $context);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test class implementing ComplexDataInterface and IteratorAggregate.
|
||||
*/
|
||||
class TestComplexData implements \IteratorAggregate, ComplexDataInterface {
|
||||
|
||||
private $values;
|
||||
|
||||
public function __construct(array $values = []) {
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function getIterator() {
|
||||
return new \ArrayIterator($this->values);
|
||||
}
|
||||
|
||||
public function applyDefaultValue($notify = TRUE) {
|
||||
}
|
||||
|
||||
public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL) {
|
||||
}
|
||||
|
||||
public function get($property_name) {
|
||||
}
|
||||
|
||||
public function getConstraints() {
|
||||
}
|
||||
|
||||
public function getDataDefinition() {
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
}
|
||||
|
||||
public function getParent() {
|
||||
}
|
||||
|
||||
public function getProperties($include_computed = FALSE) {
|
||||
}
|
||||
|
||||
public function getPropertyPath() {
|
||||
}
|
||||
|
||||
public function getRoot() {
|
||||
}
|
||||
|
||||
public function getString() {
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
}
|
||||
|
||||
public function isEmpty() {
|
||||
}
|
||||
|
||||
public function onChange($name) {
|
||||
}
|
||||
|
||||
public function set($property_name, $value, $notify = TRUE) {
|
||||
}
|
||||
|
||||
public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) {
|
||||
}
|
||||
|
||||
public function setValue($value, $notify = TRUE) {
|
||||
}
|
||||
|
||||
public function toArray() {
|
||||
}
|
||||
|
||||
public function validate() {
|
||||
}
|
||||
|
||||
}
|
|
@ -99,14 +99,14 @@ class EntityNormalizerTest extends UnitTestCase {
|
|||
*
|
||||
* @covers ::denormalize
|
||||
*/
|
||||
public function testDenormalizeWithBundle() {
|
||||
$test_data = array(
|
||||
public function testDenormalizeWithValidBundle() {
|
||||
$test_data = [
|
||||
'key_1' => 'value_1',
|
||||
'key_2' => 'value_2',
|
||||
'test_type' => array(
|
||||
array('value' => 'test_bundle'),
|
||||
),
|
||||
);
|
||||
'test_type' => [
|
||||
['name' => 'test_bundle'],
|
||||
],
|
||||
];
|
||||
|
||||
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
|
||||
$entity_type->expects($this->once())
|
||||
|
@ -117,11 +117,47 @@ class EntityNormalizerTest extends UnitTestCase {
|
|||
->method('getKey')
|
||||
->with('bundle')
|
||||
->will($this->returnValue('test_type'));
|
||||
$entity_type->expects($this->once())
|
||||
->method('getBundleEntityType')
|
||||
->will($this->returnValue('test_bundle'));
|
||||
|
||||
$this->entityManager->expects($this->once())
|
||||
$entity_type_storage_definition = $this->getmock('Drupal\Core\Field\FieldStorageDefinitionInterface');
|
||||
$entity_type_storage_definition->expects($this->once())
|
||||
->method('getMainPropertyName')
|
||||
->will($this->returnValue('name'));
|
||||
|
||||
$entity_type_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
|
||||
$entity_type_definition->expects($this->once())
|
||||
->method('getFieldStorageDefinition')
|
||||
->will($this->returnValue($entity_type_storage_definition));
|
||||
|
||||
$base_definitions = [
|
||||
'test_type' => $entity_type_definition,
|
||||
];
|
||||
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with('test')
|
||||
->will($this->returnValue($entity_type));
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getBaseFieldDefinitions')
|
||||
->with('test')
|
||||
->will($this->returnValue($base_definitions));
|
||||
|
||||
$entity_query_mock = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
|
||||
$entity_query_mock->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(['test_bundle' => 'test_bundle']));
|
||||
|
||||
$entity_type_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
|
||||
$entity_type_storage->expects($this->once())
|
||||
->method('getQuery')
|
||||
->will($this->returnValue($entity_query_mock));
|
||||
|
||||
$this->entityManager->expects($this->at(2))
|
||||
->method('getStorage')
|
||||
->with('test_bundle')
|
||||
->will($this->returnValue($entity_type_storage));
|
||||
|
||||
// The expected test data should have a modified test_type property.
|
||||
$expected_test_data = array(
|
||||
|
@ -136,12 +172,83 @@ class EntityNormalizerTest extends UnitTestCase {
|
|||
->with($expected_test_data)
|
||||
->will($this->returnValue($this->getMock('Drupal\Core\Entity\EntityInterface')));
|
||||
|
||||
$this->entityManager->expects($this->once())
|
||||
$this->entityManager->expects($this->at(3))
|
||||
->method('getStorage')
|
||||
->with('test')
|
||||
->will($this->returnValue($storage));
|
||||
|
||||
$this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, array('entity_type' => 'test')));
|
||||
$this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the denormalize method with a bundle property.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*
|
||||
* @covers ::denormalize
|
||||
*/
|
||||
public function testDenormalizeWithInvalidBundle() {
|
||||
$test_data = [
|
||||
'key_1' => 'value_1',
|
||||
'key_2' => 'value_2',
|
||||
'test_type' => [
|
||||
['name' => 'test_bundle'],
|
||||
],
|
||||
];
|
||||
|
||||
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
|
||||
$entity_type->expects($this->once())
|
||||
->method('hasKey')
|
||||
->with('bundle')
|
||||
->will($this->returnValue(TRUE));
|
||||
$entity_type->expects($this->once())
|
||||
->method('getKey')
|
||||
->with('bundle')
|
||||
->will($this->returnValue('test_type'));
|
||||
$entity_type->expects($this->once())
|
||||
->method('getBundleEntityType')
|
||||
->will($this->returnValue('test_bundle'));
|
||||
|
||||
$entity_type_storage_definition = $this->getmock('Drupal\Core\Field\FieldStorageDefinitionInterface');
|
||||
$entity_type_storage_definition->expects($this->once())
|
||||
->method('getMainPropertyName')
|
||||
->will($this->returnValue('name'));
|
||||
|
||||
$entity_type_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
|
||||
$entity_type_definition->expects($this->once())
|
||||
->method('getFieldStorageDefinition')
|
||||
->will($this->returnValue($entity_type_storage_definition));
|
||||
|
||||
$base_definitions = [
|
||||
'test_type' => $entity_type_definition,
|
||||
];
|
||||
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with('test')
|
||||
->will($this->returnValue($entity_type));
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getBaseFieldDefinitions')
|
||||
->with('test')
|
||||
->will($this->returnValue($base_definitions));
|
||||
|
||||
$entity_query_mock = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
|
||||
$entity_query_mock->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(['test_bundle_other' => 'test_bundle_other']));
|
||||
|
||||
$entity_type_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
|
||||
$entity_type_storage->expects($this->once())
|
||||
->method('getQuery')
|
||||
->will($this->returnValue($entity_query_mock));
|
||||
|
||||
$this->entityManager->expects($this->at(2))
|
||||
->method('getStorage')
|
||||
->with('test_bundle')
|
||||
->will($this->returnValue($entity_type_storage));
|
||||
|
||||
|
||||
$this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,6 +286,9 @@ class EntityNormalizerTest extends UnitTestCase {
|
|||
->with('test')
|
||||
->will($this->returnValue($storage));
|
||||
|
||||
$this->entityManager->expects($this->never())
|
||||
->method('getBaseFieldDefinitions');
|
||||
|
||||
$this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, array('entity_type' => 'test')));
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\serialization\Unit\Normalizer;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||
use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Prophecy\Argument;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
|
||||
* @group serialization
|
||||
*/
|
||||
class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The mock serializer.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\SerializerInterface|\Prophecy\Prophecy\ObjectProphecy
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* The normalizer under test.
|
||||
*
|
||||
* @var \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
|
||||
*/
|
||||
protected $normalizer;
|
||||
|
||||
/**
|
||||
* The mock field item.
|
||||
*
|
||||
* @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem|\Prophecy\Prophecy\ObjectProphecy
|
||||
*/
|
||||
protected $fieldItem;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->normalizer = new EntityReferenceFieldItemNormalizer();
|
||||
|
||||
$this->serializer = $this->prophesize(Serializer::class);
|
||||
// Set up the serializer to return an entity property.
|
||||
$this->serializer->normalize(Argument::cetera())
|
||||
->willReturn(['value' => 'test']);
|
||||
|
||||
$this->normalizer->setSerializer($this->serializer->reveal());
|
||||
|
||||
$this->fieldItem = $this->prophesize(EntityReferenceItem::class);
|
||||
$this->fieldItem->getIterator()
|
||||
->willReturn(new \ArrayIterator(['target_id' => []]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::supportsNormalization
|
||||
*/
|
||||
public function testSupportsNormalization() {
|
||||
$this->assertTrue($this->normalizer->supportsNormalization($this->fieldItem->reveal()));
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::normalize
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$test_url = '/test/100';
|
||||
|
||||
$entity = $this->prophesize(EntityInterface::class);
|
||||
$entity->url('canonical')
|
||||
->willReturn($test_url)
|
||||
->shouldBeCalled();
|
||||
|
||||
$entity_reference = $this->prophesize(TypedDataInterface::class);
|
||||
$entity_reference->getValue()
|
||||
->willReturn($entity->reveal())
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->fieldItem->get('entity')
|
||||
->willReturn($entity_reference)
|
||||
->shouldBeCalled();
|
||||
|
||||
$normalized = $this->normalizer->normalize($this->fieldItem->reveal());
|
||||
|
||||
$expected = [
|
||||
'target_id' => ['value' => 'test'],
|
||||
'url' => $test_url,
|
||||
];
|
||||
$this->assertSame($expected, $normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::normalize
|
||||
*/
|
||||
public function testNormalizeWithNoEntity() {
|
||||
$entity_reference = $this->prophesize(TypedDataInterface::class);
|
||||
$entity_reference->getValue()
|
||||
->willReturn(NULL)
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->fieldItem->get('entity')
|
||||
->willReturn($entity_reference->reveal())
|
||||
->shouldBeCalled();
|
||||
|
||||
$normalized = $this->normalizer->normalize($this->fieldItem->reveal());
|
||||
|
||||
$expected = [
|
||||
'target_id' => ['value' => 'test'],
|
||||
];
|
||||
$this->assertSame($expected, $normalized);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue