Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / src / Tests / ViewTestBase.php
1 <?php
2
3 namespace Drupal\views\Tests;
4
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);
6
7 use Drupal\Core\Database\Query\SelectInterface;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\views\ViewExecutable;
10
11 /**
12  * Defines a base class for Views testing in the full web test environment.
13  *
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.
17  *
18  * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
19  *   Use \Drupal\Tests\views\Functional\ViewTestBase.
20  *
21  * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase
22  * @see \Drupal\simpletest\WebTestBase
23  */
24 abstract class ViewTestBase extends WebTestBase {
25
26   use ViewResultAssertionTrait;
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = ['views', 'views_test_config'];
34
35   protected function setUp($import_test_views = TRUE) {
36     parent::setUp();
37     if ($import_test_views) {
38       ViewTestData::createTestViews(get_class($this), ['views_test_config']);
39     }
40   }
41
42   /**
43    * Sets up the views_test_data.module.
44    *
45    * Because the schema of views_test_data.module is dependent on the test
46    * using it, it cannot be enabled normally.
47    */
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());
52
53     \Drupal::service('module_installer')->install(['views_test_data']);
54     $this->resetAll();
55     $this->rebuildContainer();
56     $this->container->get('module_handler')->reload();
57
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);
64     }
65     $query->execute();
66   }
67
68   /**
69    * Orders a nested array containing a result set based on a given column.
70    *
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.
79    *
80    * @return array
81    *   The sorted result set.
82    */
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]) {
87         return 0;
88       }
89       return $order * (($a[$column] < $b[$column]) ? -1 : 1);
90     });
91     return $result_set;
92   }
93
94   /**
95    * Asserts the existence of a button with a certain ID and label.
96    *
97    * @param string $id
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.
104    *
105    * @return bool
106    *   TRUE if the assertion was successful, or FALSE on failure.
107    */
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]));
110   }
111
112   /**
113    * Executes a view with debugging.
114    *
115    * @param \Drupal\views\ViewExecutable $view
116    *   The view object.
117    * @param array $args
118    *   (optional) An array of the view arguments to use for the view.
119    */
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.
123     $view->setDisplay();
124     $view->preExecute($args);
125     $view->execute();
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>';
129     }
130     $this->verbose($verbose_message);
131   }
132
133   /**
134    * Returns the schema definition.
135    *
136    * @internal
137    */
138   protected function schemaDefinition() {
139     return ViewTestData::schemaDefinition();
140   }
141
142   /**
143    * Returns the views data definition.
144    */
145   protected function viewsData() {
146     return ViewTestData::viewsData();
147   }
148
149   /**
150    * Returns a very simple test dataset.
151    */
152   protected function dataSet() {
153     return ViewTestData::dataSet();
154   }
155
156 }