3 namespace Drupal\KernelTests\Core\Config\Storage;
5 use Drupal\Core\Config\FileStorage;
6 use Drupal\Core\Config\UnsupportedDataTypeConfigException;
7 use Drupal\Core\Serialization\Yaml;
8 use Drupal\Core\StreamWrapper\PublicStream;
11 * Tests FileStorage operations.
15 class FileStorageTest extends ConfigStorageTestBase {
18 * A directory to store configuration in.
27 protected function setUp() {
29 // Create a directory.
30 $this->directory = PublicStream::basePath() . '/config';
31 $this->storage = new FileStorage($this->directory);
32 $this->invalidStorage = new FileStorage($this->directory . '/nonexisting');
34 // FileStorage::listAll() requires other configuration data to exist.
35 $this->storage->write('system.performance', $this->config('system.performance')->get());
36 $this->storage->write('core.extension', ['module' => []]);
39 protected function read($name) {
40 $data = file_get_contents($this->storage->getFilePath($name));
41 return Yaml::decode($data);
44 protected function insert($name, $data) {
45 file_put_contents($this->storage->getFilePath($name), $data);
48 protected function update($name, $data) {
49 file_put_contents($this->storage->getFilePath($name), $data);
52 protected function delete($name) {
53 unlink($this->storage->getFilePath($name));
57 * Tests the FileStorage::listAll method with a relative and absolute path.
59 public function testlistAll() {
65 $config_files = $this->storage->listAll();
66 $this->assertIdentical($config_files, $expected_files, 'Relative path, two config files found.');
68 // @todo https://www.drupal.org/node/2666954 FileStorage::listAll() is
69 // case-sensitive. However, \Drupal\Core\Config\DatabaseStorage::listAll()
70 // is case-insensitive.
71 $this->assertSame(['system.performance'], $this->storage->listAll('system'), 'The FileStorage::listAll() with prefix works.');
72 $this->assertSame([], $this->storage->listAll('System'), 'The FileStorage::listAll() is case sensitive.');
76 * Test UnsupportedDataTypeConfigException displays path of
77 * erroneous file during read.
79 public function testReadUnsupportedDataTypeConfigException() {
80 file_put_contents($this->storage->getFilePath('core.extension'), PHP_EOL . 'foo : [bar}', FILE_APPEND);
82 $config_parsed = $this->storage->read('core.extension');
84 catch (UnsupportedDataTypeConfigException $e) {
85 $this->pass('Exception thrown when trying to read a field containing invalid data type.');
86 $this->assertTrue((strpos($e->getMessage(), $this->storage->getFilePath('core.extension')) !== FALSE), 'Erroneous file path is displayed.');