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