3 namespace Drupal\Tests\views\Kernel\Entity;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
11 use Drupal\views\Views;
12 use Drupal\comment\Entity\CommentType;
15 * Tests the calculation of dependencies for views.
19 class ViewEntityDependenciesTest extends ViewsKernelTestBase {
22 * Views used by this test.
26 public static $testViews = ['test_field_get_entity', 'test_relationship_dependency', 'test_plugin_dependencies', 'test_argument_dependency'];
33 public static $modules = ['node', 'comment', 'user', 'field', 'text', 'search'];
38 protected function setUp($import_test_views = TRUE) {
41 // Install the necessary dependencies for node type creation to work.
42 $this->installEntitySchema('node');
43 $this->installConfig(['field', 'node']);
45 $comment_type = CommentType::create([
47 'label' => 'Comment settings',
48 'description' => 'Comment settings',
49 'target_entity_type_id' => 'node',
51 $comment_type->save();
53 $content_type = NodeType::create([
54 'type' => $this->randomMachineName(),
55 'name' => $this->randomString(),
57 $content_type->save();
58 $field_storage = FieldStorageConfig::create([
59 'field_name' => Unicode::strtolower($this->randomMachineName()),
60 'entity_type' => 'node',
63 $field_storage->save();
65 'field_storage' => $field_storage,
66 'bundle' => $content_type->id(),
67 'label' => $this->randomMachineName() . '_label',
68 'description' => $this->randomMachineName() . '_description',
70 'comment_type' => $comment_type->id(),
74 'field_storage' => FieldStorageConfig::loadByName('node', 'body'),
75 'bundle' => $content_type->id(),
76 'label' => $this->randomMachineName() . '_body',
77 'settings' => ['display_summary' => TRUE],
80 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
84 * Tests the getDependencies method.
86 public function testGetDependencies() {
88 $expected['test_field_get_entity'] = [
95 // Tests dependencies of relationships.
96 $expected['test_relationship_dependency'] = [
103 $expected['test_plugin_dependencies'] = [
115 $expected['test_argument_dependency'] = [
117 'core.entity_view_mode.node.teaser',
118 'field.storage.node.body'
121 'ArgumentDefaultTest',
122 'ArgumentValidatorTest'
126 // The argument handler is provided by the search module.
133 foreach ($this::$testViews as $view_id) {
134 $view = Views::getView($view_id);
136 $dependencies = $view->getDependencies();
137 $this->assertEqual($expected[$view_id], $dependencies);
138 $config = $this->config('views.view.' . $view_id);
139 \Drupal::service('config.storage.sync')->write($view_id, $config->get());
142 // Ensure that dependencies are calculated on the display level.
143 $expected_display['default'] = [
145 'core.entity_view_mode.node.teaser',
148 'ArgumentDefaultTest',
149 'ArgumentValidatorTest'
159 $expected_display['page'] = [
161 '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());