Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / tests / src / Functional / ViewTestBase.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional;
4
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;
11
12 /**
13  * Defines a base class for Views testing in the full web test environment.
14  *
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.
18  *
19  * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase
20  * @see \Drupal\simpletest\WebTestBase
21  */
22 abstract class ViewTestBase extends BrowserTestBase {
23
24   use ViewResultAssertionTrait;
25
26   /**
27    * Modules to enable.
28    *
29    * @var array
30    */
31   public static $modules = ['views', 'views_test_config'];
32
33   protected function setUp($import_test_views = TRUE) {
34     parent::setUp();
35     if ($import_test_views) {
36       ViewTestData::createTestViews(get_class($this), ['views_test_config']);
37     }
38   }
39
40   /**
41    * Sets up the views_test_data.module.
42    *
43    * Because the schema of views_test_data.module is dependent on the test
44    * using it, it cannot be enabled normally.
45    */
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());
50
51     \Drupal::service('module_installer')->install(['views_test_data']);
52     $this->resetAll();
53     $this->rebuildContainer();
54     $this->container->get('module_handler')->reload();
55
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);
62     }
63     $query->execute();
64   }
65
66   /**
67    * Orders a nested array containing a result set based on a given column.
68    *
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.
77    *
78    * @return array
79    *   The sorted result set.
80    */
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]) {
85         return 0;
86       }
87       return $order * (($a[$column] < $b[$column]) ? -1 : 1);
88     });
89     return $result_set;
90   }
91
92   /**
93    * Asserts the existence of a button with a certain ID and label.
94    *
95    * @param string $id
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.
102    *
103    * @throws \Behat\Mink\Exception\ElementNotFoundException
104    */
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);
108
109     if (empty($field)) {
110       throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id', $field);
111     }
112
113     $this->assertEquals($expected_label, $field->getValue());
114   }
115
116   /**
117    * Executes a view with debugging.
118    *
119    * @param \Drupal\views\ViewExecutable $view
120    *   The view object.
121    * @param array $args
122    *   (optional) An array of the view arguments to use for the view.
123    */
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.
127     $view->setDisplay();
128     $view->preExecute($args);
129     $view->execute();
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>';
133     }
134     $this->verbose($verbose_message);
135   }
136
137   /**
138    * Returns the schema definition.
139    */
140   protected function schemaDefinition() {
141     return ViewTestData::schemaDefinition();
142   }
143
144   /**
145    * Returns the views data definition.
146    */
147   protected function viewsData() {
148     return ViewTestData::viewsData();
149   }
150
151   /**
152    * Returns a very simple test dataset.
153    */
154   protected function dataSet() {
155     return ViewTestData::dataSet();
156   }
157
158 }