Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Field / FieldModuleUninstallValidatorTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Field;
4
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;
9
10 /**
11  * Tests FieldModuleUninstallValidator functionality.
12  *
13  * @group Field
14  */
15 class FieldModuleUninstallValidatorTest extends EntityKernelTestBase {
16
17   /**
18    * The entity definition update manager.
19    *
20    * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
21    */
22   protected $entityDefinitionUpdateManager;
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29     $this->installSchema('user', 'users_data');
30     $this->entityDefinitionUpdateManager = $this->container->get('entity.definition_update_manager');
31
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();
45   }
46
47   /**
48    * Tests uninstall entity_test module with and without content for the field.
49    */
50   public function testUninstallingModule() {
51     // Test uninstall works fine without content.
52     $this->assertModuleInstallUninstall('entity_test_extra');
53
54     // Test uninstalling works fine with content having no field values.
55     $entity = $this->entityManager->getStorage('entity_test')->create([
56       'name' => $this->randomString(),
57     ]);
58     $entity->save();
59     $this->assertModuleInstallUninstall('entity_test_extra');
60     $entity->delete();
61
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
65     // the base field.
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(),
71     ]);
72     $entity->save();
73
74     try {
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);
78     }
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');
82     }
83
84     // Verify uninstalling entity_test is not possible when there is content for
85     // the bundle field.
86     $entity->delete();
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(),
93     ]);
94     $entity->save();
95     try {
96       $this->getModuleInstaller()->uninstall(['entity_test_extra']);
97       $this->fail('Module uninstallation fails as the module provides a bundle field which has content.');
98     }
99     catch (ModuleUninstallValidatorException $e) {
100       $this->pass('Module uninstallation fails as the module provides a bundle field which has content.');
101     }
102   }
103
104   /**
105    * Asserts the given module can be installed and uninstalled.
106    *
107    * @param string $module_name
108    *   The module to install and uninstall.
109    */
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]);
114     }
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.');
120   }
121
122   /**
123    * Returns the ModuleHandler.
124    *
125    * @return \Drupal\Core\Extension\ModuleHandlerInterface
126    */
127   protected function getModuleHandler() {
128     return $this->container->get('module_handler');
129   }
130
131   /**
132    * Returns the ModuleInstaller.
133    *
134    * @return \Drupal\Core\Extension\ModuleInstallerInterface
135    */
136   protected function getModuleInstaller() {
137     return $this->container->get('module_installer');
138   }
139
140 }