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.
62 public static function schemaDefinition() {
63 $schema['views_test_data'] = [
64 'description' => 'Basic test table for Views tests.',
72 'description' => "A person's name",
73 'type' => 'varchar_ascii',
79 'description' => "The person's age",
85 'description' => "The person's job",
89 'default' => 'Undefined',
92 'description' => "The creation date of this record",
99 'description' => "The status of this record",
106 'primary key' => ['id'],
118 * Returns the views data definition.
120 public static function viewsData() {
121 // Declaration of the base table.
122 $data['views_test_data']['table'] = [
123 'group' => 'Views test',
126 'title' => 'Views test data',
127 'help' => 'Users who have created accounts on your site.',
131 // Declaration of fields.
132 $data['views_test_data']['id'] = [
134 'help' => 'The test data ID',
148 $data['views_test_data']['name'] = [
150 'help' => 'The name of the person',
164 $data['views_test_data']['age'] = [
166 'help' => 'The age of the person',
180 $data['views_test_data']['job'] = [
182 'help' => 'The job of the person',
196 $data['views_test_data']['created'] = [
197 'title' => 'Created',
198 'help' => 'The creation date of this record',
212 $data['views_test_data']['status'] = [
214 'help' => 'The status of this record',
229 * Returns a very simple test dataset.
231 public static function dataSet() {
237 'created' => gmmktime(0, 0, 0, 1, 1, 2000),
244 'created' => gmmktime(0, 0, 0, 1, 2, 2000),
251 'created' => gmmktime(6, 30, 30, 1, 1, 2000),
257 'job' => 'Songwriter',
258 'created' => gmmktime(6, 0, 0, 1, 1, 2000),
262 'name' => 'Meredith',
265 'created' => gmmktime(6, 30, 10, 1, 1, 2000),