Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Tests / ViewKernelTestBase.php
1 <?php
2
3 namespace Drupal\views\Tests;
4
5 use Drupal\Core\Database\Query\SelectInterface;
6 use Drupal\simpletest\KernelTestBase;
7
8 /**
9  * Defines a base class for Views unit testing.
10  *
11  * Use this test class for unit tests of Views functionality. If a test
12  * requires the full web test environment provided by WebTestBase, extend
13  * ViewTestBase instead.
14  *
15  * @deprecated in Drupal 8.0.x, will be removed in Drupal 8.2.x. Use
16  *   \Drupal\Tests\views\Kernel\ViewsKernelTestBase instead.
17  *
18  * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase
19  */
20 abstract class ViewKernelTestBase extends KernelTestBase {
21
22   use ViewResultAssertionTrait;
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['system', 'views', 'views_test_config', 'views_test_data', 'user'];
30
31   /**
32    * {@inheritdoc}
33    *
34    * @param bool $import_test_views
35    *   Should the views specififed on the test class be imported. If you need
36    *   to setup some additional stuff, like fields, you need to call false and
37    *   then call createTestViews for your own.
38    */
39   protected function setUp($import_test_views = TRUE) {
40     parent::setUp();
41
42     $this->installSchema('system', ['sequences']);
43     $this->setUpFixtures();
44
45     if ($import_test_views) {
46       ViewTestData::createTestViews(get_class($this), ['views_test_config']);
47     }
48   }
49
50   /**
51    * Sets up the configuration and schema of views and views_test_data modules.
52    *
53    * Because the schema of views_test_data.module is dependent on the test
54    * using it, it cannot be enabled normally.
55    */
56   protected function setUpFixtures() {
57     // First install the system module. Many Views have Page displays have menu
58     // links, and for those to work, the system menus must already be present.
59     $this->installConfig(['system']);
60
61     // Define the schema and views data variable before enabling the test module.
62     \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
63     \Drupal::state()->set('views_test_data_views_data', $this->viewsData());
64
65     $this->installConfig(['views', 'views_test_config', 'views_test_data']);
66     foreach ($this->schemaDefinition() as $table => $schema) {
67       $this->installSchema('views_test_data', $table);
68     }
69
70     \Drupal::service('router.builder')->rebuild();
71
72     // Load the test dataset.
73     $data_set = $this->dataSet();
74     $query = db_insert('views_test_data')
75       ->fields(array_keys($data_set[0]));
76     foreach ($data_set as $record) {
77       $query->values($record);
78     }
79     $query->execute();
80   }
81
82   /**
83    * Orders a nested array containing a result set based on a given column.
84    *
85    * @param array $result_set
86    *   An array of rows from a result set, with each row as an associative
87    *   array keyed by column name.
88    * @param string $column
89    *   The column name by which to sort the result set.
90    * @param bool $reverse
91    *   (optional) Boolean indicating whether to sort the result set in reverse
92    *   order. Defaults to FALSE.
93    *
94    * @return array
95    *   The sorted result set.
96    */
97   protected function orderResultSet($result_set, $column, $reverse = FALSE) {
98     $order = $reverse ? -1 : 1;
99     usort($result_set, function ($a, $b) use ($column, $order) {
100       if ($a[$column] == $b[$column]) {
101         return 0;
102       }
103       return $order * (($a[$column] < $b[$column]) ? -1 : 1);
104     });
105     return $result_set;
106   }
107
108   /**
109    * Executes a view with debugging.
110    *
111    * @param \Drupal\views\ViewExecutable $view
112    *   The view object.
113    * @param array $args
114    *   (optional) An array of the view arguments to use for the view.
115    */
116   protected function executeView($view, array $args = []) {
117     $view->setDisplay();
118     $view->preExecute($args);
119     $view->execute();
120     $verbose_message = '<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>';
121     if ($view->build_info['query'] instanceof SelectInterface) {
122       $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>';
123     }
124     $this->verbose($verbose_message);
125   }
126
127   /**
128    * Returns the schema definition.
129    */
130   protected function schemaDefinition() {
131     return ViewTestData::schemaDefinition();
132   }
133
134   /**
135    * Returns the views data definition.
136    */
137   protected function viewsData() {
138     return ViewTestData::viewsData();
139   }
140
141   /**
142    * Returns a very simple test dataset.
143    */
144   protected function dataSet() {
145     return ViewTestData::dataSet();
146   }
147
148 }