You are here

public function ConfigDependencyTest::testContentEntityDelete in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Config/ConfigDependencyTest.php \Drupal\KernelTests\Core\Config\ConfigDependencyTest::testContentEntityDelete()

Tests getConfigEntitiesToChangeOnDependencyRemoval() with content entities.

At the moment there is no runtime code that calculates configuration dependencies on content entity delete because this calculation is expensive and all content dependencies are soft. This test ensures that the code works for content entities.

See also

\Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval()

File

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

Class

ConfigDependencyTest
Tests for configuration dependencies.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testContentEntityDelete() {
  $this
    ->installEntitySchema('entity_test');

  /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
  $config_manager = \Drupal::service('config.manager');
  $content_entity = EntityTest::create();
  $content_entity
    ->save();

  /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('config_test');
  $entity1 = $storage
    ->create([
    'id' => 'entity1',
    'dependencies' => [
      'enforced' => [
        'content' => [
          $content_entity
            ->getConfigDependencyName(),
        ],
      ],
    ],
  ]);
  $entity1
    ->save();
  $entity2 = $storage
    ->create([
    'id' => 'entity2',
    'dependencies' => [
      'enforced' => [
        'config' => [
          $entity1
            ->getConfigDependencyName(),
        ],
      ],
    ],
  ]);
  $entity2
    ->save();

  // Create a configuration entity that is not in the dependency chain.
  $entity3 = $storage
    ->create([
    'id' => 'entity3',
  ]);
  $entity3
    ->save();
  $config_entities = $config_manager
    ->getConfigEntitiesToChangeOnDependencyRemoval('content', [
    $content_entity
      ->getConfigDependencyName(),
  ]);
  $this
    ->assertEquals($entity1
    ->uuid(), $config_entities['delete'][1]
    ->uuid(), 'Entity 1 will be deleted.');
  $this
    ->assertEquals($entity2
    ->uuid(), $config_entities['delete'][0]
    ->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.');
}