3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Config\ConfigImporter;
7 use Drupal\Core\Config\StorageComparer;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\node\Entity\NodeType;
12 * Tests importing recreated configuration entities.
16 class ConfigImportRecreateTest extends KernelTestBase {
19 * Config Importer object used for testing.
21 * @var \Drupal\Core\Config\ConfigImporter
23 protected $configImporter;
30 public static $modules = ['system', 'field', 'text', 'user', 'node'];
32 protected function setUp() {
35 $this->installEntitySchema('node');
36 $this->installConfig(['field', 'node']);
38 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
40 // Set up the ConfigImporter object for testing.
41 $storage_comparer = new StorageComparer(
42 $this->container->get('config.storage.sync'),
43 $this->container->get('config.storage'),
44 $this->container->get('config.manager')
46 $this->configImporter = new ConfigImporter(
47 $storage_comparer->createChangelist(),
48 $this->container->get('event_dispatcher'),
49 $this->container->get('config.manager'),
50 $this->container->get('lock'),
51 $this->container->get('config.typed'),
52 $this->container->get('module_handler'),
53 $this->container->get('module_installer'),
54 $this->container->get('theme_handler'),
55 $this->container->get('string_translation')
59 public function testRecreateEntity() {
60 $type_name = Unicode::strtolower($this->randomMachineName(16));
61 $content_type = NodeType::create([
63 'name' => 'Node type one',
65 $content_type->save();
66 node_add_body_field($content_type);
67 /** @var \Drupal\Core\Config\StorageInterface $active */
68 $active = $this->container->get('config.storage');
69 /** @var \Drupal\Core\Config\StorageInterface $sync */
70 $sync = $this->container->get('config.storage.sync');
72 $config_name = $content_type->getEntityType()->getConfigPrefix() . '.' . $content_type->id();
73 $this->copyConfig($active, $sync);
75 // Delete the content type. This will also delete a field storage, a field,
76 // an entity view display and an entity form display.
77 $content_type->delete();
78 $this->assertFalse($active->exists($config_name), 'Content type\'s old name does not exist active store.');
79 // Recreate with the same type - this will have a different UUID.
80 $content_type = NodeType::create([
82 'name' => 'Node type two',
84 $content_type->save();
85 node_add_body_field($content_type);
87 $this->configImporter->reset();
88 // A node type, a field, an entity view display and an entity form display
90 $creates = $this->configImporter->getUnprocessedConfiguration('create');
91 $deletes = $this->configImporter->getUnprocessedConfiguration('delete');
92 $this->assertEqual(5, count($creates), 'There are 5 configuration items to create.');
93 $this->assertEqual(5, count($deletes), 'There are 5 configuration items to delete.');
94 $this->assertEqual(0, count($this->configImporter->getUnprocessedConfiguration('update')), 'There are no configuration items to update.');
95 $this->assertIdentical($creates, array_reverse($deletes), 'Deletes and creates contain the same configuration names in opposite orders due to dependencies.');
97 $this->configImporter->import();
99 // Verify that there is nothing more to import.
100 $this->assertFalse($this->configImporter->reset()->hasUnprocessedConfigurationChanges());
101 $content_type = NodeType::load($type_name);
102 $this->assertEqual('Node type one', $content_type->label());