3 namespace Drupal\Tests\views\Functional;
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\Language\LanguageInterface;
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;
17 * Tests the default views provided by views.
21 class DefaultViewsTest extends ViewTestBase {
24 use EntityReferenceTestTrait;
31 public static $modules = ['views', 'node', 'search', 'comment', 'taxonomy', 'block', 'user'];
34 * An array of argument arrays to use for default views.
38 protected $viewArgMap = [
40 'taxonomy_term' => [1],
41 'glossary' => ['all'],
44 protected function setUp($import_test_views = TRUE) {
45 parent::setUp($import_test_views);
47 $this->drupalPlaceBlock('page_title_block');
49 // Create Basic page node type.
50 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
52 $vocabulary = Vocabulary::create([
53 'name' => $this->randomMachineName(),
54 'description' => $this->randomMachineName(),
55 'vid' => mb_strtolower($this->randomMachineName()),
56 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
58 'nodes' => ['page' => 'page'],
59 'weight' => mt_rand(0, 10),
64 $field_name = mb_strtolower($this->randomMachineName());
68 $vocabulary->id() => $vocabulary->id(),
70 'auto_create' => TRUE,
72 $this->createEntityReferenceField('node', 'page', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
74 // Create a time in the past for the archive.
75 $time = REQUEST_TIME - 3600;
77 $this->addDefaultCommentField('node', 'page');
79 for ($i = 0; $i <= 10; $i++) {
80 $user = $this->drupalCreateUser();
81 $term = $this->createTerm($vocabulary);
83 $values = ['created' => $time, 'type' => 'page'];
84 $values[$field_name][]['target_id'] = $term->id();
86 // Make every other node promoted.
88 $values['promote'] = TRUE;
90 $values['body'][]['value'] = \Drupal::l('Node ' . 1, new Url('entity.node.canonical', ['node' => 1]));
92 $node = $this->drupalCreateNode($values);
96 'status' => CommentInterface::PUBLISHED,
97 'entity_id' => $node->id(),
98 'entity_type' => 'node',
99 'field_name' => 'comment',
101 Comment::create($comment)->save();
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',
110 Comment::create($unpublished_comment)->save();
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);
120 * Test that all Default views work as expected.
122 public function testDefaultViews() {
123 // Get all default views.
124 $controller = $this->container->get('entity.manager')->getStorage('view');
125 $views = $controller->loadMultiple();
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);
133 // Add any args if needed.
134 if (array_key_exists($name, $this->viewArgMap)) {
135 $view->preExecute($this->viewArgMap[$name]);
138 $this->assert(TRUE, format_string('View @view will be executed.', ['@view' => $view->storage->id()]));
141 $tokens = ['@name' => $name, '@display_id' => $display_id];
142 $this->assertTrue($view->executed, format_string('@name:@display_id has been executed.', $tokens));
144 $count = count($view->result);
145 $this->assertTrue($count > 0, format_string('@count results returned', ['@count' => $count]));
152 * Returns a new term with random properties in vocabulary $vid.
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,
170 * Tests the archive view.
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.
176 // Sun, 19 Nov 1978 05:00:00 GMT.
177 'created' => 280299600,
179 $this->drupalCreateNode($node);
180 $this->drupalCreateNode($node);
182 // Tue, 19 Dec 1978 05:00:00 GMT.
183 'created' => 282891600,
185 $this->drupalCreateNode($node);
187 // Fri, 19 Jan 1979 05:00:00 GMT.
188 'created' => 285570000,
190 $this->drupalCreateNode($node);
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);
202 'created_year_month' => $created_year_month,
207 'created_year_month' => 197901,
212 'created_year_month' => 197812,
217 'created_year_month' => 197811,
221 $this->assertIdenticalResultset($view, $expected_result, $column_map);
223 $view->storage->setStatus(TRUE);
225 \Drupal::service('router.builder')->rebuild();
227 $this->drupalGet('archive');
228 $this->assertResponse(200);