View source
<?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigNameException;
use Drupal\Core\Config\ConfigValueException;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Config\UnsupportedDataTypeConfigException;
use Drupal\KernelTests\KernelTestBase;
class ConfigCRUDTest extends KernelTestBase {
protected $strictConfigSchema = FALSE;
public static $modules = [
'system',
];
public function testCRUD() {
$event_dispatcher = $this->container
->get('event_dispatcher');
$typed_config_manager = $this->container
->get('config.typed');
$storage = $this->container
->get('config.storage');
$collection_storage = $storage
->createCollection('test_collection');
$config_factory = $this->container
->get('config.factory');
$name = 'config_test.crud';
$config = $this
->config($name);
$this
->assertIdentical($config
->isNew(), TRUE);
$config
->set('value', 'initial');
$config
->save();
$this
->assertIdentical($config
->isNew(), FALSE);
$actual_data = $storage
->read($name);
$this
->assertIdentical($actual_data, [
'value' => 'initial',
]);
$actual_data = $config_factory
->get($name)
->getRawData();
$this
->assertIdentical($actual_data, [
'value' => 'initial',
]);
$collection_config = new Config($name, $collection_storage, $event_dispatcher, $typed_config_manager);
$collection_config
->set('value', 'overridden');
$collection_config
->save();
$actual_data = $config_factory
->get($name)
->getRawData();
$this
->assertIdentical($actual_data, [
'value' => 'initial',
]);
$config
->set('value', 'instance-update');
$config
->save();
$this
->assertIdentical($config
->isNew(), FALSE);
$actual_data = $storage
->read($name);
$this
->assertIdentical($actual_data, [
'value' => 'instance-update',
]);
$new_config = $this
->config($name);
$this
->assertIdentical($new_config
->get(), $config
->get());
$this
->assertIdentical($config
->isNew(), FALSE);
$config_factory
->getEditable($name);
$collection_config
->delete();
$actual_config = $config_factory
->get($name);
$this
->assertIdentical($actual_config
->isNew(), FALSE);
$this
->assertIdentical($actual_config
->getRawData(), [
'value' => 'instance-update',
]);
$config
->delete();
$this
->assertIdentical($config
->get(), []);
$this
->assertIdentical($config
->isNew(), TRUE);
$this
->assertIdentical($config_factory
->getEditable($name)
->isNew(), TRUE);
$actual_data = $storage
->read($name);
$this
->assertIdentical($actual_data, FALSE);
$new_config = $this
->config($name);
$this
->assertIdentical($new_config
->get(), $config
->get());
$this
->assertIdentical($config
->isNew(), TRUE);
$config
->set('value', 're-created');
$config
->save();
$this
->assertIdentical($config
->isNew(), FALSE);
$actual_data = $storage
->read($name);
$this
->assertIdentical($actual_data, [
'value' => 're-created',
]);
$new_config = $this
->config($name);
$this
->assertIdentical($new_config
->get(), $config
->get());
$this
->assertIdentical($config
->isNew(), FALSE);
$new_name = 'config_test.crud_rename';
$this->container
->get('config.factory')
->rename($name, $new_name);
$renamed_config = $this
->config($new_name);
$this
->assertIdentical($renamed_config
->get(), $config
->get());
$this
->assertIdentical($renamed_config
->isNew(), FALSE);
$config = $this
->config($name);
$this
->assertIdentical($config
->get(), []);
$this
->assertIdentical($config
->isNew(), TRUE);
$name = 'config_test.crud_rename';
$config_factory
->getEditable($name);
$config = $config_factory
->get($name);
$new_name = 'config_test.crud_rename_no_cache';
$config_factory
->rename($name, $new_name);
$renamed_config = $config_factory
->get($new_name);
$this
->assertIdentical($renamed_config
->get(), $config
->get());
$this
->assertIdentical($renamed_config
->isNew(), FALSE);
$this
->assertIdentical($config_factory
->get($name)
->isNew(), TRUE);
$this
->assertIdentical($config_factory
->getEditable($name)
->isNew(), TRUE);
$new_config = $this
->config($new_name);
$expected_values = [
'value' => 'herp',
'404' => 'derp',
];
$new_config
->merge($expected_values);
$new_config
->save();
$this
->assertIdentical($new_config
->get('value'), $expected_values['value']);
$this
->assertIdentical($new_config
->get('404'), $expected_values['404']);
$new_config = $config_factory
->get('non_existing_key');
$this
->assertTrue($new_config
->isNew());
$this
->assertCount(0, $config_factory
->loadMultiple([
'non_existing_key',
]), 'loadMultiple() does not return new objects');
}
public function testNameValidation() {
$name = 'nonamespace';
try {
$this
->config($name)
->save();
$this
->fail('Expected ConfigNameException was thrown for a name without a namespace.');
} catch (\Exception $e) {
$this
->assertInstanceOf(ConfigNameException::class, $e);
}
$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';
try {
$this
->config($name)
->save();
$this
->fail('Expected ConfigNameException was thrown for a name longer than Config::MAX_NAME_LENGTH.');
} catch (\Exception $e) {
$this
->assertInstanceOf(ConfigNameException::class, $e);
}
$characters = $test_characters = [
':',
'?',
'*',
'<',
'>',
'"',
'\'',
'/',
'\\',
];
foreach ($test_characters as $i => $c) {
try {
$name = 'namespace.object' . $c;
$config = $this
->config($name);
$config
->save();
} catch (ConfigNameException $e) {
unset($test_characters[$i]);
}
}
$this
->assertTrue(empty($test_characters), new FormattableMarkup('Expected ConfigNameException was thrown for all invalid name characters: @characters', [
'@characters' => implode(' ', $characters),
]));
$name = 'namespace.object';
try {
$config = $this
->config($name);
$config
->save();
} catch (ConfigNameException $e) {
$this
->fail('ConfigNameException was not thrown for a valid object name.');
}
}
public function testValueValidation() {
try {
$this
->config('namespace.object')
->setData([
'key.value' => 12,
])
->save();
$this
->fail('Expected ConfigValueException was thrown from setData() for value with dotted keys.');
} catch (\Exception $e) {
$this
->assertInstanceOf(ConfigValueException::class, $e);
}
try {
$this
->config('namespace.object')
->set('foo', [
'key.value' => 12,
])
->save();
$this
->fail('Expected ConfigValueException was thrown from set() for value with dotted keys.');
} catch (\Exception $e) {
$this
->assertInstanceOf(ConfigValueException::class, $e);
}
}
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));
$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);
foreach ($data as $key => $value) {
$config
->set($key, $value);
}
$config
->save();
$this
->assertIdentical($config
->get(), $data);
$this
->assertIdentical($storage
->read($name), $data);
$this
->verbose('<pre>' . $name . var_export($storage
->read($name), TRUE));
$config
->setData($data)
->save();
$this
->assertIdentical($config
->get(), $data);
$this
->assertIdentical($storage
->read($name), $data);
$this
->assertSame(99, $config
->get('int'));
$config
->set('int', '99')
->save(TRUE);
$this
->assertSame('99', $config
->get('int'));
$config
->save();
$this
->assertSame($data, $config
->get());
try {
$config
->set('stream', fopen(__FILE__, 'r'))
->save();
$this
->fail('No Exception thrown upon saving invalid data type.');
} catch (UnsupportedDataTypeConfigException $e) {
}
$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) {
}
}
}