3 namespace Drupal\Tests\views\Kernel;
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;
12 * Tests integration of views with other modules.
16 class ViewsConfigDependenciesIntegrationTest extends ViewsKernelTestBase {
21 public static $modules = ['field', 'file', 'image', 'entity_test', 'user', 'text'];
26 public static $testViews = ['entity_test_fields'];
31 protected function setUp($import_test_views = TRUE) {
32 parent::setUp($import_test_views);
34 $this->installEntitySchema('user');
35 $this->installSchema('user', ['users_data']);
39 * Tests integration with image module.
41 public function testImage() {
42 /** @var \Drupal\image\ImageStyleInterface $style */
43 $style = ImageStyle::create(['name' => 'foo']);
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',
53 'entity_type' => 'entity_test',
54 'bundle' => 'entity_test',
55 'field_name' => 'bar',
58 /** @var \Drupal\views\ViewEntityInterface $view */
59 $view = View::load('entity_test_fields');
60 $display =& $view->getDisplay('default');
62 // Add the 'bar' image field to 'entity_test_fields' view.
63 $display['display_options']['fields']['bar'] = [
66 'plugin_id' => 'field',
67 'table' => 'entity_test__bar',
68 'entity_type' => 'entity_test',
69 'entity_field' => 'bar',
71 'settings' => ['image_style' => 'foo', 'image_link' => ''],
75 $dependencies = $view->getDependencies() + ['config' => []];
77 // Checks that style 'foo' is a dependency of view 'entity_test_fields'.
78 $this->assertTrue(in_array('image.style.foo', $dependencies['config']));
80 // Delete the 'foo' image style.
83 $view = View::load('entity_test_fields');
85 // Checks that the view has not been deleted too.
86 $this->assertNotNull(View::load('entity_test_fields'));
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']));
92 // Checks that the view has been disabled.
93 $this->assertFalse($view->status());
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']));
101 * Tests removing a config dependency that deletes the View.
103 public function testConfigRemovalRole() {
104 // Create a role we can add to the View and delete.
105 $role = Role::create([
112 /** @var \Drupal\views\ViewEntityInterface $view */
113 $view = View::load('entity_test_fields');
114 $display = &$view->getDisplay('default');
116 // Set the access to be restricted by the dummy role.
117 $display['display_options']['access'] = [
121 $role->id() => $role->id(),
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']));
134 $view = View::load('entity_test_fields');
136 // Checks that the view has been deleted too.
137 $this->assertNull($view);
141 * Tests uninstalling a module that provides a base table for a View.
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);
153 // Check that removing the module that provides the base table for a 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'));