Issue #3020001 by Wim Leers, jeqq: Error when normalizing entity reference field item that references to a new entity

8.7.x
Nathaniel Catchpole 2019-01-07 11:45:52 +00:00
parent 95b3b17867
commit a979848a71
2 changed files with 59 additions and 1 deletions

View File

@ -53,7 +53,7 @@ class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
// Add a 'url' value if there is a reference and a canonical URL. Hard // Add a 'url' value if there is a reference and a canonical URL. Hard
// code 'canonical' here as config entities override the default $rel // code 'canonical' here as config entities override the default $rel
// parameter value to 'edit-form. // parameter value to 'edit-form.
if ($entity->hasLinkTemplate('canonical') && $url = $entity->toUrl('canonical')->toString(TRUE)) { if ($entity->hasLinkTemplate('canonical') && !$entity->isNew() && $url = $entity->toUrl('canonical')->toString(TRUE)) {
$this->addCacheableDependency($context, $url); $this->addCacheableDependency($context, $url);
$values['url'] = $url->getGeneratedUrl(); $values['url'] = $url->getGeneratedUrl();
} }

View File

@ -117,6 +117,9 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
$entity = $this->prophesize(EntityInterface::class); $entity = $this->prophesize(EntityInterface::class);
$entity->hasLinkTemplate('canonical') $entity->hasLinkTemplate('canonical')
->willReturn(TRUE); ->willReturn(TRUE);
$entity->isNew()
->willReturn(FALSE)
->shouldBeCalled();
$entity->toUrl('canonical') $entity->toUrl('canonical')
->willReturn($url) ->willReturn($url)
->shouldBeCalled(); ->shouldBeCalled();
@ -158,6 +161,61 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
$this->assertSame($expected, $normalized); $this->assertSame($expected, $normalized);
} }
public function testNormalizeWithNewEntityReference() {
$test_url = '/test/100';
$generated_url = (new GeneratedUrl())->setGeneratedUrl($test_url);
$url = $this->prophesize(Url::class);
$url->toString(TRUE)
->willReturn($generated_url);
$entity = $this->prophesize(EntityInterface::class);
$entity->hasLinkTemplate('canonical')
->willReturn(TRUE);
$entity->isNew()
->willReturn(TRUE)
->shouldBeCalled();
$entity->uuid()
->willReturn('080e3add-f9d5-41ac-9821-eea55b7b42fb')
->shouldBeCalled();
$entity->getEntityTypeId()
->willReturn('test_type')
->shouldBeCalled();
$entity->toUrl('canonical')
->willReturn($url)
->shouldNotBeCalled();
$entity_reference = $this->prophesize(TypedDataInterface::class);
$entity_reference->getValue()
->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->fieldItem->getProperties(TRUE)
->willReturn(['target_id' => $this->getTypedDataProperty(FALSE)])
->shouldBeCalled();
$normalized = $this->normalizer->normalize($this->fieldItem->reveal());
$expected = [
'target_id' => 'test',
'target_type' => 'test_type',
'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
];
$this->assertSame($expected, $normalized);
}
/** /**
* @covers ::normalize * @covers ::normalize
*/ */