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;
->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();
$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
*/
->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();
->willReturn($entity)
->shouldBeCalled();
+ $this->fieldItem->getProperties()->willReturn([
+ 'target_id' => $this->prophesize(IntegerInterface::class),
+ ]);
$this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
$this->assertDenormalize($data);
->willReturn($entity)
->shouldBeCalled();
+ $this->fieldItem->getProperties()->willReturn([
+ 'target_id' => $this->prophesize(IntegerInterface::class),
+ ]);
$this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
$this->assertDenormalize($data);
$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);
+ }
+
}