3 namespace Drupal\views\Tests;
5 use Drupal\Core\Config\FileStorage;
8 * Provides tests view data and the base test schema with sample data records.
10 * The methods will be used by both views test base classes.
12 * @see \Drupal\Tests\views\Kernel\ViewsKernelTestBase.
13 * @see \Drupal\views\Tests\ViewTestBase.
18 * Create test views from config.
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.
25 public static function createTestViews($class, array $modules) {
28 if (property_exists($class, 'testViews')) {
29 $views = array_merge($views, $class::$testViews);
31 $class = get_parent_class($class);
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)) {
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)) {
48 ->create($file_storage->read($config_name))
55 // Rebuild the router once.
56 \Drupal::service('router.builder')->rebuild();
60 * Returns the schema definition.
64 public static function schemaDefinition() {
65 $schema['views_test_data'] = [
66 'description' => 'Basic test table for Views tests.',
74 'description' => "A person's name",
75 'type' => 'varchar_ascii',
81 'description' => "The person's age",
88 'description' => "The person's job",
92 'default' => 'Undefined',
95 'description' => "The creation date of this record",
102 'description' => "The status of this record",
109 'primary key' => ['id'],
121 * Returns the views data definition.
123 public static function viewsData() {
124 // Declaration of the base table.
125 $data['views_test_data']['table'] = [
126 'group' => 'Views test',
129 'title' => 'Views test data',
130 'help' => 'Users who have created accounts on your site.',
134 // Declaration of fields.
135 $data['views_test_data']['id'] = [
137 'help' => 'The test data ID',
151 $data['views_test_data']['name'] = [
153 'help' => 'The name of the person',
167 $data['views_test_data']['age'] = [
169 'help' => 'The age of the person',
183 $data['views_test_data']['job'] = [
185 'help' => 'The job of the person',
199 $data['views_test_data']['created'] = [
200 'title' => 'Created',
201 'help' => 'The creation date of this record',
215 $data['views_test_data']['status'] = [
217 'help' => 'The status of this record',
232 * Returns a very simple test dataset.
234 public static function dataSet() {
240 'created' => gmmktime(0, 0, 0, 1, 1, 2000),
247 'created' => gmmktime(0, 0, 0, 1, 2, 2000),
254 'created' => gmmktime(6, 30, 30, 1, 1, 2000),
260 'job' => 'Songwriter',
261 'created' => gmmktime(6, 0, 0, 1, 1, 2000),
265 'name' => 'Meredith',
268 'created' => gmmktime(6, 30, 10, 1, 1, 2000),