FileStorageTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config\Storage;
  3. use Drupal\Core\Config\FileStorage;
  4. use Drupal\Core\Config\UnsupportedDataTypeConfigException;
  5. use Drupal\Core\Serialization\Yaml;
  6. use Drupal\Core\StreamWrapper\PublicStream;
  7. /**
  8. * Tests FileStorage operations.
  9. *
  10. * @group config
  11. */
  12. class FileStorageTest extends ConfigStorageTestBase {
  13. /**
  14. * A directory to store configuration in.
  15. *
  16. * @var string
  17. */
  18. protected $directory;
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. // Create a directory.
  25. $this->directory = PublicStream::basePath() . '/config';
  26. $this->storage = new FileStorage($this->directory);
  27. $this->invalidStorage = new FileStorage($this->directory . '/nonexisting');
  28. // FileStorage::listAll() requires other configuration data to exist.
  29. $this->storage->write('system.performance', $this->config('system.performance')->get());
  30. $this->storage->write('core.extension', ['module' => []]);
  31. }
  32. protected function read($name) {
  33. $data = file_get_contents($this->storage->getFilePath($name));
  34. return Yaml::decode($data);
  35. }
  36. protected function insert($name, $data) {
  37. file_put_contents($this->storage->getFilePath($name), $data);
  38. }
  39. protected function update($name, $data) {
  40. file_put_contents($this->storage->getFilePath($name), $data);
  41. }
  42. protected function delete($name) {
  43. unlink($this->storage->getFilePath($name));
  44. }
  45. /**
  46. * Tests the FileStorage::listAll method with a relative and absolute path.
  47. */
  48. public function testlistAll() {
  49. $expected_files = [
  50. 'core.extension',
  51. 'system.performance',
  52. ];
  53. $config_files = $this->storage->listAll();
  54. $this->assertIdentical($config_files, $expected_files, 'Relative path, two config files found.');
  55. // @todo https://www.drupal.org/node/2666954 FileStorage::listAll() is
  56. // case-sensitive. However, \Drupal\Core\Config\DatabaseStorage::listAll()
  57. // is case-insensitive.
  58. $this->assertSame(['system.performance'], $this->storage->listAll('system'), 'The FileStorage::listAll() with prefix works.');
  59. $this->assertSame([], $this->storage->listAll('System'), 'The FileStorage::listAll() is case sensitive.');
  60. }
  61. /**
  62. * Test UnsupportedDataTypeConfigException displays path of
  63. * erroneous file during read.
  64. */
  65. public function testReadUnsupportedDataTypeConfigException() {
  66. file_put_contents($this->storage->getFilePath('core.extension'), PHP_EOL . 'foo : [bar}', FILE_APPEND);
  67. try {
  68. $config_parsed = $this->storage->read('core.extension');
  69. }
  70. catch (UnsupportedDataTypeConfigException $e) {
  71. $this->assertStringContainsString($this->storage->getFilePath('core.extension'), $e->getMessage(), 'Erroneous file path is displayed.');
  72. }
  73. }
  74. }