Pull merge.
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigExportImportUITest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\Core\Archiver\ArchiveTar;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests the user interface for importing/exporting configuration.
12  *
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
15  * the next rebuild.
16  *
17  * @group config
18  */
19 class ConfigExportImportUITest extends BrowserTestBase {
20
21   /**
22    * The contents of the config export tarball, held between test methods.
23    *
24    * @var string
25    */
26   protected $tarball;
27
28   /**
29    * Holds the original 'site slogan' before testing.
30    *
31    * @var string
32    */
33   protected $originalSlogan;
34
35   /**
36    * Holds a randomly generated new 'site slogan' for testing.
37    *
38    * @var string
39    */
40   protected $newSlogan;
41
42
43   /**
44    * Holds a content type.
45    *
46    * @var \Drupal\node\NodeInterface
47    */
48   protected $contentType;
49
50   /**
51    * Holds the randomly-generated name of a field.
52    *
53    * @var string
54    */
55   protected $fieldName;
56
57   /**
58    * Holds the field storage entity for $fieldName.
59    *
60    * @var \Drupal\field\FieldStorageConfigInterface
61    */
62   protected $fieldStorage;
63
64   /**
65    * Modules to enable.
66    *
67    * @var array
68    */
69   public static $modules = ['config', 'node', 'field'];
70
71   /**
72    * {@inheritdoc}
73    */
74   protected function setUp() {
75     parent::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);
81   }
82
83   /**
84    * Tests a simple site export import case.
85    */
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.'));
91
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)
97       ->save();
98     $this->assertEqual($this->config('system.site')->get('slogan'), $this->newSlogan);
99
100     // Create a content type.
101     $this->contentType = $this->drupalCreateContentType();
102
103     // Create a field.
104     $this->fieldName = mb_strtolower($this->randomMachineName());
105     $this->fieldStorage = FieldStorageConfig::create([
106       'field_name' => $this->fieldName,
107       'entity_type' => 'node',
108       'type' => 'text',
109     ]);
110     $this->fieldStorage->save();
111     FieldConfig::create([
112       'field_storage' => $this->fieldStorage,
113       'bundle' => $this->contentType->id(),
114     ])->save();
115     // Update the displays so that configuration does not change unexpectedly on
116     // import.
117     entity_get_form_display('node', $this->contentType->id(), 'default')
118       ->setComponent($this->fieldName, [
119         'type' => 'text_textfield',
120       ])
121       ->save();
122     entity_get_display('node', $this->contentType->id(), 'full')
123       ->setComponent($this->fieldName)
124       ->save();
125     entity_get_display('node', $this->contentType->id(), 'default')
126       ->setComponent($this->fieldName)
127       ->save();
128     entity_get_display('node', $this->contentType->id(), 'teaser')
129       ->removeComponent($this->fieldName)
130       ->save();
131
132     $this->drupalGet('node/add/' . $this->contentType->id());
133     $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
134
135     // Export the configuration.
136     $this->drupalPostForm('admin/config/development/configuration/full/export', [], 'Export');
137     $this->tarball = $this->getSession()->getPage()->getContent();
138
139     $this->config('system.site')
140       ->set('slogan', $this->originalSlogan)
141       ->save();
142     $this->assertEqual($this->config('system.site')->get('slogan'), $this->originalSlogan);
143
144     // Delete the custom field.
145     $fields = FieldConfig::loadMultiple();
146     foreach ($fields as $field) {
147       if ($field->getName() == $this->fieldName) {
148         $field->delete();
149       }
150     }
151     $field_storages = FieldStorageConfig::loadMultiple();
152     foreach ($field_storages as $field_storage) {
153       if ($field_storage->getName() == $this->fieldName) {
154         $field_storage->delete();
155       }
156     }
157     $this->drupalGet('node/add/' . $this->contentType->id());
158     $this->assertNoFieldByName("{$this->fieldName}[0][value]", '', 'Widget is not displayed');
159
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());
168
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.'));
173
174     $this->assertEqual($this->config('system.site')->get('slogan'), $this->newSlogan);
175
176     $this->drupalGet('node/add');
177     $this->assertFieldByName("{$this->fieldName}[0][value]", '', 'Widget is displayed');
178
179     $this->config('system.site')
180       ->set('slogan', $this->originalSlogan)
181       ->save();
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>');
209   }
210
211   /**
212    * Tests an export and import of collections.
213    */
214   public function testExportImportCollections() {
215
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']);
224
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);
230
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']);
238
239     // Create a snapshot.
240     $snapshot_storage = \Drupal::service('config.storage.snapshot');
241     \Drupal::service('config.manager')->createSnapshot($active_storage, $snapshot_storage);
242
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.');
256
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.
261     $files = [];
262     foreach ($content_list as $file) {
263       $files[] = $file['filename'];
264     }
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.');
271
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');
290
291     $this->drupalPostForm(NULL, [], 'Import all');
292     $this->assertText(t('There are no configuration changes to import.'));
293
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.');
300
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.');
306
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.');
321   }
322
323 }