createSnapshot example

// Set up the active storage collections to test import.     $test1_storage->delete('config_test.create');
    $test1_storage->write('config_test.update', ['foo' => 'baz']);
    $test1_storage->write('config_test.delete', ['foo' => 'bar']);
    $test2_storage->delete('config_test.another_create');
    $test2_storage->write('config_test.another_update', ['foo' => 'baz']);
    $test2_storage->write('config_test.another_delete', ['foo' => 'bar']);

    // Create a snapshot.     $snapshot_storage = \Drupal::service('config.storage.snapshot');
    \Drupal::service('config.manager')->createSnapshot($active_storage$snapshot_storage);

    // Ensure that the snapshot has the expected collection data before import.     $test1_snapshot = $snapshot_storage->createCollection('collection.test1');
    $data = $test1_snapshot->read('config_test.delete');
    $this->assertEquals(['foo' => 'bar']$data, 'The config_test.delete in collection.test1 exists in the snapshot storage.');
    $data = $test1_snapshot->read('config_test.update');
    $this->assertEquals(['foo' => 'baz']$data, 'The config_test.update in collection.test1 exists in the snapshot storage.');
    $this->assertFalse($test1_snapshot->read('config_test.create'), 'The config_test.create in collection.test1 does not exist in the snapshot storage.');
    $test2_snapshot = $snapshot_storage->createCollection('collection.test2');
    $data = $test2_snapshot->read('config_test.another_delete');
    $this->assertEquals(['foo' => 'bar']$data, 'The config_test.another_delete in collection.test2 exists in the snapshot storage.');
    

  protected static $modules = ['config_test', 'system'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->installConfig(['system']);
    // Update the config snapshot. This allows the parent::setUp() to write     // configuration files.     \Drupal::service('config.manager')->createSnapshot(\Drupal::service('config.storage'), \Drupal::service('config.storage.snapshot'));
    $this->copyConfig($this->container->get('config.storage')$this->container->get('config.storage.sync'));
  }

  /** * Tests config snapshot creation and updating. */
  public function testSnapshot() {
    $active = $this->container->get('config.storage');
    $sync = $this->container->get('config.storage.sync');
    $snapshot = $this->container->get('config.storage.snapshot');
    $config_name = 'config_test.system';
    
$this->sourceStorage = $source_storage;
    $this->snapshotStorage = $snapshot_storage;
  }

  /** * Creates a config snapshot. * * @param \Drupal\Core\Config\ConfigImporterEvent $event * The Event to process. */
  public function onConfigImporterImport(ConfigImporterEvent $event) {
    $this->configManager->createSnapshot($this->sourceStorage, $this->snapshotStorage);
  }

  /** * Registers the methods in this class that should be listeners. * * @return array * An array of event listener definitions. */
  public static function getSubscribedEvents(): array {
    $events[ConfigEvents::IMPORT][] = ['onConfigImporterImport', 40];
    return $events;
  }
Home | Imprint | This part of the site doesn't use cookies.