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