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