Version 1
[yaffs-website] / web / core / modules / views / src / Tests / ViewTestData.php
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   public static function schemaDefinition() {
63     $schema['views_test_data'] = [
64       'description' => 'Basic test table for Views tests.',
65       'fields' => [
66         'id' => [
67           'type' => 'serial',
68           'unsigned' => TRUE,
69           'not null' => TRUE,
70         ],
71         'name' => [
72           'description' => "A person's name",
73           'type' => 'varchar_ascii',
74           'length' => 255,
75           'not null' => TRUE,
76           'default' => '',
77         ],
78         'age' => [
79           'description' => "The person's age",
80           'type' => 'int',
81           'unsigned' => TRUE,
82           'not null' => TRUE,
83           'default' => 0],
84         'job' => [
85           'description' => "The person's job",
86           'type' => 'varchar',
87           'length' => 255,
88           'not null' => TRUE,
89           'default' => 'Undefined',
90         ],
91         'created' => [
92           'description' => "The creation date of this record",
93           'type' => 'int',
94           'unsigned' => TRUE,
95           'not null' => TRUE,
96           'default' => 0,
97         ],
98         'status' => [
99           'description' => "The status of this record",
100           'type' => 'int',
101           'unsigned' => TRUE,
102           'not null' => TRUE,
103           'default' => 0,
104         ],
105       ],
106       'primary key' => ['id'],
107       'unique keys' => [
108         'name' => ['name']
109       ],
110       'indexes' => [
111         'ages' => ['age'],
112       ],
113     ];
114     return $schema;
115   }
116
117   /**
118    * Returns the views data definition.
119    */
120   public static function viewsData() {
121     // Declaration of the base table.
122     $data['views_test_data']['table'] = [
123       'group' => 'Views test',
124       'base' => [
125         'field' => 'id',
126         'title' => 'Views test data',
127         'help' => 'Users who have created accounts on your site.',
128       ],
129     ];
130
131     // Declaration of fields.
132     $data['views_test_data']['id'] = [
133       'title' => 'ID',
134       'help' => 'The test data ID',
135       'field' => [
136         'id' => 'numeric',
137       ],
138       'argument' => [
139         'id' => 'numeric',
140       ],
141       'filter' => [
142         'id' => 'numeric',
143       ],
144       'sort' => [
145         'id' => 'standard',
146       ],
147     ];
148     $data['views_test_data']['name'] = [
149       'title' => 'Name',
150       'help' => 'The name of the person',
151       'field' => [
152         'id' => 'standard',
153       ],
154       'argument' => [
155         'id' => 'string',
156       ],
157       'filter' => [
158         'id' => 'string',
159       ],
160       'sort' => [
161         'id' => 'standard',
162       ],
163     ];
164     $data['views_test_data']['age'] = [
165       'title' => 'Age',
166       'help' => 'The age of the person',
167       'field' => [
168         'id' => 'numeric',
169       ],
170       'argument' => [
171         'id' => 'numeric',
172       ],
173       'filter' => [
174         'id' => 'numeric',
175       ],
176       'sort' => [
177         'id' => 'standard',
178       ],
179     ];
180     $data['views_test_data']['job'] = [
181       'title' => 'Job',
182       'help' => 'The job of the person',
183       'field' => [
184         'id' => 'standard',
185       ],
186       'argument' => [
187         'id' => 'string',
188       ],
189       'filter' => [
190         'id' => 'string',
191       ],
192       'sort' => [
193         'id' => 'standard',
194       ],
195     ];
196     $data['views_test_data']['created'] = [
197       'title' => 'Created',
198       'help' => 'The creation date of this record',
199       'field' => [
200         'id' => 'date',
201       ],
202       'argument' => [
203         'id' => 'date',
204       ],
205       'filter' => [
206         'id' => 'date',
207       ],
208       'sort' => [
209         'id' => 'date',
210       ],
211     ];
212     $data['views_test_data']['status'] = [
213       'title' => 'Status',
214       'help' => 'The status of this record',
215       'field' => [
216         'id' => 'boolean',
217       ],
218       'filter' => [
219         'id' => 'boolean',
220       ],
221       'sort' => [
222         'id' => 'standard',
223       ],
224     ];
225     return $data;
226   }
227
228   /**
229    * Returns a very simple test dataset.
230    */
231   public static function dataSet() {
232     return [
233       [
234         'name' => 'John',
235         'age' => 25,
236         'job' => 'Singer',
237         'created' => gmmktime(0, 0, 0, 1, 1, 2000),
238         'status' => 1,
239       ],
240       [
241         'name' => 'George',
242         'age' => 27,
243         'job' => 'Singer',
244         'created' => gmmktime(0, 0, 0, 1, 2, 2000),
245         'status' => 0,
246       ],
247       [
248         'name' => 'Ringo',
249         'age' => 28,
250         'job' => 'Drummer',
251         'created' => gmmktime(6, 30, 30, 1, 1, 2000),
252         'status' => 1,
253       ],
254       [
255         'name' => 'Paul',
256         'age' => 26,
257         'job' => 'Songwriter',
258         'created' => gmmktime(6, 0, 0, 1, 1, 2000),
259         'status' => 0,
260       ],
261       [
262         'name' => 'Meredith',
263         'age' => 30,
264         'job' => 'Speaker',
265         'created' => gmmktime(6, 30, 10, 1, 1, 2000),
266         'status' => 1,
267       ],
268     ];
269   }
270
271 }