function EntityReferenceItemTest::testConfigEntityReferenceItem

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

Tests the entity reference field type for referencing config entities.

File

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

Class

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

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testConfigEntityReferenceItem() : void {
    $referenced_entity_id = $this->vocabulary
        ->id();
    // Just being able to create the entity like this verifies a lot of code.
    $entity = EntityTest::create();
    $entity->field_test_taxonomy_vocabulary->target_id = $referenced_entity_id;
    $entity->name->value = $this->randomMachineName();
    $entity->save();
    $entity = EntityTest::load($entity->id());
    $this->assertInstanceOf(FieldItemListInterface::class, $entity->field_test_taxonomy_vocabulary);
    $this->assertInstanceOf(FieldItemInterface::class, $entity->field_test_taxonomy_vocabulary[0]);
    $this->assertEquals($referenced_entity_id, $entity->field_test_taxonomy_vocabulary->target_id);
    $this->assertEquals($this->vocabulary
        ->label(), $entity->field_test_taxonomy_vocabulary->entity
        ->label());
    $this->assertEquals($referenced_entity_id, $entity->field_test_taxonomy_vocabulary->entity
        ->id());
    $this->assertEquals($this->vocabulary
        ->uuid(), $entity->field_test_taxonomy_vocabulary->entity
        ->uuid());
    // Change the name of the term via the reference.
    $new_name = $this->randomMachineName();
    $entity->field_test_taxonomy_vocabulary->entity
        ->set('name', $new_name);
    $entity->field_test_taxonomy_vocabulary->entity
        ->save();
    // Verify it is the correct name.
    $vocabulary = Vocabulary::load($referenced_entity_id);
    $this->assertEquals($new_name, $vocabulary->label());
    // Make sure the computed term reflects updates to the term id.
    $vocabulary2 = $vocabulary = Vocabulary::create([
        'name' => $this->randomMachineName(),
        'vid' => $this->randomMachineName(),
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $vocabulary2->save();
    $entity->field_test_taxonomy_vocabulary->target_id = $vocabulary2->id();
    $this->assertEquals($vocabulary2->id(), $entity->field_test_taxonomy_vocabulary->entity
        ->id());
    $this->assertEquals($vocabulary2->label(), $entity->field_test_taxonomy_vocabulary->entity
        ->label());
    // Delete terms so we have nothing to reference and try again
    $this->vocabulary
        ->delete();
    $vocabulary2->delete();
    $entity = EntityTest::create([
        'name' => $this->randomMachineName(),
    ]);
    $entity->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.