comparison core/modules/views/tests/src/Kernel/ViewsKernelTestBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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
11 /**
12 * Defines a base class for Views kernel testing.
13 */
14 abstract class ViewsKernelTestBase extends KernelTestBase {
15
16 use ViewResultAssertionTrait;
17
18 /**
19 * Views to be enabled.
20 *
21 * Test classes should override this property and provide the list of testing
22 * views.
23 *
24 * @var array
25 */
26 public static $testViews = [];
27
28 /**
29 * {@inheritdoc}
30 */
31 public static $modules = ['system', 'views', 'views_test_config', 'views_test_data', 'user'];
32
33 /**
34 * {@inheritdoc}
35 *
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.
40 */
41 protected function setUp($import_test_views = TRUE) {
42 parent::setUp();
43
44 $this->installSchema('system', ['router', 'sequences', 'key_value_expire']);
45 $this->setUpFixtures();
46
47 if ($import_test_views) {
48 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
49 }
50 }
51 /**
52 * Sets up the configuration and schema of views and views_test_data modules.
53 *
54 * Because the schema of views_test_data.module is dependent on the test
55 * using it, it cannot be enabled normally.
56 */
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']);
61
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());
67
68 $this->installConfig(['views', 'views_test_config', 'views_test_data']);
69 foreach ($this->schemaDefinition() as $table => $schema) {
70 $this->installSchema('views_test_data', $table);
71 }
72
73 $this->container->get('router.builder')->rebuild();
74
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);
81 }
82 $query->execute();
83 }
84
85 /**
86 * Orders a nested array containing a result set based on a given column.
87 *
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.
96 *
97 * @return array
98 * The sorted result set.
99 */
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]) {
104 return 0;
105 }
106 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
107 });
108 return $result_set;
109 }
110
111 /**
112 * Executes a view with debugging.
113 *
114 * @param \Drupal\views\ViewExecutable $view
115 * The view object.
116 * @param array $args
117 * (optional) An array of the view arguments to use for the view.
118 */
119 protected function executeView($view, array $args = []) {
120 $view->setDisplay();
121 $view->preExecute($args);
122 $view->execute();
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>';
126 }
127 $this->verbose($verbose_message);
128 }
129
130 /**
131 * Returns the schema definition.
132 *
133 * @internal
134 */
135 protected function schemaDefinition() {
136 return ViewTestData::schemaDefinition();
137 }
138
139 /**
140 * Returns the views data definition.
141 */
142 protected function viewsData() {
143 return ViewTestData::viewsData();
144 }
145
146 /**
147 * Returns a very simple test dataset.
148 */
149 protected function dataSet() {
150 return ViewTestData::dataSet();
151 }
152
153 }