Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / serialization / tests / src / Unit / Normalizer / EntityReferenceFieldItemNormalizerTest.php
index 5cc6467e8b6e2d3714ea79bf45ea4c63a4986186..96274a675fdff5d58e0cdc2ce8f93fe96b646162 100644 (file)
@@ -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);
+  }
+
 }