function ViewTestData::createTestViews

Same name in other branches
  1. 9 core/modules/views/src/Tests/ViewTestData.php \Drupal\views\Tests\ViewTestData::createTestViews()
  2. 8.9.x core/modules/views/src/Tests/ViewTestData.php \Drupal\views\Tests\ViewTestData::createTestViews()
  3. 11.x core/modules/views/src/Tests/ViewTestData.php \Drupal\views\Tests\ViewTestData::createTestViews()

Create test views from config.

Parameters

string $class: The name of the test class. Installs the listed test views *in order*.

array $modules: The module directories to look in for test views.

79 calls to ViewTestData::createTestViews()
AccessPermissionTest::setUp in core/modules/user/tests/src/Kernel/Views/AccessPermissionTest.php
AccessTest::setUp in core/modules/views/tests/src/Functional/Plugin/AccessTest.php
Sets up the test.
ArgumentDefaultTest::testPluginArgumentDefaultCurrentUser in core/modules/user/tests/src/Kernel/Views/ArgumentDefaultTest.php
Tests the current user with argument default.
ArgumentNodeRevisionIdTest::setUp in core/modules/node/tests/src/Kernel/Views/ArgumentNodeRevisionIdTest.php
ArgumentUidRevisionTest::setUp in core/modules/node/tests/src/Kernel/Views/ArgumentUidRevisionTest.php

... See full list

File

core/modules/views/src/Tests/ViewTestData.php, line 25

Class

ViewTestData
Provides tests view data and the base test schema with sample data records.

Namespace

Drupal\views\Tests

Code

public static function createTestViews($class, array $modules) {
    $views = [];
    while ($class) {
        if (property_exists($class, 'testViews')) {
            $views = array_merge($views, $class::$testViews);
        }
        $class = get_parent_class($class);
    }
    if (!empty($views)) {
        $storage = \Drupal::entityTypeManager()->getStorage('view');
        $module_handler = \Drupal::moduleHandler();
        foreach ($modules as $module) {
            $config_dir = \Drupal::service('extension.list.module')->getPath($module) . '/test_views';
            if (!is_dir($config_dir) || !$module_handler->moduleExists($module)) {
                continue;
            }
            $file_storage = new FileStorage($config_dir);
            $available_views = $file_storage->listAll('views.view.');
            foreach ($views as $id) {
                $config_name = 'views.view.' . $id;
                if (in_array($config_name, $available_views)) {
                    $storage->create($file_storage->read($config_name))
                        ->save();
                }
            }
        }
    }
    // Rebuild the router once.
    \Drupal::service('router.builder')->rebuild();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.