function ConfigDependencyTest::testConfigEntityDelete

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testConfigEntityDelete()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testConfigEntityDelete()
  3. 11.x core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testConfigEntityDelete()

Tests deleting a configuration entity and dependency management.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php, line 493

Class

ConfigDependencyTest
Tests for configuration dependencies.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testConfigEntityDelete() : void {
    
    /** @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');
    // Test dependencies between configuration entities.
    $entity1 = $storage->create([
        'id' => 'entity1',
    ]);
    $entity1->save();
    $entity2 = $storage->create([
        'id' => 'entity2',
        'dependencies' => [
            'enforced' => [
                'config' => [
                    $entity1->getConfigDependencyName(),
                ],
            ],
        ],
    ]);
    $entity2->save();
    // Do a dry run using
    // \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval().
    $config_entities = $config_manager->getConfigEntitiesToChangeOnDependencyRemoval('config', [
        $entity1->getConfigDependencyName(),
    ]);
    $this->assertEquals($entity2->uuid(), reset($config_entities['delete'])->uuid(), 'Entity 2 will be deleted.');
    $this->assertEmpty($config_entities['update'], 'No dependent configuration entities will be updated.');
    $this->assertEmpty($config_entities['unchanged'], 'No dependent configuration entities will be unchanged.');
    // Test that doing a delete of entity1 deletes entity2 since it is dependent
    // on entity1.
    $entity1->delete();
    $this->assertNull($storage->load('entity1'), 'Entity 1 deleted');
    $this->assertNull($storage->load('entity2'), 'Entity 2 deleted');
    // Set a more complicated test where dependencies will be fixed.
    \Drupal::state()->set('config_test.fix_dependencies', [
        $entity1->getConfigDependencyName(),
    ]);
    // Entity1 will be deleted by the test.
    $entity1 = $storage->create([
        'id' => 'entity1',
    ]);
    $entity1->save();
    // Entity2 has a dependency on Entity1 but it can be fixed because
    // \Drupal\config_test\Entity::onDependencyRemoval() will remove the
    // dependency before config entities are deleted.
    $entity2 = $storage->create([
        'id' => 'entity2',
        'dependencies' => [
            'enforced' => [
                'config' => [
                    $entity1->getConfigDependencyName(),
                ],
            ],
        ],
    ]);
    $entity2->save();
    // Entity3 will be unchanged because it is dependent on Entity2 which can
    // be fixed.
    $entity3 = $storage->create([
        'id' => 'entity3',
        'dependencies' => [
            'enforced' => [
                'config' => [
                    $entity2->getConfigDependencyName(),
                ],
            ],
        ],
    ]);
    $entity3->save();
    // Do a dry run using
    // \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval().
    $config_entities = $config_manager->getConfigEntitiesToChangeOnDependencyRemoval('config', [
        $entity1->getConfigDependencyName(),
    ]);
    $this->assertEmpty($config_entities['delete'], 'No dependent configuration entities will be deleted.');
    $this->assertEquals($entity2->uuid(), reset($config_entities['update'])->uuid(), 'Entity 2 will be updated.');
    $this->assertEquals($entity3->uuid(), reset($config_entities['unchanged'])->uuid(), 'Entity 3 is not changed.');
    // Perform the uninstall.
    $entity1->delete();
    // Test that expected actions have been performed.
    $this->assertNull($storage->load('entity1'), 'Entity 1 deleted');
    $entity2 = $storage->load('entity2');
    $this->assertNotEmpty($entity2, 'Entity 2 not deleted');
    $this->assertEquals([], $entity2->calculateDependencies()
        ->getDependencies()['config'], 'Entity 2 dependencies updated to remove dependency on Entity1.');
    $entity3 = $storage->load('entity3');
    $this->assertNotEmpty($entity3, 'Entity 3 not deleted');
    $this->assertEquals($entity3->calculateDependencies()
        ->getDependencies()['config'], [
        $entity2->getConfigDependencyName(),
    ], 'Entity 3 still depends on Entity 2.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.