3 namespace Drupal\field\Tests;
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 through the UI.
14 * @see \Drupal\field\ConfigImporterFieldPurger
15 * @see field_config_import_steps_alter()
16 * @see field_form_config_admin_import_form_alter()
18 class FieldImportDeleteUninstallUiTest extends FieldTestBase {
25 public static $modules = ['entity_test', 'telephone', 'config', 'filter', 'datetime'];
27 protected function setUp() {
30 $this->drupalLogin($this->drupalCreateUser(['synchronize configuration']));
34 * Tests deleting field storages and fields as part of config import.
36 public function testImportDeleteUninstall() {
37 // Create a telephone field.
38 $field_storage = FieldStorageConfig::create([
39 'field_name' => 'field_tel',
40 'entity_type' => 'entity_test',
41 'type' => 'telephone',
43 $field_storage->save();
45 'field_storage' => $field_storage,
46 'bundle' => 'entity_test',
49 // Create a text field.
50 $date_field_storage = FieldStorageConfig::create([
51 'field_name' => 'field_date',
52 'entity_type' => 'entity_test',
55 $date_field_storage->save();
57 'field_storage' => $date_field_storage,
58 'bundle' => 'entity_test',
61 // Create an entity which has values for the telephone and text field.
62 $entity = EntityTest::create();
63 $value = '+0123456789';
64 $entity->field_tel = $value;
65 $entity->field_date = time();
66 $entity->name->value = $this->randomMachineName();
69 // Delete the text field before exporting configuration so that we can test
70 // that deleted fields that are provided by modules that will be uninstalled
71 // are also purged and that the UI message includes such fields.
72 $date_field_storage->delete();
74 // Verify entity has been created properly.
76 $entity = EntityTest::load($id);
77 $this->assertEqual($entity->field_tel->value, $value);
78 $this->assertEqual($entity->field_tel[0]->value, $value);
80 $active = $this->container->get('config.storage');
81 $sync = $this->container->get('config.storage.sync');
82 $this->copyConfig($active, $sync);
84 // Stage uninstall of the Telephone module.
85 $core_extension = $this->config('core.extension')->get();
86 unset($core_extension['module']['telephone']);
87 $sync->write('core.extension', $core_extension);
89 // Stage the field deletion
90 $sync->delete('field.storage.entity_test.field_tel');
91 $sync->delete('field.field.entity_test.entity_test.field_tel');
92 $this->drupalGet('admin/config/development/configuration');
93 // Test that the message for one field being purged during a configuration
94 // synchronization is correct.
95 $this->assertText('This synchronization will delete data from the field entity_test.field_tel.');
97 // Stage an uninstall of the datetime module to test the message for
99 unset($core_extension['module']['datetime']);
100 $sync->write('core.extension', $core_extension);
102 $this->drupalGet('admin/config/development/configuration');
103 $this->assertText('This synchronization will delete data from the fields: entity_test.field_tel, entity_test.field_date.');
105 // This will purge all the data, delete the field and uninstall the
106 // Telephone and Text modules.
107 $this->drupalPostForm(NULL, [], t('Import all'));
108 $this->assertNoText('Field data will be deleted by this synchronization.');
109 $this->rebuildContainer();
110 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
111 $this->assertFalse(\Drupal::entityManager()->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The telephone field has been deleted by the configuration synchronization');
112 $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: [];
113 $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Telephone field has been completed removed from the system.');
114 $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Text field has been completed removed from the system.');