X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Ftests%2FDrupal%2FKernelTests%2FCore%2FEntity%2FEntityFieldTest.php;fp=web%2Fcore%2Ftests%2FDrupal%2FKernelTests%2FCore%2FEntity%2FEntityFieldTest.php;h=3fd17d86b47798da09b4aa795e4a3663c7b66b10;hp=a42a74baae1eded269ba737258095503bdbe62b1;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php b/web/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php index a42a74baa..3fd17d86b 100644 --- a/web/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php +++ b/web/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php @@ -16,6 +16,7 @@ use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\ListInterface; use Drupal\Core\TypedData\Type\StringInterface; use Drupal\Core\TypedData\TypedDataInterface; +use Drupal\entity_test\Entity\EntityTest; use Drupal\entity_test\Entity\EntityTestComputedField; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; @@ -870,6 +871,32 @@ class EntityFieldTest extends EntityKernelTestBase { $this->assertFalse($computed_item_list1->equals($computed_item_list2)); } + /** + * Tests an entity reference computed field. + */ + public function testEntityReferenceComputedField() { + $this->installEntitySchema('entity_test_computed_field'); + + // Create 2 entities to be referenced. + $ref1 = EntityTest::create(['name' => 'foo', 'type' => 'bar']); + $ref1->save(); + $ref2 = EntityTest::create(['name' => 'baz', 'type' => 'bar']); + $ref2->save(); + \Drupal::state()->set('entity_test_reference_computed_target_ids', [$ref1->id(), $ref2->id()]); + + $entity = EntityTestComputedField::create([]); + $entity->save(); + + /** @var \Drupal\entity_test\Plugin\Field\ComputedReferenceTestFieldItemList $field */ + $field = $entity->get('computed_reference_field'); + /** @var \Drupal\Core\Entity\EntityInterface[] $referenced_entities */ + $referenced_entities = $field->referencedEntities(); + + // Check that ::referencedEntities() is working with computed fields. + $this->assertEquals($ref1->id(), $referenced_entities[0]->id()); + $this->assertEquals($ref2->id(), $referenced_entities[1]->id()); + } + /** * Executes the computed properties tests for the given entity type. *