function EntityReferenceAutoCreateTest::testAutoCreate

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::testAutoCreate()
  2. 10 core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::testAutoCreate()
  3. 11.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::testAutoCreate()

Tests the autocomplete input element and entity auto-creation.

File

core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php, line 108

Class

EntityReferenceAutoCreateTest
Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.

Namespace

Drupal\Tests\field\Functional\EntityReference

Code

public function testAutoCreate() {
    $this->drupalGet('node/add/' . $this->referencingType);
    $target = $this->assertSession()
        ->fieldExists("edit-test-field-0-target-id");
    $this->assertTrue($target->hasClass("form-autocomplete"));
    $new_title = $this->randomMachineName();
    // Assert referenced node does not exist.
    $base_query = \Drupal::entityQuery('node')->accessCheck(FALSE);
    $base_query->condition('type', $this->referencedType)
        ->condition('title', $new_title);
    $query = clone $base_query;
    $result = $query->execute();
    $this->assertEmpty($result, 'Referenced node does not exist yet.');
    $edit = [
        'title[0][value]' => $this->randomMachineName(),
        'test_field[0][target_id]' => $new_title,
    ];
    $this->drupalGet("node/add/{$this->referencingType}");
    $this->submitForm($edit, 'Save');
    // Assert referenced node was created.
    $query = clone $base_query;
    $result = $query->execute();
    $this->assertNotEmpty($result, 'Referenced node was created.');
    $referenced_nid = key($result);
    $referenced_node = Node::load($referenced_nid);
    // Assert the referenced node is associated with referencing node.
    $result = \Drupal::entityQuery('node')->accessCheck(FALSE)
        ->condition('type', $this->referencingType)
        ->execute();
    $referencing_nid = key($result);
    $referencing_node = Node::load($referencing_nid);
    $this->assertEquals($referenced_nid, $referencing_node->test_field->target_id, 'Newly created node is referenced from the referencing node.');
    // Now try to view the node and check that the referenced node is shown.
    $this->drupalGet('node/' . $referencing_node->id());
    $this->assertSession()
        ->pageTextContains($referencing_node->label());
    $this->assertSession()
        ->pageTextContains($referenced_node->label());
}

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