View source
<?php
namespace Drupal\field\Tests;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class FieldImportDeleteTest extends FieldUnitTestBase {
public static $modules = array(
'field_test_config',
);
public function testImportDelete() {
$this
->installConfig([
'field_test_config',
]);
$field_name = 'field_test_import';
$field_storage_id = "entity_test.{$field_name}";
$field_name_2 = 'field_test_import_2';
$field_storage_id_2 = "entity_test.{$field_name_2}";
$field_id = "entity_test.entity_test.{$field_name}";
$field_id_2a = "entity_test.entity_test.{$field_name_2}";
$field_id_2b = "entity_test.test_bundle.{$field_name_2}";
$field_storage_config_name = "field.storage.{$field_storage_id}";
$field_storage_config_name_2 = "field.storage.{$field_storage_id_2}";
$field_config_name = "field.field.{$field_id}";
$field_config_name_2a = "field.field.{$field_id_2a}";
$field_config_name_2b = "field.field.{$field_id_2b}";
entity_test_create_bundle('test_bundle');
$field_storage_uuid = FieldStorageConfig::load($field_storage_id)
->uuid();
$field_storage_uuid_2 = FieldStorageConfig::load($field_storage_id_2)
->uuid();
$active = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$this
->copyConfig($active, $sync);
$this
->assertTrue($sync
->delete($field_storage_config_name), SafeMarkup::format('Deleted field storage: @field_storage', array(
'@field_storage' => $field_storage_config_name,
)));
$this
->assertTrue($sync
->delete($field_storage_config_name_2), SafeMarkup::format('Deleted field storage: @field_storage', array(
'@field_storage' => $field_storage_config_name_2,
)));
$this
->assertTrue($sync
->delete($field_config_name), SafeMarkup::format('Deleted field: @field', array(
'@field' => $field_config_name,
)));
$this
->assertTrue($sync
->delete($field_config_name_2a), SafeMarkup::format('Deleted field: @field', array(
'@field' => $field_config_name_2a,
)));
$this
->assertTrue($sync
->delete($field_config_name_2b), SafeMarkup::format('Deleted field: @field', array(
'@field' => $field_config_name_2b,
)));
$deletes = $this
->configImporter()
->getUnprocessedConfiguration('delete');
$this
->assertEqual(count($deletes), 5, 'Importing configuration will delete 3 fields and 2 field storages.');
$this
->configImporter()
->import();
\Drupal::entityManager()
->getStorage('field_storage_config')
->resetCache(array(
$field_storage_id,
));
$field_storage = FieldStorageConfig::load($field_storage_id);
$this
->assertFalse($field_storage, 'The field storage was deleted.');
\Drupal::entityManager()
->getStorage('field_storage_config')
->resetCache(array(
$field_storage_id_2,
));
$field_storage_2 = FieldStorageConfig::load($field_storage_id_2);
$this
->assertFalse($field_storage_2, 'The second field storage was deleted.');
\Drupal::entityManager()
->getStorage('field_config')
->resetCache(array(
$field_id,
));
$field = FieldConfig::load($field_id);
$this
->assertFalse($field, 'The field was deleted.');
\Drupal::entityManager()
->getStorage('field_config')
->resetCache(array(
$field_id_2a,
));
$field_2a = FieldConfig::load($field_id_2a);
$this
->assertFalse($field_2a, 'The second field on test bundle was deleted.');
\Drupal::entityManager()
->getStorage('field_config')
->resetCache(array(
$field_id_2b,
));
$field_2b = FieldConfig::load($field_id_2b);
$this
->assertFalse($field_2b, 'The second field on test bundle 2 was deleted.');
$active = $this->container
->get('config.storage');
$this
->assertIdentical($active
->listAll($field_storage_config_name), array());
$this
->assertIdentical($active
->listAll($field_storage_config_name_2), array());
$this
->assertIdentical($active
->listAll($field_config_name), array());
$this
->assertIdentical($active
->listAll($field_config_name_2a), array());
$this
->assertIdentical($active
->listAll($field_config_name_2b), array());
$deleted_storages = \Drupal::state()
->get('field.storage.deleted') ?: array();
$this
->assertTrue(isset($deleted_storages[$field_storage_uuid]));
$this
->assertTrue(isset($deleted_storages[$field_storage_uuid_2]));
field_purge_batch(10);
$deleted_storages = \Drupal::state()
->get('field.storage.deleted') ?: array();
$this
->assertTrue(empty($deleted_storages), 'Fields are deleted');
}
}