Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views_ui / tests / src / Kernel / TagTest.php
1 <?php
2
3 namespace Drupal\Tests\views_ui\Kernel;
4
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;
9
10 /**
11  * Tests the views ui tagging functionality.
12  *
13  * @group views_ui
14  */
15 class TagTest extends ViewsKernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['views', 'views_ui', 'user'];
23
24   /**
25    * Tests the views_ui_autocomplete_tag function.
26    */
27   public function testViewsUiAutocompleteTag() {
28     \Drupal::moduleHandler()->loadInclude('views_ui', 'inc', 'admin');
29
30     // Save 15 views with a tag.
31     $tags = [];
32     for ($i = 0; $i < 16; $i++) {
33       $suffix = $i % 2 ? 'odd' : 'even';
34       $tag = 'autocomplete_tag_test_' . $suffix . $this->randomMachineName();
35       $tags[] = $tag;
36       View::create(['tag' => $tag, 'id' => $this->randomMachineName()])->save();
37     }
38
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.');
46
47     // Make sure the returned array has the proper format.
48     $suggestions = array_map(function ($tag) {
49       return ['value' => $tag, 'label' => Html::escape($tag)];
50     }, $tags);
51     foreach ($matches as $match) {
52       $this->assertTrue(in_array($match, $suggestions), 'Make sure the returned array has the proper format.');
53     }
54
55     // Make sure that matching by a certain prefix works.
56     $request->query->set('q', 'autocomplete_tag_test_even');
57     $result = $controller->autocompleteTag($request);
58     $matches = (array) json_decode($result->getContent(), TRUE);
59     $this->assertEqual(count($matches), 8, 'Make sure that only a subset is returned.');
60     foreach ($matches as $tag) {
61       $this->assertTrue(array_search($tag['value'], $tags) !== FALSE, format_string('Make sure the returned tag @tag actually exists.', ['@tag' => $tag['value']]));
62     }
63
64     // Make sure an invalid result doesn't return anything.
65     $request->query->set('q', $this->randomMachineName());
66     $result = $controller->autocompleteTag($request);
67     $matches = (array) json_decode($result->getContent());
68     $this->assertEqual(count($matches), 0, "Make sure an invalid tag doesn't return anything.");
69   }
70
71 }