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->installConfig(['system']);
24 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
28 * Tests configuration override.
30 public function testConfOverride() {
31 $expected_original_data = [
37 // Set globals before installing to prove that the installed file does not
38 // contain these values.
39 $overrides['config_test.system']['foo'] = 'overridden';
40 $overrides['config_test.system']['baz'] = 'injected';
41 $overrides['config_test.system']['404'] = 'derp';
42 $GLOBALS['config'] = $overrides;
44 $this->installConfig(['config_test']);
46 // Verify that the original configuration data exists. Have to read storage
47 // directly otherwise overrides will apply.
48 $active = $this->container->get('config.storage');
49 $data = $active->read('config_test.system');
50 $this->assertIdentical($data['foo'], $expected_original_data['foo']);
51 $this->assertFalse(isset($data['baz']));
52 $this->assertIdentical($data['404'], $expected_original_data['404']);
54 // Get the configuration object with overrides.
55 $config = \Drupal::configFactory()->get('config_test.system');
57 // Verify that it contains the overridden data from $config.
58 $this->assertIdentical($config->get('foo'), $overrides['config_test.system']['foo']);
59 $this->assertIdentical($config->get('baz'), $overrides['config_test.system']['baz']);
60 $this->assertIdentical($config->get('404'), $overrides['config_test.system']['404']);
62 // Get the configuration object which does not have overrides.
63 $config = \Drupal::configFactory()->getEditable('config_test.system');
65 // Verify that it does not contains the overridden data from $config.
66 $this->assertIdentical($config->get('foo'), $expected_original_data['foo']);
67 $this->assertIdentical($config->get('baz'), NULL);
68 $this->assertIdentical($config->get('404'), $expected_original_data['404']);
70 // Set the value for 'baz' (on the original data).
71 $expected_original_data['baz'] = 'original baz';
72 $config->set('baz', $expected_original_data['baz']);
74 // Set the value for '404' (on the original data).
75 $expected_original_data['404'] = 'original 404';
76 $config->set('404', $expected_original_data['404']);
78 // Save the configuration object (having overrides applied).
81 // Reload it and verify that it still contains overridden data from $config.
82 $config = \Drupal::config('config_test.system');
83 $this->assertIdentical($config->get('foo'), $overrides['config_test.system']['foo']);
84 $this->assertIdentical($config->get('baz'), $overrides['config_test.system']['baz']);
85 $this->assertIdentical($config->get('404'), $overrides['config_test.system']['404']);
87 // Verify that raw config data has changed.
88 $this->assertIdentical($config->getOriginal('foo', FALSE), $expected_original_data['foo']);
89 $this->assertIdentical($config->getOriginal('baz', FALSE), $expected_original_data['baz']);
90 $this->assertIdentical($config->getOriginal('404', FALSE), $expected_original_data['404']);
92 // Write file to sync.
93 $sync = $this->container->get('config.storage.sync');
94 $expected_new_data = [
98 $sync->write('config_test.system', $expected_new_data);
100 // Import changed data from sync to active.
101 $this->configImporter()->import();
102 $data = $active->read('config_test.system');
104 // Verify that the new configuration data exists. Have to read storage
105 // directly otherwise overrides will apply.
106 $this->assertIdentical($data['foo'], $expected_new_data['foo']);
107 $this->assertFalse(isset($data['baz']));
108 $this->assertIdentical($data['404'], $expected_new_data['404']);
110 // Verify that the overrides are still working.
111 $config = \Drupal::config('config_test.system');
112 $this->assertIdentical($config->get('foo'), $overrides['config_test.system']['foo']);
113 $this->assertIdentical($config->get('baz'), $overrides['config_test.system']['baz']);
114 $this->assertIdentical($config->get('404'), $overrides['config_test.system']['404']);
116 // Test overrides of completely new configuration objects. In normal runtime
117 // this should only happen for configuration entities as we should not be
118 // creating simple configuration objects on the fly.
119 $GLOBALS['config']['config_test.new']['key'] = 'override';
120 $config = \Drupal::config('config_test.new');
121 $this->assertTrue($config->isNew(), 'The configuration object config_test.new is new');
122 $this->assertIdentical($config->get('key'), 'override');
123 $config_raw = \Drupal::configFactory()->getEditable('config_test.new');
124 $this->assertIdentical($config_raw->get('key'), NULL);
127 ->set('new_key', 'new_value')
129 // Ensure override is preserved but all other data has been updated
131 $config = \Drupal::config('config_test.new');
132 $this->assertFalse($config->isNew(), 'The configuration object config_test.new is not new');
133 $this->assertIdentical($config->get('key'), 'override');
134 $this->assertIdentical($config->get('new_key'), 'new_value');
135 $raw_data = $config->getRawData();
136 $this->assertIdentical($raw_data['key'], 'raw');