ReadOnlyStorageTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Drupal\Tests\Core\Config;
  3. use Drupal\Core\Config\MemoryStorage;
  4. use Drupal\Core\Config\ReadOnlyStorage;
  5. use Drupal\Core\Config\StorageCopyTrait;
  6. use Drupal\Core\Config\StorageInterface;
  7. use Drupal\Tests\UnitTestCase;
  8. /**
  9. * @coversDefaultClass \Drupal\Core\Config\ReadOnlyStorage
  10. * @group Config
  11. */
  12. class ReadOnlyStorageTest extends UnitTestCase {
  13. use StorageCopyTrait;
  14. /**
  15. * The memory storage containing the data.
  16. *
  17. * @var \Drupal\Core\Config\MemoryStorage
  18. */
  19. protected $memory;
  20. /**
  21. * The read-only storage under test.
  22. *
  23. * @var \Drupal\Core\Config\ReadOnlyStorage
  24. */
  25. protected $storage;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function setUp() {
  30. // Set up a memory storage we can manipulate to set fixtures.
  31. $this->memory = new MemoryStorage();
  32. // Wrap the memory storage in the read-only storage to test it.
  33. $this->storage = new ReadOnlyStorage($this->memory);
  34. }
  35. /**
  36. * @covers ::exists
  37. * @covers ::read
  38. * @covers ::readMultiple
  39. * @covers ::listAll
  40. *
  41. * @dataProvider readMethodsProvider
  42. */
  43. public function testReadOperations($method, $arguments, $fixture) {
  44. $this->setRandomFixtureConfig($fixture);
  45. $expected = call_user_func_array([$this->memory, $method], $arguments);
  46. $actual = call_user_func_array([$this->storage, $method], $arguments);
  47. $this->assertEquals($expected, $actual);
  48. }
  49. /**
  50. * Provide the methods that work transparently.
  51. *
  52. * @return array
  53. * The data.
  54. */
  55. public function readMethodsProvider() {
  56. $fixture = [
  57. StorageInterface::DEFAULT_COLLECTION => ['config.a', 'config.b', 'other.a'],
  58. ];
  59. $data = [];
  60. $data[] = ['exists', ['config.a'], $fixture];
  61. $data[] = ['exists', ['not.existing'], $fixture];
  62. $data[] = ['read', ['config.a'], $fixture];
  63. $data[] = ['read', ['not.existing'], $fixture];
  64. $data[] = ['readMultiple', [['config.a', 'config.b', 'not']], $fixture];
  65. $data[] = ['listAll', [''], $fixture];
  66. $data[] = ['listAll', ['config'], $fixture];
  67. $data[] = ['listAll', ['none'], $fixture];
  68. return $data;
  69. }
  70. /**
  71. * @covers ::write
  72. * @covers ::delete
  73. * @covers ::rename
  74. * @covers ::deleteAll
  75. *
  76. * @dataProvider writeMethodsProvider
  77. */
  78. public function testWriteOperations($method, $arguments, $fixture) {
  79. $this->setRandomFixtureConfig($fixture);
  80. // Create an independent memory storage as a backup.
  81. $backup = new MemoryStorage();
  82. static::replaceStorageContents($this->memory, $backup);
  83. try {
  84. call_user_func_array([$this->storage, $method], $arguments);
  85. $this->fail("exception not thrown");
  86. }
  87. catch (\BadMethodCallException $exception) {
  88. $this->assertEquals(ReadOnlyStorage::class . '::' . $method . ' is not allowed on a ReadOnlyStorage', $exception->getMessage());
  89. }
  90. // Assert that the memory storage has not been altered.
  91. $this->assertTrue($backup == $this->memory);
  92. }
  93. /**
  94. * Provide the methods that throw an exception.
  95. *
  96. * @return array
  97. * The data
  98. */
  99. public function writeMethodsProvider() {
  100. $fixture = [
  101. StorageInterface::DEFAULT_COLLECTION => ['config.a', 'config.b'],
  102. ];
  103. $data = [];
  104. $data[] = ['write', ['config.a', (array) $this->getRandomGenerator()->object()], $fixture];
  105. $data[] = ['write', [$this->randomMachineName(), (array) $this->getRandomGenerator()->object()], $fixture];
  106. $data[] = ['delete', ['config.a'], $fixture];
  107. $data[] = ['delete', [$this->randomMachineName()], $fixture];
  108. $data[] = ['rename', ['config.a', 'config.b'], $fixture];
  109. $data[] = ['rename', ['config.a', $this->randomMachineName()], $fixture];
  110. $data[] = ['rename', [$this->randomMachineName(), $this->randomMachineName()], $fixture];
  111. $data[] = ['deleteAll', [''], $fixture];
  112. $data[] = ['deleteAll', ['config'], $fixture];
  113. $data[] = ['deleteAll', ['other'], $fixture];
  114. return $data;
  115. }
  116. /**
  117. * @covers ::getAllCollectionNames
  118. * @covers ::getCollectionName
  119. * @covers ::createCollection
  120. */
  121. public function testCollections() {
  122. $fixture = [
  123. StorageInterface::DEFAULT_COLLECTION => [$this->randomMachineName()],
  124. 'A' => [$this->randomMachineName()],
  125. 'B' => [$this->randomMachineName()],
  126. 'C' => [$this->randomMachineName()],
  127. ];
  128. $this->setRandomFixtureConfig($fixture);
  129. $this->assertEquals(['A', 'B', 'C'], $this->storage->getAllCollectionNames());
  130. foreach (array_keys($fixture) as $collection) {
  131. $storage = $this->storage->createCollection($collection);
  132. // Assert that the collection storage is still a read-only storage.
  133. $this->assertInstanceOf(ReadOnlyStorage::class, $storage);
  134. $this->assertEquals($collection, $storage->getCollectionName());
  135. }
  136. }
  137. /**
  138. * @covers ::encode
  139. * @covers ::decode
  140. */
  141. public function testEncodeDecode() {
  142. $array = (array) $this->getRandomGenerator()->object();
  143. $string = $this->getRandomGenerator()->string();
  144. // Assert reversibility of encoding and decoding.
  145. $this->assertEquals($array, $this->storage->decode($this->storage->encode($array)));
  146. $this->assertEquals($string, $this->storage->encode($this->storage->decode($string)));
  147. // Assert same results as the decorated storage.
  148. $this->assertEquals($this->memory->encode($array), $this->storage->encode($array));
  149. $this->assertEquals($this->memory->decode($string), $this->storage->decode($string));
  150. }
  151. /**
  152. * Generate random config in the memory storage.
  153. *
  154. * @param array $config
  155. * The config keys, keyed by the collection.
  156. */
  157. protected function setRandomFixtureConfig($config) {
  158. // Erase previous fixture.
  159. foreach (array_merge([StorageInterface::DEFAULT_COLLECTION], $this->memory->getAllCollectionNames()) as $collection) {
  160. $this->memory->createCollection($collection)->deleteAll();
  161. }
  162. foreach ($config as $collection => $keys) {
  163. $storage = $this->memory->createCollection($collection);
  164. foreach ($keys as $key) {
  165. // Create some random config.
  166. $storage->write($key, (array) $this->getRandomGenerator()->object());
  167. }
  168. }
  169. }
  170. }