1. 8.3.x core/modules/views/tests/src/Functional/DefaultViewsTest.php
  2. 8.3.x core/modules/views_ui/tests/src/Functional/DefaultViewsTest.php
  3. 8.0.x core/modules/views_ui/src/Tests/DefaultViewsTest.php
  4. 8.0.x core/modules/views/src/Tests/DefaultViewsTest.php
  5. 8.1.x core/modules/views_ui/src/Tests/DefaultViewsTest.php
  6. 8.1.x core/modules/views/src/Tests/DefaultViewsTest.php
  7. 8.2.x core/modules/views/src/Tests/DefaultViewsTest.php
  8. 8.2.x core/modules/views_ui/src/Tests/DefaultViewsTest.php
  9. 8.4.x core/modules/views/tests/src/Functional/DefaultViewsTest.php
  10. 8.4.x core/modules/views_ui/tests/src/Functional/DefaultViewsTest.php

Namespace

Drupal\views\Tests

File

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

Classes

Namesort descending Description
DefaultViewsTest Tests the default views provided by views.