View source
<?php
namespace Drupal\config\Tests;
use Drupal\Core\Config\FileStorage;
use Drupal\simpletest\KernelTestBase;
class ConfigFileContentTest extends KernelTestBase {
protected $strictConfigSchema = FALSE;
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 = array(
'foo' => 'bar',
'biff' => array(
'bang' => 'pow',
),
);
$casting_array_key = 'casting_array';
$casting_array_false_value_key = 'casting_array.cast.false';
$casting_array_value = array(
'cast' => array(
'false' => FALSE,
),
);
$nested_array_key = 'nested.array';
$true_key = 'true';
$false_key = 'false';
$config = $this
->config($name);
$this
->assertEqual($config
->getName(), $name);
$this
->assertTrue($config, 'Config object created.');
$this
->assertEqual($config
->get(), array(), 'New config object is empty.');
$data = $storage
->read($name);
$this
->assertIdentical($data, FALSE);
$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
->assertTrue($data);
$config = $this
->config($name);
$this
->assertEqual($config
->getName(), $name);
$this
->assertTrue($config, 'Config object created.');
$this
->assertEqual($config
->get($key), 'bar', 'Top level configuration value found.');
$this
->assertEqual($config
->get($nested_key), $nested_value, 'Nested configuration value found.');
$this
->assertEqual($config
->get($array_key), $array_value, 'Top level array configuration value found.');
$this
->assertEqual($config
->get($nested_array_key), $array_value, 'Nested array configuration value found.');
$this
->assertNull($config
->get('i_dont_exist'), 'Non-existent top level value returned NULL.');
$this
->assertNull($config
->get('i.dont.exist'), 'Non-existent nested value returned NULL.');
$this
->assertEqual($config
->get($false_key), '0', "Boolean FALSE value returned the string '0'.");
$this
->assertEqual($config
->get($true_key), '1', "Boolean TRUE value returned the string '1'.");
$this
->assertIdentical($config
->get('null'), NULL);
$this
->assertEqual($config
->get($casting_array_false_value_key), '0', "Nested boolean FALSE value returned the string '0'.");
$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
->assertEqual($data, $config
->get());
$files = $storage
->listAll('foo');
$this
->assertEqual(count($files), 2, 'Two files listed with the prefix \'foo\'.');
$files = $storage
->listAll('biff');
$this
->assertEqual(count($files), 1, 'One file listed with the prefix \'biff\'.');
$files = $storage
->listAll('foo.bar');
$this
->assertEqual(count($files), 1, 'One file listed with the prefix \'foo.bar\'.');
$files = $storage
->listAll('bar');
$this
->assertEqual($files, array(), 'No files listed with the prefix \'bar\'.');
$config = $this
->config($name);
$config
->delete();
$data = $storage
->read($name);
$this
->assertIdentical($data, FALSE);
}
function testSerialization() {
$name = $this
->randomMachineName(10) . '.' . $this
->randomMachineName(10);
$config_data = array(
'numeric keys' => array(
'i',
'n',
'd',
'e',
'x',
'e',
'd',
),
'nested keys' => array(
'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($this->configDirectories[CONFIG_SYNC_DIRECTORY]);
$filestorage
->write($name, $config_data);
$config_parsed = $filestorage
->read($name);
$key = 'numeric keys';
$this
->assertIdentical($config_data[$key], $config_parsed[$key]);
$key = 'nested keys';
$this
->assertIdentical($config_data[$key], $config_parsed[$key]);
$key = 'HTML';
$this
->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
$key = 'UTF-8';
$this
->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
$key = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
$this
->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
$key = 'invalid xml';
$this
->assertIdentical($config_data[$key], $config_parsed[$key]);
}
}