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