function FieldImportDeleteUninstallTest::testImportAlreadyDeletedUninstall

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldImportDeleteUninstallTest.php \Drupal\Tests\field\Kernel\FieldImportDeleteUninstallTest::testImportAlreadyDeletedUninstall()
  2. 8.9.x core/modules/field/tests/src/Kernel/FieldImportDeleteUninstallTest.php \Drupal\Tests\field\Kernel\FieldImportDeleteUninstallTest::testImportAlreadyDeletedUninstall()
  3. 10 core/modules/field/tests/src/Kernel/FieldImportDeleteUninstallTest.php \Drupal\Tests\field\Kernel\FieldImportDeleteUninstallTest::testImportAlreadyDeletedUninstall()

Tests purging previously deleted fields and storages in config import.

File

core/modules/field/tests/src/Kernel/FieldImportDeleteUninstallTest.php, line 114

Class

FieldImportDeleteUninstallTest
Tests field storages and fields deletion during config synchronization.

Namespace

Drupal\Tests\field\Kernel

Code

public function testImportAlreadyDeletedUninstall() : void {
    // Create a telephone field for validation.
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_test',
        'entity_type' => 'entity_test',
        'type' => 'telephone',
    ]);
    $field_storage->save();
    $field_storage_uuid = $field_storage->uuid();
    FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'entity_test',
    ])->save();
    // Create 12 entities to ensure that the purging works as expected.
    for ($i = 0; $i < 12; $i++) {
        $entity = EntityTest::create();
        $value = '+0123456789';
        $entity->field_test = $value;
        $entity->name->value = $this->randomMachineName();
        $entity->save();
        // Verify entity has been created properly.
        $id = $entity->id();
        $entity = EntityTest::load($id);
        $this->assertEquals($value, $entity->field_test->value);
    }
    // Delete the field.
    $field_storage->delete();
    $active = $this->container
        ->get('config.storage');
    $sync = $this->container
        ->get('config.storage.sync');
    $this->copyConfig($active, $sync);
    // Stage uninstall of the Telephone module.
    $core_extension = $this->config('core.extension')
        ->get();
    unset($core_extension['module']['telephone']);
    $sync->write('core.extension', $core_extension);
    $deleted_storages = \Drupal::state()->get('field.storage.deleted', []);
    $this->assertTrue(isset($deleted_storages[$field_storage_uuid]), 'Field has been deleted and needs purging before configuration synchronization.');
    $steps = $this->configImporter()
        ->initialize();
    $this->assertSame([
        '\\Drupal\\field\\ConfigImporterFieldPurger',
        'process',
    ], $steps[0], 'The additional process configuration synchronization step has been added.');
    // This will purge all the data, delete the field and uninstall the
    // Telephone module.
    $this->configImporter()
        ->import();
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
    $deleted_storages = \Drupal::state()->get('field.storage.deleted', []);
    $this->assertFalse(isset($deleted_storages[$field_storage_uuid]), 'Field has been completed removed from the system.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.