You are here

public function ConfigImporterMissingContentTest::testMissingContent in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Config/ConfigImporterMissingContentTest.php \Drupal\KernelTests\Core\Config\ConfigImporterMissingContentTest::testMissingContent()
  2. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigImporterMissingContentTest.php \Drupal\KernelTests\Core\Config\ConfigImporterMissingContentTest::testMissingContent()

Tests the missing content event is fired.

See also

\Drupal\Core\Config\ConfigImporter::processMissingContent()

\Drupal\config_import_test\EventSubscriber

File

core/tests/Drupal/KernelTests/Core/Config/ConfigImporterMissingContentTest.php, line 75

Class

ConfigImporterMissingContentTest
Tests importing configuration which has missing content dependencies.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testMissingContent() {
  \Drupal::state()
    ->set('config_import_test.config_import_missing_content', TRUE);

  // Update a configuration entity in the sync directory to have a dependency
  // on two content entities that do not exist.
  $storage = $this->container
    ->get('config.storage');
  $sync = $this->container
    ->get('config.storage.sync');
  $entity_one = EntityTest::create([
    'name' => 'one',
  ]);
  $entity_two = EntityTest::create([
    'name' => 'two',
  ]);
  $entity_three = EntityTest::create([
    'name' => 'three',
  ]);
  $dynamic_name = 'config_test.dynamic.dotted.default';
  $original_dynamic_data = $storage
    ->read($dynamic_name);

  // Entity one will be resolved by
  // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentOne().
  $original_dynamic_data['dependencies']['content'][] = $entity_one
    ->getConfigDependencyName();

  // Entity two will be resolved by
  // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentTwo().
  $original_dynamic_data['dependencies']['content'][] = $entity_two
    ->getConfigDependencyName();

  // Entity three will be resolved by
  // \Drupal\Core\Config\Importer\FinalMissingContentSubscriber.
  $original_dynamic_data['dependencies']['content'][] = $entity_three
    ->getConfigDependencyName();
  $sync
    ->write($dynamic_name, $original_dynamic_data);

  // Import.
  $this->configImporter
    ->reset()
    ->import();
  $this
    ->assertEquals([], $this->configImporter
    ->getErrors(), 'There were no errors during the import.');
  $this
    ->assertEquals($entity_one
    ->uuid(), \Drupal::state()
    ->get('config_import_test.config_import_missing_content_one'), 'The missing content event is fired during configuration import.');
  $this
    ->assertEquals($entity_two
    ->uuid(), \Drupal::state()
    ->get('config_import_test.config_import_missing_content_two'), 'The missing content event is fired during configuration import.');
  $original_dynamic_data = $storage
    ->read($dynamic_name);
  $this
    ->assertEquals([
    $entity_one
      ->getConfigDependencyName(),
    $entity_two
      ->getConfigDependencyName(),
    $entity_three
      ->getConfigDependencyName(),
  ], $original_dynamic_data['dependencies']['content'], 'The imported configuration entity has the missing content entity dependency.');
}