3 namespace Drupal\Tests\views\Functional;
5 use Behat\Mink\Exception\ElementNotFoundException;
6 use Drupal\Core\Database\Query\SelectInterface;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\views\Tests\ViewResultAssertionTrait;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\views\ViewExecutable;
13 * Defines a base class for Views testing in the full web test environment.
15 * Use this base test class if you need to emulate a full Drupal installation.
16 * When possible, ViewsKernelTestBase should be used instead. Both base classes
17 * include the same methods.
19 * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase
20 * @see \Drupal\simpletest\WebTestBase
22 abstract class ViewTestBase extends BrowserTestBase {
24 use ViewResultAssertionTrait;
31 public static $modules = ['views', 'views_test_config'];
33 protected function setUp($import_test_views = TRUE) {
35 if ($import_test_views) {
36 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
41 * Sets up the views_test_data.module.
43 * Because the schema of views_test_data.module is dependent on the test
44 * using it, it cannot be enabled normally.
46 protected function enableViewsTestModule() {
47 // Define the schema and views data variable before enabling the test module.
48 \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
49 \Drupal::state()->set('views_test_data_views_data', $this->viewsData());
51 \Drupal::service('module_installer')->install(['views_test_data']);
53 $this->rebuildContainer();
54 $this->container->get('module_handler')->reload();
56 // Load the test dataset.
57 $data_set = $this->dataSet();
58 $query = db_insert('views_test_data')
59 ->fields(array_keys($data_set[0]));
60 foreach ($data_set as $record) {
61 $query->values($record);
67 * Orders a nested array containing a result set based on a given column.
69 * @param array $result_set
70 * An array of rows from a result set, with each row as an associative
71 * array keyed by column name.
72 * @param string $column
73 * The column name by which to sort the result set.
74 * @param bool $reverse
75 * (optional) Boolean indicating whether to sort the result set in reverse
76 * order. Defaults to FALSE.
79 * The sorted result set.
81 protected function orderResultSet($result_set, $column, $reverse = FALSE) {
82 $order = $reverse ? -1 : 1;
83 usort($result_set, function ($a, $b) use ($column, $order) {
84 if ($a[$column] == $b[$column]) {
87 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
93 * Asserts the existence of a button with a certain ID and label.
96 * The HTML ID of the button
97 * @param string $expected_label
98 * The expected label for the button.
99 * @param string $message
100 * (optional) A custom message to display with the assertion. If no custom
101 * message is provided, the message will indicate the button label.
103 * @throws \Behat\Mink\Exception\ElementNotFoundException
105 protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
106 $xpath = $this->assertSession()->buildXPathQuery('//button[@id=:value]|//input[@id=:value]', [':value' => $id]);
107 $field = $this->getSession()->getPage()->find('xpath', $xpath);
110 throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id', $field);
113 $this->assertEquals($expected_label, $field->getValue());
117 * Executes a view with debugging.
119 * @param \Drupal\views\ViewExecutable $view
122 * (optional) An array of the view arguments to use for the view.
124 protected function executeView(ViewExecutable $view, $args = []) {
125 // A view does not really work outside of a request scope, due to many
126 // dependencies like the current user.
128 $view->preExecute($args);
130 $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>';
131 if ($view->build_info['query'] instanceof SelectInterface) {
132 $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
134 $this->verbose($verbose_message);
138 * Returns the schema definition.
142 protected function schemaDefinition() {
143 return ViewTestData::schemaDefinition();
147 * Returns the views data definition.
149 protected function viewsData() {
150 return ViewTestData::viewsData();
154 * Returns a very simple test dataset.
156 protected function dataSet() {
157 return ViewTestData::dataSet();