diff --git a/core/core.services.yml b/core/core.services.yml index 9139f90..c90e862 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -980,6 +980,7 @@ services: class: Drupal\Core\EventSubscriber\ConfigImportSubscriber tags: - { name: event_subscriber } + arguments: ['@theme_handler'] config_snapshot_subscriber: class: Drupal\Core\EventSubscriber\ConfigSnapshotSubscriber tags: diff --git a/core/lib/Drupal/Core/Config/ConfigImporter.php b/core/lib/Drupal/Core/Config/ConfigImporter.php index 4f28425..9a4ebcd 100644 --- a/core/lib/Drupal/Core/Config/ConfigImporter.php +++ b/core/lib/Drupal/Core/Config/ConfigImporter.php @@ -240,11 +240,12 @@ public function getStorageComparer() { */ public function reset() { $this->storageComparer->reset(); + // Empty all the lists. foreach ($this->storageComparer->getAllCollectionNames() as $collection) { $this->processedConfiguration[$collection] = $this->storageComparer->getEmptyChangelist(); } - $this->processedExtensions = $this->getEmptyExtensionsProcessedList(); - $this->createExtensionChangelist(); + $this->extensionChangelist = $this->processedExtensions = $this->getEmptyExtensionsProcessedList(); + $this->validated = FALSE; $this->processedSystemTheme = FALSE; return $this; @@ -359,6 +360,9 @@ protected function setProcessedExtension($type, $op, $name) { * Populates the extension change list. */ protected function createExtensionChangelist() { + // Create an empty changelist. + $this->extensionChangelist = $this->getEmptyExtensionsProcessedList(); + // Read the extensions information to determine changes. $current_extensions = $this->storageComparer->getTargetStorage()->read('core.extension'); $new_extensions = $this->storageComparer->getSourceStorage()->read('core.extension'); @@ -396,7 +400,7 @@ protected function createExtensionChangelist() { // 0 1 actions // @todo Move this sorting functionality to the extension system. array_multisort(array_values($module_list), SORT_ASC, array_keys($module_list), SORT_DESC, $module_list); - $uninstall = array_intersect(array_keys($module_list), $uninstall); + $this->extensionChangelist['module']['uninstall'] = array_intersect(array_keys($module_list), $uninstall); // Determine which modules to install. $install = array_keys(array_diff_key($new_extensions['module'], $current_extensions['module'])); @@ -404,22 +408,19 @@ protected function createExtensionChangelist() { // (with dependencies installed first, and modules of the same weight sorted // in alphabetical order). $module_list = array_reverse($module_list); - $install = array_intersect(array_keys($module_list), $install); + $this->extensionChangelist['module']['install'] = array_intersect(array_keys($module_list), $install); + if ($missing_modules = array_diff($install, $this->extensionChangelist['module']['install'])) { + $this->logError($this->formatPlural( + count($missing_modules), + 'Unable to install module %modules since it does not exist.', + 'Unable to install modules %modules since they do not exist.', + array('%modules' => implode(', ',$missing_modules)) + )); + } // Work out what themes to install and to uninstall. - $theme_install = array_keys(array_diff_key($new_extensions['theme'], $current_extensions['theme'])); - $theme_uninstall = array_keys(array_diff_key($current_extensions['theme'], $new_extensions['theme'])); - - $this->extensionChangelist = array( - 'module' => array( - 'uninstall' => $uninstall, - 'install' => $install, - ), - 'theme' => array( - 'install' => $theme_install, - 'uninstall' => $theme_uninstall, - ), - ); + $this->extensionChangelist['theme']['install'] = array_keys(array_diff_key($new_extensions['theme'], $current_extensions['theme'])); + $this->extensionChangelist['theme']['uninstall'] = array_keys(array_diff_key($current_extensions['theme'], $new_extensions['theme'])); } /** @@ -434,7 +435,7 @@ protected function createExtensionChangelist() { * @return array * An array of extension names. */ - protected function getExtensionChangelist($type, $op = NULL) { + public function getExtensionChangelist($type, $op = NULL) { if ($op) { return $this->extensionChangelist[$type][$op]; } diff --git a/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php index f17d5bd..d3023cc 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php @@ -8,9 +8,11 @@ namespace Drupal\Core\EventSubscriber; use Drupal\Core\Config\Config; +use Drupal\Core\Config\ConfigImporter; use Drupal\Core\Config\ConfigImporterEvent; use Drupal\Core\Config\ConfigImportValidateEventSubscriberBase; use Drupal\Core\Config\ConfigNameException; +use Drupal\Core\Extension\ThemeHandlerInterface; /** * Config import subscriber for config import events. @@ -18,6 +20,23 @@ class ConfigImportSubscriber extends ConfigImportValidateEventSubscriberBase { /** + * The theme handler. + * + * @var \Drupal\Core\Extension\ThemeHandlerInterface + */ + protected $themeHandler; + + /** + * Constructs the ConfigImportSubscriber. + * + * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler + * The theme handler. + */ + public function __construct(ThemeHandlerInterface $theme_handler) { + $this->themeHandler = $theme_handler; + } + + /** * Validates the configuration to be imported. * * @param \Drupal\Core\Config\ConfigImporterEvent $event @@ -37,6 +56,122 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) { } } } + $config_importer = $event->getConfigImporter(); + if ($config_importer->getStorageComparer()->getSourceStorage()->exists('core.extension')) { + $this->validateModules($config_importer); + $this->validateThemes($config_importer); + $this->validateDependencies($config_importer); + } + else { + $config_importer->logError($this->t('The core.extension configuration does not exist.')); + } + } + + /** + * Validates module installations and uninstallations. + * + * @param \Drupal\Core\Config\ConfigImporter $config_importer + * The configuration importer. + */ + protected function validateModules(ConfigImporter $config_importer) { + $core_extension = $config_importer->getStorageComparer()->getSourceStorage()->read('core.extension'); + // Get a list of modules with dependency weights as values. + $module_data = system_rebuild_module_data(); + $nonexistent_modules = array_keys(array_diff_key($core_extension['module'], $module_data)); + foreach ($nonexistent_modules as $module) { + $config_importer->logError($this->t('Unable to install module %module since it does not exist.', array('%module' => $module))); + } + + // Ensure that module dependencies are respected. + $uninstalls = $config_importer->getExtensionChangelist('module', 'uninstall'); + foreach ($uninstalls as $module) { + foreach (array_keys($module_data[$module]->required_by) as $dependent_module) { + if ($module_data[$dependent_module]->status && !in_array($dependent_module, $uninstalls, TRUE)) { + $module_name = $module_data[$module]->info['name']; + $dependent_module_name = $module_data[$dependent_module]->info['name']; + $config_importer->logError($this->t('Unable to uninstall module %module since %dependent_module is installed.', array('%module' => $module_name, '%dependent_module' => $dependent_module_name))); + } + } + } + } + + /** + * Validates theme installations and uninstallations. + * + * @param \Drupal\Core\Config\ConfigImporter $config_importer + * The configuration importer. + */ + protected function validateThemes(ConfigImporter $config_importer) { + // Get all themes including those that are not installed. + $theme_data = $this->themeHandler->rebuildThemeData(); + foreach ($config_importer->getExtensionChangelist('theme', 'install') as $theme) { + if (!isset($theme_data[$theme])) { + $config_importer->logError($this->t('Unable to install theme %theme since it does not exist.', array('%theme' => $theme))); + } + } + + $uninstalls = $config_importer->getExtensionChangelist('theme', 'uninstall'); + foreach ($uninstalls as $theme) { + foreach (array_keys($theme_data[$theme]->required_by) as $dependent_theme) { + if ($theme_data[$dependent_theme]->status && !in_array($dependent_theme, $uninstalls, TRUE)) { + $theme_name = $theme_data[$theme]->info['name']; + $dependent_theme_name = $theme_data[$dependent_theme]->info['name']; + $config_importer->logError($this->t('Unable to uninstall theme %theme since %dependent_theme is installed.', array('%theme' => $theme_name, '%dependent_theme' => $dependent_theme_name))); + } + } + } + } + + /** + * Validates configuration being imported does not have unmet dependencies. + * + * @param \Drupal\Core\Config\ConfigImporter $config_importer + * The configuration importer. + */ + protected function validateDependencies(ConfigImporter $config_importer) { + $core_extension = $config_importer->getStorageComparer()->getSourceStorage()->read('core.extension'); + $existing_dependencies = [ + 'config' => $config_importer->getStorageComparer()->getSourceStorage()->listAll(), + 'module' => array_keys($core_extension['module']), + 'theme' => array_keys($core_extension['theme']), + ]; + + // Validate the dependencies of all the configuration. We have to validate + // the entire tree because existing configuration might depend on + // configuration that is being deleted. + foreach ($config_importer->getStorageComparer()->getSourceStorage()->listAll() as $name) { + // Ensure that the config owner is installed. This checks all + // configuration including configuration entities. + list($owner,) = explode('.', $name, 2); + if ($owner !== 'core' && !isset($core_extension['module'][$owner]) && !isset($core_extension['theme'][$owner])) { + $config_importer->logError($this->t('Configuration %name depends on the %owner extension that will not be installed after import.', array('%name' => $name, '%owner' => $owner))); + continue; + } + + $data = $config_importer->getStorageComparer()->getSourceStorage()->read($name); + // Configuration entities can be identified by having a dependencies and a + // UUID key. Check the dependencies to ensure they are met. + if (isset($data['dependencies']) && isset($data['uuid'])) { + $dependencies_to_check = array_intersect_key($data['dependencies'], array_flip(['module', 'theme', 'config'])); + foreach ($dependencies_to_check as $type => $dependencies) { + $diffs = array_diff($dependencies, $existing_dependencies[$type]); + if (!empty($diffs)) { + switch ($type) { + case 'module': + $config_importer->logError($this->t('Configuration %name depends on a module that will not be installed after import.', array('%name' => $name))); + break; + case 'theme': + $config_importer->logError($this->t('Configuration %name depends on a theme that will not be installed after import.', array('%name' => $name))); + break; + case 'config': + $config_importer->logError($this->t('Configuration %name depends on configuration that will not exist after import.', array('%name' => $name))); + break; + } + } + } + } + } + } } diff --git a/core/modules/config/src/Form/ConfigSync.php b/core/modules/config/src/Form/ConfigSync.php index b97218b..ae21f01 100644 --- a/core/modules/config/src/Form/ConfigSync.php +++ b/core/modules/config/src/Form/ConfigSync.php @@ -340,7 +340,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } catch (ConfigImporterException $e) { // There are validation errors. - drupal_set_message($this->t('The configuration synchronization failed validation.')); + drupal_set_message($this->t('The staged configuration cannot be imported because it failed validation for the following reasons:'), 'error'); foreach ($config_importer->getErrors() as $message) { drupal_set_message($message, 'error'); } diff --git a/core/modules/config/src/Tests/ConfigImportUITest.php b/core/modules/config/src/Tests/ConfigImportUITest.php index ba44925..0d3a088 100644 --- a/core/modules/config/src/Tests/ConfigImportUITest.php +++ b/core/modules/config/src/Tests/ConfigImportUITest.php @@ -326,7 +326,7 @@ public function testImportValidation() { $this->drupalPostForm(NULL, array(), t('Import all')); // Verify that the validation messages appear. - $this->assertText('The configuration synchronization failed validation.'); + $this->assertText('The staged configuration cannot be imported because it failed validation for the following reasons:'); $this->assertText('Config import validate error 1.'); $this->assertText('Config import validate error 2.'); @@ -453,4 +453,35 @@ public function testEntityBundleDelete() { $this->assertNoText(format_string('core.entity_form_display.node.@type.default', array('@type' => $node_type->id()))); } + /** + * Tests config importer cannot uninstall extensions which are depended on. + * + * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber + */ + public function testExtensionValidation() { + \Drupal::service('module_installer')->install(['node']); + \Drupal::service('theme_handler')->install(['bartik']); + $this->rebuildContainer(); + + $staging = $this->container->get('config.storage.staging'); + $this->copyConfig($this->container->get('config.storage'), $staging); + $core = $staging->read('core.extension'); + // Node depends on text. + unset($core['module']['text']); + // Bartik depends on classy. + unset($core['theme']['classy']); + // This module does not exist. + $core['module']['does_not_exist'] = 0; + // This theme does not exist. + $core['theme']['does_not_exist'] = 0; + $staging->write('core.extension', $core); + + $this->drupalPostForm('admin/config/development/configuration', array(), t('Import all')); + $this->assertText('The staged configuration cannot be imported because it failed validation for the following reasons:'); + $this->assertText('Unable to uninstall module Text since Node is installed.'); + $this->assertText('Unable to uninstall theme Classy since Bartik is installed.'); + $this->assertText('Unable to install module does_not_exist since it does not exist.'); + $this->assertText('Unable to install theme does_not_exist since it does not exist.'); + } + } diff --git a/core/modules/config/src/Tests/ConfigImporterTest.php b/core/modules/config/src/Tests/ConfigImporterTest.php index ebd6378..f1c16a6 100644 --- a/core/modules/config/src/Tests/ConfigImporterTest.php +++ b/core/modules/config/src/Tests/ConfigImporterTest.php @@ -105,7 +105,7 @@ function testSiteUuidValidate() { $staging->write('system.site', $config_data); try { $this->configImporter->reset()->import(); - $this->assertFalse(FALSE, 'ConfigImporterException not thrown, invalid import was not stopped due to mis-matching site UUID.'); + $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to mis-matching site UUID.'); } catch (ConfigImporterException $e) { $this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.'); @@ -541,4 +541,61 @@ function testIsInstallable() { $this->assertTrue($this->container->get('config.storage')->exists($config_name)); } + /** + * Tests dependency validation during configuration import. + * + * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber + * @see \Drupal\Core\Config\ConfigImporter::createExtensionChangelist() + */ + public function testUnmetDependency() { + $storage = $this->container->get('config.storage'); + $staging = $this->container->get('config.storage.staging'); + + // Test an unknown configuration owner. + $staging->write('unknown.config', ['test' => 'test']); + + // Make a config entity have unmet dependencies. + $config_entity_data = $staging->read('config_test.dynamic.dotted.default'); + $config_entity_data['dependencies'] = ['module' => ['unknown']]; + $staging->write('config_test.dynamic.dotted.module', $config_entity_data); + $config_entity_data['dependencies'] = ['theme' => ['unknown']]; + $staging->write('config_test.dynamic.dotted.theme', $config_entity_data); + $config_entity_data['dependencies'] = ['config' => ['unknown']]; + $staging->write('config_test.dynamic.dotted.config', $config_entity_data); + + // Make an active config depend on something that is missing in staging. + // The whole configuration needs to be consistent not only the updated one. + $config_entity_data['dependencies'] = []; + $storage->write('config_test.dynamic.dotted.deleted', $config_entity_data); + $config_entity_data['dependencies'] = ['config' => ['config_test.dynamic.dotted.deleted']]; + $storage->write('config_test.dynamic.dotted.existing', $config_entity_data); + $staging->write('config_test.dynamic.dotted.existing', $config_entity_data); + + // Add a module and a theme that do not exist. + $extensions = $staging->read('core.extension'); + $extensions['module']['unknown_module'] = 0; + $extensions['theme']['unknown_theme'] = 0; + $staging->write('core.extension', $extensions); + try { + $this->configImporter->reset()->import(); + $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to missing dependencies.'); + } + catch (ConfigImporterException $e) { + $this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.'); + $error_log = $this->configImporter->getErrors(); + $expected = [ + 'Unable to install module unknown_module since it does not exist.', + 'Unable to install theme unknown_theme since it does not exist.', + 'Configuration config_test.dynamic.dotted.config depends on configuration that will not exist after import.', + 'Configuration config_test.dynamic.dotted.existing depends on configuration that will not exist after import.', + 'Configuration config_test.dynamic.dotted.module depends on a module that will not be installed after import.', + 'Configuration config_test.dynamic.dotted.theme depends on a theme that will not be installed after import.', + 'Configuration unknown.config depends on the unknown extension that will not be installed after import.' + ]; + foreach ($expected as $expected_message) { + $this->assertTrue(in_array($expected_message, $error_log), $expected_message); + } + } + } + } diff --git a/core/modules/field/src/Tests/FieldImportDeleteUninstallUiTest.php b/core/modules/field/src/Tests/FieldImportDeleteUninstallUiTest.php index 53998e7..de793dd 100644 --- a/core/modules/field/src/Tests/FieldImportDeleteUninstallUiTest.php +++ b/core/modules/field/src/Tests/FieldImportDeleteUninstallUiTest.php @@ -23,7 +23,7 @@ class FieldImportDeleteUninstallUiTest extends FieldTestBase { * * @var array */ - public static $modules = array('entity_test', 'telephone', 'config', 'filter', 'text'); + public static $modules = array('entity_test', 'telephone', 'config', 'filter', 'datetime'); protected function setUp() { parent::setUp(); @@ -48,14 +48,14 @@ public function testImportDeleteUninstall() { ))->save(); // Create a text field. - $text_field_storage = entity_create('field_storage_config', array( - 'field_name' => 'field_text', + $date_field_storage = entity_create('field_storage_config', array( + 'field_name' => 'field_date', 'entity_type' => 'entity_test', - 'type' => 'text', + 'type' => 'datetime', )); - $text_field_storage->save(); + $date_field_storage->save(); entity_create('field_config', array( - 'field_storage' => $text_field_storage, + 'field_storage' => $date_field_storage, 'bundle' => 'entity_test', ))->save(); @@ -63,14 +63,14 @@ public function testImportDeleteUninstall() { $entity = entity_create('entity_test'); $value = '+0123456789'; $entity->field_tel = $value; - $entity->field_text = $this->randomMachineName(20); + $entity->field_date = time(); $entity->name->value = $this->randomMachineName(); $entity->save(); // Delete the text field before exporting configuration so that we can test // that deleted fields that are provided by modules that will be uninstalled // are also purged and that the UI message includes such fields. - $text_field_storage->delete(); + $date_field_storage->delete(); // Verify entity has been created properly. $id = $entity->id(); @@ -95,22 +95,13 @@ public function testImportDeleteUninstall() { // synchronization is correct. $this->assertText('This synchronization will delete data from the field entity_test.field_tel.'); - // Stage an uninstall of the text module to test the message for multiple - // fields. - unset($core_extension['module']['text']); + // Stage an uninstall of the datetime module to test the message for + // multiple fields. + unset($core_extension['module']['datetime']); $staging->write('core.extension', $core_extension); + $this->drupalGet('admin/config/development/configuration'); - $this->assertText('This synchronization will delete data from the fields: entity_test.field_tel, entity_test.field_text.'); - // Delete all the text fields in staging, entity_test_install() adds quite - // a few. - foreach (\Drupal::entityManager()->getFieldMap() as $entity_type => $fields) { - foreach ($fields as $field_name => $info) { - if ($info['type'] == 'text') { - $staging->delete("field.storage.$entity_type.$field_name"); - $staging->delete("field.field.$entity_type.$entity_type.$field_name"); - } - } - } + $this->assertText('This synchronization will delete data from the fields: entity_test.field_tel, entity_test.field_date.'); // This will purge all the data, delete the field and uninstall the // Telephone and Text modules. diff --git a/core/modules/system/src/SystemConfigSubscriber.php b/core/modules/system/src/SystemConfigSubscriber.php index 765b824..204d685 100644 --- a/core/modules/system/src/SystemConfigSubscriber.php +++ b/core/modules/system/src/SystemConfigSubscriber.php @@ -7,32 +7,58 @@ namespace Drupal\system; +use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Config\ConfigImporterEvent; -use Drupal\Core\Config\ConfigImportValidateEventSubscriberBase; -use Drupal\Core\Config\StorageDispatcher; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * System Config subscriber. */ -class SystemConfigSubscriber extends ConfigImportValidateEventSubscriberBase { +class SystemConfigSubscriber implements EventSubscriberInterface { + use StringTranslationTrait; /** * Checks that the configuration synchronization is valid. * - * This event listener implements two checks: - * - prevents deleting all configuration. - * - checks that the system.site:uuid's in the source and target match. + * This event listener prevents deleting all configuration. If there is + * nothing to import then event propagation is stopped because there is no + * config import to validate. * - * @param ConfigImporterEvent $event + * @param \Drupal\Core\Config\ConfigImporterEvent $event * The config import event. */ - public function onConfigImporterValidate(ConfigImporterEvent $event) { + public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) { $importList = $event->getConfigImporter()->getStorageComparer()->getSourceStorage()->listAll(); if (empty($importList)) { $event->getConfigImporter()->logError($this->t('This import is empty and if applied would delete all of your configuration, so has been rejected.')); + $event->stopPropagation(); } + } + + /** + * Checks that the configuration synchronization is valid. + * + * This event listener checks that the system.site:uuid's in the source and + * target match. + * + * @param ConfigImporterEvent $event + * The config import event. + */ + public function onConfigImporterValidateSiteUUID(ConfigImporterEvent $event) { if (!$event->getConfigImporter()->getStorageComparer()->validateSiteUuid()) { $event->getConfigImporter()->logError($this->t('Site UUID in source storage does not match the target storage.')); } } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + // The empty check has a high priority so that is can stop propagation if + // there is no configuration to import. + $events[ConfigEvents::IMPORT_VALIDATE][] = array('onConfigImporterValidateNotEmpty', 512); + $events[ConfigEvents::IMPORT_VALIDATE][] = array('onConfigImporterValidateSiteUUID', 256); + return $events; + } }