3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\KernelTests\KernelTestBase;
8 * Tests configuration overrides via $config in settings.php.
12 class ConfigOverrideTest extends KernelTestBase {
19 public static $modules = ['system', 'config_test'];
21 protected function setUp() {
23 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
27 * Tests configuration override.
29 public function testConfOverride() {
30 $expected_original_data = [
36 // Set globals before installing to prove that the installed file does not
37 // contain these values.
38 $overrides['config_test.system']['foo'] = 'overridden';
39 $overrides['config_test.system']['baz'] = 'injected';
40 $overrides['config_test.system']['404'] = 'derp';
41 $GLOBALS['config'] = $overrides;
43 $this->installConfig(['config_test']);
45 // Verify that the original configuration data exists. Have to read storage
46 // directly otherwise overrides will apply.
47 $active = $this->container->get('config.storage');
48 $data = $active->read('config_test.system');
49 $this->assertIdentical($data['foo'], $expected_original_data['foo']);
50 $this->assertFalse(isset($data['baz']));
51 $this->assertIdentical($data['404'], $expected_original_data['404']);
53 // Get the configuration object with overrides.
54 $config = \Drupal::configFactory()->get('config_test.system');
56 // Verify that it contains the overridden data from $config.
57 $this->assertIdentical($config->get('foo'), $overrides['config_test.system']['foo']);
58 $this->assertIdentical($config->get('baz'), $overrides['config_test.system']['baz']);
59 $this->assertIdentical($config->get('404'), $overrides['config_test.system']['404']);
61 // Get the configuration object which does not have overrides.
62 $config = \Drupal::configFactory()->getEditable('config_test.system');
64 // Verify that it does not contains the overridden data from $config.
65 $this->assertIdentical($config->get('foo'), $expected_original_data['foo']);
66 $this->assertIdentical($config->get('baz'), NULL);
67 $this->assertIdentical($config->get('404'), $expected_original_data['404']);
69 // Set the value for 'baz' (on the original data).
70 $expected_original_data['baz'] = 'original baz';
71 $config->set('baz', $expected_original_data['baz']);
73 // Set the value for '404' (on the original data).
74 $expected_original_data['404'] = 'original 404';
75 $config->set('404', $expected_original_data['404']);
77 // Save the configuration object (having overrides applied).
80 // Reload it and verify that it still contains overridden data from $config.
81 $config = \Drupal::config('config_test.system');
82 $this->assertIdentical($config->get('foo'), $overrides['config_test.system']['foo']);
83 $this->assertIdentical($config->get('baz'), $overrides['config_test.system']['baz']);
84 $this->assertIdentical($config->get('404'), $overrides['config_test.system']['404']);
86 // Verify that raw config data has changed.
87 $this->assertIdentical($config->getOriginal('foo', FALSE), $expected_original_data['foo']);
88 $this->assertIdentical($config->getOriginal('baz', FALSE), $expected_original_data['baz']);
89 $this->assertIdentical($config->getOriginal('404', FALSE), $expected_original_data['404']);
91 // Write file to sync.
92 $sync = $this->container->get('config.storage.sync');
93 $expected_new_data = [
97 $sync->write('config_test.system', $expected_new_data);
99 // Import changed data from sync to active.
100 $this->configImporter()->import();
101 $data = $active->read('config_test.system');
103 // Verify that the new configuration data exists. Have to read storage
104 // directly otherwise overrides will apply.
105 $this->assertIdentical($data['foo'], $expected_new_data['foo']);
106 $this->assertFalse(isset($data['baz']));
107 $this->assertIdentical($data['404'], $expected_new_data['404']);
109 // Verify that the overrides are still working.
110 $config = \Drupal::config('config_test.system');
111 $this->assertIdentical($config->get('foo'), $overrides['config_test.system']['foo']);
112 $this->assertIdentical($config->get('baz'), $overrides['config_test.system']['baz']);
113 $this->assertIdentical($config->get('404'), $overrides['config_test.system']['404']);
115 // Test overrides of completely new configuration objects. In normal runtime
116 // this should only happen for configuration entities as we should not be
117 // creating simple configuration objects on the fly.
118 $GLOBALS['config']['config_test.new']['key'] = 'override';
119 $config = \Drupal::config('config_test.new');
120 $this->assertTrue($config->isNew(), 'The configuration object config_test.new is new');
121 $this->assertIdentical($config->get('key'), 'override');
122 $config_raw = \Drupal::configFactory()->getEditable('config_test.new');
123 $this->assertIdentical($config_raw->get('key'), NULL);
126 ->set('new_key', 'new_value')
128 // Ensure override is preserved but all other data has been updated
130 $config = \Drupal::config('config_test.new');
131 $this->assertFalse($config->isNew(), 'The configuration object config_test.new is not new');
132 $this->assertIdentical($config->get('key'), 'override');
133 $this->assertIdentical($config->get('new_key'), 'new_value');
134 $raw_data = $config->getRawData();
135 $this->assertIdentical($raw_data['key'], 'raw');