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