You are here

public function FieldImportDeleteUninstallTest::testImportAlreadyDeletedUninstall in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldImportDeleteUninstallTest.php \Drupal\field\Tests\FieldImportDeleteUninstallTest::testImportAlreadyDeletedUninstall()

Tests purging already deleted field storages and fields during a config import.

File

core/modules/field/src/Tests/FieldImportDeleteUninstallTest.php, line 114
Contains \Drupal\field\Tests\FieldImportDeleteUninstallTest.

Class

FieldImportDeleteUninstallTest
Delete field storages and fields during config synchronization and uninstall module that provides the field type.

Namespace

Drupal\field\Tests

Code

public function testImportAlreadyDeletedUninstall() {

  // Create a telephone field for validation.
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => 'field_test',
    'entity_type' => 'entity_test',
    'type' => 'telephone',
  ));
  $field_storage
    ->save();
  $field_storage_uuid = $field_storage
    ->uuid();
  entity_create('field_config', array(
    '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 = entity_create('entity_test');
    $value = '+0123456789';
    $entity->field_test = $value;
    $entity->name->value = $this
      ->randomMachineName();
    $entity
      ->save();

    // Verify entity has been created properly.
    $id = $entity
      ->id();
    $entity = entity_load('entity_test', $id);
    $this
      ->assertEqual($entity->field_test->value, $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') ?: array();
  $this
    ->assertTrue(isset($deleted_storages[$field_storage_uuid]), 'Field has been deleted and needs purging before configuration synchronization.');
  $steps = $this
    ->configImporter()
    ->initialize();
  $this
    ->assertIdentical($steps[0], array(
    '\\Drupal\\field\\ConfigImporterFieldPurger',
    'process',
  ), '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') ?: array();
  $this
    ->assertFalse(isset($deleted_storages[$field_storage_uuid]), 'Field has been completed removed from the system.');
}