53666292114a4d87e8d329b390147c3aec8aaa11
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigFileContentTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
5 use Drupal\Core\Config\FileStorage;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests reading and writing of configuration files.
10  *
11  * @group config
12  */
13 class ConfigFileContentTest extends KernelTestBase {
14
15   /**
16    * Exempt from strict schema checking.
17    *
18    * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
19    *
20    * @var bool
21    */
22   protected $strictConfigSchema = FALSE;
23
24   /**
25    * Tests setting, writing, and reading of a configuration setting.
26    */
27   public function testReadWriteConfig() {
28     $storage = $this->container->get('config.storage');
29
30     $name = 'foo.bar';
31     $key = 'foo';
32     $value = 'bar';
33     $nested_key = 'biff.bang';
34     $nested_value = 'pow';
35     $array_key = 'array';
36     $array_value = [
37       'foo' => 'bar',
38       'biff' => [
39         'bang' => 'pow',
40       ],
41     ];
42     $casting_array_key = 'casting_array';
43     $casting_array_false_value_key = 'casting_array.cast.false';
44     $casting_array_value = [
45       'cast' => [
46         'false' => FALSE,
47       ],
48     ];
49     $nested_array_key = 'nested.array';
50     $true_key = 'true';
51     $false_key = 'false';
52
53     // Attempt to read non-existing configuration.
54     $config = $this->config($name);
55
56     // Verify a configuration object is returned.
57     $this->assertEqual($config->getName(), $name);
58     $this->assertTrue($config, 'Config object created.');
59
60     // Verify the configuration object is empty.
61     $this->assertEqual($config->get(), [], 'New config object is empty.');
62
63     // Verify nothing was saved.
64     $data = $storage->read($name);
65     $this->assertIdentical($data, FALSE);
66
67     // Add a top level value.
68     $config = $this->config($name);
69     $config->set($key, $value);
70
71     // Add a nested value.
72     $config->set($nested_key, $nested_value);
73
74     // Add an array.
75     $config->set($array_key, $array_value);
76
77     // Add a nested array.
78     $config->set($nested_array_key, $array_value);
79
80     // Add a boolean false value. Should get cast to 0.
81     $config->set($false_key, FALSE);
82
83     // Add a boolean true value. Should get cast to 1.
84     $config->set($true_key, TRUE);
85
86     // Add a null value. Should get cast to an empty string.
87     $config->set('null', NULL);
88
89     // Add an array with a nested boolean false that should get cast to 0.
90     $config->set($casting_array_key, $casting_array_value);
91     $config->save();
92
93     // Verify the database entry exists.
94     $data = $storage->read($name);
95     $this->assertTrue($data);
96
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.');
102
103     // Read nested value.
104     $this->assertEqual($config->get($nested_key), $nested_value, 'Nested configuration value found.');
105
106     // Read array.
107     $this->assertEqual($config->get($array_key), $array_value, 'Top level array configuration value found.');
108
109     // Read nested array.
110     $this->assertEqual($config->get($nested_array_key), $array_value, 'Nested array configuration value found.');
111
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.');
114
115     // Read a nested value that doesn't exist.
116     $this->assertNull($config->get('i.dont.exist'), 'Non-existent nested value returned NULL.');
117
118     // Read false value.
119     $this->assertFalse($config->get($false_key), "Boolean FALSE value returned the FALSE.");
120
121     // Read true value.
122     $this->assertTrue($config->get($true_key), "Boolean TRUE value returned the TRUE.");
123
124     // Read null value.
125     $this->assertIdentical($config->get('null'), NULL);
126
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.");
129
130     // Unset a top level value.
131     $config->clear($key);
132
133     // Unset a nested value.
134     $config->clear($nested_key);
135     $config->save();
136     $config = $this->config($name);
137
138     // Read unset top level value.
139     $this->assertNull($config->get($key), 'Top level value unset.');
140
141     // Read unset nested value.
142     $this->assertNull($config->get($nested_key), 'Nested value unset.');
143
144     // Create two new configuration files to test listing.
145     $config = $this->config('foo.baz');
146     $config->set($key, $value);
147     $config->save();
148
149     // Test chained set()->save().
150     $chained_name = 'biff.bang';
151     $config = $this->config($chained_name);
152     $config->set($key, $value)->save();
153
154     // Verify the database entry exists from a chained save.
155     $data = $storage->read($chained_name);
156     $this->assertEqual($data, $config->get());
157
158     // Get file listing for all files starting with 'foo'. Should return
159     // two elements.
160     $files = $storage->listAll('foo');
161     $this->assertEqual(count($files), 2, 'Two files listed with the prefix \'foo\'.');
162
163     // Get file listing for all files starting with 'biff'. Should return
164     // one element.
165     $files = $storage->listAll('biff');
166     $this->assertEqual(count($files), 1, 'One file listed with the prefix \'biff\'.');
167
168     // Get file listing for all files starting with 'foo.bar'. Should return
169     // one element.
170     $files = $storage->listAll('foo.bar');
171     $this->assertEqual(count($files), 1, 'One file listed with the prefix \'foo.bar\'.');
172
173     // Get file listing for all files starting with 'bar'. Should return
174     // an empty array.
175     $files = $storage->listAll('bar');
176     $this->assertEqual($files, [], 'No files listed with the prefix \'bar\'.');
177
178     // Delete the configuration.
179     $config = $this->config($name);
180     $config->delete();
181
182     // Verify the database entry no longer exists.
183     $data = $storage->read($name);
184     $this->assertIdentical($data, FALSE);
185   }
186
187   /**
188    * Tests serialization of configuration to file.
189    */
190   public function testSerialization() {
191     $name = $this->randomMachineName(10) . '.' . $this->randomMachineName(10);
192     $config_data = [
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.
196       'nested keys' => [
197         // HTML/XML in values.
198         'HTML' => '<strong> <bold> <em> <blockquote>',
199         // UTF-8 in values.
200         'UTF-8' => 'FrançAIS is ÜBER-åwesome',
201         // Unicode in keys and values.
202         'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ' => 'αβγδεζηθικλμνξοσὠ',
203       ],
204       'invalid xml' => '</title><script type="text/javascript">alert("Title XSS!");</script> & < > " \' ',
205     ];
206
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);
211
212     $key = 'numeric keys';
213     $this->assertIdentical($config_data[$key], $config_parsed[$key]);
214
215     $key = 'nested keys';
216     $this->assertIdentical($config_data[$key], $config_parsed[$key]);
217
218     $key = 'HTML';
219     $this->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
220
221     $key = 'UTF-8';
222     $this->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
223
224     $key = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
225     $this->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
226
227     $key = 'invalid xml';
228     $this->assertIdentical($config_data[$key], $config_parsed[$key]);
229   }
230
231 }