3 namespace Drupal\Tests\field\Kernel;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
10 * Delete field storages and fields during config synchronization and uninstall
11 * module that provides the field type.
14 * @see \Drupal\field\ConfigImporterFieldPurger
15 * @see field_config_import_steps_alter()
17 class FieldImportDeleteUninstallTest extends FieldKernelTestBase {
24 public static $modules = ['telephone'];
26 protected function setUp() {
28 // Module uninstall requires users_data tables.
29 // @see drupal_flush_all_caches()
30 // @see user_modules_uninstalled()
31 $this->installSchema('user', ['users_data']);
35 * Tests deleting field storages and fields as part of config import.
37 public function testImportDeleteUninstall() {
38 // Create a field to delete to prove that
39 // \Drupal\field\ConfigImporterFieldPurger does not purge fields that are
40 // not related to the configuration synchronization.
41 $unrelated_field_storage = FieldStorageConfig::create([
42 'field_name' => 'field_int',
43 'entity_type' => 'entity_test',
46 $unrelated_field_storage->save();
48 'field_storage' => $unrelated_field_storage,
49 'bundle' => 'entity_test',
52 // Create a telephone field for validation.
53 $field_storage = FieldStorageConfig::create([
54 'field_name' => 'field_test',
55 'entity_type' => 'entity_test',
56 'type' => 'telephone',
58 $field_storage->save();
60 'field_storage' => $field_storage,
61 'bundle' => 'entity_test',
64 $entity = EntityTest::create();
65 $value = '+0123456789';
66 $entity->field_test = $value;
67 $entity->field_int = '99';
68 $entity->name->value = $this->randomMachineName();
71 // Verify entity has been created properly.
73 $entity = EntityTest::load($id);
74 $this->assertEqual($entity->field_test->value, $value);
75 $this->assertEqual($entity->field_test[0]->value, $value);
76 $this->assertEqual($entity->field_int->value, '99');
78 // Delete unrelated field before copying configuration and running the
80 $unrelated_field_storage->delete();
82 $active = $this->container->get('config.storage');
83 $sync = $this->container->get('config.storage.sync');
84 $this->copyConfig($active, $sync);
86 // Stage uninstall of the Telephone module.
87 $core_extension = $this->config('core.extension')->get();
88 unset($core_extension['module']['telephone']);
89 $sync->write('core.extension', $core_extension);
91 // Stage the field deletion
92 $sync->delete('field.storage.entity_test.field_test');
93 $sync->delete('field.field.entity_test.entity_test.field_test');
95 $steps = $this->configImporter()->initialize();
96 $this->assertIdentical($steps[0], ['\Drupal\field\ConfigImporterFieldPurger', 'process'], 'The additional process configuration synchronization step has been added.');
98 // This will purge all the data, delete the field and uninstall the
100 $this->configImporter()->import();
102 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
103 $this->assertFalse(\Drupal::entityManager()->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The test field has been deleted by the configuration synchronization');
104 $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
105 $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Telephone field has been completed removed from the system.');
106 $this->assertTrue(isset($deleted_storages[$unrelated_field_storage->uuid()]), 'Unrelated field not purged by configuration synchronization.');
110 * Tests purging already deleted field storages and fields during a config
113 public function testImportAlreadyDeletedUninstall() {
114 // Create a telephone field for validation.
115 $field_storage = FieldStorageConfig::create([
116 'field_name' => 'field_test',
117 'entity_type' => 'entity_test',
118 'type' => 'telephone',
120 $field_storage->save();
121 $field_storage_uuid = $field_storage->uuid();
122 FieldConfig::create([
123 'field_storage' => $field_storage,
124 'bundle' => 'entity_test',
127 // Create 12 entities to ensure that the purging works as expected.
128 for ($i = 0; $i < 12; $i++) {
129 $entity = EntityTest::create();
130 $value = '+0123456789';
131 $entity->field_test = $value;
132 $entity->name->value = $this->randomMachineName();
135 // Verify entity has been created properly.
137 $entity = EntityTest::load($id);
138 $this->assertEqual($entity->field_test->value, $value);
142 $field_storage->delete();
144 $active = $this->container->get('config.storage');
145 $sync = $this->container->get('config.storage.sync');
146 $this->copyConfig($active, $sync);
148 // Stage uninstall of the Telephone module.
149 $core_extension = $this->config('core.extension')->get();
150 unset($core_extension['module']['telephone']);
151 $sync->write('core.extension', $core_extension);
153 $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
154 $this->assertTrue(isset($deleted_storages[$field_storage_uuid]), 'Field has been deleted and needs purging before configuration synchronization.');
156 $steps = $this->configImporter()->initialize();
157 $this->assertIdentical($steps[0], ['\Drupal\field\ConfigImporterFieldPurger', 'process'], 'The additional process configuration synchronization step has been added.');
159 // This will purge all the data, delete the field and uninstall the
161 $this->configImporter()->import();
163 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
164 $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
165 $this->assertFalse(isset($deleted_storages[$field_storage_uuid]), 'Field has been completed removed from the system.');