Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php \Drupal\KernelTests\Core\Entity\EntityAutocompleteTest::testSelectionSettingsHandling()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php \Drupal\KernelTests\Core\Entity\EntityAutocompleteTest::testSelectionSettingsHandling()

Tests that missing or invalid selection setting key are handled correctly.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php, line 148

Class

EntityAutocompleteTest
Tests the autocomplete functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testSelectionSettingsHandling() {
  $entity_reference_controller = EntityAutocompleteController::create($this->container);
  $request = Request::create('entity_reference_autocomplete/' . $this->entityType . '/default');
  $request->query
    ->set('q', $this
    ->randomString());
  try {

    // Pass an invalid selection settings key (i.e. one that does not exist
    // in the key/value store).
    $selection_settings_key = $this
      ->randomString();
    $entity_reference_controller
      ->handleAutocomplete($request, $this->entityType, 'default', $selection_settings_key);
    $this
      ->fail('Non-existent selection settings key throws an exception.');
  } catch (AccessDeniedHttpException $e) {

    // Expected exception; just continue testing.
  }
  try {

    // Generate a valid hash key but store a modified settings array.
    $selection_settings = [];
    $selection_settings_key = Crypt::hmacBase64(serialize($selection_settings) . $this->entityType . 'default', Settings::getHashSalt());
    $selection_settings[$this
      ->randomMachineName()] = $this
      ->randomString();
    \Drupal::keyValue('entity_autocomplete')
      ->set($selection_settings_key, $selection_settings);
    $entity_reference_controller
      ->handleAutocomplete($request, $this->entityType, 'default', $selection_settings_key);
  } catch (AccessDeniedHttpException $e) {
    $this
      ->assertSame('Invalid selection settings key.', $e
      ->getMessage());
  }
}