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