3 namespace Drupal\Tests\config\Functional;
5 use Drupal\Core\Archiver\ArchiveTar;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests the user interface for importing/exporting configuration.
13 * Each testX method does a complete rebuild of a Drupal site, so values being
14 * tested need to be stored in protected properties in order to survive until
19 class ConfigExportImportUITest extends BrowserTestBase {
22 * The contents of the config export tarball, held between test methods.
29 * Holds the original 'site slogan' before testing.
33 protected $originalSlogan;
36 * Holds a randomly generated new 'site slogan' for testing.
44 * Holds a content type.
46 * @var \Drupal\node\NodeInterface
48 protected $contentType;
51 * Holds the randomly-generated name of a field.
58 * Holds the field storage entity for $fieldName.
60 * @var \Drupal\field\FieldStorageConfigInterface
62 protected $fieldStorage;
69 public static $modules = ['config', 'node', 'field'];
74 protected function setUp() {
76 // The initial import must be done with uid 1 because if separately named
77 // roles are created then the role is lost after import. If the roles
78 // created have the same name then the sync will fail because they will
79 // have different UUIDs.
80 $this->drupalLogin($this->rootUser);
84 * Tests a simple site export import case.
86 public function testExportImport() {
87 // After installation there is no snapshot and nothing to import.
88 $this->drupalGet('admin/config/development/configuration');
89 $this->assertNoText(t('Warning message'));
90 $this->assertText(t('There are no configuration changes to import.'));
92 $this->originalSlogan = $this->config('system.site')->get('slogan');
93 $this->newSlogan = $this->randomString(16);
94 $this->assertNotEqual($this->newSlogan, $this->originalSlogan);
95 $this->config('system.site')
96 ->set('slogan', $this->newSlogan)
98 $this->assertEqual($this->config('system.site')->get('slogan'), $this->newSlogan);
100 // Create a content type.
101 $this->contentType = $this->drupalCreateContentType();
104 $this->fieldName = mb_strtolower($this->randomMachineName());
105 $this->fieldStorage = FieldStorageConfig::create([
106 'field_name' => $this->fieldName,
107 'entity_type' => 'node',
110 $this->fieldStorage->save();
111 FieldConfig::create([
112 'field_storage' => $this->fieldStorage,
113 'bundle' => $this->contentType->id(),
115 // Update the displays so that configuration does not change unexpectedly on
117 entity_get_form_display('node', $this->contentType->id(), 'default')
118 ->setComponent($this->fieldName, [
119 'type' => 'text_textfield',
122 entity_get_display('node', $this->contentType->id(), 'full')
123 ->setComponent($this->fieldName)
125 entity_get_display('node', $this->contentType->id(), 'default')
126 ->setComponent($this->fieldName)
128 entity_get_display('node', $this->contentType->id(), 'teaser')
129 ->removeComponent($this->fieldName)
132 $this->drupalGet('node/add/' . $this->contentType->id());
133 $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
135 // Export the configuration.
136 $this->drupalPostForm('admin/config/development/configuration/full/export', [], 'Export');
137 $this->tarball = $this->getSession()->getPage()->getContent();
139 $this->config('system.site')
140 ->set('slogan', $this->originalSlogan)
142 $this->assertEqual($this->config('system.site')->get('slogan'), $this->originalSlogan);
144 // Delete the custom field.
145 $fields = FieldConfig::loadMultiple();
146 foreach ($fields as $field) {
147 if ($field->getName() == $this->fieldName) {
151 $field_storages = FieldStorageConfig::loadMultiple();
152 foreach ($field_storages as $field_storage) {
153 if ($field_storage->getName() == $this->fieldName) {
154 $field_storage->delete();
157 $this->drupalGet('node/add/' . $this->contentType->id());
158 $this->assertNoFieldByName("{$this->fieldName}[0][value]", '', 'Widget is not displayed');
160 // Import the configuration.
161 $filename = 'temporary://' . $this->randomMachineName();
162 file_put_contents($filename, $this->tarball);
163 $this->drupalPostForm('admin/config/development/configuration/full/import', ['files[import_tarball]' => $filename], 'Upload');
164 // There is no snapshot yet because an import has never run.
165 $this->assertNoText(t('Warning message'));
166 $this->assertNoText(t('There are no configuration changes to import.'));
167 $this->assertText($this->contentType->label());
169 $this->drupalPostForm(NULL, [], 'Import all');
170 // After importing the snapshot has been updated an there are no warnings.
171 $this->assertNoText(t('Warning message'));
172 $this->assertText(t('There are no configuration changes to import.'));
174 $this->assertEqual($this->config('system.site')->get('slogan'), $this->newSlogan);
176 $this->drupalGet('node/add');
177 $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
179 $this->config('system.site')
180 ->set('slogan', $this->originalSlogan)
182 $this->drupalGet('admin/config/development/configuration');
183 $this->assertText(t('Warning message'));
184 $this->assertText('The following items in your active configuration have changes since the last import that may be lost on the next import.');
185 // Ensure the item is displayed as part of a list (to avoid false matches
186 // on the rest of the page) and that the list markup is not escaped.
187 $this->assertRaw('<li>system.site</li>');
188 // Remove everything from sync. The warning about differences between the
189 // active and snapshot should no longer exist.
190 \Drupal::service('config.storage.sync')->deleteAll();
191 $this->drupalGet('admin/config/development/configuration');
192 $this->assertNoText(t('Warning message'));
193 $this->assertNoText('The following items in your active configuration have changes since the last import that may be lost on the next import.');
194 $this->assertText(t('There are no configuration changes to import.'));
195 // Write a file to sync. The warning about differences between the active
196 // and snapshot should now exist.
197 /** @var \Drupal\Core\Config\StorageInterface $sync */
198 $sync = $this->container->get('config.storage.sync');
199 $data = $this->config('system.site')->get();
200 $data['slogan'] = 'in the face';
201 $this->copyConfig($this->container->get('config.storage'), $sync);
202 $sync->write('system.site', $data);
203 $this->drupalGet('admin/config/development/configuration');
204 $this->assertText(t('Warning message'));
205 $this->assertText('The following items in your active configuration have changes since the last import that may be lost on the next import.');
206 // Ensure the item is displayed as part of a list (to avoid false matches
207 // on the rest of the page) and that the list markup is not escaped.
208 $this->assertRaw('<li>system.site</li>');
212 * Tests an export and import of collections.
214 public function testExportImportCollections() {
216 /** @var \Drupal\Core\Config\StorageInterface $active_storage */
217 $active_storage = \Drupal::service('config.storage');
218 $test1_storage = $active_storage->createCollection('collection.test1');
219 $test1_storage->write('config_test.create', ['foo' => 'bar']);
220 $test1_storage->write('config_test.update', ['foo' => 'bar']);
221 $test2_storage = $active_storage->createCollection('collection.test2');
222 $test2_storage->write('config_test.another_create', ['foo' => 'bar']);
223 $test2_storage->write('config_test.another_update', ['foo' => 'bar']);
225 // Export the configuration.
226 $this->drupalPostForm('admin/config/development/configuration/full/export', [], 'Export');
227 $this->tarball = $this->getSession()->getPage()->getContent();
228 $filename = file_directory_temp() . '/' . $this->randomMachineName();
229 file_put_contents($filename, $this->tarball);
231 // Set up the active storage collections to test import.
232 $test1_storage->delete('config_test.create');
233 $test1_storage->write('config_test.update', ['foo' => 'baz']);
234 $test1_storage->write('config_test.delete', ['foo' => 'bar']);
235 $test2_storage->delete('config_test.another_create');
236 $test2_storage->write('config_test.another_update', ['foo' => 'baz']);
237 $test2_storage->write('config_test.another_delete', ['foo' => 'bar']);
239 // Create a snapshot.
240 $snapshot_storage = \Drupal::service('config.storage.snapshot');
241 \Drupal::service('config.manager')->createSnapshot($active_storage, $snapshot_storage);
243 // Ensure that the snapshot has the expected collection data before import.
244 $test1_snapshot = $snapshot_storage->createCollection('collection.test1');
245 $data = $test1_snapshot->read('config_test.delete');
246 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.delete in collection.test1 exists in the snapshot storage.');
247 $data = $test1_snapshot->read('config_test.update');
248 $this->assertEqual($data, ['foo' => 'baz'], 'The config_test.update in collection.test1 exists in the snapshot storage.');
249 $this->assertFalse($test1_snapshot->read('config_test.create'), 'The config_test.create in collection.test1 does not exist in the snapshot storage.');
250 $test2_snapshot = $snapshot_storage->createCollection('collection.test2');
251 $data = $test2_snapshot->read('config_test.another_delete');
252 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_delete in collection.test2 exists in the snapshot storage.');
253 $data = $test2_snapshot->read('config_test.another_update');
254 $this->assertEqual($data, ['foo' => 'baz'], 'The config_test.another_update in collection.test2 exists in the snapshot storage.');
255 $this->assertFalse($test2_snapshot->read('config_test.another_create'), 'The config_test.another_create in collection.test2 does not exist in the snapshot storage.');
257 // Create the tar that contains the expected content for the collections.
258 $tar = new ArchiveTar($filename, 'gz');
259 $content_list = $tar->listContent();
260 // Convert the list of files into something easy to search.
262 foreach ($content_list as $file) {
263 $files[] = $file['filename'];
265 $this->assertTrue(in_array('collection/test1/config_test.create.yml', $files), 'Config export contains collection/test1/config_test.create.yml.');
266 $this->assertTrue(in_array('collection/test2/config_test.another_create.yml', $files), 'Config export contains collection/test2/config_test.another_create.yml.');
267 $this->assertTrue(in_array('collection/test1/config_test.update.yml', $files), 'Config export contains collection/test1/config_test.update.yml.');
268 $this->assertTrue(in_array('collection/test2/config_test.another_update.yml', $files), 'Config export contains collection/test2/config_test.another_update.yml.');
269 $this->assertFalse(in_array('collection/test1/config_test.delete.yml', $files), 'Config export does not contain collection/test1/config_test.delete.yml.');
270 $this->assertFalse(in_array('collection/test2/config_test.another_delete.yml', $files), 'Config export does not contain collection/test2/config_test.another_delete.yml.');
272 $this->drupalPostForm('admin/config/development/configuration/full/import', ['files[import_tarball]' => $filename], 'Upload');
273 // Verify that there are configuration differences to import.
274 $this->drupalGet('admin/config/development/configuration');
275 $this->assertNoText(t('There are no configuration changes to import.'));
276 $this->assertText(t('@collection configuration collection', ['@collection' => 'collection.test1']));
277 $this->assertText(t('@collection configuration collection', ['@collection' => 'collection.test2']));
278 $this->assertText('config_test.create');
279 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.create');
280 $this->assertText('config_test.update');
281 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.update');
282 $this->assertText('config_test.delete');
283 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test1/config_test.delete');
284 $this->assertText('config_test.another_create');
285 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_create');
286 $this->assertText('config_test.another_update');
287 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_update');
288 $this->assertText('config_test.another_delete');
289 $this->assertLinkByHref('admin/config/development/configuration/sync/diff_collection/collection.test2/config_test.another_delete');
291 $this->drupalPostForm(NULL, [], 'Import all');
292 $this->assertText(t('There are no configuration changes to import.'));
294 // Test data in collections.
295 $data = $test1_storage->read('config_test.create');
296 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.create in collection.test1 has been created.');
297 $data = $test1_storage->read('config_test.update');
298 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.update in collection.test1 has been updated.');
299 $this->assertFalse($test1_storage->read('config_test.delete'), 'The config_test.delete in collection.test1 has been deleted.');
301 $data = $test2_storage->read('config_test.another_create');
302 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_create in collection.test2 has been created.');
303 $data = $test2_storage->read('config_test.another_update');
304 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_update in collection.test2 has been updated.');
305 $this->assertFalse($test2_storage->read('config_test.another_delete'), 'The config_test.another_delete in collection.test2 has been deleted.');
307 // Ensure that the snapshot has been updated with the collection data.
308 $snapshot_storage = \Drupal::service('config.storage.snapshot');
309 $test1_snapshot = $snapshot_storage->createCollection('collection.test1');
310 $data = $test1_snapshot->read('config_test.create');
311 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.create in collection.test1 has been created in the snapshot storage.');
312 $data = $test1_snapshot->read('config_test.update');
313 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.update in collection.test1 has been updated in the snapshot storage.');
314 $this->assertFalse($test1_snapshot->read('config_test.delete'), 'The config_test.delete in collection.test1 does not exist in the snapshot storage.');
315 $test2_snapshot = $snapshot_storage->createCollection('collection.test2');
316 $data = $test2_snapshot->read('config_test.another_create');
317 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_create in collection.test2 has been created in the snapshot storage.');
318 $data = $test2_snapshot->read('config_test.another_update');
319 $this->assertEqual($data, ['foo' => 'bar'], 'The config_test.another_update in collection.test2 has been updated in the snapshot storage.');
320 $this->assertFalse($test2_snapshot->read('config_test.another_delete'), 'The config_test.another_delete in collection.test2 does not exist in the snapshot storage.');