function LinkFieldTest::testEditNonNodeEntityLink

Tests editing a link to a non-node entity.

File

core/modules/link/tests/src/Functional/LinkFieldTest.php, line 695

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\Tests\link\Functional

Code

public function testEditNonNodeEntityLink() {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_test_storage = $entity_type_manager->getStorage('entity_test');
    // Create a field with settings to validate.
    $this->fieldStorage = FieldStorageConfig::create([
        'field_name' => 'field_link',
        'entity_type' => 'entity_test',
        'type' => 'link',
        'cardinality' => 1,
    ]);
    $this->fieldStorage
        ->save();
    FieldConfig::create([
        'field_storage' => $this->fieldStorage,
        'label' => 'Read more about this entity',
        'bundle' => 'entity_test',
        'settings' => [
            'title' => DRUPAL_OPTIONAL,
        ],
    ])
        ->save();
    $entity_type_manager->getStorage('entity_form_display')
        ->load('entity_test.entity_test.default')
        ->setComponent('field_link', [
        'type' => 'link_default',
    ])
        ->save();
    // Create a node and a test entity to have a possibly valid reference for
    // both. Create another test entity that references the first test entity.
    $entity_test_link = $entity_test_storage->create([
        'name' => 'correct link target',
    ]);
    $entity_test_link->save();
    // Create a node with the same ID as the test entity to ensure that the link
    // doesn't match incorrectly.
    $this->drupalCreateNode([
        'title' => 'wrong link target',
    ]);
    $correct_link = 'entity:entity_test/' . $entity_test_link->id();
    $entity_test = $entity_test_storage->create([
        'name' => 'correct link target',
        'field_link' => $correct_link,
    ]);
    $entity_test->save();
    // Edit the entity and save it, verify the correct link is kept and not
    // changed to point to a node. Currently, widget does not support non-node
    // autocomplete and therefore must show the link unaltered.
    $this->drupalGet($entity_test->toUrl('edit-form'));
    $this->assertSession()
        ->fieldValueEquals('field_link[0][uri]', $correct_link);
    $this->drupalPostForm(NULL, [], 'Save');
    $entity_test_storage->resetCache();
    $entity_test = $entity_test_storage->load($entity_test->id());
    $this->assertEquals($correct_link, $entity_test->get('field_link')->uri);
}

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