Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\views\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\CommentInterface;
|
Chris@0
|
6 use Drupal\comment\Tests\CommentTestTrait;
|
Chris@0
|
7 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@0
|
9 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
10 use Drupal\Core\Url;
|
Chris@0
|
11 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
|
Chris@0
|
12 use Drupal\views\Views;
|
Chris@0
|
13 use Drupal\comment\Entity\Comment;
|
Chris@0
|
14 use Drupal\taxonomy\Entity\Vocabulary;
|
Chris@0
|
15 use Drupal\taxonomy\Entity\Term;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Tests the default views provided by views.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @group views
|
Chris@0
|
21 */
|
Chris@0
|
22 class DefaultViewsTest extends ViewTestBase {
|
Chris@0
|
23
|
Chris@0
|
24 use CommentTestTrait;
|
Chris@0
|
25 use EntityReferenceTestTrait;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Modules to enable.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var array
|
Chris@0
|
31 */
|
Chris@0
|
32 public static $modules = ['views', 'node', 'search', 'comment', 'taxonomy', 'block', 'user'];
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * An array of argument arrays to use for default views.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var array
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $viewArgMap = [
|
Chris@0
|
40 'backlinks' => [1],
|
Chris@0
|
41 'taxonomy_term' => [1],
|
Chris@0
|
42 'glossary' => ['all'],
|
Chris@0
|
43 ];
|
Chris@0
|
44
|
Chris@0
|
45 protected function setUp($import_test_views = TRUE) {
|
Chris@0
|
46 parent::setUp($import_test_views);
|
Chris@0
|
47
|
Chris@0
|
48 $this->drupalPlaceBlock('page_title_block');
|
Chris@0
|
49
|
Chris@0
|
50 // Create Basic page node type.
|
Chris@0
|
51 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
|
Chris@0
|
52
|
Chris@0
|
53 $vocabulary = Vocabulary::create([
|
Chris@0
|
54 'name' => $this->randomMachineName(),
|
Chris@0
|
55 'description' => $this->randomMachineName(),
|
Chris@0
|
56 'vid' => Unicode::strtolower($this->randomMachineName()),
|
Chris@0
|
57 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
Chris@0
|
58 'help' => '',
|
Chris@0
|
59 'nodes' => ['page' => 'page'],
|
Chris@0
|
60 'weight' => mt_rand(0, 10),
|
Chris@0
|
61 ]);
|
Chris@0
|
62 $vocabulary->save();
|
Chris@0
|
63
|
Chris@0
|
64 // Create a field.
|
Chris@0
|
65 $field_name = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
66
|
Chris@0
|
67 $handler_settings = [
|
Chris@0
|
68 'target_bundles' => [
|
Chris@0
|
69 $vocabulary->id() => $vocabulary->id(),
|
Chris@0
|
70 ],
|
Chris@0
|
71 'auto_create' => TRUE,
|
Chris@0
|
72 ];
|
Chris@0
|
73 $this->createEntityReferenceField('node', 'page', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
Chris@0
|
74
|
Chris@0
|
75 // Create a time in the past for the archive.
|
Chris@0
|
76 $time = REQUEST_TIME - 3600;
|
Chris@0
|
77
|
Chris@0
|
78 $this->addDefaultCommentField('node', 'page');
|
Chris@0
|
79
|
Chris@0
|
80 for ($i = 0; $i <= 10; $i++) {
|
Chris@0
|
81 $user = $this->drupalCreateUser();
|
Chris@0
|
82 $term = $this->createTerm($vocabulary);
|
Chris@0
|
83
|
Chris@0
|
84 $values = ['created' => $time, 'type' => 'page'];
|
Chris@0
|
85 $values[$field_name][]['target_id'] = $term->id();
|
Chris@0
|
86
|
Chris@0
|
87 // Make every other node promoted.
|
Chris@0
|
88 if ($i % 2) {
|
Chris@0
|
89 $values['promote'] = TRUE;
|
Chris@0
|
90 }
|
Chris@0
|
91 $values['body'][]['value'] = \Drupal::l('Node ' . 1, new Url('entity.node.canonical', ['node' => 1]));
|
Chris@0
|
92
|
Chris@0
|
93 $node = $this->drupalCreateNode($values);
|
Chris@0
|
94
|
Chris@0
|
95 $comment = [
|
Chris@0
|
96 'uid' => $user->id(),
|
Chris@0
|
97 'status' => CommentInterface::PUBLISHED,
|
Chris@0
|
98 'entity_id' => $node->id(),
|
Chris@0
|
99 'entity_type' => 'node',
|
Chris@0
|
100 'field_name' => 'comment'
|
Chris@0
|
101 ];
|
Chris@0
|
102 Comment::create($comment)->save();
|
Chris@0
|
103
|
Chris@0
|
104 $unpublished_comment = [
|
Chris@0
|
105 'uid' => $user->id(),
|
Chris@0
|
106 'status' => CommentInterface::NOT_PUBLISHED,
|
Chris@0
|
107 'entity_id' => $node->id(),
|
Chris@0
|
108 'entity_type' => 'node',
|
Chris@0
|
109 'field_name' => 'comment',
|
Chris@0
|
110 ];
|
Chris@0
|
111 Comment::create($unpublished_comment)->save();
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 // Some views, such as the "Who's Online" view, only return results if at
|
Chris@0
|
115 // least one user is logged in.
|
Chris@0
|
116 $account = $this->drupalCreateUser([]);
|
Chris@0
|
117 $this->drupalLogin($account);
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * Test that all Default views work as expected.
|
Chris@0
|
122 */
|
Chris@0
|
123 public function testDefaultViews() {
|
Chris@0
|
124 // Get all default views.
|
Chris@0
|
125 $controller = $this->container->get('entity.manager')->getStorage('view');
|
Chris@0
|
126 $views = $controller->loadMultiple();
|
Chris@0
|
127
|
Chris@0
|
128 foreach ($views as $name => $view_storage) {
|
Chris@0
|
129 $view = $view_storage->getExecutable();
|
Chris@0
|
130 $view->initDisplay();
|
Chris@0
|
131 foreach ($view->storage->get('display') as $display_id => $display) {
|
Chris@0
|
132 $view->setDisplay($display_id);
|
Chris@0
|
133
|
Chris@0
|
134 // Add any args if needed.
|
Chris@0
|
135 if (array_key_exists($name, $this->viewArgMap)) {
|
Chris@0
|
136 $view->preExecute($this->viewArgMap[$name]);
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 $this->assert(TRUE, format_string('View @view will be executed.', ['@view' => $view->storage->id()]));
|
Chris@0
|
140 $view->execute();
|
Chris@0
|
141
|
Chris@0
|
142 $tokens = ['@name' => $name, '@display_id' => $display_id];
|
Chris@0
|
143 $this->assertTrue($view->executed, format_string('@name:@display_id has been executed.', $tokens));
|
Chris@0
|
144
|
Chris@0
|
145 $count = count($view->result);
|
Chris@0
|
146 $this->assertTrue($count > 0, format_string('@count results returned', ['@count' => $count]));
|
Chris@0
|
147 $view->destroy();
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * Returns a new term with random properties in vocabulary $vid.
|
Chris@0
|
154 */
|
Chris@0
|
155 public function createTerm($vocabulary) {
|
Chris@0
|
156 $filter_formats = filter_formats();
|
Chris@0
|
157 $format = array_pop($filter_formats);
|
Chris@0
|
158 $term = Term::create([
|
Chris@0
|
159 'name' => $this->randomMachineName(),
|
Chris@0
|
160 'description' => $this->randomMachineName(),
|
Chris@0
|
161 // Use the first available text format.
|
Chris@0
|
162 'format' => $format->id(),
|
Chris@0
|
163 'vid' => $vocabulary->id(),
|
Chris@0
|
164 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
Chris@0
|
165 ]);
|
Chris@0
|
166 $term->save();
|
Chris@0
|
167 return $term;
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * Tests the archive view.
|
Chris@0
|
172 */
|
Chris@0
|
173 public function testArchiveView() {
|
Chris@0
|
174 // Create additional nodes compared to the one in the setup method.
|
Chris@0
|
175 // Create two nodes in the same month, and one in each following month.
|
Chris@0
|
176 $node = [
|
Chris@0
|
177 // Sun, 19 Nov 1978 05:00:00 GMT.
|
Chris@0
|
178 'created' => 280299600,
|
Chris@0
|
179 ];
|
Chris@0
|
180 $this->drupalCreateNode($node);
|
Chris@0
|
181 $this->drupalCreateNode($node);
|
Chris@0
|
182 $node = [
|
Chris@0
|
183 // Tue, 19 Dec 1978 05:00:00 GMT.
|
Chris@0
|
184 'created' => 282891600,
|
Chris@0
|
185 ];
|
Chris@0
|
186 $this->drupalCreateNode($node);
|
Chris@0
|
187 $node = [
|
Chris@0
|
188 // Fri, 19 Jan 1979 05:00:00 GMT.
|
Chris@0
|
189 'created' => 285570000,
|
Chris@0
|
190 ];
|
Chris@0
|
191 $this->drupalCreateNode($node);
|
Chris@0
|
192
|
Chris@0
|
193 $view = Views::getView('archive');
|
Chris@0
|
194 $view->setDisplay('page_1');
|
Chris@0
|
195 $this->executeView($view);
|
Chris@0
|
196 $columns = ['nid', 'created_year_month', 'num_records'];
|
Chris@0
|
197 $column_map = array_combine($columns, $columns);
|
Chris@0
|
198 // Create time of additional nodes created in the setup method.
|
Chris@0
|
199 $created_year_month = date('Ym', REQUEST_TIME - 3600);
|
Chris@0
|
200 $expected_result = [
|
Chris@0
|
201 [
|
Chris@0
|
202 'nid' => 1,
|
Chris@0
|
203 'created_year_month' => $created_year_month,
|
Chris@0
|
204 'num_records' => 11,
|
Chris@0
|
205 ],
|
Chris@0
|
206 [
|
Chris@0
|
207 'nid' => 15,
|
Chris@0
|
208 'created_year_month' => 197901,
|
Chris@0
|
209 'num_records' => 1,
|
Chris@0
|
210 ],
|
Chris@0
|
211 [
|
Chris@0
|
212 'nid' => 14,
|
Chris@0
|
213 'created_year_month' => 197812,
|
Chris@0
|
214 'num_records' => 1,
|
Chris@0
|
215 ],
|
Chris@0
|
216 [
|
Chris@0
|
217 'nid' => 12,
|
Chris@0
|
218 'created_year_month' => 197811,
|
Chris@0
|
219 'num_records' => 2,
|
Chris@0
|
220 ],
|
Chris@0
|
221 ];
|
Chris@0
|
222 $this->assertIdenticalResultset($view, $expected_result, $column_map);
|
Chris@0
|
223
|
Chris@0
|
224 $view->storage->setStatus(TRUE);
|
Chris@0
|
225 $view->save();
|
Chris@0
|
226 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
227
|
Chris@0
|
228 $this->drupalGet('archive');
|
Chris@0
|
229 $this->assertResponse(200);
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 }
|