3 namespace Drupal\Tests\serialization\Unit\Normalizer;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\TypedData\TypedDataInterface;
8 use Drupal\Core\Entity\EntityRepositoryInterface;
9 use Drupal\Core\Entity\FieldableEntityInterface;
10 use Drupal\Core\Field\FieldItemInterface;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
13 use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer;
14 use Drupal\Tests\UnitTestCase;
15 use Prophecy\Argument;
16 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
17 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
18 use Symfony\Component\Serializer\Serializer;
21 * @coversDefaultClass \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
22 * @group serialization
24 class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
27 * The mock serializer.
29 * @var \Symfony\Component\Serializer\SerializerInterface|\Prophecy\Prophecy\ObjectProphecy
31 protected $serializer;
34 * The normalizer under test.
36 * @var \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
38 protected $normalizer;
41 * The mock field item.
43 * @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem|\Prophecy\Prophecy\ObjectProphecy
48 * The mock entity repository.
50 * @var \Drupal\Core\Entity\EntityRepositoryInterface|\Prophecy\Prophecy\ObjectProphecy
52 protected $entityRepository;
55 * The mock field definition.
57 * @var \Drupal\Core\Field\FieldDefinitionInterface|\Prophecy\Prophecy\ObjectProphecy
59 protected $fieldDefinition;
64 protected function setUp() {
65 $this->entityRepository = $this->prophesize(EntityRepositoryInterface::class);
66 $this->normalizer = new EntityReferenceFieldItemNormalizer($this->entityRepository->reveal());
68 $this->serializer = $this->prophesize(Serializer::class);
69 // Set up the serializer to return an entity property.
70 $this->serializer->normalize(Argument::cetera())
71 ->willReturn(['value' => 'test']);
73 $this->normalizer->setSerializer($this->serializer->reveal());
75 $this->fieldItem = $this->prophesize(EntityReferenceItem::class);
76 $this->fieldItem->getIterator()
77 ->willReturn(new \ArrayIterator(['target_id' => []]));
79 $this->fieldDefinition = $this->prophesize(FieldDefinitionInterface::class);
84 * @covers ::supportsNormalization
86 public function testSupportsNormalization() {
87 $this->assertTrue($this->normalizer->supportsNormalization($this->fieldItem->reveal()));
88 $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
92 * @covers ::supportsDenormalization
94 public function testSupportsDenormalization() {
95 $this->assertTrue($this->normalizer->supportsDenormalization([], EntityReferenceItem::class));
96 $this->assertFalse($this->normalizer->supportsDenormalization([], FieldItemInterface::class));
100 * @covers ::normalize
102 public function testNormalize() {
103 $test_url = '/test/100';
105 $entity = $this->prophesize(EntityInterface::class);
106 $entity->url('canonical')
107 ->willReturn($test_url)
110 ->willReturn('080e3add-f9d5-41ac-9821-eea55b7b42fb')
112 $entity->getEntityTypeId()
113 ->willReturn('test_type')
116 $entity_reference = $this->prophesize(TypedDataInterface::class);
117 $entity_reference->getValue()
118 ->willReturn($entity->reveal())
121 $this->fieldItem->get('entity')
122 ->willReturn($entity_reference)
125 $normalized = $this->normalizer->normalize($this->fieldItem->reveal());
128 'target_id' => ['value' => 'test'],
129 'target_type' => 'test_type',
130 'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
133 $this->assertSame($expected, $normalized);
137 * @covers ::normalize
139 public function testNormalizeWithNoEntity() {
140 $entity_reference = $this->prophesize(TypedDataInterface::class);
141 $entity_reference->getValue()
145 $this->fieldItem->get('entity')
146 ->willReturn($entity_reference->reveal())
149 $normalized = $this->normalizer->normalize($this->fieldItem->reveal());
152 'target_id' => ['value' => 'test'],
154 $this->assertSame($expected, $normalized);
158 * @covers ::denormalize
160 public function testDenormalizeWithTypeAndUuid() {
162 'target_id' => ['value' => 'test'],
163 'target_type' => 'test_type',
164 'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
167 $entity = $this->prophesize(FieldableEntityInterface::class);
171 $this->entityRepository
172 ->loadEntityByUuid($data['target_type'], $data['target_uuid'])
173 ->willReturn($entity)
176 $this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
178 $this->assertDenormalize($data);
182 * @covers ::denormalize
184 public function testDenormalizeWithUuidWithoutType() {
186 'target_id' => ['value' => 'test'],
187 'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
190 $entity = $this->prophesize(FieldableEntityInterface::class);
194 $this->entityRepository
195 ->loadEntityByUuid('test_type', $data['target_uuid'])
196 ->willReturn($entity)
199 $this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
201 $this->assertDenormalize($data);
205 * @covers ::denormalize
207 public function testDenormalizeWithUuidWithIncorrectType() {
208 $this->setExpectedException(UnexpectedValueException::class, 'The field "field_reference" property "target_type" must be set to "test_type" or omitted.');
211 'target_id' => ['value' => 'test'],
212 'target_type' => 'wrong_type',
213 'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
216 $this->fieldDefinition
218 ->willReturn('field_reference')
221 $this->assertDenormalize($data);
225 * @covers ::denormalize
227 public function testDenormalizeWithTypeWithIncorrectUuid() {
228 $this->setExpectedException(InvalidArgumentException::class, 'No "test_type" entity found with UUID "unique-but-none-non-existent" for field "field_reference"');
231 'target_id' => ['value' => 'test'],
232 'target_type' => 'test_type',
233 'target_uuid' => 'unique-but-none-non-existent',
235 $this->entityRepository
236 ->loadEntityByUuid($data['target_type'], $data['target_uuid'])
241 ->willReturn('field_reference')
244 $this->assertDenormalize($data);
248 * @covers ::denormalize
250 public function testDenormalizeWithEmtpyUuid() {
251 $this->setExpectedException(InvalidArgumentException::class, 'If provided "target_uuid" cannot be empty for field "test_type".');
254 'target_id' => ['value' => 'test'],
255 'target_type' => 'test_type',
260 ->willReturn('field_reference')
263 $this->assertDenormalize($data);
267 * @covers ::denormalize
269 public function testDenormalizeWithId() {
271 'target_id' => ['value' => 'test'],
273 $this->fieldItem->setValue($data)->shouldBeCalled();
275 $this->assertDenormalize($data);
279 * Asserts denormalization process is correct for give data.
282 * The data to denormalize.
284 protected function assertDenormalize(array $data) {
285 $this->fieldItem->getParent()
286 ->willReturn($this->prophesize(FieldItemListInterface::class)->reveal());
287 $this->fieldItem->getFieldDefinition()->willReturn($this->fieldDefinition->reveal());
288 if (!empty($data['target_uuid'])) {
289 $this->fieldDefinition
290 ->getSetting('target_type')
291 ->willReturn('test_type')
295 $context = ['target_instance' => $this->fieldItem->reveal()];
296 $denormalized = $this->normalizer->denormalize($data, EntityReferenceItem::class, 'json', $context);
297 $this->assertSame($context['target_instance'], $denormalized);