View source
<?php
namespace Drupal\KernelTests\Core\Config\Storage;
use Drupal\KernelTests\KernelTestBase;
abstract class ConfigStorageTestBase extends KernelTestBase {
protected $storage;
protected $invalidStorage;
public function testCRUD() {
$name = 'config_test.storage';
$this
->assertFalse($this->storage
->exists($name));
$data = $this->storage
->read($name);
$this
->assertFalse($data);
$data = [
'foo' => 'bar',
];
$result = $this->storage
->write($name, $data);
$this
->assertTrue($result);
$raw_data = $this
->read($name);
$this
->assertSame($data, $raw_data);
$this
->assertTrue($this->storage
->exists($name));
$result = $this->storage
->write($name, $data);
$this
->assertTrue($result);
$names = $this->storage
->listAll();
$this
->assertContains('system.performance', $names);
$this
->assertContains($name, $names);
$names = $this->storage
->listAll('config_test.');
$this
->assertNotContains('system.performance', $names);
$this
->assertContains($name, $names);
$new_name = 'config_test.storage_rename';
$this->storage
->rename($name, $new_name);
$raw_data = $this
->read($new_name);
$this
->assertSame($data, $raw_data);
$this->storage
->rename($new_name, $name);
$result = $this->storage
->delete($name);
$this
->assertTrue($result);
$result = $this->storage
->delete($name);
$this
->assertFalse($result);
$files = [
'config_test.test.biff',
'config_test.test.bang',
'config_test.test.pow',
];
foreach ($files as $name) {
$this->storage
->write($name, $data);
}
$this
->assertFalse($this->storage
->deleteAll('some_thing_that_cannot_exist'));
$result = $this->storage
->deleteAll('config_test.');
$names = $this->storage
->listAll('config_test.');
$this
->assertTrue($result);
$this
->assertSame([], $names);
$this
->assertFalse($this->storage
->rename('config_test.storage_does_not_exist', 'config_test.storage_does_not_exist_rename'));
$data = [
'foo' => 'bar',
];
$this
->assertTrue($this->storage
->write($name, $data));
$this
->assertFalse($this->storage
->rename('config_test.storage_does_not_exist', $name));
}
public function testInvalidStorage() {
$name = 'config_test.storage';
$data = [
'foo' => 'bar',
];
$result = $this->storage
->write($name, $data);
$this
->assertTrue($result);
$raw_data = $this
->read($name);
$this
->assertSame($data, $raw_data);
$result = $this->invalidStorage
->read($name);
$this
->assertFalse($result);
try {
$this->invalidStorage
->delete($name);
$this
->fail('Exception not thrown upon deleting from a non-existing storage bin.');
} catch (\Exception $e) {
}
$result = $this->invalidStorage
->listAll();
$this
->assertSame([], $result);
$this
->assertSame([], $this->invalidStorage
->getAllCollectionNames());
$this->invalidStorage
->write($name, [
'foo' => 'bar',
]);
$result = $this->invalidStorage
->read($name);
$this
->assertSame([
'foo' => 'bar',
], $result);
}
public function testDataTypes() {
$name = 'config_test.types';
$data = [
'array' => [],
'boolean' => TRUE,
'exp' => 1.2E+34,
'float' => 3.14159,
'hex' => 0xc,
'int' => 99,
'octal' => 0775,
'string' => 'string',
'string_int' => '1',
];
$result = $this->storage
->write($name, $data);
$this
->assertTrue($result);
$read_data = $this->storage
->read($name);
$this
->assertSame($data, $read_data);
}
public function testCollection() {
$name = 'config_test.storage';
$data = [
'foo' => 'bar',
];
$result = $this->storage
->write($name, $data);
$this
->assertTrue($result);
$this
->assertSame($data, $this->storage
->read($name));
$new_storage = $this->storage
->createCollection('collection.sub.new');
$this
->assertFalse($new_storage
->exists($name));
$this
->assertEquals([], $new_storage
->listAll());
$this
->assertFalse($new_storage
->delete($name));
$this
->assertFalse($new_storage
->deleteAll('config_test.'));
$this
->assertFalse($new_storage
->deleteAll());
$this
->assertFalse($new_storage
->rename($name, 'config_test.another_name'));
$new_storage
->write($name, $data);
$this
->assertTrue($result);
$this
->assertSame($data, $new_storage
->read($name));
$this
->assertEquals([
$name,
], $new_storage
->listAll());
$this
->assertTrue($new_storage
->exists($name));
$new_data = [
'foo' => 'baz',
];
$new_storage
->write($name, $new_data);
$this
->assertTrue($result);
$this
->assertSame($new_data, $new_storage
->read($name));
$another_storage = $this->storage
->createCollection('collection.sub.another');
$this
->assertFalse($another_storage
->exists($name));
$this
->assertEquals([], $another_storage
->listAll());
$another_storage
->write($name, $new_data);
$this
->assertTrue($result);
$this
->assertSame($new_data, $another_storage
->read($name));
$this
->assertEquals([
$name,
], $another_storage
->listAll());
$this
->assertTrue($another_storage
->exists($name));
$alt_storage = $this->storage
->createCollection('alternate');
$alt_storage
->write($name, $new_data);
$this
->assertTrue($result);
$this
->assertSame($new_data, $alt_storage
->read($name));
$this
->assertSame($data, $this->storage
->read($name));
$this
->assertSame([
'alternate',
'collection.sub.another',
'collection.sub.new',
], $this->storage
->getAllCollectionNames());
$alt_storage
->delete($name);
$this
->assertSame([
'collection.sub.another',
'collection.sub.new',
], $this->storage
->getAllCollectionNames());
$parent_storage = $this->storage
->createCollection('collection');
$this
->assertFalse($parent_storage
->exists($name));
$this
->assertEquals([], $parent_storage
->listAll());
$parent_storage
->write($name, $new_data);
$this
->assertTrue($result);
$this
->assertSame($new_data, $parent_storage
->read($name));
$this
->assertEquals([
$name,
], $parent_storage
->listAll());
$this
->assertTrue($parent_storage
->exists($name));
$this
->assertSame([
'collection',
'collection.sub.another',
'collection.sub.new',
], $this->storage
->getAllCollectionNames());
$parent_storage
->deleteAll();
$this
->assertSame([
'collection.sub.another',
'collection.sub.new',
], $this->storage
->getAllCollectionNames());
$this
->assertFalse($parent_storage
->exists($name));
$this
->assertEquals([], $parent_storage
->listAll());
$this
->assertFalse($parent_storage
->delete($name));
$this
->assertFalse($parent_storage
->deleteAll('config_test.'));
$this
->assertFalse($parent_storage
->deleteAll());
$this
->assertFalse($parent_storage
->rename($name, 'config_test.another_name'));
$this
->assertSame($data, $this->storage
->read($name));
$this->storage
->delete($name);
$this
->assertSame([
'collection.sub.another',
'collection.sub.new',
], $this->storage
->getAllCollectionNames());
}
protected abstract function read($name);
protected abstract function insert($name, $data);
protected abstract function update($name, $data);
protected abstract function delete($name);
}