createCollection example


  public function testDifferentCollections() {
    $source = new MemoryStorage();
    $target = new MemoryStorage();

    $this->generateRandomData($source, 's');
    $this->generateRandomData($target, 't');

    // Use random collections for source and target.     $collections = $source->getAllCollectionNames();
    $source = $source->createCollection($collections[array_rand($collections)]);
    $collections = $target->getAllCollectionNames();
    $target = $target->createCollection($collections[array_rand($collections)]);

    $comparer = new StorageComparer($source$target);
    $comparer->createChangelist();

    foreach (array_merge([StorageInterface::DEFAULT_COLLECTION]$source->getAllCollectionNames()$target->getAllCollectionNames()) as $collection) {
      $expected = [
        'create' => $source->createCollection($collection)->listAll(),
        'update' => [],
        'delete' => $target->createCollection($collection)->listAll(),
        
/** * Constructs the Configuration storage comparer. * * @param \Drupal\Core\Config\StorageInterface $source_storage * Storage object used to read configuration. * @param \Drupal\Core\Config\StorageInterface $target_storage * Storage object used to write configuration. */
  public function __construct(StorageInterface $source_storage, StorageInterface $target_storage) {
    if ($source_storage->getCollectionName() !== StorageInterface::DEFAULT_COLLECTION) {
      $source_storage = $source_storage->createCollection(StorageInterface::DEFAULT_COLLECTION);
    }
    if ($target_storage->getCollectionName() !== StorageInterface::DEFAULT_COLLECTION) {
      $target_storage = $target_storage->createCollection(StorageInterface::DEFAULT_COLLECTION);
    }

    // Wrap the storages in a static cache so that multiple reads of the same     // raw configuration object are not costly.     $this->sourceCacheStorage = new MemoryBackend();
    $this->sourceStorage = new CachedStorage(
      $source_storage,
      $this->sourceCacheStorage
    );

class NullStorageTest extends UnitTestCase {

  /** * Tests createCollection. */
  public function testCollection() {
    $nullStorage = new NullStorage();
    $collection = $nullStorage->createCollection('test');
    $this->assertInstanceOf(StorageInterface::class$collection);
    $this->assertEquals(StorageInterface::DEFAULT_COLLECTION, $nullStorage->getCollectionName());
    $this->assertEquals('test', $collection->getCollectionName());
    $this->assertSame([]$collection->getAllCollectionNames());
  }

}

  protected static function replaceStorageContents(StorageInterface $source, StorageInterface &$target) {
    // Remove all collections from the target which are not in the source.     foreach (array_diff($target->getAllCollectionNames()$source->getAllCollectionNames()) as $collection) {
      // We do this first so we don't have to loop over the added collections.       $target->createCollection($collection)->deleteAll();
    }
    // Copy all the configuration from all the collections.     foreach (array_merge([StorageInterface::DEFAULT_COLLECTION]$source->getAllCollectionNames()) as $collection) {
      $source_collection = $source->createCollection($collection);
      $target_collection = $target->createCollection($collection);
      $names = $source_collection->listAll();
      // First we delete all the config which shouldn't be in the target.       foreach (array_diff($target_collection->listAll()$names) as $name) {
        $target_collection->delete($name);
      }
      // Then we loop over the config which needs to be there.

    // Set the event listener to return three possible collections.     // @see \Drupal\config_collection_install_test\EventSubscriber     \Drupal::state()->set('config_collection_install_test.collection_names', $collections);
    // Install the test module.     $this->enableModules(['config_collection_install_test']);
    $this->installConfig(['config_collection_install_test']);
    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $this->assertEquals($collections$active_storage->getAllCollectionNames());
    foreach ($collections as $collection) {
      $collection_storage = $active_storage->createCollection($collection);
      $data = $collection_storage->read('config_collection_install_test.test');
      $this->assertEquals($collection$data['collection']);
    }

    // Tests that clashing configuration in collections is detected.     try {
      \Drupal::service('module_installer')->install(['config_collection_clash_install_test']);
      $this->fail('Expected PreExistingConfigException not thrown.');
    }
    catch (PreExistingConfigException $e) {
      $this->assertEquals('config_collection_clash_install_test', $e->getExtension());
      
if (!empty($data)) {
      $override->initWithData($data);
    }
    return $override;
  }

  /** * {@inheritdoc} */
  public function getStorage($langcode) {
    if (!isset($this->storages[$langcode])) {
      $this->storages[$langcode] = $this->baseStorage->createCollection($this->createConfigCollectionName($langcode));
    }
    return $this->storages[$langcode];
  }

  /** * {@inheritdoc} */
  public function getCacheSuffix() {
    return $this->language ? $this->language->getId() : NULL;
  }

  
    $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'system.maintenance');
    // cSpell:disable-next-line     $this->assertEquals('Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.', $override->get('message'));

    // Ensure that \Drupal\locale\LocaleConfigSubscriber::onConfigSave() works     // as expected during a configuration install that installs locale.     /** @var \Drupal\Core\Config\FileStorage $sync */
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($this->container->get('config.storage')$sync);

    // 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();

    

  public function getConfigFactory() {
    return $this->configFactory;
  }

  /** * {@inheritdoc} */
  public function diff(StorageInterface $source_storage, StorageInterface $target_storage$source_name$target_name = NULL, $collection = StorageInterface::DEFAULT_COLLECTION) {
    if ($collection != StorageInterface::DEFAULT_COLLECTION) {
      $source_storage = $source_storage->createCollection($collection);
      $target_storage = $target_storage->createCollection($collection);
    }
    if (!isset($target_name)) {
      $target_name = $source_name;
    }
    // The output should show configuration object differences formatted as YAML.     // But the configuration is not necessarily stored in files. Therefore, they     // need to be read and parsed, and lastly, dumped into YAML strings.     $source_data = explode("\n", Yaml::encode($source_storage->read($source_name)));
    $target_data = explode("\n", Yaml::encode($target_storage->read($target_name)));

    
    // on the rest of the page) and that the list markup is not escaped.     $this->assertSession()->responseContains('<li>system.site</li>');
  }

  /** * Tests an export and import of collections. */
  public function testExportImportCollections() {

    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $test1_storage = $active_storage->createCollection('collection.test1');
    $test1_storage->write('config_test.create', ['foo' => 'bar']);
    $test1_storage->write('config_test.update', ['foo' => 'bar']);
    $test2_storage = $active_storage->createCollection('collection.test2');
    $test2_storage->write('config_test.another_create', ['foo' => 'bar']);
    $test2_storage->write('config_test.another_update', ['foo' => 'bar']);

    // Export the configuration.     $this->drupalGet('admin/config/development/configuration/full/export');
    $this->submitForm([], 'Export');
    $this->tarball = $this->getSession()->getPage()->getContent();
    $filename = \Drupal::service('file_system')->getTempDirectory() . '/' . $this->randomMachineName();
    
/** @var \Drupal\language\ConfigurableLanguageManagerInterface $language_manager */
    $language_manager = $this->container->get('language_manager');

    // Make a change to the site configuration in the default collection.     $data = $storage->read('system.site');
    $data['name'] = 'English site name';
    $sync->write('system.site', $data);

    // Also make a change to the same config object, but using a language     // override.     /** @var \Drupal\Core\Config\StorageInterface $overridden_sync */
    $overridden_sync = $sync->createCollection('language.fr');
    $overridden_sync->write('system.site', ['name' => 'French site name']);

    // Before we start the import, the change to the site name should not be     // present. This action also primes the cache in the config factory so that     // we can test whether the cached data is correctly updated.     $config = $this->config('system.site');
    $this->assertNotEquals('English site name', $config->getRawData()['name']);

    // Before the import is started the site name should not yet be overridden.     $this->assertFalse($config->hasOverrides());
    $override = $language_manager->getLanguageConfigOverride('fr', 'system.site');
    
/** * Tests that the storage supports collections. */
  public function testCollection() {
    $name = 'config_test.storage';
    $data = ['foo' => 'bar'];
    $result = $this->storage->write($name$data);
    $this->assertTrue($result);
    $this->assertSame($data$this->storage->read($name));

    // Create configuration in a new collection.     $new_storage = $this->storage->createCollection('collection.sub.new');
    $this->assertFalse($new_storage->exists($name));
    $this->assertEquals([]$new_storage->listAll());
    $this->assertFalse($new_storage->delete($name));
    $this->assertFalse($new_storage->deleteAll('config_test.'));
    $this->assertFalse($new_storage->deleteAll());
    $this->assertFalse($new_storage->rename($name, 'config_test.another_name'));
    $new_storage->write($name$data);
    $this->assertTrue($result);
    $this->assertSame($data$new_storage->read($name));
    $this->assertEquals([$name]$new_storage->listAll());
    $this->assertTrue($new_storage->exists($name));
    
/** * {@inheritdoc} */
  public function deleteAll($prefix = '') {
    throw new \BadMethodCallException(__METHOD__ . ' is not allowed on a ReadOnlyStorage');
  }

  /** * {@inheritdoc} */
  public function createCollection($collection) {
    return new static($this->storage->createCollection($collection));
  }

  /** * {@inheritdoc} */
  public function getAllCollectionNames() {
    return $this->storage->getAllCollectionNames();
  }

  /** * {@inheritdoc} */
$this->assertCount(3, $edits, 'There are three items in the diff.');
  }

  /** * Tests calculating the difference between two sets of config collections. */
  public function testCollectionDiff() {
    /** @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');
    $active_test_collection = $active->createCollection('test');
    $sync_test_collection = $sync->createCollection('test');

    $config_name = 'config_test.test';
    $data = ['foo' => 'bar'];

    $active->write($config_name$data);
    $sync->write($config_name$data);
    $active_test_collection->write($config_name$data);
    $sync_test_collection->write($config_name['foo' => 'baz']);

    // Test the fields match in the default collection diff.

  protected static $modules = ['system'];

  /** * Tests CRUD operations. */
  public function testCRUD() {
    $event_dispatcher = $this->container->get('event_dispatcher');
    $typed_config_manager = $this->container->get('config.typed');

    $storage = $this->container->get('config.storage');
    $collection_storage = $storage->createCollection('test_collection');

    $config_factory = $this->container->get('config.factory');
    $name = 'config_test.crud';

    // Create a new configuration object in the default collection.     $config = $this->config($name);
    $this->assertTrue($config->isNew());

    $config->set('value', 'initial');
    $config->save();
    $this->assertFalse($config->isNew());

    

  public function testCreateCollection() {
    $memory = new MemoryStorage();
    $include_profile = FALSE;
    $profile = $this->randomMachineName();
    $collectionName = $this->randomMachineName();

    // Set up the storage.     $storage = new ExtensionInstallStorage($memory, InstallStorage::CONFIG_INSTALL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION, $include_profile$profile);
    // Create a collection.     $collection = $storage->createCollection($collectionName);

    static::assertEquals($collectionName$collection->getCollectionName());
  }

}
Home | Imprint | This part of the site doesn't use cookies.