/** * @covers ::merge * @dataProvider mergeDataProvider */ public function testMerge($data, $data_to_merge, $merged_data) { // Set initial data. $this->config->setData($data); // Data to merge. $this->config->merge($data_to_merge); // Check that data has merged correctly. $this->assertEquals($merged_data, $this->config->getRawData()); }
/** * @covers ::setData * @covers ::set * @covers ::initWithData */ public function testSafeStringHandling() { // Safe strings are cast when using ::set(). $safe_string = Markup::create('bar'); $this->config->set('foo', $safe_string); $this->assertSame('bar', $this->config->get('foo')); $this->config->set('foo', ['bar' => $safe_string]); $this->assertSame('bar', $this->config->get('foo.bar')); // Safe strings are cast when using ::setData(). $this->config->setData(['bar' => $safe_string]); $this->assertSame('bar', $this->config->get('bar')); // Safe strings are not cast when using ::initWithData(). $this->config->initWithData(['bar' => $safe_string]); $this->assertSame($safe_string, $this->config->get('bar')); }
/** * Imports a configuration entity rename. * * @param string $collection * The configuration collection. * @param string $rename_name * The rename configuration name, as provided by * \Drupal\Core\Config\StorageComparer::createRenameName(). * * @throws \Drupal\Core\Entity\EntityStorageException * Thrown if the data is owned by an entity type, but the entity storage * does not support imports. * * @return bool * TRUE if the configuration was imported as a configuration entity. FALSE * otherwise. * * @see \Drupal\Core\Config\ConfigImporter::createRenameName() */ protected function importInvokeRename($collection, $rename_name) { $names = $this->storageComparer->extractRenameNames($rename_name); $entity_type_id = $this->configManager->getEntityTypeIdByName($names['old_name']); $old_config = new Config($names['old_name'], $this->storageComparer->getTargetStorage($collection), $this->eventDispatcher, $this->typedConfigManager); if ($old_data = $this->storageComparer->getTargetStorage($collection)->read($names['old_name'])) { $old_config->initWithData($old_data); } $data = $this->storageComparer->getSourceStorage($collection)->read($names['new_name']); $new_config = new Config($names['new_name'], $this->storageComparer->getTargetStorage($collection), $this->eventDispatcher, $this->typedConfigManager); if ($data !== FALSE) { $new_config->setData($data); } $entity_storage = $this->configManager->getEntityManager()->getStorage($entity_type_id); // Call to the configuration entity's storage to handle the configuration // change. if (!$entity_storage instanceof ImportableEntityStorageInterface) { throw new EntityStorageException(SafeMarkup::format('The entity storage "@storage" for the "@entity_type" entity type does not support imports', array('@storage' => get_class($entity_storage), '@entity_type' => $entity_type_id))); } $entity_storage->importRename($names['old_name'], $new_config, $old_config); $this->setProcessedConfiguration($collection, 'rename', $rename_name); return TRUE; }
/** * Creates configuration in a collection based on the provided list. * * @param string $collection * The configuration collection. * @param array $config_to_create * An array of configuration data to create, keyed by name. */ protected function createConfiguration($collection, array $config_to_create) { // Order the configuration to install in the order of dependencies. if ($collection == StorageInterface::DEFAULT_COLLECTION) { $dependency_manager = new ConfigDependencyManager(); $config_names = $dependency_manager->setData($config_to_create)->sortAll(); } else { $config_names = array_keys($config_to_create); } foreach ($config_names as $name) { // Allow config factory overriders to use a custom configuration object if // they are responsible for the collection. $overrider = $this->configManager->getConfigCollectionInfo()->getOverrideService($collection); if ($overrider) { $new_config = $overrider->createConfigObject($name, $collection); } else { $new_config = new Config($name, $this->getActiveStorages($collection), $this->eventDispatcher, $this->typedConfig); } if ($config_to_create[$name] !== FALSE) { $new_config->setData($config_to_create[$name]); } if ($collection == StorageInterface::DEFAULT_COLLECTION && ($entity_type = $this->configManager->getEntityTypeIdByName($name))) { // If we are syncing do not create configuration entities. Pluggable // configuration entities can have dependencies on modules that are // not yet enabled. This approach means that any code that expects // default configuration entities to exist will be unstable after the // module has been enabled and before the config entity has been // imported. if ($this->isSyncing()) { continue; } /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */ $entity_storage = $this->configManager->getEntityManager()->getStorage($entity_type); // It is possible that secondary writes can occur during configuration // creation. Updates of such configuration are allowed. if ($this->getActiveStorages($collection)->exists($name)) { $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix()); $entity = $entity_storage->load($id); $entity = $entity_storage->updateFromStorageRecord($entity, $new_config->get()); } else { $entity = $entity_storage->createFromStorageRecord($new_config->get()); } if ($entity->isInstallable()) { $entity->trustData()->save(); } } else { $new_config->save(TRUE); } } }
/** * Creates configuration in a collection based on the provided list. * * @param string $collection * The configuration collection. * @param array $config_to_install * A list of configuration object names to create. */ protected function createConfiguration($collection, array $config_to_install) { // Order the configuration to install in the order of dependencies. $data = $this->getSourceStorage($collection)->readMultiple($config_to_install); $config_entity_support = $this->configManager->supportsConfigurationEntities($collection); if ($config_entity_support) { $dependency_manager = new ConfigDependencyManager(); $config_to_install = $dependency_manager->setData($data)->sortAll(); } // Remove configuration that already exists in the active storage. $config_to_install = array_diff($config_to_install, $this->getActiveStorage($collection)->listAll()); foreach ($config_to_install as $name) { // Allow config factory overriders to use a custom configuration object if // they are responsible for the collection. $overrider = $this->configManager->getConfigCollectionInfo()->getOverrideService($collection); if ($overrider) { $new_config = $overrider->createConfigObject($name, $collection); } else { $new_config = new Config($name, $this->getActiveStorage($collection), $this->eventDispatcher, $this->typedConfig); } if ($data[$name] !== FALSE) { $new_config->setData($data[$name]); } if ($config_entity_support && ($entity_type = $this->configManager->getEntityTypeIdByName($name))) { // If we are syncing do not create configuration entities. Pluggable // configuration entities can have dependencies on modules that are // not yet enabled. This approach means that any code that expects // default configuration entities to exist will be unstable after the // module has been enabled and before the config entity has been // imported. if ($this->isSyncing) { continue; } $entity_storage = $this->configManager->getEntityManager()->getStorage($entity_type); // It is possible that secondary writes can occur during configuration // creation. Updates of such configuration are allowed. if ($this->getActiveStorage($collection)->exists($name)) { $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix()); $entity = $entity_storage->load($id); foreach ($new_config->get() as $property => $value) { $entity->set($property, $value); } $entity->save(); } else { $entity_storage->create($new_config->get())->save(); } } else { $new_config->save(); } } }