public function EntityAutocompleteTest::testSelectionSettingsHandling in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php \Drupal\system\Tests\Entity\EntityAutocompleteTest::testSelectionSettingsHandling()
Tests that missing or invalid selection setting key are handled correctly.
File
- core/
modules/ system/ src/ Tests/ Entity/ EntityAutocompleteTest.php, line 103 - Contains \Drupal\system\Tests\Entity\EntityAutocompleteTest.
Class
- EntityAutocompleteTest
- Tests the autocomplete functionality.
Namespace
Drupal\system\Tests\EntityCode
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) {
$this
->pass('Non-existent selection settings key throws an exception.');
}
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) {
if ($e
->getMessage() == 'Invalid selection settings key.') {
$this
->pass('Invalid selection settings key throws an exception.');
}
else {
$this
->fail('Invalid selection settings key throws an exception.');
}
}
}