Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\views\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Behat\Mink\Exception\ElementNotFoundException;
|
Chris@18
|
6 use Drupal\Core\Database\Database;
|
Chris@0
|
7 use Drupal\Core\Database\Query\SelectInterface;
|
Chris@0
|
8 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
9 use Drupal\views\Tests\ViewResultAssertionTrait;
|
Chris@0
|
10 use Drupal\views\Tests\ViewTestData;
|
Chris@0
|
11 use Drupal\views\ViewExecutable;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Defines a base class for Views testing in the full web test environment.
|
Chris@0
|
15 *
|
Chris@0
|
16 * Use this base test class if you need to emulate a full Drupal installation.
|
Chris@0
|
17 * When possible, ViewsKernelTestBase should be used instead. Both base classes
|
Chris@0
|
18 * include the same methods.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase
|
Chris@0
|
21 * @see \Drupal\simpletest\WebTestBase
|
Chris@0
|
22 */
|
Chris@0
|
23 abstract class ViewTestBase extends BrowserTestBase {
|
Chris@0
|
24
|
Chris@0
|
25 use ViewResultAssertionTrait;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Modules to enable.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var array
|
Chris@0
|
31 */
|
Chris@0
|
32 public static $modules = ['views', 'views_test_config'];
|
Chris@0
|
33
|
Chris@0
|
34 protected function setUp($import_test_views = TRUE) {
|
Chris@0
|
35 parent::setUp();
|
Chris@0
|
36 if ($import_test_views) {
|
Chris@0
|
37 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
|
Chris@0
|
38 }
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Sets up the views_test_data.module.
|
Chris@0
|
43 *
|
Chris@0
|
44 * Because the schema of views_test_data.module is dependent on the test
|
Chris@0
|
45 * using it, it cannot be enabled normally.
|
Chris@0
|
46 */
|
Chris@0
|
47 protected function enableViewsTestModule() {
|
Chris@0
|
48 // Define the schema and views data variable before enabling the test module.
|
Chris@0
|
49 \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
|
Chris@0
|
50 \Drupal::state()->set('views_test_data_views_data', $this->viewsData());
|
Chris@0
|
51
|
Chris@0
|
52 \Drupal::service('module_installer')->install(['views_test_data']);
|
Chris@0
|
53 $this->resetAll();
|
Chris@0
|
54 $this->rebuildContainer();
|
Chris@0
|
55 $this->container->get('module_handler')->reload();
|
Chris@0
|
56
|
Chris@0
|
57 // Load the test dataset.
|
Chris@0
|
58 $data_set = $this->dataSet();
|
Chris@18
|
59 $query = Database::getConnection()->insert('views_test_data')
|
Chris@0
|
60 ->fields(array_keys($data_set[0]));
|
Chris@0
|
61 foreach ($data_set as $record) {
|
Chris@0
|
62 $query->values($record);
|
Chris@0
|
63 }
|
Chris@0
|
64 $query->execute();
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Orders a nested array containing a result set based on a given column.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @param array $result_set
|
Chris@0
|
71 * An array of rows from a result set, with each row as an associative
|
Chris@0
|
72 * array keyed by column name.
|
Chris@0
|
73 * @param string $column
|
Chris@0
|
74 * The column name by which to sort the result set.
|
Chris@0
|
75 * @param bool $reverse
|
Chris@0
|
76 * (optional) Boolean indicating whether to sort the result set in reverse
|
Chris@0
|
77 * order. Defaults to FALSE.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return array
|
Chris@0
|
80 * The sorted result set.
|
Chris@0
|
81 */
|
Chris@0
|
82 protected function orderResultSet($result_set, $column, $reverse = FALSE) {
|
Chris@0
|
83 $order = $reverse ? -1 : 1;
|
Chris@0
|
84 usort($result_set, function ($a, $b) use ($column, $order) {
|
Chris@0
|
85 if ($a[$column] == $b[$column]) {
|
Chris@0
|
86 return 0;
|
Chris@0
|
87 }
|
Chris@0
|
88 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
|
Chris@0
|
89 });
|
Chris@0
|
90 return $result_set;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Asserts the existence of a button with a certain ID and label.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @param string $id
|
Chris@0
|
97 * The HTML ID of the button
|
Chris@0
|
98 * @param string $expected_label
|
Chris@0
|
99 * The expected label for the button.
|
Chris@0
|
100 * @param string $message
|
Chris@0
|
101 * (optional) A custom message to display with the assertion. If no custom
|
Chris@0
|
102 * message is provided, the message will indicate the button label.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @throws \Behat\Mink\Exception\ElementNotFoundException
|
Chris@0
|
105 */
|
Chris@0
|
106 protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
|
Chris@0
|
107 $xpath = $this->assertSession()->buildXPathQuery('//button[@id=:value]|//input[@id=:value]', [':value' => $id]);
|
Chris@0
|
108 $field = $this->getSession()->getPage()->find('xpath', $xpath);
|
Chris@0
|
109
|
Chris@0
|
110 if (empty($field)) {
|
Chris@0
|
111 throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id', $field);
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 $this->assertEquals($expected_label, $field->getValue());
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Executes a view with debugging.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @param \Drupal\views\ViewExecutable $view
|
Chris@0
|
121 * The view object.
|
Chris@0
|
122 * @param array $args
|
Chris@0
|
123 * (optional) An array of the view arguments to use for the view.
|
Chris@0
|
124 */
|
Chris@0
|
125 protected function executeView(ViewExecutable $view, $args = []) {
|
Chris@0
|
126 // A view does not really work outside of a request scope, due to many
|
Chris@0
|
127 // dependencies like the current user.
|
Chris@0
|
128 $view->setDisplay();
|
Chris@0
|
129 $view->preExecute($args);
|
Chris@0
|
130 $view->execute();
|
Chris@0
|
131 $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>';
|
Chris@0
|
132 if ($view->build_info['query'] instanceof SelectInterface) {
|
Chris@0
|
133 $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
|
Chris@0
|
134 }
|
Chris@0
|
135 $this->verbose($verbose_message);
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * Returns the schema definition.
|
Chris@0
|
140 *
|
Chris@0
|
141 * @internal
|
Chris@0
|
142 */
|
Chris@0
|
143 protected function schemaDefinition() {
|
Chris@0
|
144 return ViewTestData::schemaDefinition();
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Returns the views data definition.
|
Chris@0
|
149 */
|
Chris@0
|
150 protected function viewsData() {
|
Chris@0
|
151 return ViewTestData::viewsData();
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 /**
|
Chris@0
|
155 * Returns a very simple test dataset.
|
Chris@0
|
156 */
|
Chris@0
|
157 protected function dataSet() {
|
Chris@0
|
158 return ViewTestData::dataSet();
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 }
|