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