3 namespace Drupal\Tests\config\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Archiver\ArchiveTar;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\Tests\BrowserTestBase;
12 * Tests the user interface for importing/exporting configuration.
14 * Each testX method does a complete rebuild of a Drupal site, so values being
15 * tested need to be stored in protected properties in order to survive until
20 class ConfigExportImportUITest extends BrowserTestBase {
23 * The contents of the config export tarball, held between test methods.
30 * Holds the original 'site slogan' before testing.
34 protected $originalSlogan;
37 * Holds a randomly generated new 'site slogan' for testing.
45 * Holds a content type.
47 * @var \Drupal\node\NodeInterface
49 protected $contentType;
52 * Holds the randomly-generated name of a field.
59 * Holds the field storage entity for $fieldName.
61 * @var \Drupal\field\FieldStorageConfigInterface
63 protected $fieldStorage;
70 public static $modules = ['config', 'node', 'field'];
75 protected function setUp() {
77 // The initial import must be done with uid 1 because if separately named
78 // roles are created then the role is lost after import. If the roles
79 // created have the same name then the sync will fail because they will
80 // have different UUIDs.
81 $this->drupalLogin($this->rootUser);
85 * Tests a simple site export import case.
87 public function testExportImport() {
88 // After installation there is no snapshot and nothing to import.
89 $this->drupalGet('admin/config/development/configuration');
90 $this->assertNoText(t('Warning message'));
91 $this->assertText(t('There are no configuration changes to import.'));
93 $this->originalSlogan = $this->config('system.site')->get('slogan');
94 $this->newSlogan = $this->randomString(16);
95 $this->assertNotEqual($this->newSlogan, $this->originalSlogan);
96 $this->config('system.site')
97 ->set('slogan', $this->newSlogan)
99 $this->assertEqual($this->config('system.site')->get('slogan'), $this->newSlogan);
101 // Create a content type.
102 $this->contentType = $this->drupalCreateContentType();
105 $this->fieldName = Unicode::strtolower($this->randomMachineName());
106 $this->fieldStorage = FieldStorageConfig::create([
107 'field_name' => $this->fieldName,
108 'entity_type' => 'node',
111 $this->fieldStorage->save();
112 FieldConfig::create([
113 'field_storage' => $this->fieldStorage,
114 'bundle' => $this->contentType->id(),
116 // Update the displays so that configuration does not change unexpectedly on
118 entity_get_form_display('node', $this->contentType->id(), 'default')
119 ->setComponent($this->fieldName, [
120 'type' => 'text_textfield',
123 entity_get_display('node', $this->contentType->id(), 'full')
124 ->setComponent($this->fieldName)
126 entity_get_display('node', $this->contentType->id(), 'default')
127 ->setComponent($this->fieldName)
129 entity_get_display('node', $this->contentType->id(), 'teaser')
130 ->removeComponent($this->fieldName)
133 $this->drupalGet('node/add/' . $this->contentType->id());
134 $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
136 // Export the configuration.
137 $this->drupalPostForm('admin/config/development/configuration/full/export', [], 'Export');
138 $this->tarball = $this->getRawContent();
140 $this->config('system.site')
141 ->set('slogan', $this->originalSlogan)
143 $this->assertEqual($this->config('system.site')->get('slogan'), $this->originalSlogan);
145 // Delete the custom field.
146 $fields = FieldConfig::loadMultiple();
147 foreach ($fields as $field) {
148 if ($field->getName() == $this->fieldName) {
152 $field_storages = FieldStorageConfig::loadMultiple();
153 foreach ($field_storages as $field_storage) {
154 if ($field_storage->getName() == $this->fieldName) {
155 $field_storage->delete();
158 $this->drupalGet('node/add/' . $this->contentType->id());
159 $this->assertNoFieldByName("{$this->fieldName}[0][value]", '', 'Widget is not displayed');
161 // Import the configuration.
162 $filename = 'temporary://' . $this->randomMachineName();
163 file_put_contents($filename, $this->tarball);
164 $this->drupalPostForm('admin/config/development/configuration/full/import', ['files[import_tarball]' => $filename], 'Upload');
165 // There is no snapshot yet because an import has never run.
166 $this->assertNoText(t('Warning message'));
167 $this->assertNoText(t('There are no configuration changes to import.'));
168 $this->assertText($this->contentType->label());
170 $this->drupalPostForm(NULL, [], 'Import all');
171 // After importing the snapshot has been updated an there are no warnings.
172 $this->assertNoText(t('Warning message'));
173 $this->assertText(t('There are no configuration changes to import.'));
175 $this->assertEqual($this->config('system.site')->get('slogan'), $this->newSlogan);
177 $this->drupalGet('node/add');
178 $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
180 $this->config('system.site')
181 ->set('slogan', $this->originalSlogan)
183 $this->drupalGet('admin/config/development/configuration');
184 $this->assertText(t('Warning message'));
185 $this->assertText('The following items in your active configuration have changes since the last import that may be lost on the next import.');
186 // Ensure the item is displayed as part of a list (to avoid false matches
187 // on the rest of the page) and that the list markup is not escaped.
188 $this->assertRaw('<li>system.site</li>');
189 // Remove everything from sync. The warning about differences between the
190 // active and snapshot should no longer exist.
191 \Drupal::service('config.storage.sync')->deleteAll();
192 $this->drupalGet('admin/config/development/configuration');
193 $this->assertNoText(t('Warning message'));
194 $this->assertNoText('The following items in your active configuration have changes since the last import that may be lost on the next import.');
195 $this->assertText(t('There are no configuration changes to import.'));
196 // Write a file to sync. The warning about differences between the active
197 // and snapshot should now exist.
198 /** @var \Drupal\Core\Config\StorageInterface $sync */
199 $sync = $this->container->get('config.storage.sync');
200 $data = $this->config('system.site')->get();
201 $data['slogan'] = 'in the face';
202 $this->copyConfig($this->container->get('config.storage'), $sync);
203 $sync->write('system.site', $data);
204 $this->drupalGet('admin/config/development/configuration');
205 $this->assertText(t('Warning message'));
206 $this->assertText('The following items in your active configuration have changes since the last import that may be lost on the next import.');
207 // Ensure the item is displayed as part of a list (to avoid false matches
208 // on the rest of the page) and that the list markup is not escaped.
209 $this->assertRaw('<li>system.site</li>');
213 * Tests an export and import of collections.
215 public function testExportImportCollections() {
217 /** @var \Drupal\Core\Config\StorageInterface $active_storage */
218 $active_storage = \Drupal::service('config.storage');
219 $test1_storage = $active_storage->createCollection('collection.test1');
220 $test1_storage->write('config_test.create', ['foo' => 'bar']);
221 $test1_storage->write('config_test.update', ['foo' => 'bar']);
222 $test2_storage = $active_storage->createCollection('collection.test2');
223 $test2_storage->write('config_test.another_create', ['foo' => 'bar']);
224 $test2_storage->write('config_test.another_update', ['foo' => 'bar']);
226 // Export the configuration.
227 $this->drupalPostForm('admin/config/development/configuration/full/export', [], 'Export');
228 $this->tarball = $this->getRawContent();
229 $filename = file_directory_temp() . '/' . $this->randomMachineName();
230 file_put_contents($filename, $this->tarball);
232 // Set up the active storage collections to test import.
233 $test1_storage->delete('config_test.create');
234 $test1_storage->write('config_test.update', ['foo' => 'baz']);
235 $test1_storage->write('config_test.delete', ['foo' => 'bar']);
236 $test2_storage->delete('config_test.another_create');
237 $test2_storage->write('config_test.another_update', ['foo' => 'baz']);
238 $test2_storage->write('config_test.another_delete', ['foo' => 'bar']);
240 // Create a snapshot.
241 $snapshot_storage = \Drupal::service('config.storage.snapshot');
242 \Drupal::service('config.manager')->createSnapshot($active_storage, $snapshot_storage);
244 // Ensure that the snapshot has the expected collection data before import.
245 $test1_snapshot = $snapshot_storage->createCollection('collection.test1');
246 $data = $test1_snapshot->read('config_test.delete');
247 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.delete in collection.test1 exists in the snapshot storage.');
248 $data = $test1_snapshot->read('config_test.update');
249 $this->assertEqual($data, ['foo' => 'baz'], 'The config_test.update in collection.test1 exists in the snapshot storage.');
250 $this->assertFalse($test1_snapshot->read('config_test.create'), 'The config_test.create in collection.test1 does not exist in the snapshot storage.');
251 $test2_snapshot = $snapshot_storage->createCollection('collection.test2');
252 $data = $test2_snapshot->read('config_test.another_delete');
253 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_delete in collection.test2 exists in the snapshot storage.');
254 $data = $test2_snapshot->read('config_test.another_update');
255 $this->assertEqual($data, ['foo' => 'baz'], 'The config_test.another_update in collection.test2 exists in the snapshot storage.');
256 $this->assertFalse($test2_snapshot->read('config_test.another_create'), 'The config_test.another_create in collection.test2 does not exist in the snapshot storage.');
258 // Create the tar that contains the expected content for the collections.
259 $tar = new ArchiveTar($filename, 'gz');
260 $content_list = $tar->listContent();
261 // Convert the list of files into something easy to search.
263 foreach ($content_list as $file) {
264 $files[] = $file['filename'];
266 $this->assertTrue(in_array('collection/test1/config_test.create.yml', $files), 'Config export contains collection/test1/config_test.create.yml.');
267 $this->assertTrue(in_array('collection/test2/config_test.another_create.yml', $files), 'Config export contains collection/test2/config_test.another_create.yml.');
268 $this->assertTrue(in_array('collection/test1/config_test.update.yml', $files), 'Config export contains collection/test1/config_test.update.yml.');
269 $this->assertTrue(in_array('collection/test2/config_test.another_update.yml', $files), 'Config export contains collection/test2/config_test.another_update.yml.');
270 $this->assertFalse(in_array('collection/test1/config_test.delete.yml', $files), 'Config export does not contain collection/test1/config_test.delete.yml.');
271 $this->assertFalse(in_array('collection/test2/config_test.another_delete.yml', $files), 'Config export does not contain collection/test2/config_test.another_delete.yml.');
273 $this->drupalPostForm('admin/config/development/configuration/full/import', ['files[import_tarball]' => $filename], 'Upload');
274 // Verify that there are configuration differences to import.
275 $this->drupalGet('admin/config/development/configuration');
276 $this->assertNoText(t('There are no configuration changes to import.'));
277 $this->assertText(t('@collection configuration collection', ['@collection' => 'collection.test1']));
278 $this->assertText(t('@collection configuration collection', ['@collection' => 'collection.test2']));
279 $this->assertText('config_test.create');
280 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.create');
281 $this->assertText('config_test.update');
282 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.update');
283 $this->assertText('config_test.delete');
284 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.delete');
285 $this->assertText('config_test.another_create');
286 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_create');
287 $this->assertText('config_test.another_update');
288 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_update');
289 $this->assertText('config_test.another_delete');
290 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_delete');
292 $this->drupalPostForm(NULL, [], 'Import all');
293 $this->assertText(t('There are no configuration changes to import.'));
295 // Test data in collections.
296 $data = $test1_storage->read('config_test.create');
297 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.create in collection.test1 has been created.');
298 $data = $test1_storage->read('config_test.update');
299 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.update in collection.test1 has been updated.');
300 $this->assertFalse($test1_storage->read('config_test.delete'), 'The config_test.delete in collection.test1 has been deleted.');
302 $data = $test2_storage->read('config_test.another_create');
303 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_create in collection.test2 has been created.');
304 $data = $test2_storage->read('config_test.another_update');
305 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_update in collection.test2 has been updated.');
306 $this->assertFalse($test2_storage->read('config_test.another_delete'), 'The config_test.another_delete in collection.test2 has been deleted.');
308 // Ensure that the snapshot has been updated with the collection data.
309 $snapshot_storage = \Drupal::service('config.storage.snapshot');
310 $test1_snapshot = $snapshot_storage->createCollection('collection.test1');
311 $data = $test1_snapshot->read('config_test.create');
312 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.create in collection.test1 has been created in the snapshot storage.');
313 $data = $test1_snapshot->read('config_test.update');
314 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.update in collection.test1 has been updated in the snapshot storage.');
315 $this->assertFalse($test1_snapshot->read('config_test.delete'), 'The config_test.delete in collection.test1 does not exist in the snapshot storage.');
316 $test2_snapshot = $snapshot_storage->createCollection('collection.test2');
317 $data = $test2_snapshot->read('config_test.another_create');
318 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_create in collection.test2 has been created in the snapshot storage.');
319 $data = $test2_snapshot->read('config_test.another_update');
320 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_update in collection.test2 has been updated in the snapshot storage.');
321 $this->assertFalse($test2_snapshot->read('config_test.another_delete'), 'The config_test.another_delete in collection.test2 does not exist in the snapshot storage.');