function SelectionTest::testAutocompleteOutput

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php \Drupal\Tests\field\Functional\EntityReference\Views\SelectionTest::testAutocompleteOutput()
  2. 8.9.x core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php \Drupal\Tests\field\Functional\EntityReference\Views\SelectionTest::testAutocompleteOutput()
  3. 11.x core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php \Drupal\Tests\field\Functional\EntityReference\Views\SelectionTest::testAutocompleteOutput()

Tests that the Views selection handles the views output properly.

File

core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php, line 89

Class

SelectionTest
Tests entity reference selection handler.

Namespace

Drupal\Tests\field\Functional\EntityReference\Views

Code

public function testAutocompleteOutput() : void {
    // Reset any internal static caching.
    \Drupal::service('entity_type.manager')->getStorage('node')
        ->resetCache();
    $view = Views::getView('test_entity_reference');
    $view->setDisplay();
    // Enable the display of the 'type' field so we can test that the output
    // does not contain only the entity label.
    $fields = $view->displayHandlers
        ->get('entity_reference_1')
        ->getOption('fields');
    $fields['type']['exclude'] = FALSE;
    $view->displayHandlers
        ->get('entity_reference_1')
        ->setOption('fields', $fields);
    $view->save();
    // Prepare the selection settings key needed by the entity reference
    // autocomplete route.
    $target_type = 'node';
    $selection_handler = 'views';
    $selection_settings = $this->handlerSettings;
    $selection_settings_key = Crypt::hmacBase64(serialize($selection_settings) . $target_type . $selection_handler, Settings::getHashSalt());
    \Drupal::keyValue('entity_autocomplete')->set($selection_settings_key, $selection_settings);
    $result = Json::decode($this->drupalGet('entity_reference_autocomplete/' . $target_type . '/' . $selection_handler . '/' . $selection_settings_key, [
        'query' => [
            'q' => 't',
        ],
    ]));
    $expected = [
        0 => [
            'value' => $this->nodes[1]
                ->bundle() . ': ' . $this->nodes[1]
                ->label() . ' (' . $this->nodes[1]
                ->id() . ')',
            'label' => '<span class="views-field views-field-type"><span class="field-content">' . $this->nodes[1]
                ->bundle() . '</span></span>: <span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[1]
                ->label()) . '</span></span>',
        ],
        1 => [
            'value' => $this->nodes[2]
                ->bundle() . ': ' . $this->nodes[2]
                ->label() . ' (' . $this->nodes[2]
                ->id() . ')',
            'label' => '<span class="views-field views-field-type"><span class="field-content">' . $this->nodes[2]
                ->bundle() . '</span></span>: <span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[2]
                ->label()) . '</span></span>',
        ],
        2 => [
            'value' => $this->nodes[3]
                ->bundle() . ': ' . $this->nodes[3]
                ->label() . ' (' . $this->nodes[3]
                ->id() . ')',
            'label' => '<span class="views-field views-field-type"><span class="field-content">' . $this->nodes[3]
                ->bundle() . '</span></span>: <span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[3]
                ->label()) . '</span></span>',
        ],
    ];
    $this->assertEquals($expected, $result, 'The autocomplete result of the Views entity reference selection handler contains the proper output.');
}

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