You are here

public function ConfigImportRecreateTest::testRecreateEntity in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Config/ConfigImportRecreateTest.php \Drupal\KernelTests\Core\Config\ConfigImportRecreateTest::testRecreateEntity()
  2. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigImportRecreateTest.php \Drupal\KernelTests\Core\Config\ConfigImportRecreateTest::testRecreateEntity()

File

core/tests/Drupal/KernelTests/Core/Config/ConfigImportRecreateTest.php, line 58

Class

ConfigImportRecreateTest
Tests importing recreated configuration entities.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testRecreateEntity() {
  $type_name = mb_strtolower($this
    ->randomMachineName(16));
  $content_type = NodeType::create([
    'type' => $type_name,
    'name' => 'Node type one',
  ]);
  $content_type
    ->save();
  node_add_body_field($content_type);

  /** @var \Drupal\Core\Config\StorageInterface $active */
  $active = $this->container
    ->get('config.storage');

  /** @var \Drupal\Core\Config\StorageInterface $sync */
  $sync = $this->container
    ->get('config.storage.sync');
  $config_name = $content_type
    ->getEntityType()
    ->getConfigPrefix() . '.' . $content_type
    ->id();
  $this
    ->copyConfig($active, $sync);

  // Delete the content type. This will also delete a field storage, a field,
  // an entity view display and an entity form display.
  $content_type
    ->delete();
  $this
    ->assertFalse($active
    ->exists($config_name), 'Content type\'s old name does not exist active store.');

  // Recreate with the same type - this will have a different UUID.
  $content_type = NodeType::create([
    'type' => $type_name,
    'name' => 'Node type two',
  ]);
  $content_type
    ->save();
  node_add_body_field($content_type);
  $this->configImporter
    ->reset();

  // A node type, a field, an entity view display and an entity form display
  // will be recreated.
  $creates = $this->configImporter
    ->getUnprocessedConfiguration('create');
  $deletes = $this->configImporter
    ->getUnprocessedConfiguration('delete');
  $this
    ->assertCount(5, $creates, 'There are 5 configuration items to create.');
  $this
    ->assertCount(5, $deletes, 'There are 5 configuration items to delete.');
  $this
    ->assertCount(0, $this->configImporter
    ->getUnprocessedConfiguration('update'), 'There are no configuration items to update.');
  $this
    ->assertSame($creates, array_reverse($deletes), 'Deletes and creates contain the same configuration names in opposite orders due to dependencies.');
  $this->configImporter
    ->import();

  // Verify that there is nothing more to import.
  $this
    ->assertFalse($this->configImporter
    ->reset()
    ->hasUnprocessedConfigurationChanges());
  $content_type = NodeType::load($type_name);
  $this
    ->assertEquals('Node type one', $content_type
    ->label());
}