View source
<?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\KernelTests\KernelTestBase;
class ConfigOverrideTest extends KernelTestBase {
protected static $modules = [
'system',
'config_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'system',
]);
$this
->copyConfig($this->container
->get('config.storage'), $this->container
->get('config.storage.sync'));
}
public function testConfOverride() {
$expected_original_data = [
'foo' => 'bar',
'baz' => NULL,
'404' => 'herp',
];
$overrides['config_test.system']['foo'] = 'overridden';
$overrides['config_test.system']['baz'] = 'injected';
$overrides['config_test.system']['404'] = 'derp';
$GLOBALS['config'] = $overrides;
$this
->installConfig([
'config_test',
]);
$active = $this->container
->get('config.storage');
$data = $active
->read('config_test.system');
$this
->assertSame($expected_original_data['foo'], $data['foo']);
$this
->assertFalse(isset($data['baz']));
$this
->assertSame($expected_original_data['404'], $data['404']);
$config = \Drupal::configFactory()
->get('config_test.system');
$this
->assertSame($overrides['config_test.system']['foo'], $config
->get('foo'));
$this
->assertSame($overrides['config_test.system']['baz'], $config
->get('baz'));
$this
->assertSame($overrides['config_test.system']['404'], $config
->get('404'));
$config = \Drupal::configFactory()
->getEditable('config_test.system');
$this
->assertSame($expected_original_data['foo'], $config
->get('foo'));
$this
->assertNull($config
->get('baz'));
$this
->assertSame($expected_original_data['404'], $config
->get('404'));
$expected_original_data['baz'] = 'original baz';
$config
->set('baz', $expected_original_data['baz']);
$expected_original_data['404'] = 'original 404';
$config
->set('404', $expected_original_data['404']);
$config
->save();
$config = \Drupal::config('config_test.system');
$this
->assertSame($overrides['config_test.system']['foo'], $config
->get('foo'));
$this
->assertSame($overrides['config_test.system']['baz'], $config
->get('baz'));
$this
->assertSame($overrides['config_test.system']['404'], $config
->get('404'));
$this
->assertSame($expected_original_data['foo'], $config
->getOriginal('foo', FALSE));
$this
->assertSame($expected_original_data['baz'], $config
->getOriginal('baz', FALSE));
$this
->assertSame($expected_original_data['404'], $config
->getOriginal('404', FALSE));
$sync = $this->container
->get('config.storage.sync');
$expected_new_data = [
'foo' => 'barbar',
'404' => 'herpderp',
];
$sync
->write('config_test.system', $expected_new_data);
$this
->configImporter()
->import();
$data = $active
->read('config_test.system');
$this
->assertSame($expected_new_data['foo'], $data['foo']);
$this
->assertFalse(isset($data['baz']));
$this
->assertSame($expected_new_data['404'], $data['404']);
$config = \Drupal::config('config_test.system');
$this
->assertSame($overrides['config_test.system']['foo'], $config
->get('foo'));
$this
->assertSame($overrides['config_test.system']['baz'], $config
->get('baz'));
$this
->assertSame($overrides['config_test.system']['404'], $config
->get('404'));
$GLOBALS['config']['config_test.new']['key'] = 'override';
$config = \Drupal::config('config_test.new');
$this
->assertTrue($config
->isNew(), 'The configuration object config_test.new is new');
$this
->assertSame('override', $config
->get('key'));
$config_raw = \Drupal::configFactory()
->getEditable('config_test.new');
$this
->assertNull($config_raw
->get('key'));
$config_raw
->set('key', 'raw')
->set('new_key', 'new_value')
->save();
$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']);
}
}