34f58122cafb799541c8077cb8afc2cf4e5fe0e1
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Entity / ViewEntityDependenciesTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Entity;
4
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;
12
13 /**
14  * Tests the calculation of dependencies for views.
15  *
16  * @group views
17  */
18 class ViewEntityDependenciesTest extends ViewsKernelTestBase {
19
20   /**
21    * Views used by this test.
22    *
23    * @var array
24    */
25   public static $testViews = ['test_field_get_entity', 'test_relationship_dependency', 'test_plugin_dependencies', 'test_argument_dependency'];
26
27   /**
28    * Modules to enable.
29    *
30    * @var array
31    */
32   public static $modules = ['node', 'comment', 'user', 'field', 'text', 'search'];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp($import_test_views = TRUE) {
38     parent::setUp(FALSE);
39
40     // Install the necessary dependencies for node type creation to work.
41     $this->installEntitySchema('node');
42     $this->installConfig(['field', 'node']);
43
44     $comment_type = CommentType::create([
45       'id' => 'comment',
46       'label' => 'Comment settings',
47       'description' => 'Comment settings',
48       'target_entity_type_id' => 'node',
49     ]);
50     $comment_type->save();
51
52     $content_type = NodeType::create([
53       'type' => $this->randomMachineName(),
54       'name' => $this->randomString(),
55     ]);
56     $content_type->save();
57     $field_storage = FieldStorageConfig::create([
58       'field_name' => mb_strtolower($this->randomMachineName()),
59       'entity_type' => 'node',
60       'type' => 'comment',
61     ]);
62     $field_storage->save();
63     FieldConfig::create([
64       'field_storage' => $field_storage,
65       'bundle' => $content_type->id(),
66       'label' => $this->randomMachineName() . '_label',
67       'description' => $this->randomMachineName() . '_description',
68       'settings' => [
69         'comment_type' => $comment_type->id(),
70       ],
71     ])->save();
72     FieldConfig::create([
73       'field_storage' => FieldStorageConfig::loadByName('node', 'body'),
74       'bundle' => $content_type->id(),
75       'label' => $this->randomMachineName() . '_body',
76       'settings' => ['display_summary' => TRUE],
77     ])->save();
78
79     ViewTestData::createTestViews(get_class($this), ['views_test_config']);
80   }
81
82   /**
83    * Tests the getDependencies method.
84    */
85   public function testGetDependencies() {
86     $expected = [];
87     $expected['test_field_get_entity'] = [
88       'module' => [
89         'comment',
90         'node',
91         'user',
92       ],
93     ];
94     // Tests dependencies of relationships.
95     $expected['test_relationship_dependency'] = [
96       'module' => [
97         'comment',
98         'node',
99         'user',
100       ],
101     ];
102     $expected['test_plugin_dependencies'] = [
103       'module' => [
104         'comment',
105         'views_test_data',
106       ],
107       'content' => [
108         'RowTest',
109         'StaticTest',
110         'StyleTest',
111       ],
112     ];
113
114     $expected['test_argument_dependency'] = [
115       'config' => [
116         'core.entity_view_mode.node.teaser',
117         'field.storage.node.body',
118       ],
119       'content' => [
120         'ArgumentDefaultTest',
121         'ArgumentValidatorTest',
122       ],
123       'module' => [
124         'node',
125         // The argument handler is provided by the search module.
126         'search',
127         'text',
128         'user',
129       ],
130     ];
131
132     foreach ($this::$testViews as $view_id) {
133       $view = Views::getView($view_id);
134
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());
139     }
140
141     // Ensure that dependencies are calculated on the display level.
142     $expected_display['default'] = [
143       'config' => [
144         'core.entity_view_mode.node.teaser',
145       ],
146       'content' => [
147         'ArgumentDefaultTest',
148         'ArgumentValidatorTest',
149       ],
150       'module' => [
151         'core',
152         'node',
153         'search',
154         'user',
155         'views',
156       ],
157     ];
158     $expected_display['page'] = [
159       'config' => [
160         'field.storage.node.body',
161       ],
162       'module' => [
163         'core',
164         'node',
165         'text',
166         'views',
167       ],
168     ];
169
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());
175     }
176   }
177
178 }