function ConfigCRUDTest::testDataTypes
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testDataTypes()
- 10 core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testDataTypes()
- 11.x core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testDataTypes()
Tests data type handling.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Config/ ConfigCRUDTest.php, line 268
Class
- ConfigCRUDTest
- Tests CRUD operations on configuration objects.
Namespace
Drupal\KernelTests\Core\ConfigCode
public function testDataTypes() {
\Drupal::service('module_installer')->install([
'config_test',
]);
$storage = new DatabaseStorage($this->container
->get('database'), 'config');
$name = 'config_test.types';
$config = $this->config($name);
$original_content = file_get_contents(drupal_get_path('module', 'config_test') . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY . "/{$name}.yml");
$this->verbose('<pre>' . $original_content . "\n" . var_export($storage->read($name), TRUE));
// Verify variable data types are intact.
$data = [
'array' => [],
'boolean' => TRUE,
'exp' => 1.2E+34,
'float' => 3.14159,
'float_as_integer' => (double) 1,
'hex' => 0xc,
'int' => 99,
'octal' => 0775,
'string' => 'string',
'string_int' => '1',
];
$data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($data));
$this->assertIdentical($config->get(), $data);
// Re-set each key using Config::set().
foreach ($data as $key => $value) {
$config->set($key, $value);
}
$config->save();
$this->assertIdentical($config->get(), $data);
// Assert the data against the file storage.
$this->assertIdentical($storage->read($name), $data);
$this->verbose('<pre>' . $name . var_export($storage->read($name), TRUE));
// Set data using config::setData().
$config->setData($data)
->save();
$this->assertIdentical($config->get(), $data);
$this->assertIdentical($storage->read($name), $data);
// Test that schema type enforcement can be overridden by trusting the data.
$this->assertSame(99, $config->get('int'));
$config->set('int', '99')
->save(TRUE);
$this->assertSame('99', $config->get('int'));
// Test that re-saving without testing the data enforces the schema type.
$config->save();
$this->assertSame($data, $config->get());
// Test that setting an unsupported type for a config object with a schema
// fails.
try {
$config->set('stream', fopen(__FILE__, 'r'))
->save();
$this->fail('No Exception thrown upon saving invalid data type.');
} catch (UnsupportedDataTypeConfigException $e) {
// Expected exception; just continue testing.
}
// Test that setting an unsupported type for a config object with no schema
// also fails.
$typed_config_manager = $this->container
->get('config.typed');
$config_name = 'config_test.no_schema';
$config = $this->config($config_name);
$this->assertFalse($typed_config_manager->hasConfigSchema($config_name));
try {
$config->set('stream', fopen(__FILE__, 'r'))
->save();
$this->fail('No Exception thrown upon saving invalid data type.');
} catch (UnsupportedDataTypeConfigException $e) {
// Expected exception; just continue testing.
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.