updated core to 8.6.1 via composer
This commit is contained in:
@@ -85,7 +85,7 @@ services:
|
||||
class: Drupal\serialization\EntityResolver\UuidResolver
|
||||
tags:
|
||||
- { name: entity_resolver}
|
||||
arguments: ['@entity.manager']
|
||||
arguments: ['@entity.repository']
|
||||
serialization.entity_resolver.target_id:
|
||||
class: Drupal\serialization\EntityResolver\TargetIdResolver
|
||||
tags:
|
||||
|
||||
@@ -4,8 +4,9 @@ namespace Drupal\serialization\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
||||
use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
||||
use Symfony\Component\Serializer\Encoder\SerializerAwareEncoder;
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
|
||||
use Symfony\Component\Serializer\SerializerAwareInterface;
|
||||
use Symfony\Component\Serializer\SerializerAwareTrait;
|
||||
|
||||
/**
|
||||
* Adds XML support for serializer.
|
||||
@@ -17,7 +18,9 @@ use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
|
||||
* This encoder should not be used directly. Rather, use the `serializer`
|
||||
* service.
|
||||
*/
|
||||
class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface {
|
||||
class XmlEncoder implements SerializerAwareInterface, EncoderInterface, DecoderInterface {
|
||||
|
||||
use SerializerAwareTrait;
|
||||
|
||||
/**
|
||||
* The formats that this Encoder supports.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\serialization\EntityResolver;
|
||||
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
@@ -11,20 +11,20 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
class UuidResolver implements EntityResolverInterface {
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
* The entity repository.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface
|
||||
* @var \Drupal\Core\Entity\EntityRepositoryInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
protected $entityRepository;
|
||||
|
||||
/**
|
||||
* Constructs a UuidResolver object.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
||||
* The entity repository.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager) {
|
||||
$this->entityManager = $entity_manager;
|
||||
public function __construct(EntityRepositoryInterface $entity_repository) {
|
||||
$this->entityRepository = $entity_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +35,7 @@ class UuidResolver implements EntityResolverInterface {
|
||||
// deserialized. If it can return a UUID from that data, and if there's an
|
||||
// entity with that UUID, then return its ID.
|
||||
if (($normalizer instanceof UuidReferenceInterface) && ($uuid = $normalizer->getUuid($data))) {
|
||||
if ($entity = $this->entityManager->loadEntityByUuid($entity_type, $uuid)) {
|
||||
if ($entity = $this->entityRepository->loadEntityByUuid($entity_type, $uuid)) {
|
||||
return $entity->id();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,10 @@ class ComplexDataNormalizer extends NormalizerBase {
|
||||
// Other normalizers that extend this class may only provide $object that
|
||||
// implements \Traversable.
|
||||
if ($object instanceof ComplexDataInterface) {
|
||||
$object = TypedDataInternalPropertiesHelper::getNonInternalProperties($object);
|
||||
// If there are no properties to normalize, just normalize the value.
|
||||
$object = !empty($object->getProperties(TRUE))
|
||||
? TypedDataInternalPropertiesHelper::getNonInternalProperties($object)
|
||||
: $object->getValue();
|
||||
}
|
||||
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
|
||||
foreach ($object as $name => $property) {
|
||||
|
||||
@@ -40,7 +40,7 @@ class EntityNormalizer extends ComplexDataNormalizer implements DenormalizerInte
|
||||
|
||||
// The bundle property will be required to denormalize a bundleable
|
||||
// fieldable entity.
|
||||
if ($entity_type_definition->isSubclassOf(FieldableEntityInterface::class)) {
|
||||
if ($entity_type_definition->entityClassImplements(FieldableEntityInterface::class)) {
|
||||
// Extract bundle data to pass into entity creation if the entity type uses
|
||||
// bundles.
|
||||
if ($entity_type_definition->hasKey('bundle')) {
|
||||
|
||||
@@ -12,6 +12,8 @@ use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
*/
|
||||
class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
|
||||
|
||||
use EntityReferenceFieldItemNormalizerTrait;
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
@@ -42,6 +44,8 @@ class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
|
||||
public function normalize($field_item, $format = NULL, array $context = []) {
|
||||
$values = parent::normalize($field_item, $format, $context);
|
||||
|
||||
$this->normalizeRootReferenceValue($values, $field_item);
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityInterface $entity */
|
||||
if ($entity = $field_item->get('entity')->getValue()) {
|
||||
$values['target_type'] = $entity->getEntityTypeId();
|
||||
@@ -55,6 +59,7 @@ class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
|
||||
$values['url'] = $url;
|
||||
}
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
@@ -73,7 +78,7 @@ class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
|
||||
throw new UnexpectedValueException(sprintf('The field "%s" property "target_type" must be set to "%s" or omitted.', $field_item->getFieldDefinition()->getName(), $target_type));
|
||||
}
|
||||
if ($entity = $this->entityRepository->loadEntityByUuid($target_type, $data['target_uuid'])) {
|
||||
return ['target_id' => $entity->id()];
|
||||
return ['target_id' => $entity->id()] + array_intersect_key($data, $field_item->getProperties());
|
||||
}
|
||||
else {
|
||||
// Unable to load entity by uuid.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||
|
||||
/**
|
||||
* Converts empty reference values for entity reference items.
|
||||
*/
|
||||
trait EntityReferenceFieldItemNormalizerTrait {
|
||||
|
||||
protected function normalizeRootReferenceValue(&$values, EntityReferenceItem $field_item) {
|
||||
// @todo Generalize for all tree-structured entity types.
|
||||
if ($this->fieldItemReferencesTaxonomyTerm($field_item) && empty($values['target_id'])) {
|
||||
$values['target_id'] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a field item references a taxonomy term.
|
||||
*
|
||||
* @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $field_item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function fieldItemReferencesTaxonomyTerm(EntityReferenceItem $field_item) {
|
||||
return $field_item->getFieldDefinition()->getSetting('target_type') === 'taxonomy_term';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,12 +3,15 @@
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Drupal\Core\Cache\CacheableDependencyInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
|
||||
use Symfony\Component\Serializer\SerializerAwareInterface;
|
||||
use Symfony\Component\Serializer\SerializerAwareTrait;
|
||||
|
||||
/**
|
||||
* Base class for Normalizers.
|
||||
*/
|
||||
abstract class NormalizerBase extends SerializerAwareNormalizer implements CacheableNormalizerInterface {
|
||||
abstract class NormalizerBase implements SerializerAwareInterface, CacheableNormalizerInterface {
|
||||
|
||||
use SerializerAwareTrait;
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
|
||||
@@ -23,6 +23,9 @@ class RegisterSerializationClassesCompilerPass implements CompilerPassInterface
|
||||
|
||||
// Retrieve registered Normalizers and Encoders from the container.
|
||||
foreach ($container->findTaggedServiceIds('normalizer') as $id => $attributes) {
|
||||
// The 'serializer' service is the public API: mark normalizers private.
|
||||
$container->getDefinition($id)->setPublic(FALSE);
|
||||
|
||||
// If there is a BC key present, pass this to determine if the normalizer
|
||||
// should be skipped.
|
||||
if (isset($attributes[0]['bc']) && $this->normalizerBcSettingIsEnabled($attributes[0]['bc'], $attributes[0]['bc_config_name'])) {
|
||||
@@ -33,6 +36,9 @@ class RegisterSerializationClassesCompilerPass implements CompilerPassInterface
|
||||
$normalizers[$priority][] = new Reference($id);
|
||||
}
|
||||
foreach ($container->findTaggedServiceIds('encoder') as $id => $attributes) {
|
||||
// The 'serializer' service is the public API: mark encoders private.
|
||||
$container->getDefinition($id)->setPublic(FALSE);
|
||||
|
||||
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
|
||||
$encoders[$priority][] = new Reference($id);
|
||||
}
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ class TextItemSillyNormalizer extends FieldItemNormalizer {
|
||||
*/
|
||||
protected function constructValue($data, $context) {
|
||||
$value = parent::constructValue($data, $context);
|
||||
$value['value'] = str_replace('::silly_suffix', '', $value['value']);
|
||||
$value['value'] = str_replace('::silly_suffix', '', $value['value']);
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Drupal\Tests\serialization\Kernel;
|
||||
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\entity_test\Entity\EntityTestMulRev;
|
||||
use Drupal\filter\Entity\FilterFormat;
|
||||
use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
|
||||
@@ -48,7 +48,7 @@ class EntitySerializationTest extends NormalizerTestBase {
|
||||
/**
|
||||
* The serializer service.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\Serializer.
|
||||
* @var \Symfony\Component\Serializer\Serializer
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
@@ -253,7 +253,7 @@ class EntitySerializationTest extends NormalizerTestBase {
|
||||
|
||||
foreach (['json', 'xml'] as $type) {
|
||||
$denormalized = $this->serializer->denormalize($normalized, $this->entityClass, $type, ['entity_type' => 'entity_test_mulrev']);
|
||||
$this->assertTrue($denormalized instanceof $this->entityClass, SafeMarkup::format('Denormalized entity is an instance of @class', ['@class' => $this->entityClass]));
|
||||
$this->assertTrue($denormalized instanceof $this->entityClass, new FormattableMarkup('Denormalized entity is an instance of @class', ['@class' => $this->entityClass]));
|
||||
$this->assertIdentical($denormalized->getEntityTypeId(), $this->entity->getEntityTypeId(), 'Expected entity type found.');
|
||||
$this->assertIdentical($denormalized->bundle(), $this->entity->bundle(), 'Expected entity bundle found.');
|
||||
$this->assertIdentical($denormalized->uuid(), $this->entity->uuid(), 'Expected entity UUID found.');
|
||||
|
||||
@@ -43,7 +43,7 @@ class FieldItemSerializationTest extends NormalizerTestBase {
|
||||
/**
|
||||
* The serializer service.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\Serializer.
|
||||
* @var \Symfony\Component\Serializer\Serializer
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\serialization\Kernel;
|
||||
|
||||
use Drupal\Core\TypedData\DataDefinition;
|
||||
use Drupal\Core\TypedData\MapDataDefinition;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* @group typedData
|
||||
*/
|
||||
class MapDataNormalizerTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['system', 'serialization'];
|
||||
|
||||
/**
|
||||
* The serializer service.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\Serializer
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* The typed data manager.
|
||||
*
|
||||
* @var \Drupal\Core\TypedData\TypedDataManagerInterface
|
||||
*/
|
||||
protected $typedDataManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->serializer = \Drupal::service('serializer');
|
||||
$this->typedDataManager = \Drupal::typedDataManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether map data can be normalized.
|
||||
*/
|
||||
public function testMapNormalize() {
|
||||
$typed_data = $this->buildExampleTypedData();
|
||||
$data = $this->serializer->normalize($typed_data, 'json');
|
||||
$expect_value = [
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
'key3' => 3,
|
||||
'key4' => [
|
||||
0 => TRUE,
|
||||
1 => 'value6',
|
||||
'key7' => 'value7',
|
||||
],
|
||||
];
|
||||
$this->assertSame($expect_value, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether map data with properties can be normalized.
|
||||
*/
|
||||
public function testMapWithPropertiesNormalize() {
|
||||
$typed_data = $this->buildExampleTypedDataWithProperties();
|
||||
$data = $this->serializer->normalize($typed_data, 'json');
|
||||
$expect_value = [
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
'key3' => 3,
|
||||
'key4' => [
|
||||
0 => TRUE,
|
||||
1 => 'value6',
|
||||
'key7' => 'value7',
|
||||
],
|
||||
];
|
||||
$this->assertSame($expect_value, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds some example typed data object with no properties.
|
||||
*/
|
||||
protected function buildExampleTypedData() {
|
||||
$tree = [
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
'key3' => 3,
|
||||
'key4' => [
|
||||
0 => TRUE,
|
||||
1 => 'value6',
|
||||
'key7' => 'value7',
|
||||
],
|
||||
];
|
||||
$map_data_definition = MapDataDefinition::create();
|
||||
$typed_data = $this->typedDataManager->create(
|
||||
$map_data_definition,
|
||||
$tree,
|
||||
'test name'
|
||||
);
|
||||
return $typed_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds some example typed data object with properties.
|
||||
*/
|
||||
protected function buildExampleTypedDataWithProperties() {
|
||||
$tree = [
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
'key3' => 3,
|
||||
'key4' => [
|
||||
0 => TRUE,
|
||||
1 => 'value6',
|
||||
'key7' => 'value7',
|
||||
],
|
||||
];
|
||||
$map_data_definition = MapDataDefinition::create()
|
||||
->setPropertyDefinition('key1', DataDefinition::create('string'))
|
||||
->setPropertyDefinition('key2', DataDefinition::create('string'))
|
||||
->setPropertyDefinition('key3', DataDefinition::create('integer'))
|
||||
->setPropertyDefinition('key4', MapDataDefinition::create()
|
||||
->setPropertyDefinition(0, DataDefinition::create('boolean'))
|
||||
->setPropertyDefinition(1, DataDefinition::create('string'))
|
||||
->setPropertyDefinition('key7', DataDefinition::create('string'))
|
||||
);
|
||||
|
||||
$typed_data = $this->typedDataManager->create(
|
||||
$map_data_definition,
|
||||
$tree,
|
||||
'test name'
|
||||
);
|
||||
|
||||
return $typed_data;
|
||||
}
|
||||
|
||||
}
|
||||
+24
-12
@@ -21,26 +21,38 @@ class RegisterSerializationClassesCompilerPassTest extends UnitTestCase {
|
||||
$container = new ContainerBuilder();
|
||||
$container->setDefinition('serializer', new Definition(Serializer::class, [[], []]));
|
||||
|
||||
$definition = new Definition('TestClass');
|
||||
$definition->addTag('encoder', ['format' => 'xml']);
|
||||
$definition->addTag('_provider', ['provider' => 'test_provider_a']);
|
||||
$container->setDefinition('encoder_1', $definition);
|
||||
$encoder_1_definition = new Definition('TestClass');
|
||||
$encoder_1_definition->addTag('encoder', ['format' => 'xml']);
|
||||
$encoder_1_definition->addTag('_provider', ['provider' => 'test_provider_a']);
|
||||
$container->setDefinition('encoder_1', $encoder_1_definition);
|
||||
|
||||
$definition = new Definition('TestClass');
|
||||
$definition->addTag('encoder', ['format' => 'json']);
|
||||
$definition->addTag('_provider', ['provider' => 'test_provider_a']);
|
||||
$container->setDefinition('encoder_2', $definition);
|
||||
$encoder_2_definition = new Definition('TestClass');
|
||||
$encoder_2_definition->addTag('encoder', ['format' => 'json']);
|
||||
$encoder_2_definition->addTag('_provider', ['provider' => 'test_provider_a']);
|
||||
$container->setDefinition('encoder_2', $encoder_2_definition);
|
||||
|
||||
$definition = new Definition('TestClass');
|
||||
$definition->addTag('encoder', ['format' => 'hal_json']);
|
||||
$definition->addTag('_provider', ['provider' => 'test_provider_b']);
|
||||
$container->setDefinition('encoder_3', $definition);
|
||||
$encoder_3_definition = new Definition('TestClass');
|
||||
$encoder_3_definition->addTag('encoder', ['format' => 'hal_json']);
|
||||
$encoder_3_definition->addTag('_provider', ['provider' => 'test_provider_b']);
|
||||
$container->setDefinition('encoder_3', $encoder_3_definition);
|
||||
|
||||
$normalizer_1_definition = new Definition('TestClass');
|
||||
$normalizer_1_definition->addTag('normalizer');
|
||||
$container->setDefinition('normalizer_1', $normalizer_1_definition);
|
||||
|
||||
$compiler_pass = new RegisterSerializationClassesCompilerPass();
|
||||
$compiler_pass->process($container);
|
||||
|
||||
// Check registration of formats and providers.
|
||||
$this->assertEquals(['xml', 'json', 'hal_json'], $container->getParameter('serializer.formats'));
|
||||
$this->assertEquals(['xml' => 'test_provider_a', 'json' => 'test_provider_a', 'hal_json' => 'test_provider_b'], $container->getParameter('serializer.format_providers'));
|
||||
|
||||
// Check all encoder and normalizer service definitions are marked private.
|
||||
$this->assertFalse($encoder_1_definition->isPublic());
|
||||
$this->assertFalse($encoder_2_definition->isPublic());
|
||||
$this->assertFalse($encoder_3_definition->isPublic());
|
||||
|
||||
$this->assertFalse($normalizer_1_definition->isPublic());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ class XmlEncoderTest extends UnitTestCase {
|
||||
}
|
||||
|
||||
class TestObject {
|
||||
|
||||
public function getA() {
|
||||
return 'A';
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Drupal\Tests\serialization\Unit\EntityResolver;
|
||||
|
||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\serialization\EntityResolver\UuidResolver;
|
||||
|
||||
@@ -29,7 +30,7 @@ class UuidResolverTest extends UnitTestCase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->entityManager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager')
|
||||
$this->entityManager = $this->getMockBuilder(EntityRepositoryInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ class EntityNormalizerTest extends UnitTestCase {
|
||||
->with('bundle')
|
||||
->will($this->returnValue('test_type'));
|
||||
$entity_type->expects($this->once())
|
||||
->method('isSubClassOf')
|
||||
->method('entityClassImplements')
|
||||
->with(FieldableEntityInterface::class)
|
||||
->willReturn(TRUE);
|
||||
|
||||
@@ -240,7 +240,7 @@ class EntityNormalizerTest extends UnitTestCase {
|
||||
->with('bundle')
|
||||
->will($this->returnValue('test_type'));
|
||||
$entity_type->expects($this->once())
|
||||
->method('isSubClassOf')
|
||||
->method('entityClassImplements')
|
||||
->with(FieldableEntityInterface::class)
|
||||
->willReturn(TRUE);
|
||||
|
||||
@@ -303,7 +303,7 @@ class EntityNormalizerTest extends UnitTestCase {
|
||||
|
||||
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
|
||||
$entity_type->expects($this->once())
|
||||
->method('isSubClassOf')
|
||||
->method('entityClassImplements')
|
||||
->with(FieldableEntityInterface::class)
|
||||
->willReturn(TRUE);
|
||||
$entity_type->expects($this->once())
|
||||
@@ -376,7 +376,7 @@ class EntityNormalizerTest extends UnitTestCase {
|
||||
|
||||
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
|
||||
$entity_type->expects($this->once())
|
||||
->method('isSubClassOf')
|
||||
->method('entityClassImplements')
|
||||
->with(FieldableEntityInterface::class)
|
||||
->willReturn(FALSE);
|
||||
|
||||
|
||||
+94
@@ -4,12 +4,14 @@ namespace Drupal\Tests\serialization\Unit\Normalizer;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\TypedData\Type\IntegerInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||
use Drupal\locale\StringInterface;
|
||||
use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Prophecy\Argument;
|
||||
@@ -120,6 +122,13 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||
->willReturn($entity->reveal())
|
||||
->shouldBeCalled();
|
||||
|
||||
$field_definition = $this->prophesize(FieldDefinitionInterface::class);
|
||||
$field_definition->getSetting('target_type')
|
||||
->willReturn('test_type');
|
||||
|
||||
$this->fieldItem->getFieldDefinition()
|
||||
->willReturn($field_definition->reveal());
|
||||
|
||||
$this->fieldItem->get('entity')
|
||||
->willReturn($entity_reference)
|
||||
->shouldBeCalled();
|
||||
@@ -139,6 +148,46 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||
$this->assertSame($expected, $normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::normalize
|
||||
*/
|
||||
public function testNormalizeWithEmptyTaxonomyTermReference() {
|
||||
// Override the serializer prophecy from setUp() to return a zero value.
|
||||
$this->serializer = $this->prophesize(Serializer::class);
|
||||
// Set up the serializer to return an entity property.
|
||||
$this->serializer->normalize(Argument::cetera())
|
||||
->willReturn(0);
|
||||
|
||||
$this->normalizer->setSerializer($this->serializer->reveal());
|
||||
|
||||
$entity_reference = $this->prophesize(TypedDataInterface::class);
|
||||
$entity_reference->getValue()
|
||||
->willReturn(NULL)
|
||||
->shouldBeCalled();
|
||||
|
||||
$field_definition = $this->prophesize(FieldDefinitionInterface::class);
|
||||
$field_definition->getSetting('target_type')
|
||||
->willReturn('taxonomy_term');
|
||||
|
||||
$this->fieldItem->getFieldDefinition()
|
||||
->willReturn($field_definition->reveal());
|
||||
|
||||
$this->fieldItem->get('entity')
|
||||
->willReturn($entity_reference)
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->fieldItem->getProperties(TRUE)
|
||||
->willReturn(['target_id' => $this->getTypedDataProperty(FALSE)])
|
||||
->shouldBeCalled();
|
||||
|
||||
$normalized = $this->normalizer->normalize($this->fieldItem->reveal());
|
||||
|
||||
$expected = [
|
||||
'target_id' => NULL,
|
||||
];
|
||||
$this->assertSame($expected, $normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::normalize
|
||||
*/
|
||||
@@ -148,6 +197,13 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||
->willReturn(NULL)
|
||||
->shouldBeCalled();
|
||||
|
||||
$field_definition = $this->prophesize(FieldDefinitionInterface::class);
|
||||
$field_definition->getSetting('target_type')
|
||||
->willReturn('test_type');
|
||||
|
||||
$this->fieldItem->getFieldDefinition()
|
||||
->willReturn($field_definition->reveal());
|
||||
|
||||
$this->fieldItem->get('entity')
|
||||
->willReturn($entity_reference->reveal())
|
||||
->shouldBeCalled();
|
||||
@@ -183,6 +239,9 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||
->willReturn($entity)
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->fieldItem->getProperties()->willReturn([
|
||||
'target_id' => $this->prophesize(IntegerInterface::class),
|
||||
]);
|
||||
$this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
|
||||
|
||||
$this->assertDenormalize($data);
|
||||
@@ -206,6 +265,9 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||
->willReturn($entity)
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->fieldItem->getProperties()->willReturn([
|
||||
'target_id' => $this->prophesize(IntegerInterface::class),
|
||||
]);
|
||||
$this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
|
||||
|
||||
$this->assertDenormalize($data);
|
||||
@@ -307,4 +369,36 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||
$this->assertSame($context['target_instance'], $denormalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::constructValue
|
||||
*/
|
||||
public function testConstructValueProperties() {
|
||||
$data = [
|
||||
'target_id' => 'test',
|
||||
'target_type' => 'test_type',
|
||||
'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
|
||||
'extra_property' => 'extra_value',
|
||||
];
|
||||
|
||||
$entity = $this->prophesize(FieldableEntityInterface::class);
|
||||
$entity->id()
|
||||
->willReturn('test')
|
||||
->shouldBeCalled();
|
||||
$this->entityRepository
|
||||
->loadEntityByUuid($data['target_type'], $data['target_uuid'])
|
||||
->willReturn($entity)
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->fieldItem->getProperties()->willReturn([
|
||||
'target_id' => $this->prophesize(IntegerInterface::class),
|
||||
'extra_property' => $this->prophesize(StringInterface::class),
|
||||
]);
|
||||
$this->fieldItem->setValue([
|
||||
'target_id' => 'test',
|
||||
'extra_property' => 'extra_value',
|
||||
])->shouldBeCalled();
|
||||
|
||||
$this->assertDenormalize($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user