3 namespace Drupal\config\Tests;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\Core\Config\InstallStorage;
8 use Drupal\simpletest\WebTestBase;
11 * Tests the user interface for importing/exporting configuration.
15 class ConfigImportUITest extends WebTestBase {
22 public static $modules = ['config', 'config_test', 'config_import_test', 'text', 'options'];
25 * A user with the 'synchronize configuration' permission.
27 * @var \Drupal\user\UserInterface
31 protected function setUp() {
34 $this->webUser = $this->drupalCreateUser(['synchronize configuration']);
35 $this->drupalLogin($this->webUser);
36 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
40 * Tests importing configuration.
42 public function testImport() {
43 $name = 'system.site';
44 $dynamic_name = 'config_test.dynamic.new';
45 /** @var \Drupal\Core\Config\StorageInterface $sync */
46 $sync = $this->container->get('config.storage.sync');
48 $this->drupalGet('admin/config/development/configuration');
49 $this->assertText('There are no configuration changes to import.');
50 $this->assertNoFieldById('edit-submit', t('Import all'));
52 // Create updated configuration object.
53 $new_site_name = 'Config import test ' . $this->randomString();
54 $this->prepareSiteNameUpdate($new_site_name);
55 $this->assertIdentical($sync->exists($name), TRUE, $name . ' found.');
57 // Create new config entity.
58 $original_dynamic_data = [
59 'uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651',
60 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
69 'protected_property' => '',
71 $sync->write($dynamic_name, $original_dynamic_data);
72 $this->assertIdentical($sync->exists($dynamic_name), TRUE, $dynamic_name . ' found.');
74 // Enable the Action and Ban modules during import. The Ban
75 // module is used because it creates a table during the install. The Action
76 // module is used because it creates a single simple configuration file
77 // during the install.
78 $core_extension = $this->config('core.extension')->get();
79 $core_extension['module']['action'] = 0;
80 $core_extension['module']['ban'] = 0;
81 $core_extension['module'] = module_config_sort($core_extension['module']);
82 // Bartik is a subtheme of classy so classy must be enabled.
83 $core_extension['theme']['classy'] = 0;
84 $core_extension['theme']['bartik'] = 0;
85 $sync->write('core.extension', $core_extension);
87 // Use the install storage so that we can read configuration from modules
88 // and themes that are not installed.
89 $install_storage = new InstallStorage();
91 // Set the Bartik theme as default.
92 $system_theme = $this->config('system.theme')->get();
93 $system_theme['default'] = 'bartik';
94 $sync->write('system.theme', $system_theme);
96 // Read the action config from module default config folder.
97 $action_settings = $install_storage->read('action.settings');
98 $action_settings['recursion_limit'] = 50;
99 $sync->write('action.settings', $action_settings);
101 // Uninstall the Options and Text modules to ensure that dependencies are
102 // handled correctly. Options depends on Text so Text should be installed
103 // first. Since they were enabled during the test setup the core.extension
104 // file in sync will already contain them.
105 \Drupal::service('module_installer')->uninstall(['text', 'options']);
107 // Set the state system to record installations and uninstallations.
108 \Drupal::state()->set('ConfigImportUITest.core.extension.modules_installed', []);
109 \Drupal::state()->set('ConfigImportUITest.core.extension.modules_uninstalled', []);
111 // Verify that both appear as ready to import.
112 $this->drupalGet('admin/config/development/configuration');
113 $this->assertRaw('<td>' . $name);
114 $this->assertRaw('<td>' . $dynamic_name);
115 $this->assertRaw('<td>core.extension');
116 $this->assertRaw('<td>system.theme');
117 $this->assertRaw('<td>action.settings');
118 $this->assertFieldById('edit-submit', t('Import all'));
120 // Import and verify that both do not appear anymore.
121 $this->drupalPostForm(NULL, [], t('Import all'));
122 $this->assertNoRaw('<td>' . $name);
123 $this->assertNoRaw('<td>' . $dynamic_name);
124 $this->assertNoRaw('<td>core.extension');
125 $this->assertNoRaw('<td>system.theme');
126 $this->assertNoRaw('<td>action.settings');
128 $this->assertNoFieldById('edit-submit', t('Import all'));
130 // Verify that there are no further changes to import.
131 $this->assertText(t('There are no configuration changes to import.'));
133 // Verify site name has changed.
134 $this->assertIdentical($new_site_name, $this->config('system.site')->get('name'));
136 // Verify that new config entity exists.
137 $this->assertIdentical($original_dynamic_data, $this->config($dynamic_name)->get());
139 // Verify the cache got cleared.
140 $this->assertTrue(isset($GLOBALS['hook_cache_flush']));
142 $this->rebuildContainer();
143 $this->assertTrue(\Drupal::moduleHandler()->moduleExists('ban'), 'Ban module installed during import.');
144 $this->assertTrue(\Drupal::database()->schema()->tableExists('ban_ip'), 'The database table ban_ip exists.');
145 $this->assertTrue(\Drupal::moduleHandler()->moduleExists('action'), 'Action module installed during import.');
146 $this->assertTrue(\Drupal::moduleHandler()->moduleExists('options'), 'Options module installed during import.');
147 $this->assertTrue(\Drupal::moduleHandler()->moduleExists('text'), 'Text module installed during import.');
149 $theme_info = \Drupal::service('theme_handler')->listInfo();
150 $this->assertTrue($theme_info['bartik']->status, 'Bartik theme installed during import.');
152 // Ensure installations and uninstallation occur as expected.
153 $installed = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_installed', []);
154 $uninstalled = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_uninstalled', []);
155 $expected = ['action', 'ban', 'text', 'options'];
156 $this->assertIdentical($expected, $installed, 'Action, Ban, Text and Options modules installed in the correct order.');
157 $this->assertTrue(empty($uninstalled), 'No modules uninstalled during import');
159 // Verify that the action.settings configuration object was only written
160 // once during the import process and only with the value set in the staged
161 // configuration. This verifies that the module's default configuration is
162 // used during configuration import and, additionally, that after installing
163 // a module, that configuration is not synced twice.
164 $recursion_limit_values = \Drupal::state()->get('ConfigImportUITest.action.settings.recursion_limit', []);
165 $this->assertIdentical($recursion_limit_values, [50]);
167 $core_extension = $this->config('core.extension')->get();
168 unset($core_extension['module']['action']);
169 unset($core_extension['module']['ban']);
170 unset($core_extension['module']['options']);
171 unset($core_extension['module']['text']);
172 unset($core_extension['theme']['bartik']);
173 $sync->write('core.extension', $core_extension);
174 $sync->delete('action.settings');
175 $sync->delete('text.settings');
177 $system_theme = $this->config('system.theme')->get();
178 $system_theme['default'] = 'stark';
179 $system_theme['admin'] = 'stark';
180 $sync->write('system.theme', $system_theme);
182 // Set the state system to record installations and uninstallations.
183 \Drupal::state()->set('ConfigImportUITest.core.extension.modules_installed', []);
184 \Drupal::state()->set('ConfigImportUITest.core.extension.modules_uninstalled', []);
186 // Verify that both appear as ready to import.
187 $this->drupalGet('admin/config/development/configuration');
188 $this->assertRaw('<td>core.extension');
189 $this->assertRaw('<td>system.theme');
190 $this->assertRaw('<td>action.settings');
192 // Import and verify that both do not appear anymore.
193 $this->drupalPostForm(NULL, [], t('Import all'));
194 $this->assertNoRaw('<td>core.extension');
195 $this->assertNoRaw('<td>system.theme');
196 $this->assertNoRaw('<td>action.settings');
198 $this->rebuildContainer();
199 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('ban'), 'Ban module uninstalled during import.');
200 $this->assertFalse(\Drupal::database()->schema()->tableExists('ban_ip'), 'The database table ban_ip does not exist.');
201 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('action'), 'Action module uninstalled during import.');
202 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('options'), 'Options module uninstalled during import.');
203 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('text'), 'Text module uninstalled during import.');
205 // Ensure installations and uninstallation occur as expected.
206 $installed = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_installed', []);
207 $uninstalled = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_uninstalled', []);
208 $expected = ['options', 'text', 'ban', 'action'];
209 $this->assertIdentical($expected, $uninstalled, 'Options, Text, Ban and Action modules uninstalled in the correct order.');
210 $this->assertTrue(empty($installed), 'No modules installed during import');
212 $theme_info = \Drupal::service('theme_handler')->listInfo();
213 $this->assertFalse(isset($theme_info['bartik']), 'Bartik theme uninstalled during import.');
215 // Verify that the action.settings configuration object was only deleted
216 // once during the import process.
217 $delete_called = \Drupal::state()->get('ConfigImportUITest.action.settings.delete', 0);
218 $this->assertIdentical($delete_called, 1, "The action.settings configuration was deleted once during configuration import.");
222 * Tests concurrent importing of configuration.
224 public function testImportLock() {
225 // Create updated configuration object.
226 $new_site_name = 'Config import test ' . $this->randomString();
227 $this->prepareSiteNameUpdate($new_site_name);
229 // Verify that there are configuration differences to import.
230 $this->drupalGet('admin/config/development/configuration');
231 $this->assertNoText(t('There are no configuration changes to import.'));
233 // Acquire a fake-lock on the import mechanism.
234 $config_importer = $this->configImporter();
235 $this->container->get('lock.persistent')->acquire($config_importer::LOCK_NAME);
237 // Attempt to import configuration and verify that an error message appears.
238 $this->drupalPostForm(NULL, [], t('Import all'));
239 $this->assertText(t('Another request may be synchronizing configuration already.'));
241 // Release the lock, just to keep testing sane.
242 $this->container->get('lock.persistent')->release($config_importer::LOCK_NAME);
244 // Verify site name has not changed.
245 $this->assertNotEqual($new_site_name, $this->config('system.site')->get('name'));
249 * Tests verification of site UUID before importing configuration.
251 public function testImportSiteUuidValidation() {
252 $sync = \Drupal::service('config.storage.sync');
253 // Create updated configuration object.
254 $config_data = $this->config('system.site')->get();
255 // Generate a new site UUID.
256 $config_data['uuid'] = \Drupal::service('uuid')->generate();
257 $sync->write('system.site', $config_data);
259 // Verify that there are configuration differences to import.
260 $this->drupalGet('admin/config/development/configuration');
261 $this->assertText(t('The staged configuration cannot be imported, because it originates from a different site than this site. You can only synchronize configuration between cloned instances of this site.'));
262 $this->assertNoFieldById('edit-submit', t('Import all'));
266 * Tests the screen that shows differences between active and sync.
268 public function testImportDiff() {
269 $sync = $this->container->get('config.storage.sync');
270 $config_name = 'config_test.system';
274 $add_data = '<em>bangpow</em>';
275 $change_data = '<p><em>foobar</em></p>';
277 'foo' => '<p>foobar</p>',
278 'baz' => '<strong>no change</strong>',
279 '404' => '<em>herp</em>',
281 // Update active storage to have html in config data.
282 $this->config($config_name)->setData($original_data)->save();
284 // Change a configuration value in sync.
285 $sync_data = $original_data;
286 $sync_data[$change_key] = $change_data;
287 $sync_data[$add_key] = $add_data;
288 unset($sync_data[$remove_key]);
289 $sync->write($config_name, $sync_data);
291 // Load the diff UI and verify that the diff reflects the change.
292 $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
293 $this->assertNoRaw('&nbsp;');
294 $this->assertTitle(format_string('View changes of @config_name | Drupal', ['@config_name' => $config_name]));
296 // The following assertions do not use $this::assertEscaped() because
297 // \Drupal\Component\Diff\DiffFormatter adds markup that signifies what has
300 // Changed values are escaped.
301 $this->assertText(Html::escape("foo: '<p><em>foobar</em></p>'"));
302 $this->assertText(Html::escape("foo: '<p>foobar</p>'"));
303 // The no change values are escaped.
304 $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
305 // Added value is escaped.
306 $this->assertText(Html::escape("biff: '<em>bangpow</em>'"));
307 // Deleted value is escaped.
308 $this->assertText(Html::escape("404: '<em>herp</em>'"));
310 // Verify diff colors are displayed.
311 $result = $this->xpath('//table[contains(@class, :class)]', [':class' => 'diff']);
312 $this->assertEqual(count($result), 1, "Diff UI is displaying colors.");
314 // Reset data back to original, and remove a key
315 $sync_data = $original_data;
316 unset($sync_data[$remove_key]);
317 $sync->write($config_name, $sync_data);
319 // Load the diff UI and verify that the diff reflects a removed key.
320 $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
321 // The no change values are escaped.
322 $this->assertText(Html::escape("foo: '<p>foobar</p>'"));
323 $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
324 // Removed key is escaped.
325 $this->assertText(Html::escape("404: '<em>herp</em>'"));
327 // Reset data back to original and add a key
328 $sync_data = $original_data;
329 $sync_data[$add_key] = $add_data;
330 $sync->write($config_name, $sync_data);
332 // Load the diff UI and verify that the diff reflects an added key.
333 $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
334 // The no change values are escaped.
335 $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
336 $this->assertText(Html::escape("404: '<em>herp</em>'"));
337 // Added key is escaped.
338 $this->assertText(Html::escape("biff: '<em>bangpow</em>'"));
342 * Tests that multiple validation errors are listed on the page.
344 public function testImportValidation() {
345 // Set state value so that
346 // \Drupal\config_import_test\EventSubscriber::onConfigImportValidate() logs
347 // validation errors.
348 \Drupal::state()->set('config_import_test.config_import_validate_fail', TRUE);
349 // Ensure there is something to import.
350 $new_site_name = 'Config import test ' . $this->randomString();
351 $this->prepareSiteNameUpdate($new_site_name);
353 $this->drupalGet('admin/config/development/configuration');
354 $this->assertNoText(t('There are no configuration changes to import.'));
355 $this->drupalPostForm(NULL, [], t('Import all'));
357 // Verify that the validation messages appear.
358 $this->assertText('The configuration cannot be imported because it failed validation for the following reasons:');
359 $this->assertText('Config import validate error 1.');
360 $this->assertText('Config import validate error 2.');
362 // Verify site name has not changed.
363 $this->assertNotEqual($new_site_name, $this->config('system.site')->get('name'));
366 public function testConfigUninstallConfigException() {
367 $sync = $this->container->get('config.storage.sync');
369 $core_extension = $this->config('core.extension')->get();
370 unset($core_extension['module']['config']);
371 $sync->write('core.extension', $core_extension);
373 $this->drupalGet('admin/config/development/configuration');
374 $this->assertText('core.extension');
376 // Import and verify that both do not appear anymore.
377 $this->drupalPostForm(NULL, [], t('Import all'));
378 $this->assertText('Can not uninstall the Configuration module as part of a configuration synchronization through the user interface.');
381 public function prepareSiteNameUpdate($new_site_name) {
382 $sync = $this->container->get('config.storage.sync');
383 // Create updated configuration object.
384 $config_data = $this->config('system.site')->get();
385 $config_data['name'] = $new_site_name;
386 $sync->write('system.site', $config_data);
390 * Tests an import that results in an error.
392 public function testImportErrorLog() {
393 $name_primary = 'config_test.dynamic.primary';
394 $name_secondary = 'config_test.dynamic.secondary';
395 $sync = $this->container->get('config.storage.sync');
396 $uuid = $this->container->get('uuid');
399 'uuid' => $uuid->generate(),
402 'dependencies' => [],
404 'label' => 'Primary',
408 'size_value' => NULL,
409 'protected_property' => NULL,
411 $sync->write($name_primary, $values_primary);
412 $values_secondary = [
413 'uuid' => $uuid->generate(),
416 // Add a dependency on primary, to ensure that is synced first.
418 'config' => [$name_primary],
421 'label' => 'Secondary Sync',
425 'size_value' => NULL,
426 'protected_property' => NULL,
428 $sync->write($name_secondary, $values_secondary);
429 // Verify that there are configuration differences to import.
430 $this->drupalGet('admin/config/development/configuration');
431 $this->assertNoText(t('There are no configuration changes to import.'));
433 // Attempt to import configuration and verify that an error message appears.
434 $this->drupalPostForm(NULL, [], t('Import all'));
435 $this->assertText(SafeMarkup::format('Deleted and replaced configuration entity "@name"', ['@name' => $name_secondary]));
436 $this->assertText(t('The configuration was imported with errors.'));
437 $this->assertNoText(t('The configuration was imported successfully.'));
438 $this->assertText(t('There are no configuration changes to import.'));
442 * Tests the config importer cannot delete bundles with existing entities.
444 * @see \Drupal\Core\Entity\Event\BundleConfigImportValidate
446 public function testEntityBundleDelete() {
447 \Drupal::service('module_installer')->install(['node']);
448 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
450 $node_type = $this->drupalCreateContentType();
451 $node = $this->drupalCreateNode(['type' => $node_type->id()]);
452 $this->drupalGet('admin/config/development/configuration');
453 // The node type, body field and entity displays will be scheduled for
455 $this->assertText(format_string('node.type.@type', ['@type' => $node_type->id()]));
456 $this->assertText(format_string('field.field.node.@type.body', ['@type' => $node_type->id()]));
457 $this->assertText(format_string('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
458 $this->assertText(format_string('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
459 $this->assertText(format_string('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
461 // Attempt to import configuration and verify that an error message appears
462 // and the node type, body field and entity displays are still scheduled for
464 $this->drupalPostForm(NULL, [], t('Import all'));
465 $validation_message = t('Entities exist of type %entity_type and %bundle_label %bundle. These entities need to be deleted before importing.', ['%entity_type' => $node->getEntityType()->getLabel(), '%bundle_label' => $node->getEntityType()->getBundleLabel(), '%bundle' => $node_type->label()]);
466 $this->assertRaw($validation_message);
467 $this->assertText(format_string('node.type.@type', ['@type' => $node_type->id()]));
468 $this->assertText(format_string('field.field.node.@type.body', ['@type' => $node_type->id()]));
469 $this->assertText(format_string('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
470 $this->assertText(format_string('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
471 $this->assertText(format_string('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
473 // Delete the node and try to import again.
475 $this->drupalPostForm(NULL, [], t('Import all'));
476 $this->assertNoRaw($validation_message);
477 $this->assertText(t('There are no configuration changes to import.'));
478 $this->assertNoText(format_string('node.type.@type', ['@type' => $node_type->id()]));
479 $this->assertNoText(format_string('field.field.node.@type.body', ['@type' => $node_type->id()]));
480 $this->assertNoText(format_string('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
481 $this->assertNoText(format_string('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
482 $this->assertNoText(format_string('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
486 * Tests config importer cannot uninstall extensions which are depended on.
488 * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
490 public function testExtensionValidation() {
491 \Drupal::service('module_installer')->install(['node']);
492 \Drupal::service('theme_handler')->install(['bartik']);
493 $this->rebuildContainer();
495 $sync = $this->container->get('config.storage.sync');
496 $this->copyConfig($this->container->get('config.storage'), $sync);
497 $core = $sync->read('core.extension');
498 // Node depends on text.
499 unset($core['module']['text']);
500 $module_data = system_rebuild_module_data();
501 $this->assertTrue(isset($module_data['node']->requires['text']), 'The Node module depends on the Text module.');
502 // Bartik depends on classy.
503 unset($core['theme']['classy']);
504 $theme_data = \Drupal::service('theme_handler')->rebuildThemeData();
505 $this->assertTrue(isset($theme_data['bartik']->requires['classy']), 'The Bartik theme depends on the Classy theme.');
506 // This module does not exist.
507 $core['module']['does_not_exist'] = 0;
508 // This theme does not exist.
509 $core['theme']['does_not_exist'] = 0;
510 $sync->write('core.extension', $core);
512 $this->drupalPostForm('admin/config/development/configuration', [], t('Import all'));
513 $this->assertText('The configuration cannot be imported because it failed validation for the following reasons:');
514 $this->assertText('Unable to uninstall the Text module since the Node module is installed.');
515 $this->assertText('Unable to uninstall the Classy theme since the Bartik theme is installed.');
516 $this->assertText('Unable to install the does_not_exist module since it does not exist.');
517 $this->assertText('Unable to install the does_not_exist theme since it does not exist.');