View source
<?php
namespace Drupal\Tests\Core\Config;
use Drupal\Core\Config\MemoryStorage;
use Drupal\Core\Config\ReadOnlyStorage;
use Drupal\Core\Config\StorageCopyTrait;
use Drupal\Core\Config\StorageInterface;
use Drupal\Tests\UnitTestCase;
class ReadOnlyStorageTest extends UnitTestCase {
use StorageCopyTrait;
protected $memory;
protected $storage;
protected function setUp() : void {
$this->memory = new MemoryStorage();
$this->storage = new ReadOnlyStorage($this->memory);
}
public function testReadOperations($method, $arguments, $fixture) {
$this
->setRandomFixtureConfig($fixture);
$expected = call_user_func_array([
$this->memory,
$method,
], $arguments);
$actual = call_user_func_array([
$this->storage,
$method,
], $arguments);
$this
->assertEquals($expected, $actual);
}
public function readMethodsProvider() {
$fixture = [
StorageInterface::DEFAULT_COLLECTION => [
'config.a',
'config.b',
'other.a',
],
];
$data = [];
$data[] = [
'exists',
[
'config.a',
],
$fixture,
];
$data[] = [
'exists',
[
'not.existing',
],
$fixture,
];
$data[] = [
'read',
[
'config.a',
],
$fixture,
];
$data[] = [
'read',
[
'not.existing',
],
$fixture,
];
$data[] = [
'readMultiple',
[
[
'config.a',
'config.b',
'not',
],
],
$fixture,
];
$data[] = [
'listAll',
[
'',
],
$fixture,
];
$data[] = [
'listAll',
[
'config',
],
$fixture,
];
$data[] = [
'listAll',
[
'none',
],
$fixture,
];
return $data;
}
public function testWriteOperations($method, $arguments, $fixture) {
$this
->setRandomFixtureConfig($fixture);
$backup = new MemoryStorage();
static::replaceStorageContents($this->memory, $backup);
try {
call_user_func_array([
$this->storage,
$method,
], $arguments);
$this
->fail("exception not thrown");
} catch (\BadMethodCallException $exception) {
$this
->assertEquals(ReadOnlyStorage::class . '::' . $method . ' is not allowed on a ReadOnlyStorage', $exception
->getMessage());
}
$this
->assertEquals($backup, $this->memory);
}
public function writeMethodsProvider() {
$fixture = [
StorageInterface::DEFAULT_COLLECTION => [
'config.a',
'config.b',
],
];
$data = [];
$data[] = [
'write',
[
'config.a',
(array) $this
->getRandomGenerator()
->object(),
],
$fixture,
];
$data[] = [
'write',
[
$this
->randomMachineName(),
(array) $this
->getRandomGenerator()
->object(),
],
$fixture,
];
$data[] = [
'delete',
[
'config.a',
],
$fixture,
];
$data[] = [
'delete',
[
$this
->randomMachineName(),
],
$fixture,
];
$data[] = [
'rename',
[
'config.a',
'config.b',
],
$fixture,
];
$data[] = [
'rename',
[
'config.a',
$this
->randomMachineName(),
],
$fixture,
];
$data[] = [
'rename',
[
$this
->randomMachineName(),
$this
->randomMachineName(),
],
$fixture,
];
$data[] = [
'deleteAll',
[
'',
],
$fixture,
];
$data[] = [
'deleteAll',
[
'config',
],
$fixture,
];
$data[] = [
'deleteAll',
[
'other',
],
$fixture,
];
return $data;
}
public function testCollections() {
$fixture = [
StorageInterface::DEFAULT_COLLECTION => [
$this
->randomMachineName(),
],
'A' => [
$this
->randomMachineName(),
],
'B' => [
$this
->randomMachineName(),
],
'C' => [
$this
->randomMachineName(),
],
];
$this
->setRandomFixtureConfig($fixture);
$this
->assertEquals([
'A',
'B',
'C',
], $this->storage
->getAllCollectionNames());
foreach (array_keys($fixture) as $collection) {
$storage = $this->storage
->createCollection($collection);
$this
->assertInstanceOf(ReadOnlyStorage::class, $storage);
$this
->assertEquals($collection, $storage
->getCollectionName());
}
}
public function testEncodeDecode() {
$array = (array) $this
->getRandomGenerator()
->object();
$string = $this
->getRandomGenerator()
->string();
$this
->assertEquals($array, $this->storage
->decode($this->storage
->encode($array)));
$this
->assertEquals($string, $this->storage
->encode($this->storage
->decode($string)));
$this
->assertEquals($this->memory
->encode($array), $this->storage
->encode($array));
$this
->assertEquals($this->memory
->decode($string), $this->storage
->decode($string));
}
protected function setRandomFixtureConfig($config) {
foreach (array_merge([
StorageInterface::DEFAULT_COLLECTION,
], $this->memory
->getAllCollectionNames()) as $collection) {
$this->memory
->createCollection($collection)
->deleteAll();
}
foreach ($config as $collection => $keys) {
$storage = $this->memory
->createCollection($collection);
foreach ($keys as $key) {
$storage
->write($key, (array) $this
->getRandomGenerator()
->object());
}
}
}
}