3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Component\Uuid\Php;
8 use Drupal\Core\Config\ConfigImporter;
9 use Drupal\Core\Config\ConfigImporterException;
10 use Drupal\Core\Config\StorageComparer;
11 use Drupal\node\Entity\NodeType;
12 use Drupal\KernelTests\KernelTestBase;
15 * Tests validating renamed configuration in a configuration import.
19 class ConfigImportRenameValidationTest extends KernelTestBase {
22 * Config Importer object used for testing.
24 * @var \Drupal\Core\Config\ConfigImporter
26 protected $configImporter;
33 public static $modules = ['system', 'user', 'node', 'field', 'text', 'config_test'];
38 protected function setUp() {
41 $this->installEntitySchema('user');
42 $this->installEntitySchema('node');
43 $this->installConfig(['field']);
45 // Set up the ConfigImporter object for testing.
46 $storage_comparer = new StorageComparer(
47 $this->container->get('config.storage.sync'),
48 $this->container->get('config.storage'),
49 $this->container->get('config.manager')
51 $this->configImporter = new ConfigImporter(
52 $storage_comparer->createChangelist(),
53 $this->container->get('event_dispatcher'),
54 $this->container->get('config.manager'),
55 $this->container->get('lock.persistent'),
56 $this->container->get('config.typed'),
57 $this->container->get('module_handler'),
58 $this->container->get('module_installer'),
59 $this->container->get('theme_handler'),
60 $this->container->get('string_translation')
65 * Tests configuration renaming validation.
67 public function testRenameValidation() {
68 // Create a test entity.
69 $test_entity_id = $this->randomMachineName();
70 $test_entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
71 'id' => $test_entity_id,
72 'label' => $this->randomMachineName(),
75 $uuid = $test_entity->uuid();
77 // Stage the test entity and then delete it from the active storage.
78 $active = $this->container->get('config.storage');
79 $sync = $this->container->get('config.storage.sync');
80 $this->copyConfig($active, $sync);
81 $test_entity->delete();
83 // Create a content type with a matching UUID in the active storage.
84 $content_type = NodeType::create([
85 'type' => Unicode::strtolower($this->randomMachineName(16)),
86 'name' => $this->randomMachineName(),
89 $content_type->save();
91 // Confirm that the staged configuration is detected as a rename since the
93 $this->configImporter->reset();
95 'node.type.' . $content_type->id() . '::config_test.dynamic.' . $test_entity_id,
97 $renames = $this->configImporter->getUnprocessedConfiguration('rename');
98 $this->assertSame($expected, $renames);
100 // Try to import the configuration. We expect an exception to be thrown
101 // because the staged entity is of a different type.
103 $this->configImporter->import();
104 $this->fail('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
106 catch (ConfigImporterException $e) {
107 $this->pass('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
109 SafeMarkup::format('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', ['@old_type' => 'node_type', '@new_type' => 'config_test', '@old_name' => 'node.type.' . $content_type->id(), '@new_name' => 'config_test.dynamic.' . $test_entity_id])
111 $this->assertEqual($expected, $this->configImporter->getErrors());
116 * Tests configuration renaming validation for simple configuration.
118 public function testRenameSimpleConfigValidation() {
120 // Create a simple configuration with a UUID.
121 $config = $this->config('config_test.new');
122 $uuid_value = $uuid->generate();
123 $config->set('uuid', $uuid_value)->save();
125 $active = $this->container->get('config.storage');
126 $sync = $this->container->get('config.storage.sync');
127 $this->copyConfig($active, $sync);
130 // Create another simple configuration with the same UUID.
131 $config = $this->config('config_test.old');
132 $config->set('uuid', $uuid_value)->save();
134 // Confirm that the staged configuration is detected as a rename since the
136 $this->configImporter->reset();
138 'config_test.old::config_test.new'
140 $renames = $this->configImporter->getUnprocessedConfiguration('rename');
141 $this->assertSame($expected, $renames);
143 // Try to import the configuration. We expect an exception to be thrown
144 // because the rename is for simple configuration.
146 $this->configImporter->import();
147 $this->fail('Expected ConfigImporterException thrown when simple configuration is renamed.');
149 catch (ConfigImporterException $e) {
150 $this->pass('Expected ConfigImporterException thrown when simple configuration is renamed.');
152 SafeMarkup::format('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', ['@old_name' => 'config_test.old', '@new_name' => 'config_test.new'])
154 $this->assertEqual($expected, $this->configImporter->getErrors());