AutocompleteHandlerTest.php in Entity Reference Hierarchy 8.2
File
tests/src/Kernel/AutocompleteHandlerTest.php
View source
<?php
namespace Drupal\Tests\entity_hierarchy\Kernel;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Site\Settings;
use Drupal\system\Controller\EntityAutocompleteController;
use Symfony\Component\HttpFoundation\Request;
class AutocompleteHandlerTest extends EntityHierarchyKernelTestBase {
protected $settings = [];
protected function setUp() {
parent::setUp();
$user = $this
->createUser([], [
'view test entity',
]);
$this->container
->get('account_switcher')
->switchTo($user);
}
public function testAutoCompleteHandler() {
$child = $this
->createTestEntity($this->parent
->id(), 'Child');
$grandchild = $this
->createTestEntity($child
->id(), 'Grandchild');
$great_grandchild = $this
->createTestEntity($grandchild
->id(), 'Great Grandchild');
$results = $this
->getAutocompleteResult('Great');
$this
->assertCount(1, $results);
$result = reset($results);
$label = sprintf('Great Grandchild (%s ❭ Child ❭ Grandchild)', $this->parent
->label());
$this
->assertEquals([
'value' => sprintf('%s (%s)', $label, $great_grandchild
->id()),
'label' => $label,
], $result);
}
protected function getAutocompleteResult($input) {
$request = Request::create('entity_reference_autocomplete/' . self::ENTITY_TYPE . '/entity_hierarchy');
$request->query
->set('q', $input);
$selection_settings_key = Crypt::hmacBase64(serialize($this->settings) . self::ENTITY_TYPE . 'entity_hierarchy', Settings::getHashSalt());
\Drupal::keyValue('entity_autocomplete')
->set($selection_settings_key, $this->settings);
$entity_reference_controller = EntityAutocompleteController::create($this->container);
$result = $entity_reference_controller
->handleAutocomplete($request, self::ENTITY_TYPE, 'entity_hierarchy', $selection_settings_key)
->getContent();
return Json::decode($result);
}
}