View source
<?php
namespace Drupal\config\Tests;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
class ConfigDependencyTest extends EntityUnitTestBase {
public static $modules = array(
'config_test',
'entity_test',
'user',
);
public function testNonEntity() {
$this
->installConfig(array(
'system',
));
$config_manager = \Drupal::service('config.manager');
$dependents = $config_manager
->findConfigEntityDependents('module', array(
'system',
));
$this
->assertTrue(isset($dependents['system.site']), 'Simple configuration system.site has a UUID key even though it is not a configuration entity and therefore is found when looking for dependencies of the System module.');
$config_manager
->findConfigEntityDependentsAsEntities('module', array(
'system',
));
}
public function testDependencyManagement() {
$config_manager = \Drupal::service('config.manager');
$storage = $this->container
->get('entity.manager')
->getStorage('config_test');
$entity1 = $storage
->create(array(
'id' => 'entity1',
'dependencies' => array(
'enforced' => array(
'module' => array(
'node',
),
),
),
));
$entity1
->save();
$dependents = $config_manager
->findConfigEntityDependents('module', array(
'node',
));
$this
->assertTrue(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 has a dependency on the Node module.');
$dependents = $config_manager
->findConfigEntityDependents('module', array(
'config_test',
));
$this
->assertTrue(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 has a dependency on the config_test module.');
$dependents = $config_manager
->findConfigEntityDependents('module', array(
'views',
));
$this
->assertFalse(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 does not have a dependency on the Views module.');
$raw_config = $this
->config('config_test.dynamic.entity1');
$root_module_dependencies = $raw_config
->get('dependencies.module');
$this
->assertTrue(empty($root_module_dependencies), 'Node module is not written to the root dependencies array as it is enforced.');
$entity2 = $storage
->create(array(
'id' => 'entity2',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity1
->getConfigDependencyName(),
),
),
),
));
$entity2
->save();
$entity3 = $storage
->create(array(
'id' => 'entity3',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity2
->getConfigDependencyName(),
),
),
),
));
$entity3
->save();
$entity4 = $storage
->create(array(
'id' => 'entity4',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity3
->getConfigDependencyName(),
),
),
),
));
$entity4
->save();
$dependents = $config_manager
->findConfigEntityDependents('config', array(
$entity1
->getConfigDependencyName(),
));
$this
->assertFalse(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 does not have a dependency on itself.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity2']), 'config_test.dynamic.entity2 has a dependency on config_test.dynamic.entity1.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity3']), 'config_test.dynamic.entity3 has a dependency on config_test.dynamic.entity1.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity4']), 'config_test.dynamic.entity4 has a dependency on config_test.dynamic.entity1.');
$dependents = $config_manager
->findConfigEntityDependentsAsEntities('config', array(
$entity2
->getConfigDependencyName(),
));
$dependent_ids = $this
->getDependentIds($dependents);
$this
->assertFalse(in_array('config_test:entity1', $dependent_ids), 'config_test.dynamic.entity1 does not have a dependency on config_test.dynamic.entity1.');
$this
->assertFalse(in_array('config_test:entity2', $dependent_ids), 'config_test.dynamic.entity2 does not have a dependency on itself.');
$this
->assertTrue(in_array('config_test:entity3', $dependent_ids), 'config_test.dynamic.entity3 has a dependency on config_test.dynamic.entity2.');
$this
->assertTrue(in_array('config_test:entity4', $dependent_ids), 'config_test.dynamic.entity4 has a dependency on config_test.dynamic.entity2.');
$dependents = $config_manager
->findConfigEntityDependents('module', array(
'node',
));
$this
->assertTrue(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 has a dependency on the Node module.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity2']), 'config_test.dynamic.entity2 has a dependency on the Node module.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity3']), 'config_test.dynamic.entity3 has a dependency on the Node module.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity4']), 'config_test.dynamic.entity4 has a dependency on the Node module.');
$entity1
->setEnforcedDependencies([])
->save();
$entity3
->setEnforcedDependencies([
'module' => [
'node',
],
'config' => [
$entity2
->getConfigDependencyName(),
],
])
->save();
$dependents = $config_manager
->findConfigEntityDependents('module', array(
'node',
));
$this
->assertFalse(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 does not have a dependency on the Node module.');
$this
->assertFalse(isset($dependents['config_test.dynamic.entity2']), 'config_test.dynamic.entity2 does not have a dependency on the Node module.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity3']), 'config_test.dynamic.entity3 has a dependency on the Node module.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity4']), 'config_test.dynamic.entity4 has a dependency on the Node module.');
$entity_test = entity_create('entity_test', array(
'name' => $this
->randomString(),
'type' => 'entity_test',
));
$entity_test
->save();
$entity2
->setEnforcedDependencies([
'config' => [
$entity1
->getConfigDependencyName(),
],
'content' => [
$entity_test
->getConfigDependencyName(),
],
])
->save();
$dependents = $config_manager
->findConfigEntityDependents('content', array(
$entity_test
->getConfigDependencyName(),
));
$this
->assertFalse(isset($dependents['config_test.dynamic.entity1']), 'config_test.dynamic.entity1 does not have a dependency on the content entity.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity2']), 'config_test.dynamic.entity2 has a dependency on the content entity.');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity3']), 'config_test.dynamic.entity3 has a dependency on the content entity (via entity2).');
$this
->assertTrue(isset($dependents['config_test.dynamic.entity4']), 'config_test.dynamic.entity4 has a dependency on the content entity (via entity3).');
$alt_storage = $this->container
->get('entity.manager')
->getStorage('config_query_test');
$alt_storage
->create(array(
'id' => 'entity1',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity1
->getConfigDependencyName(),
),
),
),
))
->save();
$alt_storage
->create(array(
'id' => 'entity2',
'dependencies' => array(
'enforced' => array(
'module' => array(
'views',
),
),
),
))
->save();
$dependents = $config_manager
->findConfigEntityDependentsAsEntities('config', array(
$entity1
->getConfigDependencyName(),
));
$dependent_ids = $this
->getDependentIds($dependents);
$this
->assertFalse(in_array('config_test:entity1', $dependent_ids), 'config_test.dynamic.entity1 does not have a dependency on itself.');
$this
->assertTrue(in_array('config_test:entity2', $dependent_ids), 'config_test.dynamic.entity2 has a dependency on config_test.dynamic.entity1.');
$this
->assertTrue(in_array('config_test:entity3', $dependent_ids), 'config_test.dynamic.entity3 has a dependency on config_test.dynamic.entity1.');
$this
->assertTrue(in_array('config_test:entity4', $dependent_ids), 'config_test.dynamic.entity4 has a dependency on config_test.dynamic.entity1.');
$this
->assertTrue(in_array('config_query_test:entity1', $dependent_ids), 'config_query_test.dynamic.entity1 has a dependency on config_test.dynamic.entity1.');
$this
->assertFalse(in_array('config_query_test:entity2', $dependent_ids), 'config_query_test.dynamic.entity2 does not have a dependency on config_test.dynamic.entity1.');
$dependents = $config_manager
->findConfigEntityDependentsAsEntities('module', array(
'node',
'views',
));
$dependent_ids = $this
->getDependentIds($dependents);
$this
->assertFalse(in_array('config_test:entity1', $dependent_ids), 'config_test.dynamic.entity1 does not have a dependency on Views or Node.');
$this
->assertFalse(in_array('config_test:entity2', $dependent_ids), 'config_test.dynamic.entity2 does not have a dependency on Views or Node.');
$this
->assertTrue(in_array('config_test:entity3', $dependent_ids), 'config_test.dynamic.entity3 has a dependency on Views or Node.');
$this
->assertTrue(in_array('config_test:entity4', $dependent_ids), 'config_test.dynamic.entity4 has a dependency on Views or Node.');
$this
->assertFalse(in_array('config_query_test:entity1', $dependent_ids), 'config_test.query.entity1 does not have a dependency on Views or Node.');
$this
->assertTrue(in_array('config_query_test:entity2', $dependent_ids), 'config_test.query.entity2 has a dependency on Views or Node.');
$dependents = $config_manager
->findConfigEntityDependentsAsEntities('module', array(
'config_test',
));
$dependent_ids = $this
->getDependentIds($dependents);
$this
->assertTrue(in_array('config_test:entity1', $dependent_ids), 'config_test.dynamic.entity1 has a dependency on config_test module.');
$this
->assertTrue(in_array('config_test:entity2', $dependent_ids), 'config_test.dynamic.entity2 has a dependency on config_test module.');
$this
->assertTrue(in_array('config_test:entity3', $dependent_ids), 'config_test.dynamic.entity3 has a dependency on config_test module.');
$this
->assertTrue(in_array('config_test:entity4', $dependent_ids), 'config_test.dynamic.entity4 has a dependency on config_test module.');
$this
->assertTrue(in_array('config_query_test:entity1', $dependent_ids), 'config_test.query.entity1 has a dependency on config_test module.');
$this
->assertTrue(in_array('config_query_test:entity2', $dependent_ids), 'config_test.query.entity2 has a dependency on config_test module.');
$missing_dependencies = $config_manager
->findMissingContentDependencies();
$this
->assertEqual([], $missing_dependencies);
$expected = [
$entity_test
->uuid() => [
'entity_type' => 'entity_test',
'bundle' => $entity_test
->bundle(),
'uuid' => $entity_test
->uuid(),
],
];
$entity_test
->delete();
$missing_dependencies = $config_manager
->findMissingContentDependencies();
$this
->assertEqual($expected, $missing_dependencies);
$entity1
->setEnforcedDependencies([
'content' => [
$entity_test
->getConfigDependencyName(),
'entity_test:bundle:uuid',
],
])
->save();
$expected['uuid'] = [
'entity_type' => 'entity_test',
'bundle' => 'bundle',
'uuid' => 'uuid',
];
$missing_dependencies = $config_manager
->findMissingContentDependencies();
$this
->assertEqual($expected, $missing_dependencies);
}
public function testConfigEntityUninstall() {
$config_manager = \Drupal::service('config.manager');
$storage = $this->container
->get('entity.manager')
->getStorage('config_test');
$entity1 = $storage
->create(array(
'id' => 'entity1',
'dependencies' => array(
'enforced' => array(
'module' => array(
'node',
'config_test',
),
),
),
));
$entity1
->save();
$entity2 = $storage
->create(array(
'id' => 'entity2',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity1
->getConfigDependencyName(),
),
),
),
));
$entity2
->save();
system_rebuild_module_data();
$config_manager
->uninstall('module', 'node');
$this
->assertFalse($storage
->load('entity1'), 'Entity 1 deleted');
$this
->assertFalse($storage
->load('entity2'), 'Entity 2 deleted');
\Drupal::state()
->set('config_test.fix_dependencies', array(
$entity1
->getConfigDependencyName(),
));
$entity1 = $storage
->create(array(
'id' => 'entity1',
'dependencies' => array(
'enforced' => array(
'module' => array(
'node',
'config_test',
),
),
),
));
$entity1
->save();
$entity2 = $storage
->create(array(
'id' => 'entity2',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity1
->getConfigDependencyName(),
),
),
),
));
$entity2
->save();
$entity3 = $storage
->create(array(
'id' => 'entity3',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity2
->getConfigDependencyName(),
),
),
),
));
$entity3
->save();
$entity4 = $storage
->create(array(
'id' => 'entity4',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity1
->getConfigDependencyName(),
),
'module' => array(
'node',
'config_test',
),
),
),
));
$entity4
->save();
$config_entities = $config_manager
->getConfigEntitiesToChangeOnDependencyRemoval('module', [
'node',
]);
$this
->assertEqual($entity1
->uuid(), $config_entities['delete'][0]
->uuid(), 'Entity 1 will be deleted.');
$this
->assertEqual($entity2
->uuid(), reset($config_entities['update'])
->uuid(), 'Entity 2 will be updated.');
$this
->assertEqual($entity3
->uuid(), reset($config_entities['unchanged'])
->uuid(), 'Entity 3 is not changed.');
$this
->assertEqual($entity4
->uuid(), $config_entities['delete'][1]
->uuid(), 'Entity 4 will be deleted.');
$config_manager
->uninstall('module', 'node');
$this
->assertFalse($storage
->load('entity1'), 'Entity 1 deleted');
$entity2 = $storage
->load('entity2');
$this
->assertTrue($entity2, 'Entity 2 not deleted');
$this
->assertEqual($entity2
->calculateDependencies()
->getDependencies()['config'], array(), 'Entity 2 dependencies updated to remove dependency on Entity1.');
$entity3 = $storage
->load('entity3');
$this
->assertTrue($entity3, 'Entity 3 not deleted');
$this
->assertEqual($entity3
->calculateDependencies()
->getDependencies()['config'], [
$entity2
->getConfigDependencyName(),
], 'Entity 3 still depends on Entity 2.');
$this
->assertFalse($storage
->load('entity4'), 'Entity 4 deleted');
}
public function testConfigEntityDelete() {
$config_manager = \Drupal::service('config.manager');
$storage = $this->container
->get('entity.manager')
->getStorage('config_test');
$entity1 = $storage
->create(array(
'id' => 'entity1',
));
$entity1
->save();
$entity2 = $storage
->create(array(
'id' => 'entity2',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity1
->getConfigDependencyName(),
),
),
),
));
$entity2
->save();
$config_entities = $config_manager
->getConfigEntitiesToChangeOnDependencyRemoval('config', [
$entity1
->getConfigDependencyName(),
]);
$this
->assertEqual($entity2
->uuid(), reset($config_entities['delete'])
->uuid(), 'Entity 2 will be deleted.');
$this
->assertTrue(empty($config_entities['update']), 'No dependent configuration entities will be updated.');
$this
->assertTrue(empty($config_entities['unchanged']), 'No dependent configuration entities will be unchanged.');
$entity1
->delete();
$this
->assertFalse($storage
->load('entity1'), 'Entity 1 deleted');
$this
->assertFalse($storage
->load('entity2'), 'Entity 2 deleted');
\Drupal::state()
->set('config_test.fix_dependencies', array(
$entity1
->getConfigDependencyName(),
));
$entity1 = $storage
->create(array(
'id' => 'entity1',
));
$entity1
->save();
$entity2 = $storage
->create(array(
'id' => 'entity2',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity1
->getConfigDependencyName(),
),
),
),
));
$entity2
->save();
$entity3 = $storage
->create(array(
'id' => 'entity3',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity2
->getConfigDependencyName(),
),
),
),
));
$entity3
->save();
$config_entities = $config_manager
->getConfigEntitiesToChangeOnDependencyRemoval('config', [
$entity1
->getConfigDependencyName(),
]);
$this
->assertTrue(empty($config_entities['delete']), 'No dependent configuration entities will be deleted.');
$this
->assertEqual($entity2
->uuid(), reset($config_entities['update'])
->uuid(), 'Entity 2 will be updated.');
$this
->assertEqual($entity3
->uuid(), reset($config_entities['unchanged'])
->uuid(), 'Entity 3 is not changed.');
$entity1
->delete();
$this
->assertFalse($storage
->load('entity1'), 'Entity 1 deleted');
$entity2 = $storage
->load('entity2');
$this
->assertTrue($entity2, 'Entity 2 not deleted');
$this
->assertEqual($entity2
->calculateDependencies()
->getDependencies()['config'], array(), 'Entity 2 dependencies updated to remove dependency on Entity1.');
$entity3 = $storage
->load('entity3');
$this
->assertTrue($entity3, 'Entity 3 not deleted');
$this
->assertEqual($entity3
->calculateDependencies()
->getDependencies()['config'], [
$entity2
->getConfigDependencyName(),
], 'Entity 3 still depends on Entity 2.');
}
public function testContentEntityDelete() {
$this
->installEntitySchema('entity_test');
$config_manager = \Drupal::service('config.manager');
$content_entity = EntityTest::create();
$content_entity
->save();
$storage = $this->container
->get('entity.manager')
->getStorage('config_test');
$entity1 = $storage
->create(array(
'id' => 'entity1',
'dependencies' => array(
'enforced' => array(
'content' => array(
$content_entity
->getConfigDependencyName(),
),
),
),
));
$entity1
->save();
$entity2 = $storage
->create(array(
'id' => 'entity2',
'dependencies' => array(
'enforced' => array(
'config' => array(
$entity1
->getConfigDependencyName(),
),
),
),
));
$entity2
->save();
$entity3 = $storage
->create(array(
'id' => 'entity3',
));
$entity3
->save();
$config_entities = $config_manager
->getConfigEntitiesToChangeOnDependencyRemoval('content', [
$content_entity
->getConfigDependencyName(),
]);
$this
->assertEqual($entity1
->uuid(), $config_entities['delete'][0]
->uuid(), 'Entity 1 will be deleted.');
$this
->assertEqual($entity2
->uuid(), $config_entities['delete'][1]
->uuid(), 'Entity 2 will be deleted.');
$this
->assertTrue(empty($config_entities['update']), 'No dependencies of the content entity will be updated.');
$this
->assertTrue(empty($config_entities['unchanged']), 'No dependencies of the content entity will be unchanged.');
}
protected function getDependentIds(array $dependents) {
$dependent_ids = array();
foreach ($dependents as $dependent) {
$dependent_ids[] = $dependent
->getEntityTypeId() . ':' . $dependent
->id();
}
return $dependent_ids;
}
}