Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigImportRenameValidationTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
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;
13
14 /**
15  * Tests validating renamed configuration in a configuration import.
16  *
17  * @group config
18  */
19 class ConfigImportRenameValidationTest extends KernelTestBase {
20
21   /**
22    * Config Importer object used for testing.
23    *
24    * @var \Drupal\Core\Config\ConfigImporter
25    */
26   protected $configImporter;
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = ['system', 'user', 'node', 'field', 'text', 'config_test'];
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40
41     $this->installEntitySchema('user');
42     $this->installEntitySchema('node');
43     $this->installConfig(['field']);
44
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')
50     );
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')
61     );
62   }
63
64   /**
65    * Tests configuration renaming validation.
66    */
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(),
73     ]);
74     $test_entity->save();
75     $uuid = $test_entity->uuid();
76
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();
82
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(),
87       'uuid' => $uuid,
88     ]);
89     $content_type->save();
90
91     // Confirm that the staged configuration is detected as a rename since the
92     // UUIDs match.
93     $this->configImporter->reset();
94     $expected = [
95       'node.type.' . $content_type->id() . '::config_test.dynamic.' . $test_entity_id,
96     ];
97     $renames = $this->configImporter->getUnprocessedConfiguration('rename');
98     $this->assertSame($expected, $renames);
99
100     // Try to import the configuration. We expect an exception to be thrown
101     // because the staged entity is of a different type.
102     try {
103       $this->configImporter->import();
104       $this->fail('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
105     }
106     catch (ConfigImporterException $e) {
107       $this->pass('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
108       $expected = [
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])
110       ];
111       $this->assertEqual($expected, $this->configImporter->getErrors());
112     }
113   }
114
115   /**
116    * Tests configuration renaming validation for simple configuration.
117    */
118   public function testRenameSimpleConfigValidation() {
119     $uuid = new Php();
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();
124
125     $active = $this->container->get('config.storage');
126     $sync = $this->container->get('config.storage.sync');
127     $this->copyConfig($active, $sync);
128     $config->delete();
129
130     // Create another simple configuration with the same UUID.
131     $config = $this->config('config_test.old');
132     $config->set('uuid', $uuid_value)->save();
133
134     // Confirm that the staged configuration is detected as a rename since the
135     // UUIDs match.
136     $this->configImporter->reset();
137     $expected = [
138       'config_test.old::config_test.new'
139     ];
140     $renames = $this->configImporter->getUnprocessedConfiguration('rename');
141     $this->assertSame($expected, $renames);
142
143     // Try to import the configuration. We expect an exception to be thrown
144     // because the rename is for simple configuration.
145     try {
146       $this->configImporter->import();
147       $this->fail('Expected ConfigImporterException thrown when simple configuration is renamed.');
148     }
149     catch (ConfigImporterException $e) {
150       $this->pass('Expected ConfigImporterException thrown when simple configuration is renamed.');
151       $expected = [
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'])
153       ];
154       $this->assertEqual($expected, $this->configImporter->getErrors());
155     }
156   }
157
158 }