Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Functional / GlossaryTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\Core\Url;
7 use Drupal\views\Tests\AssertViewsCacheTagsTrait;
8 use Drupal\views\Views;
9
10 /**
11  * Tests glossary functionality of views.
12  *
13  * @group views
14  */
15 class GlossaryTest extends ViewTestBase {
16
17   use AssertViewsCacheTagsTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['node'];
25
26   /**
27    * Tests the default glossary view.
28    */
29   public function testGlossaryView() {
30     // Create a content type and add some nodes, with a non-random title.
31     $type = $this->drupalCreateContentType();
32     $nodes_per_char = [
33       'd' => 1,
34       'r' => 4,
35       'u' => 10,
36       'p' => 2,
37       'a' => 3,
38       'l' => 6,
39     ];
40     $nodes_by_char = [];
41     foreach ($nodes_per_char as $char => $count) {
42       $setting = [
43         'type' => $type->id(),
44       ];
45       for ($i = 0; $i < $count; $i++) {
46         $node = $setting;
47         $node['title'] = $char . $this->randomString(3);
48         $node = $this->drupalCreateNode($node);
49         $nodes_by_char[$char][] = $node;
50       }
51     }
52
53     // Execute glossary view
54     $view = Views::getView('glossary');
55     $view->setDisplay('attachment_1');
56     $view->executeDisplay('attachment_1');
57
58     // Check that the amount of nodes per char.
59     foreach ($view->result as $item) {
60       $this->assertEqual($nodes_per_char[$item->title_truncated], $item->num_records);
61     }
62
63     // Enable the glossary to be displayed.
64     $view->storage->enable()->save();
65     $this->container->get('router.builder')->rebuildIfNeeded();
66     $url = Url::fromRoute('view.glossary.page_1');
67
68     // Verify cache tags.
69     $this->assertPageCacheContextsAndTags(
70       $url,
71       [
72         'timezone',
73         'languages:' . LanguageInterface::TYPE_CONTENT,
74         'languages:' . LanguageInterface::TYPE_INTERFACE,
75         'theme',
76         'url',
77         'user.node_grants:view',
78         'user.permissions',
79         'route',
80       ],
81       [
82         'config:views.view.glossary',
83         // Listed for letter 'a'
84         'node:' . $nodes_by_char['a'][0]->id(), 'node:' . $nodes_by_char['a'][1]->id(), 'node:' . $nodes_by_char['a'][2]->id(),
85         // Link for letter 'd'.
86         'node:1',
87         // Link for letter 'p'.
88         'node:16',
89         // Link for letter 'r'.
90         'node:2',
91         // Link for letter 'l'.
92         'node:21',
93         // Link for letter 'u'.
94         'node:6',
95         'node_list',
96         'user:0',
97         'user_list',
98         'http_response',
99         'rendered',
100         // FinishResponseSubscriber adds this cache tag to responses that have
101         // the 'user.permissions' cache context for anonymous users.
102         'config:user.role.anonymous',
103       ]
104     );
105
106     // Check the actual page response.
107     $this->drupalGet($url);
108     $this->assertResponse(200);
109     foreach ($nodes_per_char as $char => $count) {
110       $href = Url::fromRoute('view.glossary.page_1', ['arg_0' => $char])->toString();
111       $label = mb_strtoupper($char);
112       // Get the summary link for a certain character. Filter by label and href
113       // to ensure that both of them are correct.
114       $result = $this->xpath('//a[contains(@href, :href) and normalize-space(text())=:label]/..', [':href' => $href, ':label' => $label]);
115       $this->assertTrue(count($result));
116       // The rendered output looks like "<a href=''>X</a> | (count)" so let's
117       // figure out the int.
118       $result_count = explode(' ', trim(str_replace(['|', '(', ')'], '', $result[0]->getText())))[1];
119       $this->assertEqual($result_count, $count, 'The expected number got rendered.');
120     }
121   }
122
123 }