3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\Core\Config\FileStorage;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests reading and writing of configuration files.
13 class ConfigFileContentTest extends KernelTestBase {
16 * Exempt from strict schema checking.
18 * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
22 protected $strictConfigSchema = FALSE;
25 * Tests setting, writing, and reading of a configuration setting.
27 public function testReadWriteConfig() {
28 $storage = $this->container->get('config.storage');
33 $nested_key = 'biff.bang';
34 $nested_value = 'pow';
42 $casting_array_key = 'casting_array';
43 $casting_array_false_value_key = 'casting_array.cast.false';
44 $casting_array_value = [
49 $nested_array_key = 'nested.array';
53 // Attempt to read non-existing configuration.
54 $config = $this->config($name);
56 // Verify a configuration object is returned.
57 $this->assertEqual($config->getName(), $name);
58 $this->assertTrue($config, 'Config object created.');
60 // Verify the configuration object is empty.
61 $this->assertEqual($config->get(), [], 'New config object is empty.');
63 // Verify nothing was saved.
64 $data = $storage->read($name);
65 $this->assertIdentical($data, FALSE);
67 // Add a top level value.
68 $config = $this->config($name);
69 $config->set($key, $value);
71 // Add a nested value.
72 $config->set($nested_key, $nested_value);
75 $config->set($array_key, $array_value);
77 // Add a nested array.
78 $config->set($nested_array_key, $array_value);
80 // Add a boolean false value. Should get cast to 0.
81 $config->set($false_key, FALSE);
83 // Add a boolean true value. Should get cast to 1.
84 $config->set($true_key, TRUE);
86 // Add a null value. Should get cast to an empty string.
87 $config->set('null', NULL);
89 // Add an array with a nested boolean false that should get cast to 0.
90 $config->set($casting_array_key, $casting_array_value);
93 // Verify the database entry exists.
94 $data = $storage->read($name);
95 $this->assertTrue($data);
97 // Read top level value.
98 $config = $this->config($name);
99 $this->assertEqual($config->getName(), $name);
100 $this->assertTrue($config, 'Config object created.');
101 $this->assertEqual($config->get($key), 'bar', 'Top level configuration value found.');
103 // Read nested value.
104 $this->assertEqual($config->get($nested_key), $nested_value, 'Nested configuration value found.');
107 $this->assertEqual($config->get($array_key), $array_value, 'Top level array configuration value found.');
109 // Read nested array.
110 $this->assertEqual($config->get($nested_array_key), $array_value, 'Nested array configuration value found.');
112 // Read a top level value that doesn't exist.
113 $this->assertNull($config->get('i_dont_exist'), 'Non-existent top level value returned NULL.');
115 // Read a nested value that doesn't exist.
116 $this->assertNull($config->get('i.dont.exist'), 'Non-existent nested value returned NULL.');
119 $this->assertFalse($config->get($false_key), "Boolean FALSE value returned the FALSE.");
122 $this->assertTrue($config->get($true_key), "Boolean TRUE value returned the TRUE.");
125 $this->assertIdentical($config->get('null'), NULL);
127 // Read false that had been nested in an array value.
128 $this->assertSame($config->get($casting_array_false_value_key), FALSE, "Nested boolean FALSE value returned FALSE.");
130 // Unset a top level value.
131 $config->clear($key);
133 // Unset a nested value.
134 $config->clear($nested_key);
136 $config = $this->config($name);
138 // Read unset top level value.
139 $this->assertNull($config->get($key), 'Top level value unset.');
141 // Read unset nested value.
142 $this->assertNull($config->get($nested_key), 'Nested value unset.');
144 // Create two new configuration files to test listing.
145 $config = $this->config('foo.baz');
146 $config->set($key, $value);
149 // Test chained set()->save().
150 $chained_name = 'biff.bang';
151 $config = $this->config($chained_name);
152 $config->set($key, $value)->save();
154 // Verify the database entry exists from a chained save.
155 $data = $storage->read($chained_name);
156 $this->assertEqual($data, $config->get());
158 // Get file listing for all files starting with 'foo'. Should return
160 $files = $storage->listAll('foo');
161 $this->assertEqual(count($files), 2, 'Two files listed with the prefix \'foo\'.');
163 // Get file listing for all files starting with 'biff'. Should return
165 $files = $storage->listAll('biff');
166 $this->assertEqual(count($files), 1, 'One file listed with the prefix \'biff\'.');
168 // Get file listing for all files starting with 'foo.bar'. Should return
170 $files = $storage->listAll('foo.bar');
171 $this->assertEqual(count($files), 1, 'One file listed with the prefix \'foo.bar\'.');
173 // Get file listing for all files starting with 'bar'. Should return
175 $files = $storage->listAll('bar');
176 $this->assertEqual($files, [], 'No files listed with the prefix \'bar\'.');
178 // Delete the configuration.
179 $config = $this->config($name);
182 // Verify the database entry no longer exists.
183 $data = $storage->read($name);
184 $this->assertIdentical($data, FALSE);
188 * Tests serialization of configuration to file.
190 public function testSerialization() {
191 $name = $this->randomMachineName(10) . '.' . $this->randomMachineName(10);
193 // Indexed arrays; the order of elements is essential.
194 'numeric keys' => ['i', 'n', 'd', 'e', 'x', 'e', 'd'],
195 // Infinitely nested keys using arbitrary element names.
197 // HTML/XML in values.
198 'HTML' => '<strong> <bold> <em> <blockquote>',
200 'UTF-8' => 'FrançAIS is ÜBER-åwesome',
201 // Unicode in keys and values.
202 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ' => 'αβγδεζηθικλμνξοσὠ',
204 'invalid xml' => '</title><script type="text/javascript">alert("Title XSS!");</script> & < > " \' ',
207 // Encode and write, and reload and decode the configuration data.
208 $filestorage = new FileStorage(config_get_config_directory(CONFIG_SYNC_DIRECTORY));
209 $filestorage->write($name, $config_data);
210 $config_parsed = $filestorage->read($name);
212 $key = 'numeric keys';
213 $this->assertIdentical($config_data[$key], $config_parsed[$key]);
215 $key = 'nested keys';
216 $this->assertIdentical($config_data[$key], $config_parsed[$key]);
219 $this->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
222 $this->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
224 $key = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
225 $this->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
227 $key = 'invalid xml';
228 $this->assertIdentical($config_data[$key], $config_parsed[$key]);