Chris@0: drupalLogin($this->drupalCreateUser(['synchronize configuration'])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests deleting field storages and fields as part of config import. Chris@0: */ Chris@0: public function testImportDeleteUninstall() { Chris@0: // Create a telephone field. Chris@0: $field_storage = FieldStorageConfig::create([ Chris@0: 'field_name' => 'field_tel', Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'telephone', Chris@0: ]); Chris@0: $field_storage->save(); Chris@0: FieldConfig::create([ Chris@0: 'field_storage' => $field_storage, Chris@0: 'bundle' => 'entity_test', Chris@0: ])->save(); Chris@0: Chris@0: // Create a text field. Chris@0: $date_field_storage = FieldStorageConfig::create([ Chris@0: 'field_name' => 'field_date', Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'datetime', Chris@0: ]); Chris@0: $date_field_storage->save(); Chris@0: FieldConfig::create([ Chris@0: 'field_storage' => $date_field_storage, Chris@0: 'bundle' => 'entity_test', Chris@0: ])->save(); Chris@0: Chris@0: // Create an entity which has values for the telephone and text field. Chris@0: $entity = EntityTest::create(); Chris@0: $value = '+0123456789'; Chris@0: $entity->field_tel = $value; Chris@0: $entity->field_date = time(); Chris@0: $entity->name->value = $this->randomMachineName(); Chris@0: $entity->save(); Chris@0: Chris@0: // Delete the text field before exporting configuration so that we can test Chris@0: // that deleted fields that are provided by modules that will be uninstalled Chris@0: // are also purged and that the UI message includes such fields. Chris@0: $date_field_storage->delete(); Chris@0: Chris@0: // Verify entity has been created properly. Chris@0: $id = $entity->id(); Chris@0: $entity = EntityTest::load($id); Chris@0: $this->assertEqual($entity->field_tel->value, $value); Chris@0: $this->assertEqual($entity->field_tel[0]->value, $value); Chris@0: Chris@0: $active = $this->container->get('config.storage'); Chris@0: $sync = $this->container->get('config.storage.sync'); Chris@0: $this->copyConfig($active, $sync); Chris@0: Chris@0: // Stage uninstall of the Telephone module. Chris@0: $core_extension = $this->config('core.extension')->get(); Chris@0: unset($core_extension['module']['telephone']); Chris@0: $sync->write('core.extension', $core_extension); Chris@0: Chris@0: // Stage the field deletion Chris@0: $sync->delete('field.storage.entity_test.field_tel'); Chris@0: $sync->delete('field.field.entity_test.entity_test.field_tel'); Chris@0: $this->drupalGet('admin/config/development/configuration'); Chris@0: // Test that the message for one field being purged during a configuration Chris@0: // synchronization is correct. Chris@0: $this->assertText('This synchronization will delete data from the field entity_test.field_tel.'); Chris@0: Chris@0: // Stage an uninstall of the datetime module to test the message for Chris@0: // multiple fields. Chris@0: unset($core_extension['module']['datetime']); Chris@0: $sync->write('core.extension', $core_extension); Chris@0: Chris@0: $this->drupalGet('admin/config/development/configuration'); Chris@0: $this->assertText('This synchronization will delete data from the fields: entity_test.field_tel, entity_test.field_date.'); Chris@0: Chris@0: // This will purge all the data, delete the field and uninstall the Chris@0: // Telephone and Text modules. Chris@0: $this->drupalPostForm(NULL, [], t('Import all')); Chris@0: $this->assertNoText('Field data will be deleted by this synchronization.'); Chris@0: $this->rebuildContainer(); Chris@0: $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone')); Chris@0: $this->assertFalse(\Drupal::entityManager()->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The telephone field has been deleted by the configuration synchronization'); Chris@0: $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: []; Chris@0: $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Telephone field has been completed removed from the system.'); Chris@0: $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Text field has been completed removed from the system.'); Chris@0: } Chris@0: Chris@0: }