function EntityReferenceAutocompleteWidgetTest::testEntityReferenceAutocompleteWidgetAttachedEntity

Same name in other branches
  1. 10 core/tests/Drupal/FunctionalJavascriptTests/EntityReference/EntityReferenceAutocompleteWidgetTest.php \Drupal\FunctionalJavascriptTests\EntityReference\EntityReferenceAutocompleteWidgetTest::testEntityReferenceAutocompleteWidgetAttachedEntity()

Tests that the autocomplete widget knows about the entity its attached to.

Ensures that the entity the autocomplete widget stores the entity it is rendered on, and is available in the autocomplete results' AJAX request.

File

core/tests/Drupal/FunctionalJavascriptTests/EntityReference/EntityReferenceAutocompleteWidgetTest.php, line 167

Class

EntityReferenceAutocompleteWidgetTest
Tests the output of entity reference autocomplete widgets.

Namespace

Drupal\FunctionalJavascriptTests\EntityReference

Code

public function testEntityReferenceAutocompleteWidgetAttachedEntity() : void {
    $user = $this->drupalCreateUser([
        'administer entity_test content',
    ]);
    $this->drupalLogin($user);
    $field_name = 'field_test';
    $this->createEntityReferenceField('entity_test', 'entity_test', $field_name, $field_name, 'entity_test', 'entity_test_all_except_host', [
        'target_bundles' => [
            'entity_test',
        ],
    ]);
    $form_display = EntityFormDisplay::load('entity_test.entity_test.default');
    $form_display->setComponent($field_name, [
        'type' => 'entity_reference_autocomplete',
        'settings' => [
            'match_operator' => 'CONTAINS',
        ],
    ]);
    $form_display->save();
    $host = EntityTest::create([
        'name' => 'dark green',
    ]);
    $host->save();
    EntityTest::create([
        'name' => 'dark blue',
    ])->save();
    $this->drupalGet($host->toUrl('edit-form'));
    // Trigger the autocomplete.
    $page = $this->getSession()
        ->getPage();
    $autocomplete_field = $page->findField($field_name . '[0][target_id]');
    $autocomplete_field->setValue('dark');
    $this->getSession()
        ->getDriver()
        ->keyDown($autocomplete_field->getXpath(), ' ');
    $this->assertSession()
        ->waitOnAutocomplete();
    // Check the autocomplete results.
    $results = $page->findAll('css', '.ui-autocomplete li');
    $this->assertCount(1, $results);
    $this->assertSession()
        ->elementTextNotContains('css', '.ui-autocomplete li', 'dark green');
    $this->assertSession()
        ->elementTextContains('css', '.ui-autocomplete li', 'dark blue');
}

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