function EntityReferenceFieldCreationTest::testAddReferenceFieldTargetingEntityTypeWithoutId

Same name and namespace in other branches
  1. 10 core/modules/system/tests/src/Functional/Entity/EntityReferenceFieldCreationTest.php \Drupal\Tests\system\Functional\Entity\EntityReferenceFieldCreationTest::testAddReferenceFieldTargetingEntityTypeWithoutId()
  2. 11.x core/modules/system/tests/src/Functional/Entity/EntityReferenceFieldCreationTest.php \Drupal\Tests\system\Functional\Entity\EntityReferenceFieldCreationTest::testAddReferenceFieldTargetingEntityTypeWithoutId()

Tests that entity reference fields cannot target entity types without IDs.

File

core/modules/system/tests/src/Functional/Entity/EntityReferenceFieldCreationTest.php, line 30

Class

EntityReferenceFieldCreationTest
Tests creating entity reference fields in the UI.

Namespace

Drupal\Tests\system\Functional\Entity

Code

public function testAddReferenceFieldTargetingEntityTypeWithoutId() {
    $this->drupalLogin($this->rootUser);
    $node_type = $this->drupalCreateContentType()
        ->id();
    // Entity types without an ID key should not be presented as options when
    // creating an entity reference field in the UI.
    $this->drupalGet("/admin/structure/types/manage/{$node_type}/fields/add-field");
    $edit = [
        'new_storage_type' => 'entity_reference',
        'label' => 'Test Field',
        'field_name' => 'test_reference_field',
    ];
    $this->submitForm($edit, 'Save and continue');
    $this->assertSession()
        ->optionNotExists('settings[target_type]', 'entity_test_no_id');
    // Trying to do it programmatically should raise an exception.
    $this->expectException('\\Drupal\\Core\\Field\\FieldException');
    $this->expectExceptionMessage('Entity type "entity_test_no_id" has no ID key and cannot be targeted by entity reference field "test_reference_field"');
    $this->createEntityReferenceField('node', $node_type, 'test_reference_field', 'Test Field', 'entity_test_no_id');
}

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