3 namespace Drupal\Tests\views\Kernel\Entity;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\views\Tests\ViewTestData;
9 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
10 use Drupal\views\Views;
11 use Drupal\comment\Entity\CommentType;
14 * Tests the calculation of dependencies for views.
18 class ViewEntityDependenciesTest extends ViewsKernelTestBase {
21 * Views used by this test.
25 public static $testViews = ['test_field_get_entity', 'test_relationship_dependency', 'test_plugin_dependencies', 'test_argument_dependency'];
32 public static $modules = ['node', 'comment', 'user', 'field', 'text', 'search'];
37 protected function setUp($import_test_views = TRUE) {
40 // Install the necessary dependencies for node type creation to work.
41 $this->installEntitySchema('node');
42 $this->installConfig(['field', 'node']);
44 $comment_type = CommentType::create([
46 'label' => 'Comment settings',
47 'description' => 'Comment settings',
48 'target_entity_type_id' => 'node',
50 $comment_type->save();
52 $content_type = NodeType::create([
53 'type' => $this->randomMachineName(),
54 'name' => $this->randomString(),
56 $content_type->save();
57 $field_storage = FieldStorageConfig::create([
58 'field_name' => mb_strtolower($this->randomMachineName()),
59 'entity_type' => 'node',
62 $field_storage->save();
64 'field_storage' => $field_storage,
65 'bundle' => $content_type->id(),
66 'label' => $this->randomMachineName() . '_label',
67 'description' => $this->randomMachineName() . '_description',
69 'comment_type' => $comment_type->id(),
73 'field_storage' => FieldStorageConfig::loadByName('node', 'body'),
74 'bundle' => $content_type->id(),
75 'label' => $this->randomMachineName() . '_body',
76 'settings' => ['display_summary' => TRUE],
79 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
83 * Tests the getDependencies method.
85 public function testGetDependencies() {
87 $expected['test_field_get_entity'] = [
94 // Tests dependencies of relationships.
95 $expected['test_relationship_dependency'] = [
102 $expected['test_plugin_dependencies'] = [
114 $expected['test_argument_dependency'] = [
116 'core.entity_view_mode.node.teaser',
117 'field.storage.node.body',
120 'ArgumentDefaultTest',
121 'ArgumentValidatorTest',
125 // The argument handler is provided by the search module.
132 foreach ($this::$testViews as $view_id) {
133 $view = Views::getView($view_id);
135 $dependencies = $view->getDependencies();
136 $this->assertEqual($expected[$view_id], $dependencies);
137 $config = $this->config('views.view.' . $view_id);
138 \Drupal::service('config.storage.sync')->write($view_id, $config->get());
141 // Ensure that dependencies are calculated on the display level.
142 $expected_display['default'] = [
144 'core.entity_view_mode.node.teaser',
147 'ArgumentDefaultTest',
148 'ArgumentValidatorTest',
158 $expected_display['page'] = [
160 'field.storage.node.body',
170 $view = Views::getView('test_argument_dependency');
171 $view->initDisplay();
172 foreach ($view->displayHandlers as $display) {
173 // Calculate the dependencies each display has.
174 $this->assertEqual($expected_display[$display->getPluginId()], $display->calculateDependencies());