3 namespace Drupal\Tests\views\Kernel;
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;
12 * Defines a base class for Views kernel testing.
14 abstract class ViewsKernelTestBase extends KernelTestBase {
16 use ViewResultAssertionTrait;
19 * Views to be enabled.
21 * Test classes should override this property and provide the list of testing
26 public static $testViews = [];
31 public static $modules = ['system', 'views', 'views_test_config', 'views_test_data', 'user'];
36 * @param bool $import_test_views
37 * Should the views specified on the test class be imported. If you need
38 * to setup some additional stuff, like fields, you need to call false and
39 * then call createTestViews for your own.
41 protected function setUp($import_test_views = TRUE) {
44 $this->installSchema('system', ['router', 'sequences', 'key_value_expire']);
45 $this->setUpFixtures();
47 if ($import_test_views) {
48 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
52 * Sets up the configuration and schema of views and views_test_data modules.
54 * Because the schema of views_test_data.module is dependent on the test
55 * using it, it cannot be enabled normally.
57 protected function setUpFixtures() {
58 // First install the system module. Many Views have Page displays have menu
59 // links, and for those to work, the system menus must already be present.
60 $this->installConfig(['system']);
62 /** @var \Drupal\Core\State\StateInterface $state */
63 $state = $this->container->get('state');
64 // Define the schema and views data variable before enabling the test module.
65 $state->set('views_test_data_schema', $this->schemaDefinition());
66 $state->set('views_test_data_views_data', $this->viewsData());
68 $this->installConfig(['views', 'views_test_config', 'views_test_data']);
69 foreach ($this->schemaDefinition() as $table => $schema) {
70 $this->installSchema('views_test_data', $table);
73 $this->container->get('router.builder')->rebuild();
75 // Load the test dataset.
76 $data_set = $this->dataSet();
77 $query = Database::getConnection()->insert('views_test_data')
78 ->fields(array_keys($data_set[0]));
79 foreach ($data_set as $record) {
80 $query->values($record);
86 * Orders a nested array containing a result set based on a given column.
88 * @param array $result_set
89 * An array of rows from a result set, with each row as an associative
90 * array keyed by column name.
91 * @param string $column
92 * The column name by which to sort the result set.
93 * @param bool $reverse
94 * (optional) Boolean indicating whether to sort the result set in reverse
95 * order. Defaults to FALSE.
98 * The sorted result set.
100 protected function orderResultSet($result_set, $column, $reverse = FALSE) {
101 $order = $reverse ? -1 : 1;
102 usort($result_set, function ($a, $b) use ($column, $order) {
103 if ($a[$column] == $b[$column]) {
106 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
112 * Executes a view with debugging.
114 * @param \Drupal\views\ViewExecutable $view
117 * (optional) An array of the view arguments to use for the view.
119 protected function executeView($view, array $args = []) {
121 $view->preExecute($args);
123 $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>';
124 if ($view->build_info['query'] instanceof SelectInterface) {
125 $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
127 $this->verbose($verbose_message);
131 * Returns the schema definition.
135 protected function schemaDefinition() {
136 return ViewTestData::schemaDefinition();
140 * Returns the views data definition.
142 protected function viewsData() {
143 return ViewTestData::viewsData();
147 * Returns a very simple test dataset.
149 protected function dataSet() {
150 return ViewTestData::dataSet();