3 namespace Drupal\Tests\views\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\views\Tests\AssertViewsCacheTagsTrait;
9 use Drupal\views\Views;
12 * Tests glossary functionality of views.
16 class GlossaryTest extends ViewTestBase {
18 use AssertViewsCacheTagsTrait;
25 public static $modules = ['node'];
28 * Tests the default glossary view.
30 public function testGlossaryView() {
31 // Create a content type and add some nodes, with a non-random title.
32 $type = $this->drupalCreateContentType();
42 foreach ($nodes_per_char as $char => $count) {
46 for ($i = 0; $i < $count; $i++) {
48 $node['title'] = $char . $this->randomString(3);
49 $node = $this->drupalCreateNode($node);
50 $nodes_by_char[$char][] = $node;
54 // Execute glossary view
55 $view = Views::getView('glossary');
56 $view->setDisplay('attachment_1');
57 $view->executeDisplay('attachment_1');
59 // Check that the amount of nodes per char.
60 foreach ($view->result as $item) {
61 $this->assertEqual($nodes_per_char[$item->title_truncated], $item->num_records);
64 // Enable the glossary to be displayed.
65 $view->storage->enable()->save();
66 $this->container->get('router.builder')->rebuildIfNeeded();
67 $url = Url::fromRoute('view.glossary.page_1');
70 $this->assertPageCacheContextsAndTags(
74 'languages:' . LanguageInterface::TYPE_CONTENT,
75 'languages:' . LanguageInterface::TYPE_INTERFACE,
78 'user.node_grants:view',
83 'config:views.view.glossary',
84 // Listed for letter 'a'
85 'node:' . $nodes_by_char['a'][0]->id(), 'node:' . $nodes_by_char['a'][1]->id(), 'node:' . $nodes_by_char['a'][2]->id(),
86 // Link for letter 'd'.
88 // Link for letter 'p'.
90 // Link for letter 'r'.
92 // Link for letter 'l'.
94 // Link for letter 'u'.
101 // FinishResponseSubscriber adds this cache tag to responses that have
102 // the 'user.permissions' cache context for anonymous users.
103 'config:user.role.anonymous',
107 // Check the actual page response.
108 $this->drupalGet($url);
109 $this->assertResponse(200);
110 foreach ($nodes_per_char as $char => $count) {
111 $href = Url::fromRoute('view.glossary.page_1', ['arg_0' => $char])->toString();
112 $label = Unicode::strtoupper($char);
113 // Get the summary link for a certain character. Filter by label and href
114 // to ensure that both of them are correct.
115 $result = $this->xpath('//a[contains(@href, :href) and normalize-space(text())=:label]/..', [':href' => $href, ':label' => $label]);
116 $this->assertTrue(count($result));
117 // The rendered output looks like "<a href=''>X</a> | (count)" so let's
118 // figure out the int.
119 $result_count = explode(' ', trim(str_replace(['|', '(', ')'], '', $result[0]->getText())))[1];
120 $this->assertEqual($result_count, $count, 'The expected number got rendered.');