configImporter example

$active = $this->container->get('config.storage');
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($active$sync);

    // Save as files in the sync directory.     $field = $active->read($field_config_name);
    $new_label = 'Test update import field';
    $field['label'] = $new_label;
    $sync->write($field_config_name$field);

    // Import the content of the sync directory.     $this->configImporter()->import();

    // Check that the updated config was correctly imported.     $field = FieldConfig::load($field_id);
    $this->assertEquals($new_label$field->getLabel(), 'field label updated');
  }

}
$this->assertSame($expected_original_data['404']$config->getOriginal('404', FALSE));

    // Write file to sync.     $sync = $this->container->get('config.storage.sync');
    $expected_new_data = [
      'foo' => 'barbar',
      '404' => 'try again',
    ];
    $sync->write('config_test.system', $expected_new_data);

    // Import changed data from sync to active.     $this->configImporter()->import();
    $data = $active->read('config_test.system');

    // Verify that the new configuration data exists. Have to read storage     // directly otherwise overrides will apply.     $this->assertSame($expected_new_data['foo']$data['foo']);
    $this->assertFalse(isset($data['baz']));
    $this->assertSame($expected_new_data['404']$data['404']);

    // Verify that the overrides are still working.     $config = \Drupal::config('config_test.system');
    $this->assertSame($overrides['config_test.system']['foo']$config->get('foo'));
    
    $config_data = $this->config('node.type.example')->get();
    $config_data['description'] = 'A new description';
    $config_sync->write('node.type.example', $config_data);

    // Alter the values of simple config.     $config_data = $this->config('core.extension')->get();
    $config_data['module']['node'] = 1;
    $config_sync->write('core.extension', $config_data);

    // There are no Nodes with the moderation state test1, so this should run     // with no errors.     $this->configImporter()->reset()->import();

    $node = Node::create([
      'type' => 'example',
      'title' => 'Test title',
      'moderation_state' => 'test2',
    ]);
    $node->save();

    $config_data = $this->config('workflows.workflow.editorial')->get();
    unset($config_data['type_settings']['states']['test2']);
    unset($config_data['type_settings']['states']['test3']);
    
// Add our own translation to the config that will be imported.     $af_sync = $sync->createCollection('language.af');
    $data = $af_sync->read('system.maintenance');
    $data['message'] = 'Test af message';
    $af_sync->write('system.maintenance', $data);

    // Uninstall locale module.     $this->container->get('module_installer')->uninstall(['locale_test_translate']);
    $this->container->get('module_installer')->uninstall(['locale']);
    $this->resetAll();

    $this->configImporter()->import();

    $this->drupalGet('admin/reports/translations/check');
    $status = locale_translation_get_status();
    $status['drupal']['af']->type = 'current';
    \Drupal::state()->set('locale.translation_status', $status);
    $this->drupalGet('admin/reports/translations');
    $this->submitForm([], 'Update translations');

    // Check if configuration translations have been imported.     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'system.maintenance');
    $this->assertEquals('Test af message', $override->get('message'));
  }
file_put_contents($this->publicFilesDirectory . '/translations/drupal-8.0.0.es.po', $this->getPo('es'));
    $locale_settings = Yaml::decode(file_get_contents($this->siteDirectory . '/config/sync/locale.settings.yml'));
    $locale_settings['translation']['use_source'] = 'local';
    $locale_settings['translation']['path'] = $this->publicFilesDirectory . '/translations';
    file_put_contents($this->siteDirectory . '/config/sync/locale.settings.yml', Yaml::encode($locale_settings));
  }

  /** * Confirms that the installation installed the configuration correctly. */
  public function testConfigSync() {
    $comparer = $this->configImporter()->getStorageComparer();
    $expected_changelist_default_collection = [
      'create' => [],
      // The system.mail is changed configuration because the test system       // changes it to ensure that mails are not sent.       'update' => ['system.mail'],
      'delete' => [],
      'rename' => [],
    ];
    $this->assertEquals($expected_changelist_default_collection$comparer->getChangelist());
    $expected_changelist_spanish_collection = [
      'create' => [],
      
$field_storage_uuid_2 = FieldStorageConfig::load($field_storage_id_2)->uuid();

    $active = $this->container->get('config.storage');
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($active$sync);
    $this->assertTrue($sync->delete($field_storage_config_name)new FormattableMarkup('Deleted field storage: @field_storage', ['@field_storage' => $field_storage_config_name]));
    $this->assertTrue($sync->delete($field_storage_config_name_2)new FormattableMarkup('Deleted field storage: @field_storage', ['@field_storage' => $field_storage_config_name_2]));
    $this->assertTrue($sync->delete($field_config_name)new FormattableMarkup('Deleted field: @field', ['@field' => $field_config_name]));
    $this->assertTrue($sync->delete($field_config_name_2a)new FormattableMarkup('Deleted field: @field', ['@field' => $field_config_name_2a]));
    $this->assertTrue($sync->delete($field_config_name_2b)new FormattableMarkup('Deleted field: @field', ['@field' => $field_config_name_2b]));

    $deletes = $this->configImporter()->getUnprocessedConfiguration('delete');
    $this->assertCount(5, $deletes, 'Importing configuration will delete 3 fields and 2 field storages.');

    // Import the content of the sync directory.     $this->configImporter()->import();

    // Check that the field storages and fields are gone.     \Drupal::entityTypeManager()->getStorage('field_storage_config')->resetCache([$field_storage_id]);
    $field_storage = FieldStorageConfig::load($field_storage_id);
    $this->assertNull($field_storage, 'The field storage was deleted.');
    \Drupal::entityTypeManager()->getStorage('field_storage_config')->resetCache([$field_storage_id_2]);
    $field_storage_2 = FieldStorageConfig::load($field_storage_id_2);
    

  public function assertConfigUpdateImport(string $name, array $original_data, array $custom_data): void {
    $this->container->get('config.storage.sync')->write($name$custom_data);

    // Verify the active configuration still returns the default values.     $config = $this->config($name);
    $this->assertSame($config->get()$original_data);

    // Import.     $this->configImporter()->import();

    // Verify the values were updated.     $this->container->get('config.factory')->reset($name);
    $config = $this->config($name);
    $this->assertSame($config->get()$custom_data);
  }

}

  public function testImportLock() {
    // 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.     $this->drupalGet('admin/config/development/configuration');
    $this->assertSession()->pageTextNotContains('The staged configuration is identical to the active configuration.');

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

    // Attempt to import configuration and verify that an error message appears.     $this->submitForm([], 'Import all');
    $this->assertSession()->pageTextContains('Another request may be synchronizing configuration already.');

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

    // Verify site name has not changed.     $this->assertNotEquals($this->config('system.site')->get('name')$new_site_name);
  }
// Simulate config data to import.     $active = $this->container->get('config.storage');
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($active$sync);
    // Manually add new node type.     $src_dir = __DIR__ . '/../../../modules/node_test_config/sync';
    $target_dir = Settings::get('config_sync_directory');
    $this->assertNotFalse(\Drupal::service('file_system')->copy("$src_dir/$node_type_config_name.yml", "$target_dir/$node_type_config_name.yml"));

    // Import the content of the sync directory.     $this->configImporter()->import();

    // Check that the content type was created.     $node_type = NodeType::load($node_type_id);
    $this->assertNotEmpty($node_type, 'Import node type from sync was created.');
    $this->assertNull(FieldConfig::loadByName('node', $node_type_id, 'body'));
  }

}
// Copy config to sync, and delete the image style.     $sync = $this->container->get('config.storage.sync');
    $active = $this->container->get('config.storage');
    // Remove the image field from the display, to avoid a dependency error     // during import.     EntityViewDisplay::load('node.article.default')
      ->removeComponent($field_name)
      ->save();
    $this->copyConfig($active$sync);
    $sync->delete('image.style.' . $style_name);
    $this->configImporter()->import();

    $this->assertNull(ImageStyle::load($style_name), 'Style deleted after config import.');
    $this->assertEquals(0, $this->getImageCount($style), 'Image style was flushed after being deleted by config import.');
  }

  /** * Tests access for the image style listing. */
  public function testImageStyleAccess() {
    $style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
    $style->save();

    
public function assertConfigEntityImport(ConfigEntityInterface $entity) {
    // Save original config information.     $entity_uuid = $entity->uuid();
    $entity_type_id = $entity->getEntityTypeId();
    $original_data = $entity->toArray();
    // Copy everything to sync.     $this->copyConfig(\Drupal::service('config.storage'), \Drupal::service('config.storage.sync'));
    // Delete the configuration from active. Don't worry about side effects of     // deleting config like fields cleaning up field storages. The coming import     // should recreate everything as necessary.     $entity->delete();
    $this->configImporter()->reset()->import();
    $imported_entity = \Drupal::service('entity.repository')->loadEntityByUuid($entity_type_id$entity_uuid);
    $this->assertSame($original_data$imported_entity->toArray());
  }

}
// Change a configuration value in sync.     $sync_data = $this->config($config_name)->get();
    $sync_data[$config_key] = $new_data;
    $sync->write($config_name$sync_data);

    // Verify that active and snapshot match, and that sync doesn't match     // active.     $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
    $this->assertTrue($sync_snapshot_comparer->createChangelist()->hasChanges());

    // Import changed data from sync to active.     $this->configImporter()->import();

    // Verify changed config was properly imported.     \Drupal::configFactory()->reset($config_name);
    $this->assertSame($new_data$this->config($config_name)->get($config_key));

    // Verify that a new snapshot was created which and that it matches     // the active config.     $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
  }

}
// Verify that a bare $this->config() does not involve module APIs.     $this->assertFalse(isset($GLOBALS['hook_config_test']));
  }

  /** * Tests that trying to import from empty sync configuration directory fails. */
  public function testEmptyImportFails() {
    $this->expectException(ConfigImporterException::class);
    $this->container->get('config.storage.sync')->deleteAll();
    $this->configImporter()->import();
  }

  /** * Tests verification of site UUID before importing configuration. */
  public function testSiteUuidValidate() {
    $sync = \Drupal::service('config.storage.sync');
    // Create updated configuration object.     $config_data = $this->config('system.site')->get();
    // Generate a new site UUID.     $config_data['uuid'] = \Drupal::service('uuid')->generate();
    
unset($parameters['forms']['install_configure_form']['enable_update_status_module']);
    unset($parameters['forms']['install_configure_form']['enable_update_status_emails']);

    return $parameters;
  }

  /** * Confirms that the installation installed the configuration correctly. */
  public function testConfigSync() {
    // After installation there is no snapshot and nothing to import.     $change_list = $this->configImporter()->getStorageComparer()->getChangelist();
    $expected = [
      'create' => [],
      // The system.mail is changed configuration because the test system       // changes it to ensure that mails are not sent.       'update' => ['system.mail'],
      'delete' => [],
      'rename' => [],
    ];
    $this->assertEquals($expected$change_list);
  }

  
$this->assertNoModuleConfig($module);
      $this->assertModuleTablesDoNotExist($module);
    }

    // Import the configuration thereby re-installing all the modules.     $this->drupalGet('admin/config/development/configuration');
    $this->submitForm([], 'Import all');
    // Modules have been installed that have services.     $this->rebuildContainer();

    // Check that there are no errors.     $this->assertSame([]$this->configImporter()->getErrors());

    // Check that all modules that were uninstalled are now reinstalled.     $this->assertModules(array_keys($modules_to_uninstall), TRUE);
    foreach ($modules_to_uninstall as $module => $info) {
      $this->assertModuleConfig($module);
      $this->assertModuleTablesExist($module);
    }

    // Ensure that we have no configuration changes to import.     $storage_comparer = new StorageComparer(
      $this->container->get('config.storage.sync'),
      
Home | Imprint | This part of the site doesn't use cookies.