99a94d182f47d2baf8b1f28d3346271a2c2869f1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigCRUDTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
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;
13
14 /**
15  * Tests CRUD operations on configuration objects.
16  *
17  * @group config
18  */
19 class ConfigCRUDTest extends KernelTestBase {
20
21   /**
22    * Exempt from strict schema checking.
23    *
24    * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
25    *
26    * @var bool
27    */
28   protected $strictConfigSchema = FALSE;
29
30   /**
31    * Modules to enable.
32    *
33    * @var array
34    */
35   public static $modules = ['system'];
36
37   /**
38    * Tests CRUD operations.
39    */
40   public function testCRUD() {
41     $storage = $this->container->get('config.storage');
42     $config_factory = $this->container->get('config.factory');
43     $name = 'config_test.crud';
44
45     $config = $this->config($name);
46     $this->assertIdentical($config->isNew(), TRUE);
47
48     // Create a new configuration object.
49     $config->set('value', 'initial');
50     $config->save();
51     $this->assertIdentical($config->isNew(), FALSE);
52
53     // Verify the active configuration contains the saved value.
54     $actual_data = $storage->read($name);
55     $this->assertIdentical($actual_data, ['value' => 'initial']);
56
57     // Update the configuration object instance.
58     $config->set('value', 'instance-update');
59     $config->save();
60     $this->assertIdentical($config->isNew(), FALSE);
61
62     // Verify the active configuration contains the updated value.
63     $actual_data = $storage->read($name);
64     $this->assertIdentical($actual_data, ['value' => 'instance-update']);
65
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);
70
71     // Pollute the config factory static cache.
72     $config_factory->getEditable($name);
73
74     // Delete the configuration object.
75     $config->delete();
76
77     // Verify the configuration object is empty.
78     $this->assertIdentical($config->get(), []);
79     $this->assertIdentical($config->isNew(), TRUE);
80
81     // Verify that all copies of the configuration has been removed from the
82     // static cache.
83     $this->assertIdentical($config_factory->getEditable($name)->isNew(), TRUE);
84
85     // Verify the active configuration contains no value.
86     $actual_data = $storage->read($name);
87     $this->assertIdentical($actual_data, FALSE);
88
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);
93
94     // Re-create the configuration object.
95     $config->set('value', 're-created');
96     $config->save();
97     $this->assertIdentical($config->isNew(), FALSE);
98
99     // Verify the active configuration contains the updated value.
100     $actual_data = $storage->read($name);
101     $this->assertIdentical($actual_data, ['value' => 're-created']);
102
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);
107
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);
114
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);
120
121     // Test renaming when config.factory does not have the object in its static
122     // cache.
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);
138
139     // Merge data into the configuration object.
140     $new_config = $this->config($new_name);
141     $expected_values = [
142       'value' => 'herp',
143       '404' => 'derp',
144     ];
145     $new_config->merge($expected_values);
146     $new_config->save();
147     $this->assertIdentical($new_config->get('value'), $expected_values['value']);
148     $this->assertIdentical($new_config->get('404'), $expected_values['404']);
149
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');
155   }
156
157   /**
158    * Tests the validation of configuration object names.
159    */
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.';
164     try {
165       $this->config($name)->save();
166       $this->fail($message);
167     }
168     catch (ConfigNameException $e) {
169       $this->pass($message);
170     }
171
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.';
175     try {
176       $this->config($name)->save();
177       $this->fail($message);
178     }
179     catch (ConfigNameException $e) {
180       $this->pass($message);
181     }
182
183     // Verify that disallowed characters in the name cause an exception.
184     $characters = $test_characters = [':', '?', '*', '<', '>', '"', '\'', '/', '\\'];
185     foreach ($test_characters as $i => $c) {
186       try {
187         $name = 'namespace.object' . $c;
188         $config = $this->config($name);
189         $config->save();
190       }
191       catch (ConfigNameException $e) {
192         unset($test_characters[$i]);
193       }
194     }
195     $this->assertTrue(empty($test_characters), format_string('Expected ConfigNameException was thrown for all invalid name characters: @characters', [
196       '@characters' => implode(' ', $characters),
197     ]));
198
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.';
202     try {
203       $config = $this->config($name);
204       $config->save();
205       $this->pass($message);
206     }
207     catch (ConfigNameException $e) {
208       $this->fail($message);
209     }
210
211   }
212
213   /**
214    * Tests the validation of configuration object values.
215    */
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.';
219     try {
220       $this->config('namespace.object')->setData(['key.value' => 12])->save();
221       $this->fail($message);
222     }
223     catch (ConfigValueException $e) {
224       $this->pass($message);
225     }
226
227     // Verify that set() will catch dotted keys.
228     $message = 'Expected ConfigValueException was thrown from set() for value with dotted keys.';
229     try {
230       $this->config('namespace.object')->set('foo', ['key.value' => 12])->save();
231       $this->fail($message);
232     }
233     catch (ConfigValueException $e) {
234       $this->pass($message);
235     }
236   }
237
238   /**
239    * Tests data type handling.
240    */
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));
248
249     // Verify variable data types are intact.
250     $data = [
251       'array' => [],
252       'boolean' => TRUE,
253       'exp' => 1.2e+34,
254       'float' => 3.14159,
255       'float_as_integer' => (float) 1,
256       'hex' => 0xC,
257       'int' => 99,
258       'octal' => 0775,
259       'string' => 'string',
260       'string_int' => '1',
261     ];
262     $data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($data));
263     $this->assertIdentical($config->get(), $data);
264
265     // Re-set each key using Config::set().
266     foreach ($data as $key => $value) {
267       $config->set($key, $value);
268     }
269     $config->save();
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));
274
275     // Set data using config::setData().
276     $config->setData($data)->save();
277     $this->assertIdentical($config->get(), $data);
278     $this->assertIdentical($storage->read($name), $data);
279
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.
285     $config->save();
286     $this->assertSame($data, $config->get());
287
288     // Test that setting an unsupported type for a config object with a schema
289     // fails.
290     try {
291       $config->set('stream', fopen(__FILE__, 'r'))->save();
292       $this->fail('No Exception thrown upon saving invalid data type.');
293     }
294     catch (UnsupportedDataTypeConfigException $e) {
295       $this->pass(new FormattableMarkup('%class thrown upon saving invalid data type.', [
296         '%class' => get_class($e),
297       ]));
298     }
299
300     // Test that setting an unsupported type for a config object with no schema
301     // also fails.
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));
306
307     try {
308       $config->set('stream', fopen(__FILE__, 'r'))->save();
309       $this->fail('No Exception thrown upon saving invalid data type.');
310     }
311     catch (UnsupportedDataTypeConfigException $e) {
312       $this->pass(new FormattableMarkup('%class thrown upon saving invalid data type.', [
313         '%class' => get_class($e),
314       ]));
315     }
316   }
317
318 }