View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Site\Settings;
use Drupal\system\Controller\EntityAutocompleteController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class EntityAutocompleteTest extends EntityKernelTestBase {
protected static $modules = [
'entity_reference_test',
];
protected $entityType = 'entity_test';
protected $bundle = 'entity_test';
public function testEntityReferenceAutocompletion() {
$entity_1 = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'name' => '10/16/2011',
]);
$entity_1
->save();
$entity_2 = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'name' => '10/17/2011',
]);
$entity_2
->save();
$entity_3 = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'name' => 'label with, and / test',
]);
$entity_3
->save();
$input = '10/';
$data = $this
->getAutocompleteResult($input);
$this
->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
$this
->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
$input = '10/16';
$data = $this
->getAutocompleteResult($input);
$target = [
'value' => $entity_1->name->value . ' (1)',
'label' => Html::escape($entity_1->name->value),
];
$this
->assertSame($target, reset($data), 'Autocomplete returns only the expected matching entity.');
$user = $this
->drupalCreateUser([
'administer entity_test content',
]);
$this
->drupalSetCurrentUser($user);
$data = $this
->getAutocompleteResult($input, $entity_1
->id());
$this
->assertSame([], $data, 'Autocomplete returns empty results as first entity is passed to autocomplete request.');
$input = $entity_1->name->value . ' (1), 10/17';
$data = $this
->getAutocompleteResult($input);
$this
->assertSame(Html::escape($entity_2->name->value), $data[0]['label'], 'Autocomplete returned the second matching entity');
$input = '"label with, and /"';
$data = $this
->getAutocompleteResult($input);
$n = $entity_3->name->value . ' (3)';
$n = Tags::encode($n);
$target = [
'value' => $n,
'label' => Html::escape($entity_3->name->value),
];
$this
->assertSame($target, reset($data), 'Autocomplete returns an entity label containing a comma and a slash.');
foreach ([
'',
NULL,
FALSE,
0,
0.0,
] as $input) {
$data = $this
->getAutocompleteResult($input);
$this
->assertSame([], $data, 'Autocomplete of empty input returns empty result');
}
$input = ',';
$data = $this
->getAutocompleteResult($input);
$this
->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
$this
->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
$this
->assertSame(Html::escape($entity_3->name->value), $data[2]['label'], 'Autocomplete returned the third matching entity');
$data = $this
->getAutocompleteResult($input, $entity_1
->id());
$this
->assertCount(2, $data, 'Autocomplete returned only 2 entities');
$this
->assertSame(Html::escape($entity_2->name->value), $data[0]['label'], 'Autocomplete returned the second matching entity');
$this
->assertSame(Html::escape($entity_3->name->value), $data[1]['label'], 'Autocomplete returned the third matching entity');
$input = '"l!J>&Tw';
$data = $this
->getAutocompleteResult($input);
$this
->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
$this
->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
$this
->assertSame(Html::escape($entity_3->name->value), $data[2]['label'], 'Autocomplete returned the third matching entity');
$input = '0';
$data = $this
->getAutocompleteResult($input);
$this
->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
$this
->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
}
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 {
$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) {
}
try {
$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());
}
}
protected function getAutocompleteResult($input, $entity_id = NULL) {
$request = Request::create('entity_reference_autocomplete/' . $this->entityType . '/entity_test_all_except_host');
$request->query
->set('q', $input);
$selection_settings = [];
if ($entity_id) {
$request->query
->set('entity_type', $this->entityType);
$request->query
->set('entity_id', $entity_id);
}
$selection_settings_key = Crypt::hmacBase64(serialize($selection_settings) . $this->entityType . 'entity_test_all_except_host', Settings::getHashSalt());
\Drupal::keyValue('entity_autocomplete')
->set($selection_settings_key, $selection_settings);
$entity_reference_controller = EntityAutocompleteController::create($this->container);
$result = $entity_reference_controller
->handleAutocomplete($request, $this->entityType, 'entity_test_all_except_host', $selection_settings_key)
->getContent();
return Json::decode($result);
}
}