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