getRawData example


  protected function setUp(): void {
    parent::setUp();
    $this->installConfig(['system']);
  }

  /** * Tests the import transformation. */
  public function testTransform() {
    // Get the raw system.site config and set it in the sync storage.     $rawConfig = $this->config('system.site')->getRawData();

    $storage = new MemoryStorage();
    $this->copyConfig($this->container->get('config.storage')$storage);

    $import = $this->container->get('config.import_transformer')->transform($storage);
    $config = $import->read('system.site');
    // The test subscriber always adds "Arrr" to the current site name.     $this->assertEquals($rawConfig['name'] . ' Arrr', $config['name']);
    $this->assertEquals($rawConfig['slogan']$config['slogan']);

    // Update the site config in the storage to test a second transformation.

  public function testConfigLanguageOverride() {
    // The language module implements a config factory override object that     // overrides configuration when the Language module is enabled. This test ensures that     // English overrides work.     \Drupal::languageManager()->setConfigOverrideLanguage(\Drupal::languageManager()->getLanguage('en'));
    $config = \Drupal::config('config_test.system');
    $this->assertSame('en bar', $config->get('foo'));

    // Ensure that the raw data is not translated.     $raw = $config->getRawData();
    $this->assertSame('bar', $raw['foo']);

    ConfigurableLanguage::createFromLangcode('fr')->save();
    ConfigurableLanguage::createFromLangcode('de')->save();

    \Drupal::languageManager()->setConfigOverrideLanguage(\Drupal::languageManager()->getLanguage('fr'));
    $config = \Drupal::config('config_test.system');
    $this->assertSame('fr bar', $config->get('foo'));

    \Drupal::languageManager()->setConfigOverrideLanguage(\Drupal::languageManager()->getLanguage('de'));
    $config = \Drupal::config('config_test.system');
    
// Config is no longer new once saved.     $this->config->save();
    $this->assertFalse($this->config->isNew());
  }

  /** * @covers ::setData * @dataProvider nestedDataProvider */
  public function testSetData($data) {
    $this->config->setData($data);
    $this->assertEquals($data$this->config->getRawData());
    $this->assertConfigDataEquals($data);
  }

  /** * @covers ::save * @dataProvider nestedDataProvider */
  public function testSaveNew($data) {
    $this->cacheTagsInvalidator->expects($this->never())
      ->method('invalidateTags');

    
$this->assertNull($config_raw->get('key'));
    $config_raw
      ->set('key', 'raw')
      ->set('new_key', 'new_value')
      ->save();
    // Ensure override is preserved but all other data has been updated     // accordingly.     $config = \Drupal::config('config_test.new');
    $this->assertFalse($config->isNew(), 'The configuration object config_test.new is not new');
    $this->assertSame('override', $config->get('key'));
    $this->assertSame('new_value', $config->get('new_key'));
    $raw_data = $config->getRawData();
    $this->assertSame('raw', $raw_data['key']);
  }

}
/** * @covers ::rename */
  public function testRename() {
    $old = new Config($this->randomMachineName()$this->storage, $this->eventDispatcher, $this->typedConfig);
    $new = new Config($this->randomMachineName()$this->storage, $this->eventDispatcher, $this->typedConfig);

    $this->storage->expects($this->exactly(2))
      ->method('readMultiple')
      ->willReturnMap([
        [[$old->getName()]$old->getRawData()],
        [[$new->getName()]$new->getRawData()],
      ]);

    $this->cacheTagsInvalidator->expects($this->once())
      ->method('invalidateTags')
      ->with($old->getCacheTags());

    $this->storage->expects($this->once())
      ->method('rename')
      ->with($old->getName()$new->getName());

    

  public function configEventRecorder(ConfigCrudEvent $event$name) {
    $config = $event->getConfig();
    $this->state->set('config_events_test.event', [
      'event_name' => $name,
      'current_config_data' => $config->get(),
      'original_config_data' => $config->getOriginal(),
      'raw_config_data' => $config->getRawData(),
    ]);
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    $events[ConfigEvents::SAVE][] = ['configEventRecorder'];
    $events[ConfigEvents::DELETE][] = ['configEventRecorder'];
    $events[ConfigEvents::RENAME][] = ['configEventRecorder'];
    return $events;
  }
// 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');
    $this->assertTrue($override->isNew());

    // Start the import of the new configuration.     $this->configImporter->reset()->import();

    // Verify the new site name in the default language.     $config = $this->config('system.site')->getRawData();
    


  public function testNormalize() {
    $config_entity = \Drupal::entityTypeManager()->getStorage('config_test')->create(['id' => 'system', 'label' => 'foobar', 'weight' => 1]);
    $config_entity->save();

    // Modify stored config entity, this is comparable with a schema change.     $config = $this->config('config_test.dynamic.system');
    $data = [
      'label' => 'foobar',
      'additional_key' => TRUE,
    ] + $config->getRawData();
    $config->setData($data)->save();
    $this->assertNotSame($config_entity->toArray()$config->getRawData(), 'Stored config entity is not is equivalent to config schema.');
    $config_entity = \Drupal::entityTypeManager()->getStorage('config_test')->load('system');
    $config_entity->save();

    $config = $this->config('config_test.dynamic.system');
    $this->assertSame($config_entity->toArray()$config->getRawData(), 'Stored config entity is equivalent to config schema.');
  }

}

  protected function setUp(): void {
    parent::setUp();
    $this->installConfig(['system']);
  }

  /** * Tests getting the export storage. */
  public function testGetStorage() {
    // Get the raw system.site config and set it in the sync storage.     $rawConfig = $this->config('system.site')->getRawData();
    $this->container->get('config.storage.sync')->write('system.site', $rawConfig);

    // The export storage manager under test.     $manager = new ExportStorageManager(
      $this->container->get('config.storage'),
      $this->container->get('database'),
      $this->container->get('event_dispatcher'),
      new NullLockBackend()
    );

    $storage = $manager->getStorage();
    
$this->assertTrue($config->isNew());

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

    // Verify the active configuration contains the saved value.     $actual_data = $storage->read($name);
    $this->assertSame(['value' => 'initial']$actual_data);

    // Verify the config factory contains the saved value.     $actual_data = $config_factory->get($name)->getRawData();
    $this->assertSame(['value' => 'initial']$actual_data);

    // Create another instance of the config object using a custom collection.     $collection_config = new Config(
      $name,
      $collection_storage,
      $event_dispatcher,
      $typed_config_manager
    );
    $collection_config->set('value', 'overridden');
    $collection_config->save();

    
'system_site',
    ];
    $this->executeMigrations($migrations);
  }

  /** * Tests that all expected configuration gets migrated. */
  public function testConfigurationMigration() {
    foreach ($this->expectedConfig as $config_id => $values) {
      if ($config_id == 'system.mail') {
        $actual = \Drupal::config($config_id)->getRawData();
      }
      else {
        $actual = \Drupal::config($config_id)->get();
      }
      unset($actual['_core']);
      $this->assertSame($values$actual$config_id . ' matches expected values.');
    }
    // The d7_system_authorize migration should not create the system.authorize     // config.     $this->assertTrue($this->config('system.authorize')->isNew());
  }

}
return;
    }

    // Ensure that the static cache contains up to date configuration objects by     // replacing the data on any entries for the configuration object apart     // from the one that references the actual config object being saved.     foreach ($this->getConfigCacheKeys($saved_config->getName()) as $cache_key) {
      $cached_config = $this->cache[$cache_key];
      if ($cached_config !== $saved_config) {
        // We can not just update the data since other things about the object         // might have changed. For example, whether or not it is new.         $this->cache[$cache_key]->initWithData($saved_config->getRawData());
      }
    }
  }

  /** * Removes stale static cache entries when configuration is deleted. * * @param \Drupal\Core\Config\ConfigCrudEvent $event * The configuration event. */
  public function onConfigDelete(ConfigCrudEvent $event) {
    
Home | Imprint | This part of the site doesn't use cookies.