be9920eb31db5909710adbc6879ad41eddf3b938
[yaffs-website] / web / core / modules / views / tests / src / Kernel / ViewsKernelTestBase.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Database\Query\SelectInterface;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\views\Tests\ViewResultAssertionTrait;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\views\ViewsData;
11
12 /**
13  * Defines a base class for Views kernel testing.
14  */
15 abstract class ViewsKernelTestBase extends KernelTestBase {
16
17   use ViewResultAssertionTrait;
18
19   /**
20    * Views to be enabled.
21    *
22    * Test classes should override this property and provide the list of testing
23    * views.
24    *
25    * @var array
26    */
27   public static $testViews = [];
28
29   /**
30    * {@inheritdoc}
31    */
32   public static $modules = ['system', 'views', 'views_test_config', 'views_test_data', 'user'];
33
34   /**
35    * {@inheritdoc}
36    *
37    * @param bool $import_test_views
38    *   Should the views specified on the test class be imported. If you need
39    *   to setup some additional stuff, like fields, you need to call false and
40    *   then call createTestViews for your own.
41    */
42   protected function setUp($import_test_views = TRUE) {
43     parent::setUp();
44
45     $this->installSchema('system', ['router', 'sequences', 'key_value_expire']);
46     $this->setUpFixtures();
47
48     if ($import_test_views) {
49       ViewTestData::createTestViews(get_class($this), ['views_test_config']);
50     }
51   }
52
53   /**
54    * Sets up the configuration and schema of views and views_test_data modules.
55    *
56    * Because the schema of views_test_data.module is dependent on the test
57    * using it, it cannot be enabled normally.
58    */
59   protected function setUpFixtures() {
60     // First install the system module. Many Views have Page displays have menu
61     // links, and for those to work, the system menus must already be present.
62     $this->installConfig(['system']);
63
64     /** @var \Drupal\Core\State\StateInterface $state */
65     $state = $this->container->get('state');
66     // Define the schema and views data variable before enabling the test module.
67     $state->set('views_test_data_schema', $this->schemaDefinition());
68     $state->set('views_test_data_views_data', $this->viewsData());
69     $this->container->get('views.views_data')->clear();
70
71     $this->installConfig(['views', 'views_test_config', 'views_test_data']);
72     foreach ($this->schemaDefinition() as $table => $schema) {
73       $this->installSchema('views_test_data', $table);
74     }
75
76     $this->container->get('router.builder')->rebuild();
77
78     // Load the test dataset.
79     $data_set = $this->dataSet();
80     $query = Database::getConnection()->insert('views_test_data')
81       ->fields(array_keys($data_set[0]));
82     foreach ($data_set as $record) {
83       $query->values($record);
84     }
85     $query->execute();
86   }
87
88   /**
89    * Orders a nested array containing a result set based on a given column.
90    *
91    * @param array $result_set
92    *   An array of rows from a result set, with each row as an associative
93    *   array keyed by column name.
94    * @param string $column
95    *   The column name by which to sort the result set.
96    * @param bool $reverse
97    *   (optional) Boolean indicating whether to sort the result set in reverse
98    *   order. Defaults to FALSE.
99    *
100    * @return array
101    *   The sorted result set.
102    */
103   protected function orderResultSet($result_set, $column, $reverse = FALSE) {
104     $order = $reverse ? -1 : 1;
105     usort($result_set, function ($a, $b) use ($column, $order) {
106       if ($a[$column] == $b[$column]) {
107         return 0;
108       }
109       return $order * (($a[$column] < $b[$column]) ? -1 : 1);
110     });
111     return $result_set;
112   }
113
114   /**
115    * Executes a view with debugging.
116    *
117    * @param \Drupal\views\ViewExecutable $view
118    *   The view object.
119    * @param array $args
120    *   (optional) An array of the view arguments to use for the view.
121    */
122   protected function executeView($view, array $args = []) {
123     $view->setDisplay();
124     $view->preExecute($args);
125     $view->execute();
126     $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>';
127     if ($view->build_info['query'] instanceof SelectInterface) {
128       $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
129     }
130     $this->verbose($verbose_message);
131   }
132
133   /**
134    * Returns the schema definition.
135    *
136    * @internal
137    */
138   protected function schemaDefinition() {
139     return ViewTestData::schemaDefinition();
140   }
141
142   /**
143    * Returns the views data definition.
144    */
145   protected function viewsData() {
146     return ViewTestData::viewsData();
147   }
148
149   /**
150    * Returns a very simple test dataset.
151    */
152   protected function dataSet() {
153     return ViewTestData::dataSet();
154   }
155
156 }