3 namespace Drupal\Tests\config\Functional;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Core\Config\InstallStorage;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests the user interface for importing configuration.
15 class ConfigImportUITest extends BrowserTestBase {
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 $this->rebuildContainer();
134 // Verify site name has changed.
135 $this->assertIdentical($new_site_name, $this->config('system.site')->get('name'));
137 // Verify that new config entity exists.
138 $this->assertIdentical($original_dynamic_data, $this->config($dynamic_name)->get());
140 // Verify the cache got cleared.
141 $this->assertTrue(isset($GLOBALS['hook_cache_flush']));
143 $this->rebuildContainer();
144 $this->assertTrue(\Drupal::moduleHandler()->moduleExists('ban'), 'Ban module installed during import.');
145 $this->assertTrue(\Drupal::database()->schema()->tableExists('ban_ip'), 'The database table ban_ip exists.');
146 $this->assertTrue(\Drupal::moduleHandler()->moduleExists('action'), 'Action module installed during import.');
147 $this->assertTrue(\Drupal::moduleHandler()->moduleExists('options'), 'Options module installed during import.');
148 $this->assertTrue(\Drupal::moduleHandler()->moduleExists('text'), 'Text module installed during import.');
150 $theme_info = \Drupal::service('theme_handler')->listInfo();
151 $this->assertTrue($theme_info['bartik']->status, 'Bartik theme installed during import.');
153 // Ensure installations and uninstallation occur as expected.
154 $installed = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_installed', []);
155 $uninstalled = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_uninstalled', []);
156 $expected = ['action', 'ban', 'text', 'options'];
157 $this->assertIdentical($expected, $installed, 'Action, Ban, Text and Options modules installed in the correct order.');
158 $this->assertTrue(empty($uninstalled), 'No modules uninstalled during import');
160 // Verify that the action.settings configuration object was only written
161 // once during the import process and only with the value set in the staged
162 // configuration. This verifies that the module's default configuration is
163 // used during configuration import and, additionally, that after installing
164 // a module, that configuration is not synced twice.
165 $recursion_limit_values = \Drupal::state()->get('ConfigImportUITest.action.settings.recursion_limit', []);
166 $this->assertIdentical($recursion_limit_values, [50]);
168 $core_extension = $this->config('core.extension')->get();
169 unset($core_extension['module']['action']);
170 unset($core_extension['module']['ban']);
171 unset($core_extension['module']['options']);
172 unset($core_extension['module']['text']);
173 unset($core_extension['theme']['bartik']);
174 $sync->write('core.extension', $core_extension);
175 $sync->delete('action.settings');
176 $sync->delete('text.settings');
178 $system_theme = $this->config('system.theme')->get();
179 $system_theme['default'] = 'stark';
180 $system_theme['admin'] = 'stark';
181 $sync->write('system.theme', $system_theme);
183 // Set the state system to record installations and uninstallations.
184 \Drupal::state()->set('ConfigImportUITest.core.extension.modules_installed', []);
185 \Drupal::state()->set('ConfigImportUITest.core.extension.modules_uninstalled', []);
187 // Verify that both appear as ready to import.
188 $this->drupalGet('admin/config/development/configuration');
189 $this->assertRaw('<td>core.extension');
190 $this->assertRaw('<td>system.theme');
191 $this->assertRaw('<td>action.settings');
193 // Import and verify that both do not appear anymore.
194 $this->drupalPostForm(NULL, [], t('Import all'));
195 $this->assertNoRaw('<td>core.extension');
196 $this->assertNoRaw('<td>system.theme');
197 $this->assertNoRaw('<td>action.settings');
199 $this->rebuildContainer();
200 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('ban'), 'Ban module uninstalled during import.');
201 $this->assertFalse(\Drupal::database()->schema()->tableExists('ban_ip'), 'The database table ban_ip does not exist.');
202 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('action'), 'Action module uninstalled during import.');
203 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('options'), 'Options module uninstalled during import.');
204 $this->assertFalse(\Drupal::moduleHandler()->moduleExists('text'), 'Text module uninstalled during import.');
206 // Ensure installations and uninstallation occur as expected.
207 $installed = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_installed', []);
208 $uninstalled = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_uninstalled', []);
209 $expected = ['options', 'text', 'ban', 'action'];
210 $this->assertIdentical($expected, $uninstalled, 'Options, Text, Ban and Action modules uninstalled in the correct order.');
211 $this->assertTrue(empty($installed), 'No modules installed during import');
213 $theme_info = \Drupal::service('theme_handler')->listInfo();
214 $this->assertFalse(isset($theme_info['bartik']), 'Bartik theme uninstalled during import.');
216 // Verify that the action.settings configuration object was only deleted
217 // once during the import process.
218 $delete_called = \Drupal::state()->get('ConfigImportUITest.action.settings.delete', 0);
219 $this->assertIdentical($delete_called, 1, "The action.settings configuration was deleted once during configuration import.");
223 * Tests concurrent importing of configuration.
225 public function testImportLock() {
226 // Create updated configuration object.
227 $new_site_name = 'Config import test ' . $this->randomString();
228 $this->prepareSiteNameUpdate($new_site_name);
230 // Verify that there are configuration differences to import.
231 $this->drupalGet('admin/config/development/configuration');
232 $this->assertNoText(t('There are no configuration changes to import.'));
234 // Acquire a fake-lock on the import mechanism.
235 $config_importer = $this->configImporter();
236 $this->container->get('lock.persistent')->acquire($config_importer::LOCK_NAME);
238 // Attempt to import configuration and verify that an error message appears.
239 $this->drupalPostForm(NULL, [], t('Import all'));
240 $this->assertText(t('Another request may be synchronizing configuration already.'));
242 // Release the lock, just to keep testing sane.
243 $this->container->get('lock.persistent')->release($config_importer::LOCK_NAME);
245 // Verify site name has not changed.
246 $this->assertNotEqual($new_site_name, $this->config('system.site')->get('name'));
250 * Tests verification of site UUID before importing configuration.
252 public function testImportSiteUuidValidation() {
253 $sync = \Drupal::service('config.storage.sync');
254 // Create updated configuration object.
255 $config_data = $this->config('system.site')->get();
256 // Generate a new site UUID.
257 $config_data['uuid'] = \Drupal::service('uuid')->generate();
258 $sync->write('system.site', $config_data);
260 // Verify that there are configuration differences to import.
261 $this->drupalGet('admin/config/development/configuration');
262 $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.'));
263 $this->assertNoFieldById('edit-submit', t('Import all'));
267 * Tests the screen that shows differences between active and sync.
269 public function testImportDiff() {
270 $sync = $this->container->get('config.storage.sync');
271 $config_name = 'config_test.system';
275 $add_data = '<em>bangpow</em>';
276 $change_data = '<p><em>foobar</em></p>';
278 'foo' => '<p>foobar</p>',
279 'baz' => '<strong>no change</strong>',
280 '404' => '<em>herp</em>',
282 // Update active storage to have html in config data.
283 $this->config($config_name)->setData($original_data)->save();
285 // Change a configuration value in sync.
286 $sync_data = $original_data;
287 $sync_data[$change_key] = $change_data;
288 $sync_data[$add_key] = $add_data;
289 unset($sync_data[$remove_key]);
290 $sync->write($config_name, $sync_data);
292 // Load the diff UI and verify that the diff reflects the change.
293 $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
294 $this->assertNoRaw('&nbsp;');
295 $this->assertTitle(format_string('View changes of @config_name | Drupal', ['@config_name' => $config_name]));
297 // The following assertions do not use $this::assertEscaped() because
298 // \Drupal\Component\Diff\DiffFormatter adds markup that signifies what has
301 // Changed values are escaped.
302 $this->assertText(Html::escape("foo: '<p><em>foobar</em></p>'"));
303 $this->assertText(Html::escape("foo: '<p>foobar</p>'"));
304 // The no change values are escaped.
305 $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
306 // Added value is escaped.
307 $this->assertText(Html::escape("biff: '<em>bangpow</em>'"));
308 // Deleted value is escaped.
309 $this->assertText(Html::escape("404: '<em>herp</em>'"));
311 // Verify diff colors are displayed.
312 $result = $this->xpath('//table[contains(@class, :class)]', [':class' => 'diff']);
313 $this->assertEqual(count($result), 1, "Diff UI is displaying colors.");
315 // Reset data back to original, and remove a key
316 $sync_data = $original_data;
317 unset($sync_data[$remove_key]);
318 $sync->write($config_name, $sync_data);
320 // Load the diff UI and verify that the diff reflects a removed key.
321 $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
322 // The no change values are escaped.
323 $this->assertText(Html::escape("foo: '<p>foobar</p>'"));
324 $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
325 // Removed key is escaped.
326 $this->assertText(Html::escape("404: '<em>herp</em>'"));
328 // Reset data back to original and add a key
329 $sync_data = $original_data;
330 $sync_data[$add_key] = $add_data;
331 $sync->write($config_name, $sync_data);
333 // Load the diff UI and verify that the diff reflects an added key.
334 $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
335 // The no change values are escaped.
336 $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
337 $this->assertText(Html::escape("404: '<em>herp</em>'"));
338 // Added key is escaped.
339 $this->assertText(Html::escape("biff: '<em>bangpow</em>'"));
343 * Tests that multiple validation errors are listed on the page.
345 public function testImportValidation() {
346 // Set state value so that
347 // \Drupal\config_import_test\EventSubscriber::onConfigImportValidate() logs
348 // validation errors.
349 \Drupal::state()->set('config_import_test.config_import_validate_fail', TRUE);
350 // Ensure there is something to import.
351 $new_site_name = 'Config import test ' . $this->randomString();
352 $this->prepareSiteNameUpdate($new_site_name);
354 $this->drupalGet('admin/config/development/configuration');
355 $this->assertNoText(t('There are no configuration changes to import.'));
356 $this->drupalPostForm(NULL, [], t('Import all'));
358 // Verify that the validation messages appear.
359 $this->assertText('The configuration cannot be imported because it failed validation for the following reasons:');
360 $this->assertText('Config import validate error 1.');
361 $this->assertText('Config import validate error 2.');
363 // Verify site name has not changed.
364 $this->assertNotEqual($new_site_name, $this->config('system.site')->get('name'));
367 public function testConfigUninstallConfigException() {
368 $sync = $this->container->get('config.storage.sync');
370 $core_extension = $this->config('core.extension')->get();
371 unset($core_extension['module']['config']);
372 $sync->write('core.extension', $core_extension);
374 $this->drupalGet('admin/config/development/configuration');
375 $this->assertText('core.extension');
377 // Import and verify that both do not appear anymore.
378 $this->drupalPostForm(NULL, [], t('Import all'));
379 $this->assertText('Can not uninstall the Configuration module as part of a configuration synchronization through the user interface.');
382 public function prepareSiteNameUpdate($new_site_name) {
383 $sync = $this->container->get('config.storage.sync');
384 // Create updated configuration object.
385 $config_data = $this->config('system.site')->get();
386 $config_data['name'] = $new_site_name;
387 $sync->write('system.site', $config_data);
391 * Tests an import that results in an error.
393 public function testImportErrorLog() {
394 $name_primary = 'config_test.dynamic.primary';
395 $name_secondary = 'config_test.dynamic.secondary';
396 $sync = $this->container->get('config.storage.sync');
397 $uuid = $this->container->get('uuid');
400 'uuid' => $uuid->generate(),
403 'dependencies' => [],
405 'label' => 'Primary',
409 'size_value' => NULL,
410 'protected_property' => NULL,
412 $sync->write($name_primary, $values_primary);
413 $values_secondary = [
414 'uuid' => $uuid->generate(),
417 // Add a dependency on primary, to ensure that is synced first.
419 'config' => [$name_primary],
422 'label' => 'Secondary Sync',
426 'size_value' => NULL,
427 'protected_property' => NULL,
429 $sync->write($name_secondary, $values_secondary);
430 // Verify that there are configuration differences to import.
431 $this->drupalGet('admin/config/development/configuration');
432 $this->assertNoText(t('There are no configuration changes to import.'));
434 // Attempt to import configuration and verify that an error message appears.
435 $this->drupalPostForm(NULL, [], t('Import all'));
436 $this->assertText(new FormattableMarkup('Deleted and replaced configuration entity "@name"', ['@name' => $name_secondary]));
437 $this->assertText(t('The configuration was imported with errors.'));
438 $this->assertNoText(t('The configuration was imported successfully.'));
439 $this->assertText(t('There are no configuration changes to import.'));
443 * Tests the config importer cannot delete bundles with existing entities.
445 * @see \Drupal\Core\Entity\Event\BundleConfigImportValidate
447 public function testEntityBundleDelete() {
448 \Drupal::service('module_installer')->install(['node']);
449 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
451 $node_type = $this->drupalCreateContentType();
452 $node = $this->drupalCreateNode(['type' => $node_type->id()]);
453 $this->drupalGet('admin/config/development/configuration');
454 // The node type, body field and entity displays will be scheduled for
456 $this->assertText(format_string('node.type.@type', ['@type' => $node_type->id()]));
457 $this->assertText(format_string('field.field.node.@type.body', ['@type' => $node_type->id()]));
458 $this->assertText(format_string('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
459 $this->assertText(format_string('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
460 $this->assertText(format_string('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
462 // Attempt to import configuration and verify that an error message appears
463 // and the node type, body field and entity displays are still scheduled for
465 $this->drupalPostForm(NULL, [], t('Import all'));
466 $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()]);
467 $this->assertRaw($validation_message);
468 $this->assertText(format_string('node.type.@type', ['@type' => $node_type->id()]));
469 $this->assertText(format_string('field.field.node.@type.body', ['@type' => $node_type->id()]));
470 $this->assertText(format_string('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
471 $this->assertText(format_string('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
472 $this->assertText(format_string('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
474 // Delete the node and try to import again.
476 $this->drupalPostForm(NULL, [], t('Import all'));
477 $this->assertNoRaw($validation_message);
478 $this->assertText(t('There are no configuration changes to import.'));
479 $this->assertNoText(format_string('node.type.@type', ['@type' => $node_type->id()]));
480 $this->assertNoText(format_string('field.field.node.@type.body', ['@type' => $node_type->id()]));
481 $this->assertNoText(format_string('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
482 $this->assertNoText(format_string('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
483 $this->assertNoText(format_string('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
487 * Tests config importer cannot uninstall extensions which are depended on.
489 * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
491 public function testExtensionValidation() {
492 \Drupal::service('module_installer')->install(['node']);
493 \Drupal::service('theme_handler')->install(['bartik']);
494 $this->rebuildContainer();
496 $sync = $this->container->get('config.storage.sync');
497 $this->copyConfig($this->container->get('config.storage'), $sync);
498 $core = $sync->read('core.extension');
499 // Node depends on text.
500 unset($core['module']['text']);
501 $module_data = system_rebuild_module_data();
502 $this->assertTrue(isset($module_data['node']->requires['text']), 'The Node module depends on the Text module.');
503 // Bartik depends on classy.
504 unset($core['theme']['classy']);
505 $theme_data = \Drupal::service('theme_handler')->rebuildThemeData();
506 $this->assertTrue(isset($theme_data['bartik']->requires['classy']), 'The Bartik theme depends on the Classy theme.');
507 // This module does not exist.
508 $core['module']['does_not_exist'] = 0;
509 // This theme does not exist.
510 $core['theme']['does_not_exist'] = 0;
511 $sync->write('core.extension', $core);
513 $this->drupalPostForm('admin/config/development/configuration', [], t('Import all'));
514 $this->assertText('The configuration cannot be imported because it failed validation for the following reasons:');
515 $this->assertText('Unable to uninstall the Text module since the Node module is installed.');
516 $this->assertText('Unable to uninstall the Classy theme since the Bartik theme is installed.');
517 $this->assertText('Unable to install the does_not_exist module since it does not exist.');
518 $this->assertText('Unable to install the does_not_exist theme since it does not exist.');
522 * Tests that errors set in the batch and on the ConfigImporter are merged.
524 public function testBatchErrors() {
525 $new_site_name = 'Config import test ' . $this->randomString();
526 $this->prepareSiteNameUpdate($new_site_name);
527 \Drupal::state()->set('config_import_steps_alter.error', TRUE);
528 $this->drupalPostForm('admin/config/development/configuration', [], t('Import all'));
529 $this->assertSession()->responseContains('_config_import_test_config_import_steps_alter batch error');
530 $this->assertSession()->responseContains('_config_import_test_config_import_steps_alter ConfigImporter error');
531 $this->assertSession()->responseContains('The configuration was imported with errors.');