You are here

public function EntityReferenceItemTest::testContentEntityReferenceItem in Drupal 8

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

Tests the entity reference field type for referencing content entities.

File

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

Class

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

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testContentEntityReferenceItem() {
  $tid = $this->term
    ->id();

  // Just being able to create the entity like this verifies a lot of code.
  $entity = EntityTest::create();
  $entity->field_test_taxonomy_term->target_id = $tid;
  $entity->name->value = $this
    ->randomMachineName();
  $entity
    ->save();
  $entity = EntityTest::load($entity
    ->id());
  $this
    ->assertInstanceOf(FieldItemListInterface::class, $entity->field_test_taxonomy_term);
  $this
    ->assertInstanceOf(FieldItemInterface::class, $entity->field_test_taxonomy_term[0]);
  $this
    ->assertEqual($entity->field_test_taxonomy_term->target_id, $tid);
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $this->term
    ->getName());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->id(), $tid);
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->uuid(), $this->term
    ->uuid());

  // 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
    ->assertEqual($label
    ->render(), 'Taxonomy term ID');

  // Change the name of the term via the reference.
  $new_name = $this
    ->randomMachineName();
  $entity->field_test_taxonomy_term->entity
    ->setName($new_name);
  $entity->field_test_taxonomy_term->entity
    ->save();

  // Verify it is the correct name.
  $term = Term::load($tid);
  $this
    ->assertEqual($term
    ->getName(), $new_name);

  // Make sure the computed term reflects updates to the term id.
  $term2 = Term::create([
    'name' => $this
      ->randomMachineName(),
    'vid' => $this->term
      ->bundle(),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $term2
    ->save();

  // Test all the possible ways of assigning a value.
  $entity->field_test_taxonomy_term->target_id = $term
    ->id();
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->id(), $term
    ->id());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $term
    ->getName());
  $entity->field_test_taxonomy_term = [
    [
      'target_id' => $term2
        ->id(),
    ],
  ];
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->id(), $term2
    ->id());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $term2
    ->getName());

  // Test value assignment via the computed 'entity' property.
  $entity->field_test_taxonomy_term->entity = $term;
  $this
    ->assertEqual($entity->field_test_taxonomy_term->target_id, $term
    ->id());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $term
    ->getName());
  $entity->field_test_taxonomy_term = [
    [
      'entity' => $term2,
    ],
  ];
  $this
    ->assertEqual($entity->field_test_taxonomy_term->target_id, $term2
    ->id());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $term2
    ->getName());

  // Test assigning an invalid item throws an exception.
  try {
    $entity->field_test_taxonomy_term = [
      'target_id' => 'invalid',
      'entity' => $term2,
    ];
    $this
      ->fail('Assigning an invalid item throws an exception.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(\InvalidArgumentException::class, $e);
  }

  // Delete terms so we have nothing to reference and try again
  $term
    ->delete();
  $term2
    ->delete();
  $entity = EntityTest::create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $entity
    ->save();

  // Test the generateSampleValue() method.
  $entity = EntityTest::create();
  $entity->field_test_taxonomy_term
    ->generateSampleItems();
  $entity->field_test_taxonomy_vocabulary
    ->generateSampleItems();
  $this
    ->entityValidateAndSave($entity);

  // Tests that setting an integer target ID together with an entity object
  // succeeds and does not cause any exceptions. There is no assertion here,
  // as the assignment should not throw any exceptions and if it does the
  // test will fail.
  // @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::setValue().
  $user = User::create([
    'name' => $this
      ->randomString(),
  ]);
  $user
    ->save();
  $entity = EntityTest::create([
    'user_id' => [
      'target_id' => (int) $user
        ->id(),
      'entity' => $user,
    ],
  ]);
}