Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigOverrideTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Tests configuration overrides via $config in settings.php.
9  *
10  * @group config
11  */
12 class ConfigOverrideTest extends KernelTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['system', 'config_test'];
20
21   protected function setUp() {
22     parent::setUp();
23     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
24   }
25
26   /**
27    * Tests configuration override.
28    */
29   public function testConfOverride() {
30     $expected_original_data = [
31       'foo' => 'bar',
32       'baz' => NULL,
33       '404' => 'herp',
34     ];
35
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;
42
43     $this->installConfig(['config_test']);
44
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']);
52
53     // Get the configuration object with overrides.
54     $config = \Drupal::configFactory()->get('config_test.system');
55
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']);
60
61     // Get the configuration object which does not have overrides.
62     $config = \Drupal::configFactory()->getEditable('config_test.system');
63
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']);
68
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']);
72
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']);
76
77     // Save the configuration object (having overrides applied).
78     $config->save();
79
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']);
85
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']);
90
91     // Write file to sync.
92     $sync = $this->container->get('config.storage.sync');
93     $expected_new_data = [
94       'foo' => 'barbar',
95       '404' => 'herpderp',
96     ];
97     $sync->write('config_test.system', $expected_new_data);
98
99     // Import changed data from sync to active.
100     $this->configImporter()->import();
101     $data = $active->read('config_test.system');
102
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']);
108
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']);
114
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);
124     $config_raw
125       ->set('key', 'raw')
126       ->set('new_key', 'new_value')
127       ->save();
128     // Ensure override is preserved but all other data has been updated
129     // accordingly.
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');
136   }
137
138 }