ConfigImportUITest.php 24.8 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\config\Functional;
4

5
use Drupal\Component\Utility\Html;
6
use Drupal\Component\Render\FormattableMarkup;
7
use Drupal\Core\Config\InstallStorage;
8
use Drupal\Tests\BrowserTestBase;
9 10

/**
11
 * Tests the user interface for importing configuration.
12 13
 *
 * @group config
14
 */
15
class ConfigImportUITest extends BrowserTestBase {
16

17 18 19 20 21
  /**
   * Modules to install.
   *
   * @var array
   */
22 23 24 25 26 27 28
  protected static $modules = [
    'config',
    'config_test',
    'config_import_test',
    'text',
    'options',
  ];
29

30 31 32 33 34
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

35 36 37 38 39 40 41
  /**
   * A user with the 'synchronize configuration' permission.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $webUser;

42
  protected function setUp(): void {
43 44
    parent::setUp();

45
    $this->webUser = $this->drupalCreateUser(['synchronize configuration']);
46
    $this->drupalLogin($this->webUser);
47
    $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
48 49 50 51 52
  }

  /**
   * Tests importing configuration.
   */
53
  public function testImport() {
54 55
    $name = 'system.site';
    $dynamic_name = 'config_test.dynamic.new';
56 57
    /** @var \Drupal\Core\Config\StorageInterface $sync */
    $sync = $this->container->get('config.storage.sync');
58

59
    $this->drupalGet('admin/config/development/configuration');
60
    $this->assertText('There are no configuration changes to import.');
61
    $this->assertNoFieldById('edit-submit', t('Import all'));
62 63 64 65

    // Create updated configuration object.
    $new_site_name = 'Config import test ' . $this->randomString();
    $this->prepareSiteNameUpdate($new_site_name);
66
    $this->assertTrue($sync->exists($name), $name . ' found.');
67 68

    // Create new config entity.
69
    $original_dynamic_data = [
70
      'uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651',
71
      'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
72
      'status' => TRUE,
73
      'dependencies' => [],
74 75
      'id' => 'new',
      'label' => 'New',
76
      'weight' => 0,
77
      'style' => '',
78 79
      'size' => '',
      'size_value' => '',
80
      'protected_property' => '',
81
    ];
82
    $sync->write($dynamic_name, $original_dynamic_data);
83
    $this->assertTrue($sync->exists($dynamic_name), $dynamic_name . ' found.');
84

85 86 87 88
    // Enable the Automated Cron and Ban modules during import. The Ban
    // module is used because it creates a table during the install.
    // The Automated Cron module is used because it creates a single simple
    // configuration file during the install.
89
    $core_extension = $this->config('core.extension')->get();
90
    $core_extension['module']['automated_cron'] = 0;
91 92
    $core_extension['module']['ban'] = 0;
    $core_extension['module'] = module_config_sort($core_extension['module']);
93 94
    // Bartik is a subtheme of Stable so Stable must be enabled.
    $core_extension['theme']['stable'] = 0;
95
    $core_extension['theme']['bartik'] = 0;
96
    $sync->write('core.extension', $core_extension);
97 98 99 100 101 102

    // Use the install storage so that we can read configuration from modules
    // and themes that are not installed.
    $install_storage = new InstallStorage();

    // Set the Bartik theme as default.
103
    $system_theme = $this->config('system.theme')->get();
104
    $system_theme['default'] = 'bartik';
105
    $sync->write('system.theme', $system_theme);
106

107 108 109 110
    // Read the automated_cron config from module default config folder.
    $settings = $install_storage->read('automated_cron.settings');
    $settings['interval'] = 10000;
    $sync->write('automated_cron.settings', $settings);
111 112 113 114

    // Uninstall the Options and Text modules to ensure that dependencies are
    // handled correctly. Options depends on Text so Text should be installed
    // first. Since they were enabled during the test setup the core.extension
115
    // file in sync will already contain them.
116
    \Drupal::service('module_installer')->uninstall(['text', 'options']);
117 118

    // Set the state system to record installations and uninstallations.
119 120
    \Drupal::state()->set('ConfigImportUITest.core.extension.modules_installed', []);
    \Drupal::state()->set('ConfigImportUITest.core.extension.modules_uninstalled', []);
121

122
    // Verify that both appear as ready to import.
123
    $this->drupalGet('admin/config/development/configuration');
124 125 126 127
    $this->assertRaw('<td>' . $name);
    $this->assertRaw('<td>' . $dynamic_name);
    $this->assertRaw('<td>core.extension');
    $this->assertRaw('<td>system.theme');
128
    $this->assertRaw('<td>automated_cron.settings');
129
    $this->assertFieldById('edit-submit', t('Import all'));
130 131

    // Import and verify that both do not appear anymore.
132
    $this->drupalPostForm(NULL, [], t('Import all'));
133 134 135 136
    $this->assertNoRaw('<td>' . $name);
    $this->assertNoRaw('<td>' . $dynamic_name);
    $this->assertNoRaw('<td>core.extension');
    $this->assertNoRaw('<td>system.theme');
137
    $this->assertNoRaw('<td>automated_cron.settings');
138

139
    $this->assertNoFieldById('edit-submit', t('Import all'));
140 141

    // Verify that there are no further changes to import.
142
    $this->assertText(t('There are no configuration changes to import.'));
143

144
    $this->rebuildContainer();
145
    // Verify site name has changed.
146
    $this->assertIdentical($new_site_name, $this->config('system.site')->get('name'));
147 148

    // Verify that new config entity exists.
149
    $this->assertIdentical($original_dynamic_data, $this->config($dynamic_name)->get());
150 151 152

    // Verify the cache got cleared.
    $this->assertTrue(isset($GLOBALS['hook_cache_flush']));
153 154 155 156

    $this->rebuildContainer();
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists('ban'), 'Ban module installed during import.');
    $this->assertTrue(\Drupal::database()->schema()->tableExists('ban_ip'), 'The database table ban_ip exists.');
157
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists('automated_cron'), 'Automated Cron module installed during import.');
158 159
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists('options'), 'Options module installed during import.');
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists('text'), 'Text module installed during import.');
160
    $this->assertTrue(\Drupal::service('theme_handler')->themeExists('bartik'), 'Bartik theme installed during import.');
161 162

    // Ensure installations and uninstallation occur as expected.
163 164
    $installed = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_installed', []);
    $uninstalled = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_uninstalled', []);
165 166
    $expected = ['automated_cron', 'ban', 'text', 'options'];
    $this->assertIdentical($expected, $installed, 'Automated Cron, Ban, Text and Options modules installed in the correct order.');
167 168
    $this->assertTrue(empty($uninstalled), 'No modules uninstalled during import');

169
    // Verify that the automated_cron configuration object was only written
170 171 172 173
    // once during the import process and only with the value set in the staged
    // configuration. This verifies that the module's default configuration is
    // used during configuration import and, additionally, that after installing
    // a module, that configuration is not synced twice.
174 175
    $interval_values = \Drupal::state()->get('ConfigImportUITest.automated_cron.settings.interval', []);
    $this->assertIdentical($interval_values, [10000]);
176

177
    $core_extension = $this->config('core.extension')->get();
178
    unset($core_extension['module']['automated_cron']);
179 180 181 182
    unset($core_extension['module']['ban']);
    unset($core_extension['module']['options']);
    unset($core_extension['module']['text']);
    unset($core_extension['theme']['bartik']);
183
    $sync->write('core.extension', $core_extension);
184
    $sync->delete('automated_cron.settings');
185
    $sync->delete('text.settings');
186

187
    $system_theme = $this->config('system.theme')->get();
188 189
    $system_theme['default'] = 'stark';
    $system_theme['admin'] = 'stark';
190
    $sync->write('system.theme', $system_theme);
191 192

    // Set the state system to record installations and uninstallations.
193 194
    \Drupal::state()->set('ConfigImportUITest.core.extension.modules_installed', []);
    \Drupal::state()->set('ConfigImportUITest.core.extension.modules_uninstalled', []);
195 196 197

    // Verify that both appear as ready to import.
    $this->drupalGet('admin/config/development/configuration');
198 199
    $this->assertRaw('<td>core.extension');
    $this->assertRaw('<td>system.theme');
200
    $this->assertRaw('<td>automated_cron.settings');
201 202

    // Import and verify that both do not appear anymore.
203
    $this->drupalPostForm(NULL, [], t('Import all'));
204 205
    $this->assertNoRaw('<td>core.extension');
    $this->assertNoRaw('<td>system.theme');
206
    $this->assertNoRaw('<td>automated_cron.settings');
207 208 209 210

    $this->rebuildContainer();
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists('ban'), 'Ban module uninstalled during import.');
    $this->assertFalse(\Drupal::database()->schema()->tableExists('ban_ip'), 'The database table ban_ip does not exist.');
211
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists('automated_cron'), 'Automated cron module uninstalled during import.');
212 213 214 215
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists('options'), 'Options module uninstalled during import.');
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists('text'), 'Text module uninstalled during import.');

    // Ensure installations and uninstallation occur as expected.
216 217
    $installed = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_installed', []);
    $uninstalled = \Drupal::state()->get('ConfigImportUITest.core.extension.modules_uninstalled', []);
218 219
    $expected = ['options', 'text', 'ban', 'automated_cron'];
    $this->assertIdentical($expected, $uninstalled, 'Options, Text, Ban and Automated Cron modules uninstalled in the correct order.');
220 221 222
    $this->assertTrue(empty($installed), 'No modules installed during import');

    $theme_info = \Drupal::service('theme_handler')->listInfo();
223
    $this->assertFalse(isset($theme_info['bartik']), 'Bartik theme uninstalled during import.');
224

225 226 227 228
    // Verify that the automated_cron.settings configuration object was only
    // deleted once during the import process.
    $delete_called = \Drupal::state()->get('ConfigImportUITest.automated_cron.settings.delete', 0);
    $this->assertIdentical($delete_called, 1, "The automated_cron.settings configuration was deleted once during configuration import.");
229 230 231 232 233
  }

  /**
   * Tests concurrent importing of configuration.
   */
234
  public function testImportLock() {
235 236 237 238 239
    // Create updated configuration object.
    $new_site_name = 'Config import test ' . $this->randomString();
    $this->prepareSiteNameUpdate($new_site_name);

    // Verify that there are configuration differences to import.
240
    $this->drupalGet('admin/config/development/configuration');
241
    $this->assertNoText(t('There are no configuration changes to import.'));
242 243

    // Acquire a fake-lock on the import mechanism.
244
    $config_importer = $this->configImporter();
245
    $this->container->get('lock.persistent')->acquire($config_importer::LOCK_NAME);
246 247

    // Attempt to import configuration and verify that an error message appears.
248
    $this->drupalPostForm(NULL, [], t('Import all'));
249 250 251
    $this->assertText(t('Another request may be synchronizing configuration already.'));

    // Release the lock, just to keep testing sane.
252
    $this->container->get('lock.persistent')->release($config_importer::LOCK_NAME);
253 254

    // Verify site name has not changed.
255
    $this->assertNotEqual($new_site_name, $this->config('system.site')->get('name'));
256 257
  }

258 259 260
  /**
   * Tests verification of site UUID before importing configuration.
   */
261
  public function testImportSiteUuidValidation() {
262
    $sync = \Drupal::service('config.storage.sync');
263
    // Create updated configuration object.
264
    $config_data = $this->config('system.site')->get();
265 266
    // Generate a new site UUID.
    $config_data['uuid'] = \Drupal::service('uuid')->generate();
267
    $sync->write('system.site', $config_data);
268 269 270 271 272 273 274

    // Verify that there are configuration differences to import.
    $this->drupalGet('admin/config/development/configuration');
    $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.'));
    $this->assertNoFieldById('edit-submit', t('Import all'));
  }

275
  /**
276
   * Tests the screen that shows differences between active and sync.
277
   */
278
  public function testImportDiff() {
279
    $sync = $this->container->get('config.storage.sync');
280 281 282 283
    $config_name = 'config_test.system';
    $change_key = 'foo';
    $remove_key = '404';
    $add_key = 'biff';
284 285
    $add_data = '<em>bangpow</em>';
    $change_data = '<p><em>foobar</em></p>';
286
    $original_data = [
287 288 289
      'foo' => '<p>foobar</p>',
      'baz' => '<strong>no change</strong>',
      '404' => '<em>herp</em>',
290
    ];
291 292
    // Update active storage to have html in config data.
    $this->config($config_name)->setData($original_data)->save();
293

294 295 296 297 298 299
    // Change a configuration value in sync.
    $sync_data = $original_data;
    $sync_data[$change_key] = $change_data;
    $sync_data[$add_key] = $add_data;
    unset($sync_data[$remove_key]);
    $sync->write($config_name, $sync_data);
300 301

    // Load the diff UI and verify that the diff reflects the change.
302
    $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
303
    $this->assertNoRaw('&amp;nbsp;');
304
    $this->assertTitle("View changes of $config_name | Drupal");
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319
    // The following assertions do not use $this::assertEscaped() because
    // \Drupal\Component\Diff\DiffFormatter adds markup that signifies what has
    // changed.

    // Changed values are escaped.
    $this->assertText(Html::escape("foo: '<p><em>foobar</em></p>'"));
    $this->assertText(Html::escape("foo: '<p>foobar</p>'"));
    // The no change values are escaped.
    $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
    // Added value is escaped.
    $this->assertText(Html::escape("biff: '<em>bangpow</em>'"));
    // Deleted value is escaped.
    $this->assertText(Html::escape("404: '<em>herp</em>'"));

320
    // Verify diff colors are displayed.
321
    $result = $this->xpath('//table[contains(@class, :class)]', [':class' => 'diff']);
322
    $this->assertCount(1, $result, "Diff UI is displaying colors.");
323

324
    // Reset data back to original, and remove a key
325 326 327
    $sync_data = $original_data;
    unset($sync_data[$remove_key]);
    $sync->write($config_name, $sync_data);
328 329

    // Load the diff UI and verify that the diff reflects a removed key.
330
    $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
331 332 333 334 335
    // The no change values are escaped.
    $this->assertText(Html::escape("foo: '<p>foobar</p>'"));
    $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
    // Removed key is escaped.
    $this->assertText(Html::escape("404: '<em>herp</em>'"));
336 337

    // Reset data back to original and add a key
338 339 340
    $sync_data = $original_data;
    $sync_data[$add_key] = $add_data;
    $sync->write($config_name, $sync_data);
341 342

    // Load the diff UI and verify that the diff reflects an added key.
343
    $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
344 345 346 347 348
    // The no change values are escaped.
    $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
    $this->assertText(Html::escape("404: '<em>herp</em>'"));
    // Added key is escaped.
    $this->assertText(Html::escape("biff: '<em>bangpow</em>'"));
349 350
  }

351
  /**
352
   * Tests that multiple validation errors are listed on the page.
353 354 355 356 357 358 359 360 361 362 363
   */
  public function testImportValidation() {
    // Set state value so that
    // \Drupal\config_import_test\EventSubscriber::onConfigImportValidate() logs
    // validation errors.
    \Drupal::state()->set('config_import_test.config_import_validate_fail', TRUE);
    // Ensure there is something to import.
    $new_site_name = 'Config import test ' . $this->randomString();
    $this->prepareSiteNameUpdate($new_site_name);

    $this->drupalGet('admin/config/development/configuration');
364
    $this->assertNoText(t('There are no configuration changes to import.'));
365
    $this->drupalPostForm(NULL, [], t('Import all'));
366 367

    // Verify that the validation messages appear.
368
    $this->assertText('The configuration cannot be imported because it failed validation for the following reasons:');
369 370 371 372
    $this->assertText('Config import validate error 1.');
    $this->assertText('Config import validate error 2.');

    // Verify site name has not changed.
373
    $this->assertNotEqual($new_site_name, $this->config('system.site')->get('name'));
374 375
  }

376
  public function testConfigUninstallConfigException() {
377
    $sync = $this->container->get('config.storage.sync');
378

379
    $core_extension = $this->config('core.extension')->get();
380
    unset($core_extension['module']['config']);
381
    $sync->write('core.extension', $core_extension);
382 383 384 385 386

    $this->drupalGet('admin/config/development/configuration');
    $this->assertText('core.extension');

    // Import and verify that both do not appear anymore.
387
    $this->drupalPostForm(NULL, [], t('Import all'));
388 389 390
    $this->assertText('Can not uninstall the Configuration module as part of a configuration synchronization through the user interface.');
  }

391
  public function prepareSiteNameUpdate($new_site_name) {
392
    $sync = $this->container->get('config.storage.sync');
393
    // Create updated configuration object.
394
    $config_data = $this->config('system.site')->get();
395
    $config_data['name'] = $new_site_name;
396
    $sync->write('system.site', $config_data);
397
  }
398 399 400 401

  /**
   * Tests an import that results in an error.
   */
402
  public function testImportErrorLog() {
403 404
    $name_primary = 'config_test.dynamic.primary';
    $name_secondary = 'config_test.dynamic.secondary';
405
    $sync = $this->container->get('config.storage.sync');
406 407
    $uuid = $this->container->get('uuid');

408
    $values_primary = [
409 410 411
      'uuid' => $uuid->generate(),
      'langcode' => 'en',
      'status' => TRUE,
412
      'dependencies' => [],
413 414 415 416
      'id' => 'primary',
      'label' => 'Primary',
      'weight' => 0,
      'style' => NULL,
417 418 419
      'size' => NULL,
      'size_value' => NULL,
      'protected_property' => NULL,
420
    ];
421
    $sync->write($name_primary, $values_primary);
422
    $values_secondary = [
423 424
      'uuid' => $uuid->generate(),
      'langcode' => 'en',
425
      'status' => TRUE,
426
      // Add a dependency on primary, to ensure that is synced first.
427 428 429
      'dependencies' => [
        'config' => [$name_primary],
      ],
430 431 432 433
      'id' => 'secondary',
      'label' => 'Secondary Sync',
      'weight' => 0,
      'style' => NULL,
434 435 436
      'size' => NULL,
      'size_value' => NULL,
      'protected_property' => NULL,
437
    ];
438
    $sync->write($name_secondary, $values_secondary);
439 440
    // Verify that there are configuration differences to import.
    $this->drupalGet('admin/config/development/configuration');
441
    $this->assertNoText(t('There are no configuration changes to import.'));
442 443

    // Attempt to import configuration and verify that an error message appears.
444
    $this->drupalPostForm(NULL, [], t('Import all'));
445
    $this->assertText(new FormattableMarkup('Deleted and replaced configuration entity "@name"', ['@name' => $name_secondary]));
446 447
    $this->assertText(t('The configuration was imported with errors.'));
    $this->assertNoText(t('The configuration was imported successfully.'));
448
    $this->assertText(t('There are no configuration changes to import.'));
449 450
  }

451 452 453 454 455 456
  /**
   * Tests the config importer cannot delete bundles with existing entities.
   *
   * @see \Drupal\Core\Entity\Event\BundleConfigImportValidate
   */
  public function testEntityBundleDelete() {
457
    \Drupal::service('module_installer')->install(['node']);
458
    $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
459 460

    $node_type = $this->drupalCreateContentType();
461
    $node = $this->drupalCreateNode(['type' => $node_type->id()]);
462 463 464
    $this->drupalGet('admin/config/development/configuration');
    // The node type, body field and entity displays will be scheduled for
    // removal.
465 466 467 468 469
    $this->assertText(new FormattableMarkup('node.type.@type', ['@type' => $node_type->id()]));
    $this->assertText(new FormattableMarkup('field.field.node.@type.body', ['@type' => $node_type->id()]));
    $this->assertText(new FormattableMarkup('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
    $this->assertText(new FormattableMarkup('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
    $this->assertText(new FormattableMarkup('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
470 471 472 473

    // Attempt to import configuration and verify that an error message appears
    // and the node type, body field and entity displays are still scheduled for
    // removal.
474 475
    $this->drupalPostForm(NULL, [], t('Import all'));
    $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()]);
476
    $this->assertRaw($validation_message);
477 478 479 480 481
    $this->assertText(new FormattableMarkup('node.type.@type', ['@type' => $node_type->id()]));
    $this->assertText(new FormattableMarkup('field.field.node.@type.body', ['@type' => $node_type->id()]));
    $this->assertText(new FormattableMarkup('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
    $this->assertText(new FormattableMarkup('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
    $this->assertText(new FormattableMarkup('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
482 483 484

    // Delete the node and try to import again.
    $node->delete();
485
    $this->drupalPostForm(NULL, [], t('Import all'));
486 487
    $this->assertNoRaw($validation_message);
    $this->assertText(t('There are no configuration changes to import.'));
488 489 490 491 492
    $this->assertNoText(new FormattableMarkup('node.type.@type', ['@type' => $node_type->id()]));
    $this->assertNoText(new FormattableMarkup('field.field.node.@type.body', ['@type' => $node_type->id()]));
    $this->assertNoText(new FormattableMarkup('core.entity_view_display.node.@type.teaser', ['@type' => $node_type->id()]));
    $this->assertNoText(new FormattableMarkup('core.entity_view_display.node.@type.default', ['@type' => $node_type->id()]));
    $this->assertNoText(new FormattableMarkup('core.entity_form_display.node.@type.default', ['@type' => $node_type->id()]));
493 494
  }

495 496 497 498 499 500 501
  /**
   * Tests config importer cannot uninstall extensions which are depended on.
   *
   * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
   */
  public function testExtensionValidation() {
    \Drupal::service('module_installer')->install(['node']);
502
    \Drupal::service('theme_installer')->install(['test_subtheme']);
503 504
    $this->rebuildContainer();

505 506 507
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($this->container->get('config.storage'), $sync);
    $core = $sync->read('core.extension');
508 509
    // Node depends on text.
    unset($core['module']['text']);
510
    $module_data = $this->container->get('extension.list.module')->getList();
511
    $this->assertTrue(isset($module_data['node']->requires['text']), 'The Node module depends on the Text module.');
512
    // Bartik depends on Stable.
513
    unset($core['theme']['test_basetheme']);
514
    $theme_data = \Drupal::service('theme_handler')->rebuildThemeData();
515
    $this->assertTrue(isset($theme_data['test_subtheme']->requires['test_basetheme']), 'The Test Subtheme theme depends on the Test Basetheme theme.');
516 517 518 519
    // This module does not exist.
    $core['module']['does_not_exist'] = 0;
    // This theme does not exist.
    $core['theme']['does_not_exist'] = 0;
520
    $sync->write('core.extension', $core);
521

522
    $this->drupalPostForm('admin/config/development/configuration', [], t('Import all'));
523 524
    $this->assertText('The configuration cannot be imported because it failed validation for the following reasons:');
    $this->assertText('Unable to uninstall the Text module since the Node module is installed.');
525
    $this->assertText('Unable to uninstall the Theme test base theme theme since the Theme test subtheme theme is installed.');
526 527 528 529
    $this->assertText('Unable to install the does_not_exist module since it does not exist.');
    $this->assertText('Unable to install the does_not_exist theme since it does not exist.');
  }

530 531 532 533 534 535 536 537 538 539 540 541 542
  /**
   * Tests that errors set in the batch and on the ConfigImporter are merged.
   */
  public function testBatchErrors() {
    $new_site_name = 'Config import test ' . $this->randomString();
    $this->prepareSiteNameUpdate($new_site_name);
    \Drupal::state()->set('config_import_steps_alter.error', TRUE);
    $this->drupalPostForm('admin/config/development/configuration', [], t('Import all'));
    $this->assertSession()->responseContains('_config_import_test_config_import_steps_alter batch error');
    $this->assertSession()->responseContains('_config_import_test_config_import_steps_alter ConfigImporter error');
    $this->assertSession()->responseContains('The configuration was imported with errors.');
  }

543
}