View source
<?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Site\Settings;
use Drupal\KernelTests\KernelTestBase;
class ConfigFileContentTest extends KernelTestBase {
protected $strictConfigSchema = FALSE;
public function testReadWriteConfig() {
$storage = $this->container
->get('config.storage');
$name = 'foo.bar';
$key = 'foo';
$value = 'bar';
$nested_key = 'biff.bang';
$nested_value = 'pow';
$array_key = 'array';
$array_value = [
'foo' => 'bar',
'biff' => [
'bang' => 'pow',
],
];
$casting_array_key = 'casting_array';
$casting_array_false_value_key = 'casting_array.cast.false';
$casting_array_value = [
'cast' => [
'false' => FALSE,
],
];
$nested_array_key = 'nested.array';
$true_key = 'true';
$false_key = 'false';
$config = $this
->config($name);
$this
->assertEquals($name, $config
->getName());
$this
->assertNotEmpty($config, 'Config object created.');
$this
->assertEquals([], $config
->get(), 'New config object is empty.');
$data = $storage
->read($name);
$this
->assertFalse($data);
$config = $this
->config($name);
$config
->set($key, $value);
$config
->set($nested_key, $nested_value);
$config
->set($array_key, $array_value);
$config
->set($nested_array_key, $array_value);
$config
->set($false_key, FALSE);
$config
->set($true_key, TRUE);
$config
->set('null', NULL);
$config
->set($casting_array_key, $casting_array_value);
$config
->save();
$data = $storage
->read($name);
$this
->assertNotEmpty($data);
$config = $this
->config($name);
$this
->assertEquals($name, $config
->getName());
$this
->assertNotEmpty($config, 'Config object created.');
$this
->assertEquals('bar', $config
->get($key), 'Top level configuration value found.');
$this
->assertEquals($nested_value, $config
->get($nested_key), 'Nested configuration value found.');
$this
->assertEquals($array_value, $config
->get($array_key), 'Top level array configuration value found.');
$this
->assertEquals($array_value, $config
->get($nested_array_key), 'Nested array configuration value found.');
$this
->assertNull($config
->get('i_do_not_exist'), 'Non-existent top level value returned NULL.');
$this
->assertNull($config
->get('i.do.not.exist'), 'Non-existent nested value returned NULL.');
$this
->assertFalse($config
->get($false_key), "Boolean FALSE value returned the FALSE.");
$this
->assertTrue($config
->get($true_key), "Boolean TRUE value returned the TRUE.");
$this
->assertNull($config
->get('null'));
$this
->assertFalse($config
->get($casting_array_false_value_key), "Nested boolean FALSE value returned FALSE.");
$config
->clear($key);
$config
->clear($nested_key);
$config
->save();
$config = $this
->config($name);
$this
->assertNull($config
->get($key), 'Top level value unset.');
$this
->assertNull($config
->get($nested_key), 'Nested value unset.');
$config = $this
->config('foo.baz');
$config
->set($key, $value);
$config
->save();
$chained_name = 'biff.bang';
$config = $this
->config($chained_name);
$config
->set($key, $value)
->save();
$data = $storage
->read($chained_name);
$this
->assertEquals($config
->get(), $data);
$files = $storage
->listAll('foo');
$this
->assertCount(2, $files, 'Two files listed with the prefix \'foo\'.');
$files = $storage
->listAll('biff');
$this
->assertCount(1, $files, 'One file listed with the prefix \'biff\'.');
$files = $storage
->listAll('foo.bar');
$this
->assertCount(1, $files, 'One file listed with the prefix \'foo.bar\'.');
$files = $storage
->listAll('bar');
$this
->assertEquals([], $files, 'No files listed with the prefix \'bar\'.');
$config = $this
->config($name);
$config
->delete();
$data = $storage
->read($name);
$this
->assertFalse($data);
}
public function testSerialization() {
$name = $this
->randomMachineName(10) . '.' . $this
->randomMachineName(10);
$config_data = [
'numeric keys' => [
'i',
'n',
'd',
'e',
'x',
'e',
'd',
],
'nested keys' => [
'HTML' => '<strong> <bold> <em> <blockquote>',
'UTF-8' => 'FrançAIS is ÜBER-åwesome',
'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ' => 'αβγδεζηθικλμνξοσὠ',
],
'invalid xml' => '</title><script type="text/javascript">alert("Title XSS!");</script> & < > " \' ',
];
$filestorage = new FileStorage(Settings::get('config_sync_directory'));
$filestorage
->write($name, $config_data);
$config_parsed = $filestorage
->read($name);
$key = 'numeric keys';
$this
->assertSame($config_data[$key], $config_parsed[$key]);
$key = 'nested keys';
$this
->assertSame($config_data[$key], $config_parsed[$key]);
$key = 'HTML';
$this
->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
$key = 'UTF-8';
$this
->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
$key = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
$this
->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
$key = 'invalid xml';
$this
->assertSame($config_data[$key], $config_parsed[$key]);
}
}