View source
<?php
namespace Drupal\system\Tests\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 EntityUnitTestBase {
protected $entityType = 'entity_test';
protected $bundle = 'entity_test';
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'key_value',
]);
}
function testEntityReferenceAutocompletion() {
$entity_1 = entity_create($this->entityType, array(
'name' => '10/16/2011',
));
$entity_1
->save();
$entity_2 = entity_create($this->entityType, array(
'name' => '10/17/2011',
));
$entity_2
->save();
$entity_3 = entity_create($this->entityType, array(
'name' => 'label with, and / <em>test</em>',
));
$entity_3
->save();
$input = '10/';
$data = $this
->getAutocompleteResult($input);
$this
->assertIdentical($data[0]['label'], Html::escape($entity_1->name->value), 'Autocomplete returned the first matching entity');
$this
->assertIdentical($data[1]['label'], Html::escape($entity_2->name->value), 'Autocomplete returned the second matching entity');
$input = '10/16';
$data = $this
->getAutocompleteResult($input);
$target = array(
'value' => $entity_1->name->value . ' (1)',
'label' => Html::escape($entity_1->name->value),
);
$this
->assertIdentical(reset($data), $target, 'Autocomplete returns only the expected matching entity.');
$input = $entity_1->name->value . ' (1), 10/17';
$data = $this
->getAutocompleteResult($input);
$this
->assertIdentical($data[0]['label'], Html::escape($entity_2->name->value), 'Autocomplete returned the second matching entity');
$input = '"label with, and / <em>';
$data = $this
->getAutocompleteResult($input);
$n = $entity_3->name->value . ' (3)';
$n = Tags::encode($n);
$target = array(
'value' => $n,
'label' => Html::escape($entity_3->name->value),
);
$this
->assertIdentical(reset($data), $target, 'Autocomplete returns an entity label containing a comma and a slash.');
}
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) {
$this
->pass('Non-existent selection settings key throws an exception.');
}
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) {
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.');
}
}
}
protected function getAutocompleteResult($input) {
$request = Request::create('entity_reference_autocomplete/' . $this->entityType . '/default');
$request->query
->set('q', $input);
$selection_settings = [];
$selection_settings_key = Crypt::hmacBase64(serialize($selection_settings) . $this->entityType . 'default', 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, 'default', $selection_settings_key)
->getContent();
return Json::decode($result);
}
}