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;
10 use Drupal\views\ViewsData;
13 * Defines a base class for Views kernel testing.
15 abstract class ViewsKernelTestBase extends KernelTestBase {
17 use ViewResultAssertionTrait;
20 * Views to be enabled.
22 * Test classes should override this property and provide the list of testing
27 public static $testViews = [];
32 public static $modules = ['system', 'views', 'views_test_config', 'views_test_data', 'user'];
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.
42 protected function setUp($import_test_views = TRUE) {
45 $this->installSchema('system', ['router', 'sequences', 'key_value_expire']);
46 $this->setUpFixtures();
48 if ($import_test_views) {
49 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
54 * Sets up the configuration and schema of views and views_test_data modules.
56 * Because the schema of views_test_data.module is dependent on the test
57 * using it, it cannot be enabled normally.
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']);
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();
71 $this->installConfig(['views', 'views_test_config', 'views_test_data']);
72 foreach ($this->schemaDefinition() as $table => $schema) {
73 $this->installSchema('views_test_data', $table);
76 $this->container->get('router.builder')->rebuild();
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);
89 * Orders a nested array containing a result set based on a given column.
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.
101 * The sorted result set.
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]) {
109 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
115 * Executes a view with debugging.
117 * @param \Drupal\views\ViewExecutable $view
120 * (optional) An array of the view arguments to use for the view.
122 protected function executeView($view, array $args = []) {
124 $view->preExecute($args);
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>';
130 $this->verbose($verbose_message);
134 * Returns the schema definition.
138 protected function schemaDefinition() {
139 return ViewTestData::schemaDefinition();
143 * Returns the views data definition.
145 protected function viewsData() {
146 return ViewTestData::viewsData();
150 * Returns a very simple test dataset.
152 protected function dataSet() {
153 return ViewTestData::dataSet();