public function ConfigCRUDTest::testDataTypes in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/config/src/Tests/ConfigCRUDTest.php \Drupal\config\Tests\ConfigCRUDTest::testDataTypes()
Tests data type handling.
File
- core/
modules/ config/ src/ Tests/ ConfigCRUDTest.php, line 245 - Contains \Drupal\config\Tests\ConfigCRUDTest.
Class
- ConfigCRUDTest
- Tests CRUD operations on configuration objects.
Namespace
Drupal\config\TestsCode
public function testDataTypes() {
\Drupal::service('module_installer')
->install(array(
'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(
'array' => 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',
);
$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
->assertIdentical(99, $config
->get('int'));
$config
->set('int', '99')
->save(TRUE);
$this
->assertIdentical('99', $config
->get('int'));
// Test that re-saving without testing the data enforces the schema type.
$config
->save();
$this
->assertIdentical($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) {
$this
->pass(SafeMarkup::format('%class thrown upon saving invalid data type.', array(
'%class' => get_class($e),
)));
}
// 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) {
$this
->pass(SafeMarkup::format('%class thrown upon saving invalid data type.', array(
'%class' => get_class($e),
)));
}
}