View source
<?php
namespace Drupal\Tests\linkit\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\linkit\Controller\AutocompleteController;
use Drupal\linkit\Entity\Profile;
use Symfony\Component\HttpFoundation\Request;
class LinkitAutocompleteTest extends LinkitKernelTestBase {
public static $modules = [
'entity_test',
'language',
];
protected $linkitProfile;
protected $matcherManager;
protected $langcodes;
protected function setUp() {
parent::setUp();
$this
->createUser();
\Drupal::currentUser()
->setAccount($this
->createUser([], [
'view test entity',
]));
\Drupal::service('router.builder')
->rebuild();
$this
->installEntitySchema('user');
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('entity_test_mul');
$this->matcherManager = $this->container
->get('plugin.manager.linkit.matcher');
$this->linkitProfile = $this
->createProfile();
}
public function testAutocompletionAccess() {
$plugin = $this->matcherManager
->createInstance('entity:entity_test');
$this->linkitProfile
->addMatcher($plugin
->getConfiguration());
$this->linkitProfile
->save();
$entity_1 = EntityTest::create([
'name' => 'no_forbid_access',
]);
$entity_1
->save();
$entity_2 = EntityTest::create([
'name' => 'forbid_access',
]);
$entity_2
->save();
$suggestions = $this
->getAutocompleteResult('forbid');
$this
->assertTrue(count($suggestions) == 1, 'Autocomplete returned the expected amount of suggestions.');
$this
->assertSame($entity_1
->label(), $suggestions[0]['label'], 'Autocomplete did not include the inaccessible entity.');
}
public function testAutocompletionFront() {
$plugin = $this->matcherManager
->createInstance('front_page');
$this->linkitProfile
->addMatcher($plugin
->getConfiguration());
$this->linkitProfile
->save();
$data = $this
->getAutocompleteResult('front');
$this
->assertSame('Front page', $data[0]['label'], 'Autocomplete returned the front page suggestion.');
}
public function testAutocompletionEmail() {
$plugin = $this->matcherManager
->createInstance('email');
$this->linkitProfile
->addMatcher($plugin
->getConfiguration());
$this->linkitProfile
->save();
$email = 'drupal@example.com';
$data = $this
->getAutocompleteResult($email);
$this
->assertSame((string) new FormattableMarkup('E-mail @email', [
'@email' => $email,
]), $data[0]['label'], 'Autocomplete returned email suggestion.');
$this
->assertSame('mailto:' . $email, $data[0]['path'], 'Autocomplete returned email suggestion with an mailto href.');
}
public function testAutocompletion() {
$plugin = $this->matcherManager
->createInstance('entity:entity_test');
$this->linkitProfile
->addMatcher($plugin
->getConfiguration());
$this->linkitProfile
->save();
$entity_1 = EntityTest::create([
'name' => 'Barbar',
]);
$entity_1
->save();
$entity_2 = EntityTest::create([
'name' => 'Foobar',
]);
$entity_2
->save();
$entity_3 = EntityTest::create([
'name' => 'Basbar',
]);
$entity_3
->save();
$data = $this
->getAutocompleteResult('no_suggestions');
$this
->assertTrue(count($data) == 1, 'Autocomplete returned the expected amount of suggestions.');
$this
->assertSame(Html::escape('no_suggestions'), $data[0]['label'], 'Autocomplete returned the "no result" suggestion.');
$data = $this
->getAutocompleteResult('bas');
$this
->assertTrue(count($data) == 1, 'Autocomplete returned the expected amount of suggestions.');
$this
->assertSame(Html::escape($entity_3
->label()), $data[0]['label'], 'Autocomplete returned the matching entity');
$data = $this
->getAutocompleteResult('bar');
$this
->assertTrue(count($data) == 3, 'Autocomplete returned the expected amount of suggestions.');
$this
->assertSame(Html::escape($entity_1
->label()), $data[0]['label'], 'Autocomplete returned the first matching entity.');
$this
->assertSame(Html::escape($entity_3
->label()), $data[1]['label'], 'Autocomplete returned the second matching entity.');
$this
->assertSame(Html::escape($entity_2
->label()), $data[2]['label'], 'Autocomplete returned the third matching entity.');
$data = $this
->getAutocompleteResult('');
$this
->assertEmpty(count($data), 'Autocomplete did not return any suggestions.');
}
public function testAutocompletionWithLimit() {
$plugin = $this->matcherManager
->createInstance('entity:entity_test');
$configuration = $plugin
->getConfiguration();
$configuration['settings']['limit'] = 2;
$this->linkitProfile
->addMatcher($configuration);
$this->linkitProfile
->save();
$entity_1 = EntityTest::create([
'name' => 'foo 1',
]);
$entity_1
->save();
$entity_2 = EntityTest::create([
'name' => 'foo 2',
]);
$entity_2
->save();
$entity_3 = EntityTest::create([
'name' => 'foo 3',
]);
$entity_3
->save();
$data = $this
->getAutocompleteResult('foo');
$this
->assertTrue(count($data) == 2, 'Autocomplete returned the expected amount of suggestions.');
}
public function testAutocompletionTranslations() {
$plugin = $this->matcherManager
->createInstance('entity:entity_test_mul');
$this->linkitProfile
->addMatcher($plugin
->getConfiguration());
$this->linkitProfile
->save();
$this
->setupLanguages();
$entity = EntityTestMul::create([
'name' => 'Barbar',
]);
$translations = $this->langcodes;
array_shift($translations);
foreach ($translations as $langcode) {
$entity
->addTranslation($langcode, [
'name' => 'Barbar ' . $langcode,
]);
}
$entity
->save();
foreach ($this->langcodes as $langcode) {
$this
->config('system.site')
->set('default_langcode', $langcode)
->save();
$data = $this
->getAutocompleteResult('bar');
$this
->assertTrue(count($data) == 1, 'Autocomplete returned the expected amount of suggestions.');
$this
->assertSame($entity
->getTranslation($langcode)
->label(), $data[0]['label'], 'Autocomplete returned the "no results."');
}
}
protected function getAutocompleteResult($input) {
$request = Request::create('linkit/autocomplete/' . $this->linkitProfile
->id());
$request->query
->set('q', $input);
$controller = AutocompleteController::create($this->container);
$result = Json::decode($controller
->autocomplete($request, $this->linkitProfile
->id())
->getContent());
return $result['suggestions'];
}
protected function createProfile(array $settings = []) {
$settings += [
'id' => mb_strtolower($this
->randomMachineName()),
'label' => $this
->randomMachineName(),
];
$profile = Profile::create($settings);
$profile
->save();
return $profile;
}
protected function noResults() {
return [
'title' => 'No results',
];
}
protected function setupLanguages() {
$this->langcodes = [
'sv',
'da',
'fi',
];
foreach ($this->langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
array_unshift($this->langcodes, \Drupal::languageManager()
->getDefaultLanguage()
->getId());
}
}