ConfigImportRenameValidationTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Component\Utility\SafeMarkup;
  4. use Drupal\Component\Utility\Unicode;
  5. use Drupal\Component\Uuid\Php;
  6. use Drupal\Core\Config\ConfigImporter;
  7. use Drupal\Core\Config\ConfigImporterException;
  8. use Drupal\Core\Config\StorageComparer;
  9. use Drupal\node\Entity\NodeType;
  10. use Drupal\KernelTests\KernelTestBase;
  11. /**
  12. * Tests validating renamed configuration in a configuration import.
  13. *
  14. * @group config
  15. */
  16. class ConfigImportRenameValidationTest extends KernelTestBase {
  17. /**
  18. * Config Importer object used for testing.
  19. *
  20. * @var \Drupal\Core\Config\ConfigImporter
  21. */
  22. protected $configImporter;
  23. /**
  24. * Modules to enable.
  25. *
  26. * @var array
  27. */
  28. public static $modules = ['system', 'user', 'node', 'field', 'text', 'config_test'];
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function setUp() {
  33. parent::setUp();
  34. $this->installEntitySchema('user');
  35. $this->installEntitySchema('node');
  36. $this->installConfig(['field']);
  37. // Set up the ConfigImporter object for testing.
  38. $storage_comparer = new StorageComparer(
  39. $this->container->get('config.storage.sync'),
  40. $this->container->get('config.storage'),
  41. $this->container->get('config.manager')
  42. );
  43. $this->configImporter = new ConfigImporter(
  44. $storage_comparer->createChangelist(),
  45. $this->container->get('event_dispatcher'),
  46. $this->container->get('config.manager'),
  47. $this->container->get('lock.persistent'),
  48. $this->container->get('config.typed'),
  49. $this->container->get('module_handler'),
  50. $this->container->get('module_installer'),
  51. $this->container->get('theme_handler'),
  52. $this->container->get('string_translation')
  53. );
  54. }
  55. /**
  56. * Tests configuration renaming validation.
  57. */
  58. public function testRenameValidation() {
  59. // Create a test entity.
  60. $test_entity_id = $this->randomMachineName();
  61. $test_entity = entity_create('config_test', [
  62. 'id' => $test_entity_id,
  63. 'label' => $this->randomMachineName(),
  64. ]);
  65. $test_entity->save();
  66. $uuid = $test_entity->uuid();
  67. // Stage the test entity and then delete it from the active storage.
  68. $active = $this->container->get('config.storage');
  69. $sync = $this->container->get('config.storage.sync');
  70. $this->copyConfig($active, $sync);
  71. $test_entity->delete();
  72. // Create a content type with a matching UUID in the active storage.
  73. $content_type = NodeType::create([
  74. 'type' => Unicode::strtolower($this->randomMachineName(16)),
  75. 'name' => $this->randomMachineName(),
  76. 'uuid' => $uuid,
  77. ]);
  78. $content_type->save();
  79. // Confirm that the staged configuration is detected as a rename since the
  80. // UUIDs match.
  81. $this->configImporter->reset();
  82. $expected = [
  83. 'node.type.' . $content_type->id() . '::config_test.dynamic.' . $test_entity_id,
  84. ];
  85. $renames = $this->configImporter->getUnprocessedConfiguration('rename');
  86. $this->assertSame($expected, $renames);
  87. // Try to import the configuration. We expect an exception to be thrown
  88. // because the staged entity is of a different type.
  89. try {
  90. $this->configImporter->import();
  91. $this->fail('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
  92. }
  93. catch (ConfigImporterException $e) {
  94. $this->pass('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
  95. $expected = [
  96. 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])
  97. ];
  98. $this->assertEqual($expected, $this->configImporter->getErrors());
  99. }
  100. }
  101. /**
  102. * Tests configuration renaming validation for simple configuration.
  103. */
  104. public function testRenameSimpleConfigValidation() {
  105. $uuid = new Php();
  106. // Create a simple configuration with a UUID.
  107. $config = $this->config('config_test.new');
  108. $uuid_value = $uuid->generate();
  109. $config->set('uuid', $uuid_value)->save();
  110. $active = $this->container->get('config.storage');
  111. $sync = $this->container->get('config.storage.sync');
  112. $this->copyConfig($active, $sync);
  113. $config->delete();
  114. // Create another simple configuration with the same UUID.
  115. $config = $this->config('config_test.old');
  116. $config->set('uuid', $uuid_value)->save();
  117. // Confirm that the staged configuration is detected as a rename since the
  118. // UUIDs match.
  119. $this->configImporter->reset();
  120. $expected = [
  121. 'config_test.old::config_test.new'
  122. ];
  123. $renames = $this->configImporter->getUnprocessedConfiguration('rename');
  124. $this->assertSame($expected, $renames);
  125. // Try to import the configuration. We expect an exception to be thrown
  126. // because the rename is for simple configuration.
  127. try {
  128. $this->configImporter->import();
  129. $this->fail('Expected ConfigImporterException thrown when simple configuration is renamed.');
  130. }
  131. catch (ConfigImporterException $e) {
  132. $this->pass('Expected ConfigImporterException thrown when simple configuration is renamed.');
  133. $expected = [
  134. 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'])
  135. ];
  136. $this->assertEqual($expected, $this->configImporter->getErrors());
  137. }
  138. }
  139. }