1<?php 2 3namespace Drupal\KernelTests\Core\Config\Storage; 4 5use Drupal\Core\Config\FileStorage; 6use Drupal\Core\Config\UnsupportedDataTypeConfigException; 7use Drupal\Core\Serialization\Yaml; 8use Drupal\Core\StreamWrapper\PublicStream; 9 10/** 11 * Tests FileStorage operations. 12 * 13 * @group config 14 */ 15class FileStorageTest extends ConfigStorageTestBase { 16 17 /** 18 * A directory to store configuration in. 19 * 20 * @var string 21 */ 22 protected $directory; 23 24 /** 25 * {@inheritdoc} 26 */ 27 protected function setUp() { 28 parent::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'); 33 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' => []]); 37 } 38 39 protected function read($name) { 40 $data = file_get_contents($this->storage->getFilePath($name)); 41 return Yaml::decode($data); 42 } 43 44 protected function insert($name, $data) { 45 file_put_contents($this->storage->getFilePath($name), $data); 46 } 47 48 protected function update($name, $data) { 49 file_put_contents($this->storage->getFilePath($name), $data); 50 } 51 52 protected function delete($name) { 53 unlink($this->storage->getFilePath($name)); 54 } 55 56 /** 57 * Tests the FileStorage::listAll method with a relative and absolute path. 58 */ 59 public function testlistAll() { 60 $expected_files = [ 61 'core.extension', 62 'system.performance', 63 ]; 64 65 $config_files = $this->storage->listAll(); 66 $this->assertIdentical($config_files, $expected_files, 'Relative path, two config files found.'); 67 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.'); 73 } 74 75 /** 76 * Test UnsupportedDataTypeConfigException displays path of 77 * erroneous file during read. 78 */ 79 public function testReadUnsupportedDataTypeConfigException() { 80 file_put_contents($this->storage->getFilePath('core.extension'), PHP_EOL . 'foo : [bar}', FILE_APPEND); 81 try { 82 $config_parsed = $this->storage->read('core.extension'); 83 } 84 catch (UnsupportedDataTypeConfigException $e) { 85 $this->assertStringContainsString($this->storage->getFilePath('core.extension'), $e->getMessage(), 'Erroneous file path is displayed.'); 86 } 87 } 88 89} 90