FieldModuleUninstallValidatorTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Drupal\KernelTests\Core\Field;
  3. use Drupal\Core\Extension\ModuleUninstallValidatorException;
  4. use Drupal\Core\Field\BaseFieldDefinition;
  5. use Drupal\entity_test\FieldStorageDefinition;
  6. use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
  7. /**
  8. * Tests FieldModuleUninstallValidator functionality.
  9. *
  10. * @group Field
  11. */
  12. class FieldModuleUninstallValidatorTest extends EntityKernelTestBase {
  13. /**
  14. * The entity definition update manager.
  15. *
  16. * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
  17. */
  18. protected $entityDefinitionUpdateManager;
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->installSchema('user', 'users_data');
  25. $this->entityDefinitionUpdateManager = $this->container->get('entity.definition_update_manager');
  26. // Setup some fields for entity_test_extra to create.
  27. $definitions['extra_base_field'] = BaseFieldDefinition::create('string')
  28. ->setName('extra_base_field')
  29. ->setTargetEntityTypeId('entity_test')
  30. ->setTargetBundle('entity_test');
  31. $this->state->set('entity_test.additional_base_field_definitions', $definitions);
  32. $definitions['extra_bundle_field'] = FieldStorageDefinition::create('string')
  33. ->setName('extra_bundle_field')
  34. ->setTargetEntityTypeId('entity_test')
  35. ->setTargetBundle('entity_test');
  36. $this->state->set('entity_test.additional_field_storage_definitions', $definitions);
  37. $this->state->set('entity_test.entity_test.additional_bundle_field_definitions', $definitions);
  38. $this->entityManager->clearCachedDefinitions();
  39. }
  40. /**
  41. * Tests uninstall entity_test module with and without content for the field.
  42. */
  43. public function testUninstallingModule() {
  44. // Test uninstall works fine without content.
  45. $this->assertModuleInstallUninstall('entity_test_extra');
  46. // Test uninstalling works fine with content having no field values.
  47. $entity = $this->entityManager->getStorage('entity_test')->create([
  48. 'name' => $this->randomString(),
  49. ]);
  50. $entity->save();
  51. $this->assertModuleInstallUninstall('entity_test_extra');
  52. $entity->delete();
  53. // Verify uninstall works fine without content again.
  54. $this->assertModuleInstallUninstall('entity_test_extra');
  55. // Verify uninstalling entity_test is not possible when there is content for
  56. // the base field.
  57. $this->enableModules(['entity_test_extra']);
  58. $this->entityDefinitionUpdateManager->applyUpdates();
  59. $entity = $this->entityManager->getStorage('entity_test')->create([
  60. 'name' => $this->randomString(),
  61. 'extra_base_field' => $this->randomString(),
  62. ]);
  63. $entity->save();
  64. try {
  65. $message = 'Module uninstallation fails as the module provides a base field which has content.';
  66. $this->getModuleInstaller()->uninstall(['entity_test_extra']);
  67. $this->fail($message);
  68. }
  69. catch (ModuleUninstallValidatorException $e) {
  70. $this->pass($message);
  71. $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');
  72. }
  73. // Verify uninstalling entity_test is not possible when there is content for
  74. // the bundle field.
  75. $entity->delete();
  76. $this->assertModuleInstallUninstall('entity_test_extra');
  77. $this->enableModules(['entity_test_extra']);
  78. $this->entityDefinitionUpdateManager->applyUpdates();
  79. $entity = $this->entityManager->getStorage('entity_test')->create([
  80. 'name' => $this->randomString(),
  81. 'extra_bundle_field' => $this->randomString(),
  82. ]);
  83. $entity->save();
  84. try {
  85. $this->getModuleInstaller()->uninstall(['entity_test_extra']);
  86. $this->fail('Module uninstallation fails as the module provides a bundle field which has content.');
  87. }
  88. catch (ModuleUninstallValidatorException $e) {
  89. $this->pass('Module uninstallation fails as the module provides a bundle field which has content.');
  90. }
  91. }
  92. /**
  93. * Asserts the given module can be installed and uninstalled.
  94. *
  95. * @param string $module_name
  96. * The module to install and uninstall.
  97. */
  98. protected function assertModuleInstallUninstall($module_name) {
  99. // Install the module if it is not installed yet.
  100. if (!\Drupal::moduleHandler()->moduleExists($module_name)) {
  101. $this->enableModules([$module_name]);
  102. }
  103. $this->entityDefinitionUpdateManager->applyUpdates();
  104. $this->assertTrue($this->getModuleHandler()->moduleExists($module_name), $module_name . ' module is enabled.');
  105. $this->getModuleInstaller()->uninstall([$module_name]);
  106. $this->entityDefinitionUpdateManager->applyUpdates();
  107. $this->assertFalse($this->getModuleHandler()->moduleExists($module_name), $module_name . ' module is disabled.');
  108. }
  109. /**
  110. * Returns the ModuleHandler.
  111. *
  112. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  113. */
  114. protected function getModuleHandler() {
  115. return $this->container->get('module_handler');
  116. }
  117. /**
  118. * Returns the ModuleInstaller.
  119. *
  120. * @return \Drupal\Core\Extension\ModuleInstallerInterface
  121. */
  122. protected function getModuleInstaller() {
  123. return $this->container->get('module_installer');
  124. }
  125. }