/**
  * Tests config import updates.
  */
 function testConfigImportUpdates()
 {
     $entity_type_id = 'entity_test_mul';
     $config_id = $entity_type_id . '.' . $entity_type_id;
     $config_name = 'language.content_settings.' . $config_id;
     $storage = $this->container->get('config.storage');
     $sync = $this->container->get('config.storage.sync');
     // Verify the configuration to create does not exist yet.
     $this->assertIdentical($storage->exists($config_name), FALSE, $config_name . ' not found.');
     // Create new config entity.
     $data = array('uuid' => 'a019d89b-c4d9-4ed4-b859-894e4e2e93cf', 'langcode' => 'en', 'status' => TRUE, 'dependencies' => array('module' => array('content_translation')), 'id' => $config_id, 'target_entity_type_id' => 'entity_test_mul', 'target_bundle' => 'entity_test_mul', 'default_langcode' => 'site_default', 'language_alterable' => FALSE, 'third_party_settings' => array('content_translation' => array('enabled' => TRUE)));
     $sync->write($config_name, $data);
     $this->assertIdentical($sync->exists($config_name), TRUE, $config_name . ' found.');
     // Import.
     $this->configImporter->reset()->import();
     // Verify the values appeared.
     $config = $this->config($config_name);
     $this->assertIdentical($config->get('id'), $config_id);
     // Verify that updates were performed.
     $entity_type = $this->container->get('entity.manager')->getDefinition($entity_type_id);
     $table = $entity_type->getDataTable();
     $db_schema = $this->container->get('database')->schema();
     $result = $db_schema->fieldExists($table, 'content_translation_source') && $db_schema->fieldExists($table, 'content_translation_outdated');
     $this->assertTrue($result, 'Content translation updates were successfully performed during config import.');
 }
Пример #2
0
 private function configImport($io, StorageComparer $storage_comparer)
 {
     $config_importer = new ConfigImporter($storage_comparer, \Drupal::service('event_dispatcher'), \Drupal::service('config.manager'), \Drupal::lock(), \Drupal::service('config.typed'), \Drupal::moduleHandler(), \Drupal::service('module_installer'), \Drupal::service('theme_handler'), \Drupal::service('string_translation'));
     if ($config_importer->alreadyImporting()) {
         $io->success($this->trans('commands.config.import.messages.already-imported'));
     } else {
         try {
             if ($config_importer->validate()) {
                 $sync_steps = $config_importer->initialize();
                 foreach ($sync_steps as $step) {
                     $context = array();
                     do {
                         $config_importer->doSyncStep($step, $context);
                     } while ($context['finished'] < 1);
                 }
             }
         } catch (ConfigImporterException $e) {
             $message = 'The import failed due for the following reasons:' . "\n";
             $message .= implode("\n", $config_importer->getErrors());
             $io->error(sprintf($this->trans('commands.site.import.local.messages.error-writing'), $message));
         } catch (\Exception $e) {
             $io->error(sprintf($this->trans('commands.site.import.local.messages.error-writing'), $e->getMessage()));
         }
     }
 }
 /**
  * Tests the missing content event is fired.
  *
  * @see \Drupal\Core\Config\ConfigImporter::processMissingContent()
  * @see \Drupal\config_import_test\EventSubscriber
  */
 function testMissingContent()
 {
     \Drupal::state()->set('config_import_test.config_import_missing_content', TRUE);
     // Update a configuration entity in the sync directory to have a dependency
     // on two content entities that do not exist.
     $storage = $this->container->get('config.storage');
     $sync = $this->container->get('config.storage.sync');
     $entity_one = entity_create('entity_test', array('name' => 'one'));
     $entity_two = entity_create('entity_test', array('name' => 'two'));
     $entity_three = entity_create('entity_test', array('name' => 'three'));
     $dynamic_name = 'config_test.dynamic.dotted.default';
     $original_dynamic_data = $storage->read($dynamic_name);
     // Entity one will be resolved by
     // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentOne().
     $original_dynamic_data['dependencies']['content'][] = $entity_one->getConfigDependencyName();
     // Entity two will be resolved by
     // \Drupal\config_import_test\EventSubscriber::onConfigImporterMissingContentTwo().
     $original_dynamic_data['dependencies']['content'][] = $entity_two->getConfigDependencyName();
     // Entity three will be resolved by
     // \Drupal\Core\Config\Importer\FinalMissingContentSubscriber.
     $original_dynamic_data['dependencies']['content'][] = $entity_three->getConfigDependencyName();
     $sync->write($dynamic_name, $original_dynamic_data);
     // Import.
     $this->configImporter->reset()->import();
     $this->assertEqual([], $this->configImporter->getErrors(), 'There were no errors during the import.');
     $this->assertEqual($entity_one->uuid(), \Drupal::state()->get('config_import_test.config_import_missing_content_one'), 'The missing content event is fired during configuration import.');
     $this->assertEqual($entity_two->uuid(), \Drupal::state()->get('config_import_test.config_import_missing_content_two'), 'The missing content event is fired during configuration import.');
     $original_dynamic_data = $storage->read($dynamic_name);
     $this->assertEqual([$entity_one->getConfigDependencyName(), $entity_two->getConfigDependencyName(), $entity_three->getConfigDependencyName()], $original_dynamic_data['dependencies']['content'], 'The imported configuration entity has the missing content entity dependency.');
 }
 /**
  * Tests deleting a contact form entity via a configuration import.
  *
  * @see \Drupal\Core\Entity\Event\BundleConfigImportValidate
  */
 public function testDeleteThroughImport()
 {
     $contact_form = ContactForm::create(['id' => 'test']);
     $contact_form->save();
     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
     // Set up the ConfigImporter object for testing.
     $storage_comparer = new StorageComparer($this->container->get('config.storage.sync'), $this->container->get('config.storage'), $this->container->get('config.manager'));
     $config_importer = new ConfigImporter($storage_comparer->createChangelist(), $this->container->get('event_dispatcher'), $this->container->get('config.manager'), $this->container->get('lock'), $this->container->get('config.typed'), $this->container->get('module_handler'), $this->container->get('module_installer'), $this->container->get('theme_handler'), $this->container->get('string_translation'));
     // Delete the contact message in sync.
     $sync = $this->container->get('config.storage.sync');
     $sync->delete($contact_form->getConfigDependencyName());
     // Import.
     $config_importer->reset()->import();
     $this->assertNull(ContactForm::load($contact_form->id()), 'The contact form has been deleted.');
 }
 /**
  * Tests configuration renaming validation for simple configuration.
  */
 public function testRenameSimpleConfigValidation()
 {
     $uuid = new Php();
     // Create a simple configuration with a UUID.
     $config = $this->config('config_test.new');
     $uuid_value = $uuid->generate();
     $config->set('uuid', $uuid_value)->save();
     $active = $this->container->get('config.storage');
     $sync = $this->container->get('config.storage.sync');
     $this->copyConfig($active, $sync);
     $config->delete();
     // Create another simple configuration with the same UUID.
     $config = $this->config('config_test.old');
     $config->set('uuid', $uuid_value)->save();
     // Confirm that the staged configuration is detected as a rename since the
     // UUIDs match.
     $this->configImporter->reset();
     $expected = array('config_test.old::config_test.new');
     $renames = $this->configImporter->getUnprocessedConfiguration('rename');
     $this->assertIdentical($expected, $renames);
     // Try to import the configuration. We expect an exception to be thrown
     // because the rename is for simple configuration.
     try {
         $this->configImporter->import();
         $this->fail('Expected ConfigImporterException thrown when simple configuration is renamed.');
     } catch (ConfigImporterException $e) {
         $this->pass('Expected ConfigImporterException thrown when simple configuration is renamed.');
         $expected = array(SafeMarkup::format('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', array('@old_name' => 'config_test.old', '@new_name' => 'config_test.new')));
         $this->assertEqual($expected, $this->configImporter->getErrors());
     }
 }
Пример #6
0
 /**
  * Tests that the isConfigSyncing flag is set correctly during a custom step.
  */
 public function testCustomStep()
 {
     $this->assertFalse(\Drupal::isConfigSyncing(), 'Before an import \\Drupal::isConfigSyncing() returns FALSE');
     $context = [];
     $this->configImporter->doSyncStep([self::class, 'customStep'], $context);
     $this->assertTrue($context['is_syncing'], 'Inside a custom step \\Drupal::isConfigSyncing() returns TRUE');
     $this->assertFalse(\Drupal::isConfigSyncing(), 'After an valid custom step \\Drupal::isConfigSyncing() returns FALSE');
 }
Пример #7
0
 public function testRecreateEntity()
 {
     $type_name = Unicode::strtolower($this->randomMachineName(16));
     $content_type = entity_create('node_type', array('type' => $type_name, 'name' => 'Node type one'));
     $content_type->save();
     node_add_body_field($content_type);
     /** @var \Drupal\Core\Config\StorageInterface $active */
     $active = $this->container->get('config.storage');
     /** @var \Drupal\Core\Config\StorageInterface $sync */
     $sync = $this->container->get('config.storage.sync');
     $config_name = $content_type->getEntityType()->getConfigPrefix() . '.' . $content_type->id();
     $this->copyConfig($active, $sync);
     // Delete the content type. This will also delete a field storage, a field,
     // an entity view display and an entity form display.
     $content_type->delete();
     $this->assertFalse($active->exists($config_name), 'Content type\'s old name does not exist active store.');
     // Recreate with the same type - this will have a different UUID.
     $content_type = entity_create('node_type', array('type' => $type_name, 'name' => 'Node type two'));
     $content_type->save();
     node_add_body_field($content_type);
     $this->configImporter->reset();
     // A node type, a field, an entity view display and an entity form display
     // will be recreated.
     $creates = $this->configImporter->getUnprocessedConfiguration('create');
     $deletes = $this->configImporter->getUnprocessedConfiguration('delete');
     $this->assertEqual(5, count($creates), 'There are 5 configuration items to create.');
     $this->assertEqual(5, count($deletes), 'There are 5 configuration items to delete.');
     $this->assertEqual(0, count($this->configImporter->getUnprocessedConfiguration('update')), 'There are no configuration items to update.');
     $this->assertIdentical($creates, array_reverse($deletes), 'Deletes and creates contain the same configuration names in opposite orders due to dependencies.');
     $this->configImporter->import();
     // Verify that there is nothing more to import.
     $this->assertFalse($this->configImporter->reset()->hasUnprocessedConfigurationChanges());
     $content_type = NodeType::load($type_name);
     $this->assertEqual('Node type one', $content_type->label());
 }
Пример #8
0
 private function configImport(DrupalStyle $io, StorageComparer $storage_comparer)
 {
     $config_importer = new ConfigImporter($storage_comparer, \Drupal::service('event_dispatcher'), \Drupal::service('config.manager'), \Drupal::lock(), \Drupal::service('config.typed'), \Drupal::moduleHandler(), \Drupal::service('module_installer'), \Drupal::service('theme_handler'), \Drupal::service('string_translation'));
     if ($config_importer->alreadyImporting()) {
         $io->success($this->trans('commands.config.import.messages.already-imported'));
     } else {
         try {
             $config_importer->import();
             $io->info($this->trans('commands.config.import.messages.importing'));
         } catch (ConfigImporterException $e) {
             $message = 'The import failed due for the following reasons:' . "\n";
             $message .= implode("\n", $config_importer->getErrors());
             $io->error(sprintf($this->trans('commands.site.import.local.messages.error-writing'), $message));
         } catch (\Exception $e) {
             $io->error(sprintf($this->trans('commands.site.import.local.messages.error-writing'), $e->getMessage()));
         }
     }
 }
Пример #9
0
 /**
  * Tests missing core.extension during configuration import.
  *
  * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
  */
 public function testMissingCoreExtension()
 {
     $staging = $this->container->get('config.storage.staging');
     $staging->delete('core.extension');
     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();
         $this->assertEqual(['The core.extension configuration does not exist.'], $error_log);
     }
 }
Пример #10
0
 /**
  * Initializes the batch context sandbox for processing field deletions.
  *
  * This calculates the number of steps necessary to purge all the field data
  * and saves data for later use.
  *
  * @param array $context
  *   The batch context.
  * @param \Drupal\Core\Config\ConfigImporter $config_importer
  *   The config importer.
  */
 protected static function initializeSandbox(array &$context, ConfigImporter $config_importer)
 {
     $context['sandbox']['field']['purge_batch_size'] = \Drupal::config('field.settings')->get('purge_batch_size');
     // Save the future list of installed extensions to limit the amount of times
     // the configuration is read from disk.
     $context['sandbox']['field']['extensions'] = $config_importer->getStorageComparer()->getSourceStorage()->read('core.extension');
     $context['sandbox']['field']['steps_to_delete'] = 0;
     $fields = static::getFieldStoragesToPurge($context['sandbox']['field']['extensions'], $config_importer->getUnprocessedConfiguration('delete'));
     foreach ($fields as $field) {
         $row_count = \Drupal::entityManager()->getStorage($field->getTargetEntityTypeId())->countFieldData($field);
         if ($row_count > 0) {
             // The number of steps to delete each field is determined by the
             // purge_batch_size setting. For example if the field has 9 rows and the
             // batch size is 10 then this will add 1 step to $number_of_steps.
             $how_many_steps = ceil($row_count / $context['sandbox']['field']['purge_batch_size']);
             $context['sandbox']['field']['steps_to_delete'] += $how_many_steps;
         }
     }
     // Each field possibly needs one last field_purge_batch() call to remove the
     // last field and the field storage itself.
     $context['sandbox']['field']['steps_to_delete'] += count($fields);
     $context['sandbox']['field']['current_progress'] = 0;
 }
Пример #11
0
 /**
  * Tests install profile validation during configuration import.
  *
  * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
  */
 public function testInstallProfile()
 {
     $sync = $this->container->get('config.storage.sync');
     $extensions = $sync->read('core.extension');
     // Add an install profile.
     $extensions['module']['standard'] = 0;
     $sync->write('core.extension', $extensions);
     try {
         $this->configImporter->reset()->import();
         $this->fail('ConfigImporterException not thrown; an 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();
         // Install profiles should not even be scanned at this point.
         $this->assertEqual(['Unable to install the <em class="placeholder">standard</em> module since it does not exist.'], $error_log);
     }
 }
Пример #12
0
 /**
  * Tests updating of configuration during import.
  */
 function testUpdated()
 {
     $name = 'config_test.system';
     $dynamic_name = 'config_test.dynamic.dotted.default';
     $storage = $this->container->get('config.storage');
     $staging = $this->container->get('config.storage.staging');
     // Verify that the configuration objects to import exist.
     $this->assertIdentical($storage->exists($name), TRUE, $name . ' found.');
     $this->assertIdentical($storage->exists($dynamic_name), TRUE, $dynamic_name . ' found.');
     // Replace the file content of the existing configuration objects in the
     // staging directory.
     $original_name_data = array('foo' => 'beer');
     $staging->write($name, $original_name_data);
     $original_dynamic_data = $storage->read($dynamic_name);
     $original_dynamic_data['label'] = 'Updated';
     $staging->write($dynamic_name, $original_dynamic_data);
     // Verify the active configuration still returns the default values.
     $config = \Drupal::config($name);
     $this->assertIdentical($config->get('foo'), 'bar');
     $config = \Drupal::config($dynamic_name);
     $this->assertIdentical($config->get('label'), 'Default');
     // Import.
     $this->configImporter->reset()->import();
     // Verify the values were updated.
     \Drupal::configFactory()->reset($name);
     $config = \Drupal::config($name);
     $this->assertIdentical($config->get('foo'), 'beer');
     $config = \Drupal::config($dynamic_name);
     $this->assertIdentical($config->get('label'), 'Updated');
     // Verify that the original file content is still the same.
     $this->assertIdentical($staging->read($name), $original_name_data);
     $this->assertIdentical($staging->read($dynamic_name), $original_dynamic_data);
     // Verify that appropriate module API hooks have been invoked.
     $this->assertTrue(isset($GLOBALS['hook_config_test']['load']));
     $this->assertTrue(isset($GLOBALS['hook_config_test']['presave']));
     $this->assertFalse(isset($GLOBALS['hook_config_test']['insert']));
     $this->assertTrue(isset($GLOBALS['hook_config_test']['update']));
     $this->assertFalse(isset($GLOBALS['hook_config_test']['predelete']));
     $this->assertFalse(isset($GLOBALS['hook_config_test']['delete']));
     // Verify that there is nothing more to import.
     $this->assertFalse($this->configImporter->hasUnprocessedConfigurationChanges());
     $logs = $this->configImporter->getErrors();
     $this->assertEqual(count($logs), 0);
 }
Пример #13
0
/**
 * Alter the configuration synchronization steps.
 *
 * @param array $sync_steps
 *   A one-dimensional array of \Drupal\Core\Config\ConfigImporter method names
 *   or callables that are invoked to complete the import, in the order that
 *   they will be processed. Each callable item defined in $sync_steps should
 *   either be a global function or a public static method. The callable should
 *   accept a $context array by reference. For example:
 *   <code>
 *     function _additional_configuration_step(&$context) {
 *       // Do stuff.
 *       // If finished set $context['finished'] = 1.
 *     }
 *   </code>
 *   For more information on creating batches, see the
 *   @link batch Batch operations @endlink documentation.
 *
 * @see callback_batch_operation()
 * @see \Drupal\Core\Config\ConfigImporter::initialize()
 */
function hook_config_import_steps_alter(&$sync_steps, \Drupal\Core\Config\ConfigImporter $config_importer)
{
    $deletes = $config_importer->getUnprocessedConfiguration('delete');
    if (isset($deletes['field.storage.node.body'])) {
        $sync_steps[] = '_additional_configuration_step';
    }
}
Пример #14
0
 /**
  * Gets the list of changes that will be imported.
  *
  * @param string $op
  *   (optional) A change operation. Either delete, create or update. If
  *   supplied the returned list will be limited to this operation.
  * @param string $collection
  *   (optional) The collection to get the changelist for. Defaults to the
  *   default collection.
  *
  * @return array
  *   An array of config changes that are yet to be imported.
  *
  * @see \Drupal\Core\Config\StorageComparerInterface::getChangelist()
  */
 public function getChangelist($op = NULL, $collection = StorageInterface::DEFAULT_COLLECTION)
 {
     return $this->configImporter->getStorageComparer()->getChangelist($op, $collection);
 }
Пример #15
0
 /**
  * Processes all pending index tasks inside a batch run.
  *
  * @param array $context
  *   The current batch context.
  * @param \Drupal\Core\Config\ConfigImporter $config_importer
  *   The config importer.
  */
 public static function processIndexTasks(array &$context, ConfigImporter $config_importer)
 {
     $index_task_manager = \Drupal::getContainer()->get('search_api.index_task_manager');
     if (!isset($context['sandbox']['indexes'])) {
         $context['sandbox']['indexes'] = array();
         $indexes = \Drupal::entityTypeManager()->getStorage('search_api_index')->loadByProperties(array('status' => TRUE));
         $deleted = $config_importer->getUnprocessedConfiguration('delete');
         /** @var \Drupal\search_api\IndexInterface $index */
         foreach ($indexes as $index_id => $index) {
             if (!$index_task_manager->isTrackingComplete($index) && !in_array($index->getConfigDependencyName(), $deleted)) {
                 $context['sandbox']['indexes'][] = $index_id;
             }
         }
         $context['sandbox']['total'] = count($context['sandbox']['indexes']);
         if (!$context['sandbox']['total']) {
             $context['finished'] = 1;
             return;
         }
     }
     $index_id = array_shift($context['sandbox']['indexes']);
     $index = Index::load($index_id);
     $added = $index_task_manager->addItemsOnce($index);
     if ($added !== NULL) {
         array_unshift($context['sandbox']['indexes'], $index_id);
     }
     if (empty($context['sandbox']['indexes'])) {
         $context['finished'] = 1;
     } else {
         $finished = $context['sandbox']['total'] - count($context['sandbox']['indexes']);
         $context['finished'] = $finished / $context['sandbox']['total'];
         $args = array('%index' => $index->label(), '@num' => $finished + 1, '@total' => $context['sandbox']['total']);
         $context['message'] = \Drupal::translation()->translate('Tracking items for search index %index (@num of @total)', $args);
     }
 }
Пример #16
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $config_importer = new ConfigImporter($form_state->get('storage_comparer'), $this->eventDispatcher, $this->configManager, $this->lock, $this->typedConfigManager, $this->moduleHandler, $this->themeHandler, $this->getStringTranslation());
     if ($config_importer->alreadyImporting()) {
         drupal_set_message($this->t('Another request may be synchronizing configuration already.'));
     } else {
         try {
             $sync_steps = $config_importer->initialize();
             $batch = array('operations' => array(), 'finished' => array(get_class($this), 'finishBatch'), 'title' => t('Synchronizing configuration'), 'init_message' => t('Starting configuration synchronization.'), 'progress_message' => t('Completed @current step of @total.'), 'error_message' => t('Configuration synchronization has encountered an error.'), 'file' => drupal_get_path('module', 'config') . '/config.admin.inc');
             foreach ($sync_steps as $sync_step) {
                 $batch['operations'][] = array(array(get_class($this), 'processBatch'), array($config_importer, $sync_step));
             }
             batch_set($batch);
         } catch (ConfigImporterException $e) {
             // There are validation errors.
             drupal_set_message($this->t('The configuration synchronization failed validation.'));
             foreach ($config_importer->getErrors() as $message) {
                 drupal_set_message($message, 'error');
             }
         }
     }
 }
Пример #17
0
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     // The confirmation step needs no additional validation.
     if ($this->data) {
         return;
     }
     // Decode the submitted import.
     $data = Yaml::decode($form_state->getValue('import'));
     // Validate for config entities.
     if ($form_state->getValue('config_type') !== 'system.simple') {
         $definition = $this->entityManager->getDefinition($form_state->getValue('config_type'));
         $id_key = $definition->getKey('id');
         // If a custom entity ID is specified, override the value in the
         // configuration data being imported.
         if (!$form_state->isValueEmpty('custom_entity_id')) {
             $data[$id_key] = $form_state->getValue('custom_entity_id');
         }
         $entity_storage = $this->entityManager->getStorage($form_state->getValue('config_type'));
         // If an entity ID was not specified, set an error.
         if (!isset($data[$id_key])) {
             $form_state->setErrorByName('import', $this->t('Missing ID key "@id_key" for this @entity_type import.', array('@id_key' => $id_key, '@entity_type' => $definition->getLabel())));
             return;
         }
         $config_name = $definition->getConfigPrefix() . '.' . $data[$id_key];
         // If there is an existing entity, ensure matching ID and UUID.
         if ($entity = $entity_storage->load($data[$id_key])) {
             $this->configExists = $entity;
             if (!isset($data['uuid'])) {
                 $form_state->setErrorByName('import', $this->t('An entity with this machine name already exists but the import did not specify a UUID.'));
                 return;
             }
             if ($data['uuid'] !== $entity->uuid()) {
                 $form_state->setErrorByName('import', $this->t('An entity with this machine name already exists but the UUID does not match.'));
                 return;
             }
         } elseif (isset($data['uuid']) && $entity_storage->loadByProperties(array('uuid' => $data['uuid']))) {
             $form_state->setErrorByName('import', $this->t('An entity with this UUID already exists but the machine name does not match.'));
         }
     } else {
         $config_name = $form_state->getValue('config_name');
         $config = $this->config($config_name);
         $this->configExists = !$config->isNew() ? $config : FALSE;
     }
     // Use ConfigImporter validation.
     if (!$form_state->getErrors()) {
         $source_storage = new StorageReplaceDataWrapper($this->configStorage);
         $source_storage->replaceData($config_name, $data);
         $storage_comparer = new StorageComparer($source_storage, $this->configStorage, $this->configManager);
         if (!$storage_comparer->createChangelist()->hasChanges()) {
             $form_state->setErrorByName('import', $this->t('There are no changes to import.'));
         } else {
             $config_importer = new ConfigImporter($storage_comparer, $this->eventDispatcher, $this->configManager, $this->lock, $this->typedConfigManager, $this->moduleHandler, $this->moduleInstaller, $this->themeHandler, $this->getStringTranslation());
             try {
                 $config_importer->validate();
                 $form_state->set('config_importer', $config_importer);
             } catch (ConfigImporterException $e) {
                 // There are validation errors.
                 $item_list = ['#theme' => 'item_list', '#items' => $config_importer->getErrors(), '#title' => $this->t('The configuration cannot be imported because it failed validation for the following reasons:')];
                 $form_state->setErrorByName('import', $this->renderer->render($item_list));
             }
         }
     }
     // Store the decoded version of the submitted import.
     $form_state->setValueForElement($form['import'], $data);
 }
Пример #18
0
 /**
  * Returns a ConfigImporter object to import test importing of configuration.
  *
  * @return \Drupal\Core\Config\ConfigImporter
  *   The ConfigImporter object.
  */
 public function configImporter()
 {
     if (!$this->configImporter) {
         // Set up the ConfigImporter object for testing.
         $storage_comparer = new StorageComparer($this->container->get('config.storage.staging'), $this->container->get('config.storage'), $this->container->get('config.manager'));
         $this->configImporter = new ConfigImporter($storage_comparer, $this->container->get('event_dispatcher'), $this->container->get('config.manager'), $this->container->get('lock'), $this->container->get('config.typed'), $this->container->get('module_handler'), $this->container->get('module_installer'), $this->container->get('theme_handler'), $this->container->get('string_translation'));
     }
     // Always recalculate the changelist when called.
     return $this->configImporter->reset();
 }
Пример #19
0
 /**
  * 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'])];
     $theme_data = $this->getThemeData();
     $module_data = $this->getModuleData();
     // 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') {
             $message = FALSE;
             if (!isset($core_extension['module'][$owner]) && isset($module_data[$owner])) {
                 $message = $this->t('Configuration %name depends on the %owner module that will not be installed after import.', array('%name' => $name, '%owner' => $module_data[$owner]->info['name']));
             } elseif (!isset($core_extension['theme'][$owner]) && isset($theme_data[$owner])) {
                 $message = $this->t('Configuration %name depends on the %owner theme that will not be installed after import.', array('%name' => $name, '%owner' => $theme_data[$owner]->info['name']));
             } elseif (!isset($core_extension['module'][$owner]) && !isset($core_extension['theme'][$owner])) {
                 $message = $this->t('Configuration %name depends on the %owner extension that will not be installed after import.', array('%name' => $name, '%owner' => $owner));
             }
             if ($message) {
                 $config_importer->logError($message);
                 continue;
             }
         }
         $data = $config_importer->getStorageComparer()->getSourceStorage()->read($name);
         // Configuration entities have dependencies on modules, themes, and other
         // configuration entities that we can validate. Their content dependencies
         // are not validated since we assume that they are soft dependencies.
         // Configuration entities can be identified by having 'dependencies' and
         // 'uuid' keys.
         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)) {
                     $message = FALSE;
                     switch ($type) {
                         case 'module':
                             $message = $this->formatPlural(count($diffs), 'Configuration %name depends on the %module module that will not be installed after import.', 'Configuration %name depends on modules (%module) that will not be installed after import.', array('%name' => $name, '%module' => implode(', ', $this->getNames($diffs, $module_data))));
                             break;
                         case 'theme':
                             $message = $this->formatPlural(count($diffs), 'Configuration %name depends on the %theme theme that will not be installed after import.', 'Configuration %name depends on themes (%theme) that will not be installed after import.', array('%name' => $name, '%theme' => implode(', ', $this->getNames($diffs, $theme_data))));
                             break;
                         case 'config':
                             $message = $this->formatPlural(count($diffs), 'Configuration %name depends on the %config configuration that will not exist after import.', 'Configuration %name depends on configuration (%config) that will not exist after import.', array('%name' => $name, '%config' => implode(', ', $diffs)));
                             break;
                     }
                     if ($message) {
                         $config_importer->logError($message);
                     }
                 }
             }
         }
     }
 }