comparison core/modules/views/tests/src/Functional/GlossaryTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\views\Functional;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Url;
8 use Drupal\views\Tests\AssertViewsCacheTagsTrait;
9 use Drupal\views\Views;
10
11 /**
12 * Tests glossary functionality of views.
13 *
14 * @group views
15 */
16 class GlossaryTest extends ViewTestBase {
17
18 use AssertViewsCacheTagsTrait;
19
20 /**
21 * Modules to enable.
22 *
23 * @var array
24 */
25 public static $modules = ['node'];
26
27 /**
28 * Tests the default glossary view.
29 */
30 public function testGlossaryView() {
31 // Create a content type and add some nodes, with a non-random title.
32 $type = $this->drupalCreateContentType();
33 $nodes_per_char = [
34 'd' => 1,
35 'r' => 4,
36 'u' => 10,
37 'p' => 2,
38 'a' => 3,
39 'l' => 6,
40 ];
41 $nodes_by_char = [];
42 foreach ($nodes_per_char as $char => $count) {
43 $setting = [
44 'type' => $type->id()
45 ];
46 for ($i = 0; $i < $count; $i++) {
47 $node = $setting;
48 $node['title'] = $char . $this->randomString(3);
49 $node = $this->drupalCreateNode($node);
50 $nodes_by_char[$char][] = $node;
51 }
52 }
53
54 // Execute glossary view
55 $view = Views::getView('glossary');
56 $view->setDisplay('attachment_1');
57 $view->executeDisplay('attachment_1');
58
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);
62 }
63
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');
68
69 // Verify cache tags.
70 $this->assertPageCacheContextsAndTags(
71 $url,
72 [
73 'timezone',
74 'languages:' . LanguageInterface::TYPE_CONTENT,
75 'languages:' . LanguageInterface::TYPE_INTERFACE,
76 'theme',
77 'url',
78 'user.node_grants:view',
79 'user.permissions',
80 'route',
81 ],
82 [
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'.
87 'node:1',
88 // Link for letter 'p'.
89 'node:16',
90 // Link for letter 'r'.
91 'node:2',
92 // Link for letter 'l'.
93 'node:21',
94 // Link for letter 'u'.
95 'node:6',
96 'node_list',
97 'user:0',
98 'user_list',
99 'http_response',
100 'rendered',
101 // FinishResponseSubscriber adds this cache tag to responses that have
102 // the 'user.permissions' cache context for anonymous users.
103 'config:user.role.anonymous',
104 ]
105 );
106
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.');
121 }
122 }
123
124 }