3 namespace Drupal\Tests\views\Functional;
5 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\views\Tests\AssertViewsCacheTagsTrait;
8 use Drupal\views\Views;
11 * Tests glossary functionality of views.
15 class GlossaryTest extends ViewTestBase {
17 use AssertViewsCacheTagsTrait;
24 public static $modules = ['node'];
27 * Tests the default glossary view.
29 public function testGlossaryView() {
30 // Create a content type and add some nodes, with a non-random title.
31 $type = $this->drupalCreateContentType();
41 foreach ($nodes_per_char as $char => $count) {
43 'type' => $type->id(),
45 for ($i = 0; $i < $count; $i++) {
47 $node['title'] = $char . $this->randomString(3);
48 $node = $this->drupalCreateNode($node);
49 $nodes_by_char[$char][] = $node;
53 // Execute glossary view
54 $view = Views::getView('glossary');
55 $view->setDisplay('attachment_1');
56 $view->executeDisplay('attachment_1');
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);
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');
69 $this->assertPageCacheContextsAndTags(
73 'languages:' . LanguageInterface::TYPE_CONTENT,
74 'languages:' . LanguageInterface::TYPE_INTERFACE,
77 'user.node_grants:view',
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'.
87 // Link for letter 'p'.
89 // Link for letter 'r'.
91 // Link for letter 'l'.
93 // Link for letter 'u'.
100 // FinishResponseSubscriber adds this cache tag to responses that have
101 // the 'user.permissions' cache context for anonymous users.
102 'config:user.role.anonymous',
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.');