You are here

public function DynamicEntityReferenceItemTest::testMultipleEntityReferenceItem in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/DynamicEntityReferenceItemTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceItemTest::testMultipleEntityReferenceItem()

Tests the der field type for referencing multiple content entities.

File

tests/src/Kernel/DynamicEntityReferenceItemTest.php, line 312

Class

DynamicEntityReferenceItemTest
Tests the new entity API for the dynamic entity reference field type.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testMultipleEntityReferenceItem() {

  // Allow to reference multiple entities.
  $field_storage = FieldStorageConfig::loadByName('entity_test', 'field_der');
  $field_storage
    ->set('settings', [
    'exclude_entity_types' => FALSE,
    'entity_type_ids' => [
      'taxonomy_term',
      'user',
    ],
  ])
    ->set('cardinality', FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->save();
  $entity = EntityTest::create();
  $account = User::load(1);
  $entity->field_der[] = [
    'entity' => $this->term,
  ];
  $entity->field_der[] = [
    'entity' => $account,
  ];
  $entity
    ->save();

  // Check term reference correctly.
  $this
    ->assertEquals($entity->field_der[0]->target_id, $this->term
    ->id());
  $this
    ->assertEquals($entity->field_der[0]->target_type, $this->term
    ->getEntityTypeId());
  $this
    ->assertEquals($entity->field_der[0]->entity
    ->getName(), $this->term
    ->getName());
  $this
    ->assertEquals($entity->field_der[0]->entity
    ->id(), $this->term
    ->id());
  $this
    ->assertEquals($entity->field_der[0]->entity
    ->uuid(), $this->term
    ->uuid());

  // Check user reference correctly.
  $this
    ->assertEquals($entity->field_der[1]->target_id, $account
    ->id());
  $this
    ->assertEquals($entity->field_der[1]->target_type, $account
    ->getEntityTypeId());
  $this
    ->assertEquals($entity->field_der[1]->entity
    ->id(), $account
    ->id());
  $this
    ->assertEquals($entity->field_der[1]->entity
    ->uuid(), $account
    ->uuid());
}