3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Core\Config\ConfigNameException;
8 use Drupal\Core\Config\ConfigValueException;
9 use Drupal\Core\Config\InstallStorage;
10 use Drupal\Core\Config\DatabaseStorage;
11 use Drupal\Core\Config\UnsupportedDataTypeConfigException;
12 use Drupal\KernelTests\KernelTestBase;
15 * Tests CRUD operations on configuration objects.
19 class ConfigCRUDTest extends KernelTestBase {
22 * Exempt from strict schema checking.
24 * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
28 protected $strictConfigSchema = FALSE;
35 public static $modules = ['system'];
38 * Tests CRUD operations.
40 public function testCRUD() {
41 $storage = $this->container->get('config.storage');
42 $config_factory = $this->container->get('config.factory');
43 $name = 'config_test.crud';
45 $config = $this->config($name);
46 $this->assertIdentical($config->isNew(), TRUE);
48 // Create a new configuration object.
49 $config->set('value', 'initial');
51 $this->assertIdentical($config->isNew(), FALSE);
53 // Verify the active configuration contains the saved value.
54 $actual_data = $storage->read($name);
55 $this->assertIdentical($actual_data, ['value' => 'initial']);
57 // Update the configuration object instance.
58 $config->set('value', 'instance-update');
60 $this->assertIdentical($config->isNew(), FALSE);
62 // Verify the active configuration contains the updated value.
63 $actual_data = $storage->read($name);
64 $this->assertIdentical($actual_data, ['value' => 'instance-update']);
66 // Verify a call to $this->config() immediately returns the updated value.
67 $new_config = $this->config($name);
68 $this->assertIdentical($new_config->get(), $config->get());
69 $this->assertIdentical($config->isNew(), FALSE);
71 // Pollute the config factory static cache.
72 $config_factory->getEditable($name);
74 // Delete the configuration object.
77 // Verify the configuration object is empty.
78 $this->assertIdentical($config->get(), []);
79 $this->assertIdentical($config->isNew(), TRUE);
81 // Verify that all copies of the configuration has been removed from the
83 $this->assertIdentical($config_factory->getEditable($name)->isNew(), TRUE);
85 // Verify the active configuration contains no value.
86 $actual_data = $storage->read($name);
87 $this->assertIdentical($actual_data, FALSE);
89 // Verify $this->config() returns no data.
90 $new_config = $this->config($name);
91 $this->assertIdentical($new_config->get(), $config->get());
92 $this->assertIdentical($config->isNew(), TRUE);
94 // Re-create the configuration object.
95 $config->set('value', 're-created');
97 $this->assertIdentical($config->isNew(), FALSE);
99 // Verify the active configuration contains the updated value.
100 $actual_data = $storage->read($name);
101 $this->assertIdentical($actual_data, ['value' => 're-created']);
103 // Verify a call to $this->config() immediately returns the updated value.
104 $new_config = $this->config($name);
105 $this->assertIdentical($new_config->get(), $config->get());
106 $this->assertIdentical($config->isNew(), FALSE);
108 // Rename the configuration object.
109 $new_name = 'config_test.crud_rename';
110 $this->container->get('config.factory')->rename($name, $new_name);
111 $renamed_config = $this->config($new_name);
112 $this->assertIdentical($renamed_config->get(), $config->get());
113 $this->assertIdentical($renamed_config->isNew(), FALSE);
115 // Ensure that the old configuration object is removed from both the cache
116 // and the configuration storage.
117 $config = $this->config($name);
118 $this->assertIdentical($config->get(), []);
119 $this->assertIdentical($config->isNew(), TRUE);
121 // Test renaming when config.factory does not have the object in its static
123 $name = 'config_test.crud_rename';
124 // Pollute the non-overrides static cache.
125 $config_factory->getEditable($name);
126 // Pollute the overrides static cache.
127 $config = $config_factory->get($name);
128 // Rename and ensure that happened properly.
129 $new_name = 'config_test.crud_rename_no_cache';
130 $config_factory->rename($name, $new_name);
131 $renamed_config = $config_factory->get($new_name);
132 $this->assertIdentical($renamed_config->get(), $config->get());
133 $this->assertIdentical($renamed_config->isNew(), FALSE);
134 // Ensure the overrides static cache has been cleared.
135 $this->assertIdentical($config_factory->get($name)->isNew(), TRUE);
136 // Ensure the non-overrides static cache has been cleared.
137 $this->assertIdentical($config_factory->getEditable($name)->isNew(), TRUE);
139 // Merge data into the configuration object.
140 $new_config = $this->config($new_name);
145 $new_config->merge($expected_values);
147 $this->assertIdentical($new_config->get('value'), $expected_values['value']);
148 $this->assertIdentical($new_config->get('404'), $expected_values['404']);
150 // Test that getMultiple() does not return new config objects that were
151 // previously accessed with get()
152 $new_config = $config_factory->get('non_existing_key');
153 $this->assertTrue($new_config->isNew());
154 $this->assertEqual(0, count($config_factory->loadMultiple(['non_existing_key'])), 'loadMultiple() does not return new objects');
158 * Tests the validation of configuration object names.
160 public function testNameValidation() {
161 // Verify that an object name without namespace causes an exception.
162 $name = 'nonamespace';
163 $message = 'Expected ConfigNameException was thrown for a name without a namespace.';
165 $this->config($name)->save();
166 $this->fail($message);
168 catch (ConfigNameException $e) {
169 $this->pass($message);
172 // Verify that a name longer than the maximum length causes an exception.
173 $name = 'config_test.herman_melville.moby_dick_or_the_whale.harper_1851.now_small_fowls_flew_screaming_over_the_yet_yawning_gulf_a_sullen_white_surf_beat_against_its_steep_sides_then_all_collapsed_and_the_great_shroud_of_the_sea_rolled_on_as_it_rolled_five_thousand_years_ago';
174 $message = 'Expected ConfigNameException was thrown for a name longer than Config::MAX_NAME_LENGTH.';
176 $this->config($name)->save();
177 $this->fail($message);
179 catch (ConfigNameException $e) {
180 $this->pass($message);
183 // Verify that disallowed characters in the name cause an exception.
184 $characters = $test_characters = [':', '?', '*', '<', '>', '"', '\'', '/', '\\'];
185 foreach ($test_characters as $i => $c) {
187 $name = 'namespace.object' . $c;
188 $config = $this->config($name);
191 catch (ConfigNameException $e) {
192 unset($test_characters[$i]);
195 $this->assertTrue(empty($test_characters), format_string('Expected ConfigNameException was thrown for all invalid name characters: @characters', [
196 '@characters' => implode(' ', $characters),
199 // Verify that a valid config object name can be saved.
200 $name = 'namespace.object';
201 $message = 'ConfigNameException was not thrown for a valid object name.';
203 $config = $this->config($name);
205 $this->pass($message);
207 catch (ConfigNameException $e) {
208 $this->fail($message);
214 * Tests the validation of configuration object values.
216 public function testValueValidation() {
217 // Verify that setData() will catch dotted keys.
218 $message = 'Expected ConfigValueException was thrown from setData() for value with dotted keys.';
220 $this->config('namespace.object')->setData(['key.value' => 12])->save();
221 $this->fail($message);
223 catch (ConfigValueException $e) {
224 $this->pass($message);
227 // Verify that set() will catch dotted keys.
228 $message = 'Expected ConfigValueException was thrown from set() for value with dotted keys.';
230 $this->config('namespace.object')->set('foo', ['key.value' => 12])->save();
231 $this->fail($message);
233 catch (ConfigValueException $e) {
234 $this->pass($message);
239 * Tests data type handling.
241 public function testDataTypes() {
242 \Drupal::service('module_installer')->install(['config_test']);
243 $storage = new DatabaseStorage($this->container->get('database'), 'config');
244 $name = 'config_test.types';
245 $config = $this->config($name);
246 $original_content = file_get_contents(drupal_get_path('module', 'config_test') . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY . "/$name.yml");
247 $this->verbose('<pre>' . $original_content . "\n" . var_export($storage->read($name), TRUE));
249 // Verify variable data types are intact.
255 'float_as_integer' => (float) 1,
259 'string' => 'string',
262 $data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($data));
263 $this->assertIdentical($config->get(), $data);
265 // Re-set each key using Config::set().
266 foreach ($data as $key => $value) {
267 $config->set($key, $value);
270 $this->assertIdentical($config->get(), $data);
271 // Assert the data against the file storage.
272 $this->assertIdentical($storage->read($name), $data);
273 $this->verbose('<pre>' . $name . var_export($storage->read($name), TRUE));
275 // Set data using config::setData().
276 $config->setData($data)->save();
277 $this->assertIdentical($config->get(), $data);
278 $this->assertIdentical($storage->read($name), $data);
280 // Test that schema type enforcement can be overridden by trusting the data.
281 $this->assertSame(99, $config->get('int'));
282 $config->set('int', '99')->save(TRUE);
283 $this->assertSame('99', $config->get('int'));
284 // Test that re-saving without testing the data enforces the schema type.
286 $this->assertSame($data, $config->get());
288 // Test that setting an unsupported type for a config object with a schema
291 $config->set('stream', fopen(__FILE__, 'r'))->save();
292 $this->fail('No Exception thrown upon saving invalid data type.');
294 catch (UnsupportedDataTypeConfigException $e) {
295 $this->pass(new FormattableMarkup('%class thrown upon saving invalid data type.', [
296 '%class' => get_class($e),
300 // Test that setting an unsupported type for a config object with no schema
302 $typed_config_manager = $this->container->get('config.typed');
303 $config_name = 'config_test.no_schema';
304 $config = $this->config($config_name);
305 $this->assertFalse($typed_config_manager->hasConfigSchema($config_name));
308 $config->set('stream', fopen(__FILE__, 'r'))->save();
309 $this->fail('No Exception thrown upon saving invalid data type.');
311 catch (UnsupportedDataTypeConfigException $e) {
312 $this->pass(new FormattableMarkup('%class thrown upon saving invalid data type.', [
313 '%class' => get_class($e),