3 namespace Drupal\KernelTests\Core\Field;
5 use Drupal\Core\Extension\ModuleUninstallValidatorException;
6 use Drupal\Core\Field\BaseFieldDefinition;
7 use Drupal\entity_test\FieldStorageDefinition;
8 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
11 * Tests FieldModuleUninstallValidator functionality.
15 class FieldModuleUninstallValidatorTest extends EntityKernelTestBase {
18 * The entity definition update manager.
20 * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
22 protected $entityDefinitionUpdateManager;
27 protected function setUp() {
29 $this->installSchema('user', 'users_data');
30 $this->entityDefinitionUpdateManager = $this->container->get('entity.definition_update_manager');
32 // Setup some fields for entity_test_extra to create.
33 $definitions['extra_base_field'] = BaseFieldDefinition::create('string')
34 ->setName('extra_base_field')
35 ->setTargetEntityTypeId('entity_test')
36 ->setTargetBundle('entity_test');
37 $this->state->set('entity_test.additional_base_field_definitions', $definitions);
38 $definitions['extra_bundle_field'] = FieldStorageDefinition::create('string')
39 ->setName('extra_bundle_field')
40 ->setTargetEntityTypeId('entity_test')
41 ->setTargetBundle('entity_test');
42 $this->state->set('entity_test.additional_field_storage_definitions', $definitions);
43 $this->state->set('entity_test.entity_test.additional_bundle_field_definitions', $definitions);
44 $this->entityManager->clearCachedDefinitions();
48 * Tests uninstall entity_test module with and without content for the field.
50 public function testUninstallingModule() {
51 // Test uninstall works fine without content.
52 $this->assertModuleInstallUninstall('entity_test_extra');
54 // Test uninstalling works fine with content having no field values.
55 $entity = $this->entityManager->getStorage('entity_test')->create([
56 'name' => $this->randomString(),
59 $this->assertModuleInstallUninstall('entity_test_extra');
62 // Verify uninstall works fine without content again.
63 $this->assertModuleInstallUninstall('entity_test_extra');
64 // Verify uninstalling entity_test is not possible when there is content for
66 $this->enableModules(['entity_test_extra']);
67 $this->entityDefinitionUpdateManager->applyUpdates();
68 $entity = $this->entityManager->getStorage('entity_test')->create([
69 'name' => $this->randomString(),
70 'extra_base_field' => $this->randomString(),
75 $message = 'Module uninstallation fails as the module provides a base field which has content.';
76 $this->getModuleInstaller()->uninstall(['entity_test_extra']);
77 $this->fail($message);
79 catch (ModuleUninstallValidatorException $e) {
80 $this->pass($message);
81 $this->assertEqual($e->getMessage(), 'The following reasons prevent the modules from being uninstalled: There is data for the field extra_base_field on entity type Test entity');
84 // Verify uninstalling entity_test is not possible when there is content for
87 $this->assertModuleInstallUninstall('entity_test_extra');
88 $this->enableModules(['entity_test_extra']);
89 $this->entityDefinitionUpdateManager->applyUpdates();
90 $entity = $this->entityManager->getStorage('entity_test')->create([
91 'name' => $this->randomString(),
92 'extra_bundle_field' => $this->randomString(),
96 $this->getModuleInstaller()->uninstall(['entity_test_extra']);
97 $this->fail('Module uninstallation fails as the module provides a bundle field which has content.');
99 catch (ModuleUninstallValidatorException $e) {
100 $this->pass('Module uninstallation fails as the module provides a bundle field which has content.');
105 * Asserts the given module can be installed and uninstalled.
107 * @param string $module_name
108 * The module to install and uninstall.
110 protected function assertModuleInstallUninstall($module_name) {
111 // Install the module if it is not installed yet.
112 if (!\Drupal::moduleHandler()->moduleExists($module_name)) {
113 $this->enableModules([$module_name]);
115 $this->entityDefinitionUpdateManager->applyUpdates();
116 $this->assertTrue($this->getModuleHandler()->moduleExists($module_name), $module_name . ' module is enabled.');
117 $this->getModuleInstaller()->uninstall([$module_name]);
118 $this->entityDefinitionUpdateManager->applyUpdates();
119 $this->assertFalse($this->getModuleHandler()->moduleExists($module_name), $module_name . ' module is disabled.');
123 * Returns the ModuleHandler.
125 * @return \Drupal\Core\Extension\ModuleHandlerInterface
127 protected function getModuleHandler() {
128 return $this->container->get('module_handler');
132 * Returns the ModuleInstaller.
134 * @return \Drupal\Core\Extension\ModuleInstallerInterface
136 protected function getModuleInstaller() {
137 return $this->container->get('module_installer');