Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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->installConfig(['system']);
24     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
25   }
26
27   /**
28    * Tests configuration override.
29    */
30   public function testConfOverride() {
31     $expected_original_data = [
32       'foo' => 'bar',
33       'baz' => NULL,
34       '404' => 'herp',
35     ];
36
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;
43
44     $this->installConfig(['config_test']);
45
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']);
53
54     // Get the configuration object with overrides.
55     $config = \Drupal::configFactory()->get('config_test.system');
56
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']);
61
62     // Get the configuration object which does not have overrides.
63     $config = \Drupal::configFactory()->getEditable('config_test.system');
64
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']);
69
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']);
73
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']);
77
78     // Save the configuration object (having overrides applied).
79     $config->save();
80
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']);
86
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']);
91
92     // Write file to sync.
93     $sync = $this->container->get('config.storage.sync');
94     $expected_new_data = [
95       'foo' => 'barbar',
96       '404' => 'herpderp',
97     ];
98     $sync->write('config_test.system', $expected_new_data);
99
100     // Import changed data from sync to active.
101     $this->configImporter()->import();
102     $data = $active->read('config_test.system');
103
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']);
109
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']);
115
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);
125     $config_raw
126       ->set('key', 'raw')
127       ->set('new_key', 'new_value')
128       ->save();
129     // Ensure override is preserved but all other data has been updated
130     // accordingly.
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');
137   }
138
139 }