comparison core/modules/views/src/Tests/ViewTestData.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\views\Tests;
4
5 use Drupal\Core\Config\FileStorage;
6
7 /**
8 * Provides tests view data and the base test schema with sample data records.
9 *
10 * The methods will be used by both views test base classes.
11 *
12 * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase.
13 * @see \Drupal\views\Tests\ViewTestBase.
14 */
15 class ViewTestData {
16
17 /**
18 * Create test views from config.
19 *
20 * @param string $class
21 * The name of the test class. Installs the listed test views *in order*.
22 * @param array $modules
23 * The module directories to look in for test views.
24 */
25 public static function createTestViews($class, array $modules) {
26 $views = [];
27 while ($class) {
28 if (property_exists($class, 'testViews')) {
29 $views = array_merge($views, $class::$testViews);
30 }
31 $class = get_parent_class($class);
32 }
33 if (!empty($views)) {
34 $storage = \Drupal::entityManager()->getStorage('view');
35 $module_handler = \Drupal::moduleHandler();
36 foreach ($modules as $module) {
37 $config_dir = drupal_get_path('module', $module) . '/test_views';
38 if (!is_dir($config_dir) || !$module_handler->moduleExists($module)) {
39 continue;
40 }
41
42 $file_storage = new FileStorage($config_dir);
43 $available_views = $file_storage->listAll('views.view.');
44 foreach ($views as $id) {
45 $config_name = 'views.view.' . $id;
46 if (in_array($config_name, $available_views)) {
47 $storage
48 ->create($file_storage->read($config_name))
49 ->save();
50 }
51 }
52 }
53 }
54
55 // Rebuild the router once.
56 \Drupal::service('router.builder')->rebuild();
57 }
58
59 /**
60 * Returns the schema definition.
61 *
62 * @internal
63 */
64 public static function schemaDefinition() {
65 $schema['views_test_data'] = [
66 'description' => 'Basic test table for Views tests.',
67 'fields' => [
68 'id' => [
69 'type' => 'serial',
70 'unsigned' => TRUE,
71 'not null' => TRUE,
72 ],
73 'name' => [
74 'description' => "A person's name",
75 'type' => 'varchar_ascii',
76 'length' => 255,
77 'not null' => TRUE,
78 'default' => '',
79 ],
80 'age' => [
81 'description' => "The person's age",
82 'type' => 'int',
83 'unsigned' => TRUE,
84 'not null' => TRUE,
85 'default' => 0,
86 ],
87 'job' => [
88 'description' => "The person's job",
89 'type' => 'varchar',
90 'length' => 255,
91 'not null' => TRUE,
92 'default' => 'Undefined',
93 ],
94 'created' => [
95 'description' => "The creation date of this record",
96 'type' => 'int',
97 'unsigned' => TRUE,
98 'not null' => TRUE,
99 'default' => 0,
100 ],
101 'status' => [
102 'description' => "The status of this record",
103 'type' => 'int',
104 'unsigned' => TRUE,
105 'not null' => TRUE,
106 'default' => 0,
107 ],
108 ],
109 'primary key' => ['id'],
110 'unique keys' => [
111 'name' => ['name']
112 ],
113 'indexes' => [
114 'ages' => ['age'],
115 ],
116 ];
117 return $schema;
118 }
119
120 /**
121 * Returns the views data definition.
122 */
123 public static function viewsData() {
124 // Declaration of the base table.
125 $data['views_test_data']['table'] = [
126 'group' => 'Views test',
127 'base' => [
128 'field' => 'id',
129 'title' => 'Views test data',
130 'help' => 'Users who have created accounts on your site.',
131 ],
132 ];
133
134 // Declaration of fields.
135 $data['views_test_data']['id'] = [
136 'title' => 'ID',
137 'help' => 'The test data ID',
138 'field' => [
139 'id' => 'numeric',
140 ],
141 'argument' => [
142 'id' => 'numeric',
143 ],
144 'filter' => [
145 'id' => 'numeric',
146 ],
147 'sort' => [
148 'id' => 'standard',
149 ],
150 ];
151 $data['views_test_data']['name'] = [
152 'title' => 'Name',
153 'help' => 'The name of the person',
154 'field' => [
155 'id' => 'standard',
156 ],
157 'argument' => [
158 'id' => 'string',
159 ],
160 'filter' => [
161 'id' => 'string',
162 ],
163 'sort' => [
164 'id' => 'standard',
165 ],
166 ];
167 $data['views_test_data']['age'] = [
168 'title' => 'Age',
169 'help' => 'The age of the person',
170 'field' => [
171 'id' => 'numeric',
172 ],
173 'argument' => [
174 'id' => 'numeric',
175 ],
176 'filter' => [
177 'id' => 'numeric',
178 ],
179 'sort' => [
180 'id' => 'standard',
181 ],
182 ];
183 $data['views_test_data']['job'] = [
184 'title' => 'Job',
185 'help' => 'The job of the person',
186 'field' => [
187 'id' => 'standard',
188 ],
189 'argument' => [
190 'id' => 'string',
191 ],
192 'filter' => [
193 'id' => 'string',
194 ],
195 'sort' => [
196 'id' => 'standard',
197 ],
198 ];
199 $data['views_test_data']['created'] = [
200 'title' => 'Created',
201 'help' => 'The creation date of this record',
202 'field' => [
203 'id' => 'date',
204 ],
205 'argument' => [
206 'id' => 'date',
207 ],
208 'filter' => [
209 'id' => 'date',
210 ],
211 'sort' => [
212 'id' => 'date',
213 ],
214 ];
215 $data['views_test_data']['status'] = [
216 'title' => 'Status',
217 'help' => 'The status of this record',
218 'field' => [
219 'id' => 'boolean',
220 ],
221 'filter' => [
222 'id' => 'boolean',
223 ],
224 'sort' => [
225 'id' => 'standard',
226 ],
227 ];
228 return $data;
229 }
230
231 /**
232 * Returns a very simple test dataset.
233 */
234 public static function dataSet() {
235 return [
236 [
237 'name' => 'John',
238 'age' => 25,
239 'job' => 'Singer',
240 'created' => gmmktime(0, 0, 0, 1, 1, 2000),
241 'status' => 1,
242 ],
243 [
244 'name' => 'George',
245 'age' => 27,
246 'job' => 'Singer',
247 'created' => gmmktime(0, 0, 0, 1, 2, 2000),
248 'status' => 0,
249 ],
250 [
251 'name' => 'Ringo',
252 'age' => 28,
253 'job' => 'Drummer',
254 'created' => gmmktime(6, 30, 30, 1, 1, 2000),
255 'status' => 1,
256 ],
257 [
258 'name' => 'Paul',
259 'age' => 26,
260 'job' => 'Songwriter',
261 'created' => gmmktime(6, 0, 0, 1, 1, 2000),
262 'status' => 0,
263 ],
264 [
265 'name' => 'Meredith',
266 'age' => 30,
267 'job' => 'Speaker',
268 'created' => gmmktime(6, 30, 10, 1, 1, 2000),
269 'status' => 1,
270 ],
271 ];
272 }
273
274 }