3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\Core\Config\ConfigImporter;
6 use Drupal\Core\Config\StorageComparer;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\node\Entity\NodeType;
11 * Tests importing recreated configuration entities.
15 class ConfigImportRecreateTest extends KernelTestBase {
18 * Config Importer object used for testing.
20 * @var \Drupal\Core\Config\ConfigImporter
22 protected $configImporter;
29 public static $modules = ['system', 'field', 'text', 'user', 'node'];
31 protected function setUp() {
34 $this->installEntitySchema('node');
35 $this->installConfig(['system', 'field', 'node']);
37 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
39 // Set up the ConfigImporter object for testing.
40 $storage_comparer = new StorageComparer(
41 $this->container->get('config.storage.sync'),
42 $this->container->get('config.storage'),
43 $this->container->get('config.manager')
45 $this->configImporter = new ConfigImporter(
46 $storage_comparer->createChangelist(),
47 $this->container->get('event_dispatcher'),
48 $this->container->get('config.manager'),
49 $this->container->get('lock'),
50 $this->container->get('config.typed'),
51 $this->container->get('module_handler'),
52 $this->container->get('module_installer'),
53 $this->container->get('theme_handler'),
54 $this->container->get('string_translation')
58 public function testRecreateEntity() {
59 $type_name = mb_strtolower($this->randomMachineName(16));
60 $content_type = NodeType::create([
62 'name' => 'Node type one',
64 $content_type->save();
65 node_add_body_field($content_type);
66 /** @var \Drupal\Core\Config\StorageInterface $active */
67 $active = $this->container->get('config.storage');
68 /** @var \Drupal\Core\Config\StorageInterface $sync */
69 $sync = $this->container->get('config.storage.sync');
71 $config_name = $content_type->getEntityType()->getConfigPrefix() . '.' . $content_type->id();
72 $this->copyConfig($active, $sync);
74 // Delete the content type. This will also delete a field storage, a field,
75 // an entity view display and an entity form display.
76 $content_type->delete();
77 $this->assertFalse($active->exists($config_name), 'Content type\'s old name does not exist active store.');
78 // Recreate with the same type - this will have a different UUID.
79 $content_type = NodeType::create([
81 'name' => 'Node type two',
83 $content_type->save();
84 node_add_body_field($content_type);
86 $this->configImporter->reset();
87 // A node type, a field, an entity view display and an entity form display
89 $creates = $this->configImporter->getUnprocessedConfiguration('create');
90 $deletes = $this->configImporter->getUnprocessedConfiguration('delete');
91 $this->assertEqual(5, count($creates), 'There are 5 configuration items to create.');
92 $this->assertEqual(5, count($deletes), 'There are 5 configuration items to delete.');
93 $this->assertEqual(0, count($this->configImporter->getUnprocessedConfiguration('update')), 'There are no configuration items to update.');
94 $this->assertSame($creates, array_reverse($deletes), 'Deletes and creates contain the same configuration names in opposite orders due to dependencies.');
96 $this->configImporter->import();
98 // Verify that there is nothing more to import.
99 $this->assertFalse($this->configImporter->reset()->hasUnprocessedConfigurationChanges());
100 $content_type = NodeType::load($type_name);
101 $this->assertEqual('Node type one', $content_type->label());