View source
<?php
namespace Drupal\field\Tests;
class FieldImportDeleteUninstallTest extends FieldUnitTestBase {
public static $modules = array(
'telephone',
);
protected function setUp() {
parent::setUp();
$this
->installSchema('user', array(
'users_data',
));
}
public function testImportDeleteUninstall() {
$unrelated_field_storage = entity_create('field_storage_config', array(
'field_name' => 'field_int',
'entity_type' => 'entity_test',
'type' => 'integer',
));
$unrelated_field_storage
->save();
entity_create('field_config', array(
'field_storage' => $unrelated_field_storage,
'bundle' => 'entity_test',
))
->save();
$field_storage = entity_create('field_storage_config', array(
'field_name' => 'field_test',
'entity_type' => 'entity_test',
'type' => 'telephone',
));
$field_storage
->save();
entity_create('field_config', array(
'field_storage' => $field_storage,
'bundle' => 'entity_test',
))
->save();
$entity = entity_create('entity_test');
$value = '+0123456789';
$entity->field_test = $value;
$entity->field_int = '99';
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$entity = entity_load('entity_test', $id);
$this
->assertEqual($entity->field_test->value, $value);
$this
->assertEqual($entity->field_test[0]->value, $value);
$this
->assertEqual($entity->field_int->value, '99');
$unrelated_field_storage
->delete();
$active = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$this
->copyConfig($active, $sync);
$core_extension = $this
->config('core.extension')
->get();
unset($core_extension['module']['telephone']);
$sync
->write('core.extension', $core_extension);
$sync
->delete('field.storage.entity_test.field_test');
$sync
->delete('field.field.entity_test.entity_test.field_test');
$steps = $this
->configImporter()
->initialize();
$this
->assertIdentical($steps[0], array(
'\\Drupal\\field\\ConfigImporterFieldPurger',
'process',
), 'The additional process configuration synchronization step has been added.');
$this
->configImporter()
->import();
$this
->assertFalse(\Drupal::moduleHandler()
->moduleExists('telephone'));
$this
->assertFalse(\Drupal::entityManager()
->loadEntityByUuid('field_storage_config', $field_storage
->uuid()), 'The test field has been deleted by the configuration synchronization');
$deleted_storages = \Drupal::state()
->get('field.storage.deleted') ?: array();
$this
->assertFalse(isset($deleted_storages[$field_storage
->uuid()]), 'Telephone field has been completed removed from the system.');
$this
->assertTrue(isset($deleted_storages[$unrelated_field_storage
->uuid()]), 'Unrelated field not purged by configuration synchronization.');
}
public function testImportAlreadyDeletedUninstall() {
$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();
for ($i = 0; $i < 12; $i++) {
$entity = entity_create('entity_test');
$value = '+0123456789';
$entity->field_test = $value;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$entity = entity_load('entity_test', $id);
$this
->assertEqual($entity->field_test->value, $value);
}
$field_storage
->delete();
$active = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$this
->copyConfig($active, $sync);
$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
->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.');
}
}