3 namespace Drupal\Tests\views_ui\Kernel;
5 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
6 use Drupal\views_ui\Controller\ViewsUIController;
7 use Drupal\Component\Utility\Html;
8 use Drupal\views\Entity\View;
11 * Tests the views ui tagging functionality.
15 class TagTest extends ViewsKernelTestBase {
22 public static $modules = ['views', 'views_ui', 'user'];
25 * Tests the views_ui_autocomplete_tag function.
27 public function testViewsUiAutocompleteTag() {
28 \Drupal::moduleHandler()->loadInclude('views_ui', 'inc', 'admin');
30 // Save 15 views with a tag.
32 for ($i = 0; $i < 16; $i++) {
33 $suffix = $i % 2 ? 'odd' : 'even';
34 $tag = 'autocomplete_tag_test_' . $suffix . $this->randomMachineName();
36 View::create(['tag' => $tag, 'id' => $this->randomMachineName()])->save();
39 // Make sure just ten results are returned.
40 $controller = ViewsUIController::create($this->container);
41 $request = $this->container->get('request_stack')->getCurrentRequest();
42 $request->query->set('q', 'autocomplete_tag_test');
43 $result = $controller->autocompleteTag($request);
44 $matches = (array) json_decode($result->getContent(), TRUE);
45 $this->assertEqual(count($matches), 10, 'Make sure the maximum amount of tag results is 10.');
47 // Make sure the returned array has the proper format.
48 $suggestions = array_map(function ($tag) {
49 return ['value' => $tag, 'label' => Html::escape($tag)];
51 foreach ($matches as $match) {
52 $this->assertTrue(in_array($match, $suggestions), 'Make sure the returned array has the proper format.');
56 // Make sure that matching by a certain prefix works.
57 $request->query->set('q', 'autocomplete_tag_test_even');
58 $result = $controller->autocompleteTag($request);
59 $matches = (array) json_decode($result->getContent(), TRUE);
60 $this->assertEqual(count($matches), 8, 'Make sure that only a subset is returned.');
61 foreach ($matches as $tag) {
62 $this->assertTrue(array_search($tag['value'], $tags) !== FALSE, format_string('Make sure the returned tag @tag actually exists.', ['@tag' => $tag['value']]));
65 // Make sure an invalid result doesn't return anything.
66 $request->query->set('q', $this->randomMachineName());
67 $result = $controller->autocompleteTag($request);
68 $matches = (array) json_decode($result->getContent());
69 $this->assertEqual(count($matches), 0, "Make sure an invalid tag doesn't return anything.");