3 namespace Drupal\views\Tests;
5 @trigger_error('\Drupal\views\Tests\ViewTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\views\Functional\ViewTestBase', E_USER_DEPRECATED);
7 use Drupal\Core\Database\Query\SelectInterface;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\views\ViewExecutable;
12 * Defines a base class for Views testing in the full web test environment.
14 * Use this base test class if you need to emulate a full Drupal installation.
15 * When possible, ViewsKernelTestBase should be used instead. Both base classes
16 * include the same methods.
18 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
19 * Use \Drupal\Tests\views\Functional\ViewTestBase.
21 * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase
22 * @see \Drupal\simpletest\WebTestBase
24 abstract class ViewTestBase extends WebTestBase {
26 use ViewResultAssertionTrait;
33 public static $modules = ['views', 'views_test_config'];
35 protected function setUp($import_test_views = TRUE) {
37 if ($import_test_views) {
38 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
43 * Sets up the views_test_data.module.
45 * Because the schema of views_test_data.module is dependent on the test
46 * using it, it cannot be enabled normally.
48 protected function enableViewsTestModule() {
49 // Define the schema and views data variable before enabling the test module.
50 \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
51 \Drupal::state()->set('views_test_data_views_data', $this->viewsData());
53 \Drupal::service('module_installer')->install(['views_test_data']);
55 $this->rebuildContainer();
56 $this->container->get('module_handler')->reload();
58 // Load the test dataset.
59 $data_set = $this->dataSet();
60 $query = db_insert('views_test_data')
61 ->fields(array_keys($data_set[0]));
62 foreach ($data_set as $record) {
63 $query->values($record);
69 * Orders a nested array containing a result set based on a given column.
71 * @param array $result_set
72 * An array of rows from a result set, with each row as an associative
73 * array keyed by column name.
74 * @param string $column
75 * The column name by which to sort the result set.
76 * @param bool $reverse
77 * (optional) Boolean indicating whether to sort the result set in reverse
78 * order. Defaults to FALSE.
81 * The sorted result set.
83 protected function orderResultSet($result_set, $column, $reverse = FALSE) {
84 $order = $reverse ? -1 : 1;
85 usort($result_set, function ($a, $b) use ($column, $order) {
86 if ($a[$column] == $b[$column]) {
89 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
95 * Asserts the existence of a button with a certain ID and label.
98 * The HTML ID of the button
99 * @param string $expected_label
100 * The expected label for the button.
101 * @param string $message
102 * (optional) A custom message to display with the assertion. If no custom
103 * message is provided, the message will indicate the button label.
106 * TRUE if the assertion was successful, or FALSE on failure.
108 protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
109 return $this->assertFieldById($id, $expected_label, t($message, ['%label' => $expected_label]));
113 * Executes a view with debugging.
115 * @param \Drupal\views\ViewExecutable $view
118 * (optional) An array of the view arguments to use for the view.
120 protected function executeView(ViewExecutable $view, $args = []) {
121 // A view does not really work outside of a request scope, due to many
122 // dependencies like the current user.
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();