3 namespace Drupal\views\Tests;
4 @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);
6 use Drupal\Core\Database\Query\SelectInterface;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\views\ViewExecutable;
11 * Defines a base class for Views testing in the full web test environment.
13 * Use this base test class if you need to emulate a full Drupal installation.
14 * When possible, ViewsKernelTestBase should be used instead. Both base classes
15 * include the same methods.
17 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
18 * Use \Drupal\Tests\views\Functional\ViewTestBase.
20 * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase
21 * @see \Drupal\simpletest\WebTestBase
23 abstract class ViewTestBase extends WebTestBase {
25 use ViewResultAssertionTrait;
32 public static $modules = ['views', 'views_test_config'];
34 protected function setUp($import_test_views = TRUE) {
36 if ($import_test_views) {
37 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
42 * Sets up the views_test_data.module.
44 * Because the schema of views_test_data.module is dependent on the test
45 * using it, it cannot be enabled normally.
47 protected function enableViewsTestModule() {
48 // Define the schema and views data variable before enabling the test module.
49 \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
50 \Drupal::state()->set('views_test_data_views_data', $this->viewsData());
52 \Drupal::service('module_installer')->install(['views_test_data']);
54 $this->rebuildContainer();
55 $this->container->get('module_handler')->reload();
57 // Load the test dataset.
58 $data_set = $this->dataSet();
59 $query = db_insert('views_test_data')
60 ->fields(array_keys($data_set[0]));
61 foreach ($data_set as $record) {
62 $query->values($record);
68 * Orders a nested array containing a result set based on a given column.
70 * @param array $result_set
71 * An array of rows from a result set, with each row as an associative
72 * array keyed by column name.
73 * @param string $column
74 * The column name by which to sort the result set.
75 * @param bool $reverse
76 * (optional) Boolean indicating whether to sort the result set in reverse
77 * order. Defaults to FALSE.
80 * The sorted result set.
82 protected function orderResultSet($result_set, $column, $reverse = FALSE) {
83 $order = $reverse ? -1 : 1;
84 usort($result_set, function ($a, $b) use ($column, $order) {
85 if ($a[$column] == $b[$column]) {
88 return $order * (($a[$column] < $b[$column]) ? -1 : 1);
94 * Asserts the existence of a button with a certain ID and label.
97 * The HTML ID of the button
98 * @param string $label
99 * The expected label for the button.
100 * @param string $message
101 * (optional) A custom message to display with the assertion. If no custom
102 * message is provided, the message will indicate the button label.
105 * TRUE if the assertion was successful, or FALSE on failure.
107 protected function helperButtonHasLabel($id, $expected_label, $message = 'Label has the expected value: %label.') {
108 return $this->assertFieldById($id, $expected_label, t($message, ['%label' => $expected_label]));
112 * Executes a view with debugging.
114 * @param \Drupal\views\ViewExecutable $view
117 * (optional) An array of the view arguments to use for the view.
119 protected function executeView(ViewExecutable $view, $args = []) {
120 // A view does not really work outside of a request scope, due to many
121 // dependencies like the current user.
123 $view->preExecute($args);
125 $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>';
126 if ($view->build_info['query'] instanceof SelectInterface) {
127 $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
129 $this->verbose($verbose_message);
133 * Returns the schema definition.
135 protected function schemaDefinition() {
136 return ViewTestData::schemaDefinition();
140 * Returns the views data definition.
142 protected function viewsData() {
143 return ViewTestData::viewsData();
147 * Returns a very simple test dataset.
149 protected function dataSet() {
150 return ViewTestData::dataSet();