View source
<?php
namespace Drupal\Tests\views_ui\Kernel;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views_ui\Controller\ViewsUIController;
use Drupal\Component\Utility\Html;
use Drupal\views\Entity\View;
class TagTest extends ViewsKernelTestBase {
public static $modules = [
'views',
'views_ui',
'user',
];
public function testViewsUiAutocompleteTag() {
\Drupal::moduleHandler()
->loadInclude('views_ui', 'inc', 'admin');
$tags = [];
for ($i = 0; $i < 16; $i++) {
$suffix = $i % 2 ? 'odd' : 'even';
$tag = 'autocomplete_tag_test_' . $suffix . $this
->randomMachineName();
$tags[] = $tag;
View::create([
'tag' => $tag,
'id' => $this
->randomMachineName(),
])
->save();
}
$controller = ViewsUIController::create($this->container);
$request = $this->container
->get('request_stack')
->getCurrentRequest();
$request->query
->set('q', 'autocomplete_tag_test');
$result = $controller
->autocompleteTag($request);
$matches = (array) json_decode($result
->getContent(), TRUE);
$this
->assertCount(10, $matches, 'Make sure the maximum amount of tag results is 10.');
$suggestions = array_map(function ($tag) {
return [
'value' => $tag,
'label' => Html::escape($tag),
];
}, $tags);
foreach ($matches as $match) {
$this
->assertContains($match, $suggestions, 'Make sure the returned array has the proper format.');
}
$request->query
->set('q', 'autocomplete_tag_test_even');
$result = $controller
->autocompleteTag($request);
$matches = (array) json_decode($result
->getContent(), TRUE);
$this
->assertCount(8, $matches, 'Make sure that only a subset is returned.');
foreach ($matches as $tag) {
$this
->assertContains($tag['value'], $tags);
}
$request->query
->set('q', $this
->randomMachineName());
$result = $controller
->autocompleteTag($request);
$matches = (array) json_decode($result
->getContent());
$this
->assertCount(0, $matches, "Make sure an invalid tag doesn't return anything.");
}
public function testViewsUiAutocompleteIndividualTags($expected_tag, $search_string) {
$controller = ViewsUIController::create($this->container);
$request = $this->container
->get('request_stack')
->getCurrentRequest();
$tag = 'comma, 你好, Foo bar';
View::create([
'tag' => $tag,
'id' => $this
->randomMachineName(),
])
->save();
$request->query
->set('q', $search_string);
$result = $controller
->autocompleteTag($request);
$matches = (array) json_decode($result
->getContent());
$this
->assertCount(1, $matches);
$this
->assertSame($expected_tag, $matches[0]->value);
}
public function providerViewsUiAutocompleteIndividualTags() {
return [
'tag' => [
'comma',
'comma',
],
'case insensitive tag' => [
'comma',
'COMMA',
],
'Hello in Chinese (partial 1)' => [
'你好',
'你',
],
'Hello in Chinese (partial 2)' => [
'你好',
'好',
],
'Hello in Chinese' => [
'你好',
'你好',
],
'Starts with partial and case-sensitive' => [
'Foo bar',
'Foo',
],
'Starts with partial and case-insensitive' => [
'Foo bar',
'fOO',
],
'Ends with partial and case-sensitive' => [
'Foo bar',
'bar',
],
'Ends with partial and case-insensitive' => [
'Foo bar',
'BAR',
],
];
}
}