3 namespace Drupal\views\Tests;
5 use Drupal\Core\Database\Query\SelectInterface;
6 use Drupal\simpletest\KernelTestBase;
9 * Defines a base class for Views unit testing.
11 * Use this test class for unit tests of Views functionality. If a test
12 * requires the full web test environment provided by WebTestBase, extend
13 * ViewTestBase instead.
15 * @deprecated in Drupal 8.0.x, will be removed in Drupal 8.2.x. Use
16 * \Drupal\Tests\views\Kernel\ViewsKernelTestBase instead.
18 * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase
20 abstract class ViewKernelTestBase extends KernelTestBase {
22 use ViewResultAssertionTrait;
29 public static $modules = ['system', 'views', 'views_test_config', 'views_test_data', 'user'];
34 * @param bool $import_test_views
35 * Should the views specififed on the test class be imported. If you need
36 * to setup some additional stuff, like fields, you need to call false and
37 * then call createTestViews for your own.
39 protected function setUp($import_test_views = TRUE) {
42 $this->installSchema('system', ['sequences']);
43 $this->setUpFixtures();
45 if ($import_test_views) {
46 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
51 * Sets up the configuration and schema of views and views_test_data modules.
53 * Because the schema of views_test_data.module is dependent on the test
54 * using it, it cannot be enabled normally.
56 protected function setUpFixtures() {
57 // First install the system module. Many Views have Page displays have menu
58 // links, and for those to work, the system menus must already be present.
59 $this->installConfig(['system']);
61 // Define the schema and views data variable before enabling the test module.
62 \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
63 \Drupal::state()->set('views_test_data_views_data', $this->viewsData());
65 $this->installConfig(['views', 'views_test_config', 'views_test_data']);
66 foreach ($this->schemaDefinition() as $table => $schema) {
67 $this->installSchema('views_test_data', $table);
70 \Drupal::service('router.builder')->rebuild();
72 // Load the test dataset.
73 $data_set = $this->dataSet();
74 $query = db_insert('views_test_data')
75 ->fields(array_keys($data_set[0]));
76 foreach ($data_set as $record) {
77 $query->values($record);
83 * Orders a nested array containing a result set based on a given column.
85 * @param array $result_set
86 * An array of rows from a result set, with each row as an associative
87 * array keyed by column name.
88 * @param string $column
89 * The column name by which to sort the result set.
90 * @param bool $reverse
91 * (optional) Boolean indicating whether to sort the result set in reverse
92 * order. Defaults to FALSE.
95 * The sorted result set.
97 protected function orderResultSet($result_set, $column, $reverse = FALSE) {
98 $order = $reverse ? -1 : 1;
99 usort($result_set, function ($a, $b) use ($column, $order) {
100 if ($a[$column] == $b[$column]) {
103 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
109 * Executes a view with debugging.
111 * @param \Drupal\views\ViewExecutable $view
114 * (optional) An array of the view arguments to use for the view.
116 protected function executeView($view, array $args = []) {
118 $view->preExecute($args);
120 $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>';
121 if ($view->build_info['query'] instanceof SelectInterface) {
122 $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
124 $this->verbose($verbose_message);
128 * Returns the schema definition.
130 protected function schemaDefinition() {
131 return ViewTestData::schemaDefinition();
135 * Returns the views data definition.
137 protected function viewsData() {
138 return ViewTestData::viewsData();
142 * Returns a very simple test dataset.
144 protected function dataSet() {
145 return ViewTestData::dataSet();