3 namespace Drupal\Tests\views\Functional;
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;
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;
18 * Tests the default views provided by views.
22 class DefaultViewsTest extends ViewTestBase {
25 use EntityReferenceTestTrait;
32 public static $modules = ['views', 'node', 'search', 'comment', 'taxonomy', 'block', 'user'];
35 * An array of argument arrays to use for default views.
39 protected $viewArgMap = [
41 'taxonomy_term' => [1],
42 'glossary' => ['all'],
45 protected function setUp($import_test_views = TRUE) {
46 parent::setUp($import_test_views);
48 $this->drupalPlaceBlock('page_title_block');
50 // Create Basic page node type.
51 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
53 $vocabulary = Vocabulary::create([
54 'name' => $this->randomMachineName(),
55 'description' => $this->randomMachineName(),
56 'vid' => Unicode::strtolower($this->randomMachineName()),
57 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
59 'nodes' => ['page' => 'page'],
60 'weight' => mt_rand(0, 10),
65 $field_name = Unicode::strtolower($this->randomMachineName());
69 $vocabulary->id() => $vocabulary->id(),
71 'auto_create' => TRUE,
73 $this->createEntityReferenceField('node', 'page', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
75 // Create a time in the past for the archive.
76 $time = REQUEST_TIME - 3600;
78 $this->addDefaultCommentField('node', 'page');
80 for ($i = 0; $i <= 10; $i++) {
81 $user = $this->drupalCreateUser();
82 $term = $this->createTerm($vocabulary);
84 $values = ['created' => $time, 'type' => 'page'];
85 $values[$field_name][]['target_id'] = $term->id();
87 // Make every other node promoted.
89 $values['promote'] = TRUE;
91 $values['body'][]['value'] = \Drupal::l('Node ' . 1, new Url('entity.node.canonical', ['node' => 1]));
93 $node = $this->drupalCreateNode($values);
97 'status' => CommentInterface::PUBLISHED,
98 'entity_id' => $node->id(),
99 'entity_type' => 'node',
100 'field_name' => 'comment'
102 Comment::create($comment)->save();
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',
111 Comment::create($unpublished_comment)->save();
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);
121 * Test that all Default views work as expected.
123 public function testDefaultViews() {
124 // Get all default views.
125 $controller = $this->container->get('entity.manager')->getStorage('view');
126 $views = $controller->loadMultiple();
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);
134 // Add any args if needed.
135 if (array_key_exists($name, $this->viewArgMap)) {
136 $view->preExecute($this->viewArgMap[$name]);
139 $this->assert(TRUE, format_string('View @view will be executed.', ['@view' => $view->storage->id()]));
142 $tokens = ['@name' => $name, '@display_id' => $display_id];
143 $this->assertTrue($view->executed, format_string('@name:@display_id has been executed.', $tokens));
145 $count = count($view->result);
146 $this->assertTrue($count > 0, format_string('@count results returned', ['@count' => $count]));
153 * Returns a new term with random properties in vocabulary $vid.
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,
171 * Tests the archive view.
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.
177 // Sun, 19 Nov 1978 05:00:00 GMT.
178 'created' => 280299600,
180 $this->drupalCreateNode($node);
181 $this->drupalCreateNode($node);
183 // Tue, 19 Dec 1978 05:00:00 GMT.
184 'created' => 282891600,
186 $this->drupalCreateNode($node);
188 // Fri, 19 Jan 1979 05:00:00 GMT.
189 'created' => 285570000,
191 $this->drupalCreateNode($node);
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);
203 'created_year_month' => $created_year_month,
208 'created_year_month' => 197901,
213 'created_year_month' => 197812,
218 'created_year_month' => 197811,
222 $this->assertIdenticalResultset($view, $expected_result, $column_map);
224 $view->storage->setStatus(TRUE);
226 \Drupal::service('router.builder')->rebuild();
228 $this->drupalGet('archive');
229 $this->assertResponse(200);