You are here

public function EntityReferenceItemTest::testContentEntityReferenceItemWithStringId in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testContentEntityReferenceItemWithStringId()
  2. 10 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testContentEntityReferenceItemWithStringId()

Tests referencing content entities with string IDs.

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php, line 250

Class

EntityReferenceItemTest
Tests the new entity API for the entity reference field type.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testContentEntityReferenceItemWithStringId() {
  $entity = EntityTest::create();
  $entity->field_test_entity_test_string_id->target_id = $this->entityStringId
    ->id();
  $entity
    ->save();
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_test');
  $storage
    ->resetCache();
  $this
    ->assertEquals($this->entityStringId
    ->id(), $storage
    ->load($entity
    ->id())->field_test_entity_test_string_id->target_id);

  // Verify that the label for the target ID property definition is correct.
  $label = $entity->field_test_taxonomy_term
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getPropertyDefinition('target_id')
    ->getLabel();
  $this
    ->assertInstanceOf(TranslatableMarkup::class, $label);
  $this
    ->assertEquals('Taxonomy term ID', $label
    ->render());
}