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