public function ConfigDependencyTest::testConfigEntityUninstallComplex in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testConfigEntityUninstallComplex()
- 9 core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testConfigEntityUninstallComplex()
Tests complex configuration entity dependency handling during uninstall.
Configuration entities can be deleted or updated during module uninstall because they have dependencies on the module.
@dataProvider providerConfigEntityUninstallComplex
Parameters
array $entity_id_suffixes: The suffixes to add to the 4 entities created by the test.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Config/ ConfigDependencyTest.php, line 251
Class
- ConfigDependencyTest
- Tests for configuration dependencies.
Namespace
Drupal\KernelTests\Core\ConfigCode
public function testConfigEntityUninstallComplex(array $entity_id_suffixes) {
/** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
$config_manager = \Drupal::service('config.manager');
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
$storage = $this->container
->get('entity_type.manager')
->getStorage('config_test');
// Entity 1 will be deleted because it depends on node.
$entity_1 = $storage
->create([
'id' => 'entity_' . $entity_id_suffixes[0],
'dependencies' => [
'enforced' => [
'module' => [
'node',
'config_test',
],
],
],
]);
$entity_1
->save();
// Entity 2 has a dependency on entity 1 but it can be fixed because
// \Drupal\config_test\Entity::onDependencyRemoval() will remove the
// dependency before config entities are deleted.
$entity_2 = $storage
->create([
'id' => 'entity_' . $entity_id_suffixes[1],
'dependencies' => [
'enforced' => [
'config' => [
$entity_1
->getConfigDependencyName(),
],
],
],
]);
$entity_2
->save();
// Entity 3 will be unchanged because it is dependent on entity 2 which can
// be fixed. The ConfigEntityInterface::onDependencyRemoval() method will
// not be called for this entity.
$entity_3 = $storage
->create([
'id' => 'entity_' . $entity_id_suffixes[2],
'dependencies' => [
'enforced' => [
'config' => [
$entity_2
->getConfigDependencyName(),
],
],
],
]);
$entity_3
->save();
// Entity 4's config dependency will be fixed but it will still be deleted
// because it also depends on the node module.
$entity_4 = $storage
->create([
'id' => 'entity_' . $entity_id_suffixes[3],
'dependencies' => [
'enforced' => [
'config' => [
$entity_1
->getConfigDependencyName(),
],
'module' => [
'node',
'config_test',
],
],
],
]);
$entity_4
->save();
// Entity 5 will be fixed because it is dependent on entity 3, which is
// unchanged, and entity 1 which will be fixed because
// \Drupal\config_test\Entity::onDependencyRemoval() will remove the
// dependency.
$entity_5 = $storage
->create([
'id' => 'entity_' . $entity_id_suffixes[4],
'dependencies' => [
'enforced' => [
'config' => [
$entity_1
->getConfigDependencyName(),
$entity_3
->getConfigDependencyName(),
],
],
],
]);
$entity_5
->save();
// Set a more complicated test where dependencies will be fixed.
\Drupal::state()
->set('config_test.fix_dependencies', [
$entity_1
->getConfigDependencyName(),
]);
\Drupal::state()
->set('config_test.on_dependency_removal_called', []);
// Do a dry run using
// \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval().
$config_entities = $config_manager
->getConfigEntitiesToChangeOnDependencyRemoval('module', [
'node',
]);
// Assert that \Drupal\config_test\Entity\ConfigTest::onDependencyRemoval()
// is called as expected and with the correct dependencies.
$called = \Drupal::state()
->get('config_test.on_dependency_removal_called', []);
$this
->assertArrayNotHasKey($entity_3
->id(), $called, 'ConfigEntityInterface::onDependencyRemoval() is not called for entity 3.');
$this
->assertSame([
$entity_1
->id(),
$entity_4
->id(),
$entity_2
->id(),
$entity_5
->id(),
], array_keys($called), 'The most dependent entities have ConfigEntityInterface::onDependencyRemoval() called first.');
$this
->assertSame([
'config' => [],
'content' => [],
'module' => [
'node',
],
'theme' => [],
], $called[$entity_1
->id()]);
$this
->assertSame([
'config' => [
$entity_1
->getConfigDependencyName(),
],
'content' => [],
'module' => [],
'theme' => [],
], $called[$entity_2
->id()]);
$this
->assertSame([
'config' => [
$entity_1
->getConfigDependencyName(),
],
'content' => [],
'module' => [
'node',
],
'theme' => [],
], $called[$entity_4
->id()]);
$this
->assertSame([
'config' => [
$entity_1
->getConfigDependencyName(),
],
'content' => [],
'module' => [],
'theme' => [],
], $called[$entity_5
->id()]);
$this
->assertEquals($entity_1
->uuid(), $config_entities['delete'][1]
->uuid(), 'Entity 1 will be deleted.');
$this
->assertEquals($entity_2
->uuid(), $config_entities['update'][0]
->uuid(), 'Entity 2 will be updated.');
$this
->assertEquals($entity_3
->uuid(), reset($config_entities['unchanged'])
->uuid(), 'Entity 3 is not changed.');
$this
->assertEquals($entity_4
->uuid(), $config_entities['delete'][0]
->uuid(), 'Entity 4 will be deleted.');
$this
->assertEquals($entity_5
->uuid(), $config_entities['update'][1]
->uuid(), 'Entity 5 is updated.');
// Perform the uninstall.
$config_manager
->uninstall('module', 'node');
// Test that expected actions have been performed.
$this
->assertNull($storage
->load($entity_1
->id()), 'Entity 1 deleted');
$entity_2 = $storage
->load($entity_2
->id());
$this
->assertNotEmpty($entity_2, 'Entity 2 not deleted');
$this
->assertEquals([], $entity_2
->calculateDependencies()
->getDependencies()['config'], 'Entity 2 dependencies updated to remove dependency on entity 1.');
$entity_3 = $storage
->load($entity_3
->id());
$this
->assertNotEmpty($entity_3, 'Entity 3 not deleted');
$this
->assertEquals([
$entity_2
->getConfigDependencyName(),
], $entity_3
->calculateDependencies()
->getDependencies()['config'], 'Entity 3 still depends on entity 2.');
$this
->assertNull($storage
->load($entity_4
->id()), 'Entity 4 deleted');
}