Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\views\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
6 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
7 use Drupal\image\Entity\ImageStyle;
|
Chris@0
|
8 use Drupal\user\Entity\Role;
|
Chris@0
|
9 use Drupal\views\Entity\View;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Tests integration of views with other modules.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group views
|
Chris@0
|
15 */
|
Chris@0
|
16 class ViewsConfigDependenciesIntegrationTest extends ViewsKernelTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * {@inheritdoc}
|
Chris@0
|
20 */
|
Chris@0
|
21 public static $modules = ['field', 'file', 'image', 'entity_test', 'user', 'text'];
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * {@inheritdoc}
|
Chris@0
|
25 */
|
Chris@0
|
26 public static $testViews = ['entity_test_fields'];
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * {@inheritdoc}
|
Chris@0
|
30 */
|
Chris@0
|
31 protected function setUp($import_test_views = TRUE) {
|
Chris@0
|
32 parent::setUp($import_test_views);
|
Chris@0
|
33
|
Chris@18
|
34 $this->installEntitySchema('entity_test');
|
Chris@0
|
35 $this->installEntitySchema('user');
|
Chris@0
|
36 $this->installSchema('user', ['users_data']);
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Tests integration with image module.
|
Chris@0
|
41 */
|
Chris@0
|
42 public function testImage() {
|
Chris@0
|
43 /** @var \Drupal\image\ImageStyleInterface $style */
|
Chris@0
|
44 $style = ImageStyle::create(['name' => 'foo']);
|
Chris@0
|
45 $style->save();
|
Chris@0
|
46
|
Chris@0
|
47 // Create a new image field 'bar' to be used in 'entity_test_fields' view.
|
Chris@0
|
48 FieldStorageConfig::create([
|
Chris@0
|
49 'entity_type' => 'entity_test',
|
Chris@0
|
50 'field_name' => 'bar',
|
Chris@0
|
51 'type' => 'image',
|
Chris@0
|
52 ])->save();
|
Chris@0
|
53 FieldConfig::create([
|
Chris@0
|
54 'entity_type' => 'entity_test',
|
Chris@0
|
55 'bundle' => 'entity_test',
|
Chris@0
|
56 'field_name' => 'bar',
|
Chris@0
|
57 ])->save();
|
Chris@0
|
58
|
Chris@0
|
59 /** @var \Drupal\views\ViewEntityInterface $view */
|
Chris@0
|
60 $view = View::load('entity_test_fields');
|
Chris@0
|
61 $display =& $view->getDisplay('default');
|
Chris@0
|
62
|
Chris@0
|
63 // Add the 'bar' image field to 'entity_test_fields' view.
|
Chris@0
|
64 $display['display_options']['fields']['bar'] = [
|
Chris@0
|
65 'id' => 'bar',
|
Chris@0
|
66 'field' => 'bar',
|
Chris@0
|
67 'plugin_id' => 'field',
|
Chris@0
|
68 'table' => 'entity_test__bar',
|
Chris@0
|
69 'entity_type' => 'entity_test',
|
Chris@0
|
70 'entity_field' => 'bar',
|
Chris@0
|
71 'type' => 'image',
|
Chris@0
|
72 'settings' => ['image_style' => 'foo', 'image_link' => ''],
|
Chris@0
|
73 ];
|
Chris@0
|
74 $view->save();
|
Chris@0
|
75
|
Chris@0
|
76 $dependencies = $view->getDependencies() + ['config' => []];
|
Chris@0
|
77
|
Chris@0
|
78 // Checks that style 'foo' is a dependency of view 'entity_test_fields'.
|
Chris@0
|
79 $this->assertTrue(in_array('image.style.foo', $dependencies['config']));
|
Chris@0
|
80
|
Chris@0
|
81 // Delete the 'foo' image style.
|
Chris@0
|
82 $style->delete();
|
Chris@0
|
83
|
Chris@0
|
84 $view = View::load('entity_test_fields');
|
Chris@0
|
85
|
Chris@0
|
86 // Checks that the view has not been deleted too.
|
Chris@0
|
87 $this->assertNotNull(View::load('entity_test_fields'));
|
Chris@0
|
88
|
Chris@0
|
89 // Checks that the image field was removed from the View.
|
Chris@0
|
90 $display = $view->getDisplay('default');
|
Chris@0
|
91 $this->assertFalse(isset($display['display_options']['fields']['bar']));
|
Chris@0
|
92
|
Chris@0
|
93 // Checks that the view has been disabled.
|
Chris@0
|
94 $this->assertFalse($view->status());
|
Chris@0
|
95
|
Chris@0
|
96 $dependencies = $view->getDependencies() + ['config' => []];
|
Chris@0
|
97 // Checks that the dependency on style 'foo' has been removed.
|
Chris@0
|
98 $this->assertFalse(in_array('image.style.foo', $dependencies['config']));
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Tests removing a config dependency that deletes the View.
|
Chris@0
|
103 */
|
Chris@0
|
104 public function testConfigRemovalRole() {
|
Chris@0
|
105 // Create a role we can add to the View and delete.
|
Chris@0
|
106 $role = Role::create([
|
Chris@0
|
107 'id' => 'dummy',
|
Chris@0
|
108 'label' => 'dummy',
|
Chris@0
|
109 ]);
|
Chris@0
|
110
|
Chris@0
|
111 $role->save();
|
Chris@0
|
112
|
Chris@0
|
113 /** @var \Drupal\views\ViewEntityInterface $view */
|
Chris@0
|
114 $view = View::load('entity_test_fields');
|
Chris@0
|
115 $display = &$view->getDisplay('default');
|
Chris@0
|
116
|
Chris@0
|
117 // Set the access to be restricted by the dummy role.
|
Chris@0
|
118 $display['display_options']['access'] = [
|
Chris@0
|
119 'type' => 'role',
|
Chris@0
|
120 'options' => [
|
Chris@0
|
121 'role' => [
|
Chris@0
|
122 $role->id() => $role->id(),
|
Chris@0
|
123 ],
|
Chris@0
|
124 ],
|
Chris@0
|
125 ];
|
Chris@0
|
126 $view->save();
|
Chris@0
|
127
|
Chris@0
|
128 // Check that the View now has a dependency on the Role.
|
Chris@0
|
129 $dependencies = $view->getDependencies() + ['config' => []];
|
Chris@0
|
130 $this->assertTrue(in_array('user.role.dummy', $dependencies['config']));
|
Chris@0
|
131
|
Chris@0
|
132 // Delete the role.
|
Chris@0
|
133 $role->delete();
|
Chris@0
|
134
|
Chris@0
|
135 $view = View::load('entity_test_fields');
|
Chris@0
|
136
|
Chris@0
|
137 // Checks that the view has been deleted too.
|
Chris@0
|
138 $this->assertNull($view);
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * Tests uninstalling a module that provides a base table for a View.
|
Chris@0
|
143 */
|
Chris@0
|
144 public function testConfigRemovalBaseTable() {
|
Chris@0
|
145 // Find all the entity types provided by the entity_test module and install
|
Chris@0
|
146 // the schema for them so we can uninstall them.
|
Chris@0
|
147 $entities = \Drupal::entityTypeManager()->getDefinitions();
|
Chris@0
|
148 foreach ($entities as $entity_type_id => $definition) {
|
Chris@0
|
149 if ($definition->getProvider() == 'entity_test') {
|
Chris@0
|
150 $this->installEntitySchema($entity_type_id);
|
Chris@0
|
151 };
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 // Check that removing the module that provides the base table for a View,
|
Chris@0
|
155 // deletes the View.
|
Chris@0
|
156 $this->assertNotNull(View::load('entity_test_fields'));
|
Chris@0
|
157 $this->container->get('module_installer')->uninstall(['entity_test']);
|
Chris@0
|
158 // Check that the View has been deleted.
|
Chris@0
|
159 $this->assertNull(View::load('entity_test_fields'));
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 }
|