3 namespace Drupal\config\Tests;
5 use Drupal\Core\Serialization\Yaml;
6 use Drupal\simpletest\WebTestBase;
9 * Tests the user interface for importing/exporting a single configuration.
13 class ConfigSingleImportExportTest extends WebTestBase {
20 public static $modules = [
24 // Adding language module makes it possible to involve non-default
25 // (language.xx) collections in import/export operations.
29 protected function setUp() {
32 $this->drupalPlaceBlock('page_title_block');
36 * Tests importing a single configuration file.
38 public function testImport() {
39 $storage = \Drupal::entityManager()->getStorage('config_test');
40 $uuid = \Drupal::service('uuid');
42 $this->drupalLogin($this->drupalCreateUser(['import configuration']));
44 // Attempt an import with invalid YAML.
46 'config_type' => 'action',
50 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
51 // Assert the static portion of the error since different parsers could give different text in their error.
52 $this->assertText('The import failed with the following message: ');
61 'config_type' => 'config_test',
64 // Attempt an import with a missing ID.
65 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
66 $this->assertText(t('Missing ID key "@id_key" for this @entity_type import.', ['@id_key' => 'id', '@entity_type' => 'Test configuration']));
68 // Perform an import with no specified UUID and a unique ID.
69 $this->assertNull($storage->load('first'));
70 $edit['import'] = "id: first\n" . $edit['import'];
71 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
72 $this->assertRaw(t('Are you sure you want to create a new %name @type?', ['%name' => 'first', '@type' => 'test configuration']));
73 $this->drupalPostForm(NULL, [], t('Confirm'));
74 $entity = $storage->load('first');
75 $this->assertIdentical($entity->label(), 'First');
76 $this->assertIdentical($entity->id(), 'first');
77 $this->assertTrue($entity->status());
78 $this->assertRaw(t('The configuration was imported successfully.'));
80 // Attempt an import with an existing ID but missing UUID.
81 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
82 $this->assertText(t('An entity with this machine name already exists but the import did not specify a UUID.'));
84 // Attempt an import with a mismatched UUID and existing ID.
85 $edit['import'] .= "\nuuid: " . $uuid->generate();
86 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
87 $this->assertText(t('An entity with this machine name already exists but the UUID does not match.'));
89 // Attempt an import with a custom ID.
90 $edit['custom_entity_id'] = 'custom_id';
91 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
92 $this->assertRaw(t('Are you sure you want to create a new %name @type?', ['%name' => 'custom_id', '@type' => 'test configuration']));
93 $this->drupalPostForm(NULL, [], t('Confirm'));
94 $this->assertRaw(t('The configuration was imported successfully.'));
96 // Perform an import with a unique ID and UUID.
105 'config_type' => 'config_test',
108 $second_uuid = $uuid->generate();
109 $edit['import'] .= "\nuuid: " . $second_uuid;
110 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
111 $this->assertRaw(t('Are you sure you want to create a new %name @type?', ['%name' => 'second', '@type' => 'test configuration']));
112 $this->drupalPostForm(NULL, [], t('Confirm'));
113 $entity = $storage->load('second');
114 $this->assertRaw(t('The configuration was imported successfully.'));
115 $this->assertIdentical($entity->label(), 'Second');
116 $this->assertIdentical($entity->id(), 'second');
117 $this->assertFalse($entity->status());
118 $this->assertIdentical($entity->uuid(), $second_uuid);
120 // Perform an update.
124 label: 'Second updated'
130 'config_type' => 'config_test',
133 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
134 $this->assertRaw(t('Are you sure you want to update the %name @type?', ['%name' => 'second', '@type' => 'test configuration']));
135 $this->drupalPostForm(NULL, [], t('Confirm'));
136 $entity = $storage->load('second');
137 $this->assertRaw(t('The configuration was imported successfully.'));
138 $this->assertIdentical($entity->label(), 'Second updated');
140 // Try to perform an update which adds missing dependencies.
144 label: 'Second updated'
153 'config_type' => 'config_test',
156 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
157 $this->assertRaw(t('Configuration %name depends on the %owner module that will not be installed after import.', ['%name' => 'config_test.dynamic.second', '%owner' => 'does_not_exist']));
159 // Try to preform an update which would create a PHP object if Yaml parsing
160 // not securely set up.
161 // Perform an update.
165 label: !php/object "O:36:\"Drupal\\\Core\\\Test\\\ObjectSerialization\":0:{}"
171 'config_type' => 'config_test',
174 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
175 $this->assertRaw(t('Are you sure you want to update the %name @type?', ['%name' => 'second', '@type' => 'test configuration']));
176 $this->drupalPostForm(NULL, [], t('Confirm'));
177 $entity = $storage->load('second');
178 $this->assertRaw(t('The configuration was imported successfully.'));
179 $this->assertTrue(is_string($entity->label()), 'Entity label is a string');
180 $this->assertTrue(strpos($entity->label(), 'ObjectSerialization') > 0, 'Label contains serialized object');
184 * Tests importing a simple configuration file.
186 public function testImportSimpleConfiguration() {
187 $this->drupalLogin($this->drupalCreateUser(['import configuration']));
188 $config = $this->config('system.site')->set('name', 'Test simple import');
190 // Place branding block with site name into header region.
191 $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
194 'config_type' => 'system.simple',
195 'config_name' => $config->getName(),
196 'import' => Yaml::encode($config->get()),
198 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
199 $this->assertRaw(t('Are you sure you want to update the %name @type?', ['%name' => $config->getName(), '@type' => 'simple configuration']));
200 $this->drupalPostForm(NULL, [], t('Confirm'));
201 $this->drupalGet('');
202 $this->assertText('Test simple import');
204 // Ensure that ConfigImporter validation is running when importing simple
206 $config_data = $this->config('core.extension')->get();
207 // Simulate uninstalling the Config module.
208 unset($config_data['module']['config']);
210 'config_type' => 'system.simple',
211 'config_name' => 'core.extension',
212 'import' => Yaml::encode($config_data),
214 $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
215 $this->assertText(t('Can not uninstall the Configuration module as part of a configuration synchronization through the user interface.'));
220 * Tests exporting a single configuration file.
222 public function testExport() {
223 $this->drupalLogin($this->drupalCreateUser(['export configuration']));
225 $this->drupalGet('admin/config/development/configuration/single/export/system.simple');
226 $this->assertFieldByXPath('//select[@name="config_type"]//option[@selected="selected"]', t('Simple configuration'), 'The simple configuration option is selected when specified in the URL.');
227 // Spot check several known simple configuration files.
228 $element = $this->xpath('//select[@name="config_name"]');
229 $options = $this->getAllOptions($element[0]);
230 $expected_options = ['system.site', 'user.settings'];
231 foreach ($options as &$option) {
232 $option = (string) $option;
234 $this->assertIdentical($expected_options, array_intersect($expected_options, $options), 'The expected configuration files are listed.');
236 $this->drupalGet('admin/config/development/configuration/single/export/system.simple/system.image');
237 $this->assertFieldByXPath('//textarea[@name="export"]', "toolkit: gd\n_core:\n default_config_hash: durWHaKeBaq4d9Wpi4RqwADj1OufDepcnJuhVLmKN24\n", 'The expected system configuration is displayed.');
239 $this->drupalGet('admin/config/development/configuration/single/export/date_format');
240 $this->assertFieldByXPath('//select[@name="config_type"]//option[@selected="selected"]', t('Date format'), 'The date format entity type is selected when specified in the URL.');
242 $this->drupalGet('admin/config/development/configuration/single/export/date_format/fallback');
243 $this->assertFieldByXPath('//select[@name="config_name"]//option[@selected="selected"]', t('Fallback date format (fallback)'), 'The fallback date format config entity is selected when specified in the URL.');
245 $fallback_date = \Drupal::entityManager()->getStorage('date_format')->load('fallback');
246 $yaml_text = (string) $this->xpath('//textarea[@name="export"]')[0];
247 $this->assertEqual(Yaml::decode($yaml_text), $fallback_date->toArray(), 'The fallback date format config entity export code is displayed.');