View source
<?php
namespace Drupal\config\Tests\Storage;
use Drupal\simpletest\KernelTestBase;
abstract class ConfigStorageTestBase extends KernelTestBase {
protected $storage;
protected $invalidStorage;
function testCRUD() {
$name = 'config_test.storage';
$this
->assertIdentical($this->storage
->exists($name), FALSE);
$data = $this->storage
->read($name);
$this
->assertIdentical($data, FALSE);
$data = array(
'foo' => 'bar',
);
$result = $this->storage
->write($name, $data);
$this
->assertIdentical($result, TRUE);
$raw_data = $this
->read($name);
$this
->assertIdentical($raw_data, $data);
$this
->assertIdentical($this->storage
->exists($name), TRUE);
$result = $this->storage
->write($name, $data);
$this
->assertIdentical($result, TRUE);
$names = $this->storage
->listAll();
$this
->assertTrue(in_array('system.performance', $names));
$this
->assertTrue(in_array($name, $names));
$names = $this->storage
->listAll('config_test.');
$this
->assertFalse(in_array('system.performance', $names));
$this
->assertTrue(in_array($name, $names));
$new_name = 'config_test.storage_rename';
$this->storage
->rename($name, $new_name);
$raw_data = $this
->read($new_name);
$this
->assertIdentical($raw_data, $data);
$this->storage
->rename($new_name, $name);
$result = $this->storage
->delete($name);
$this
->assertIdentical($result, TRUE);
$result = $this->storage
->delete($name);
$this
->assertIdentical($result, FALSE);
$files = array(
'config_test.test.biff',
'config_test.test.bang',
'config_test.test.pow',
);
foreach ($files as $name) {
$this->storage
->write($name, $data);
}
$result = $this->storage
->deleteAll('config_test.');
$names = $this->storage
->listAll('config_test.');
$this
->assertIdentical($result, TRUE);
$this
->assertIdentical($names, array());
try {
$this->storage
->rename('config_test.storage_does_not_exist', 'config_test.storage_does_not_exist_rename');
} catch (\Exception $e) {
$class = get_class($e);
$this
->pass($class . ' thrown upon renaming a nonexistent storage bin.');
}
try {
$this->storage
->rename('system.cron', 'system.performance');
} catch (\Exception $e) {
$class = get_class($e);
$this
->pass($class . ' thrown upon renaming a nonexistent storage bin.');
}
}
public function testInvalidStorage() {
$name = 'config_test.storage';
$data = array(
'foo' => 'bar',
);
$result = $this->storage
->write($name, $data);
$this
->assertIdentical($result, TRUE);
$raw_data = $this
->read($name);
$this
->assertIdentical($raw_data, $data);
$result = $this->invalidStorage
->read($name);
$this
->assertIdentical($result, FALSE);
try {
$this->invalidStorage
->delete($name);
$this
->fail('Exception not thrown upon deleting from a non-existing storage bin.');
} catch (\Exception $e) {
$class = get_class($e);
$this
->pass($class . ' thrown upon deleting from a non-existing storage bin.');
}
$result = $this->invalidStorage
->listAll();
$this
->assertIdentical($result, array());
$this->invalidStorage
->write($name, array(
'foo' => 'bar',
));
$result = $this->invalidStorage
->read($name);
$this
->assertIdentical($result, array(
'foo' => 'bar',
));
}
function testDataTypes() {
$name = 'config_test.types';
$data = array(
'array' => 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
->assertIdentical($result, TRUE);
$read_data = $this->storage
->read($name);
$this
->assertIdentical($read_data, $data);
}
public function testCollection() {
$name = 'config_test.storage';
$data = array(
'foo' => 'bar',
);
$result = $this->storage
->write($name, $data);
$this
->assertIdentical($result, TRUE);
$this
->assertIdentical($data, $this->storage
->read($name));
$new_storage = $this->storage
->createCollection('collection.sub.new');
$this
->assertFalse($new_storage
->exists($name));
$this
->assertEqual(array(), $new_storage
->listAll());
$new_storage
->write($name, $data);
$this
->assertIdentical($result, TRUE);
$this
->assertIdentical($data, $new_storage
->read($name));
$this
->assertEqual(array(
$name,
), $new_storage
->listAll());
$this
->assertTrue($new_storage
->exists($name));
$new_data = array(
'foo' => 'baz',
);
$new_storage
->write($name, $new_data);
$this
->assertIdentical($result, TRUE);
$this
->assertIdentical($new_data, $new_storage
->read($name));
$another_storage = $this->storage
->createCollection('collection.sub.another');
$this
->assertFalse($another_storage
->exists($name));
$this
->assertEqual(array(), $another_storage
->listAll());
$another_storage
->write($name, $new_data);
$this
->assertIdentical($result, TRUE);
$this
->assertIdentical($new_data, $another_storage
->read($name));
$this
->assertEqual(array(
$name,
), $another_storage
->listAll());
$this
->assertTrue($another_storage
->exists($name));
$alt_storage = $this->storage
->createCollection('alternate');
$alt_storage
->write($name, $new_data);
$this
->assertIdentical($result, TRUE);
$this
->assertIdentical($new_data, $alt_storage
->read($name));
$this
->assertIdentical($data, $this->storage
->read($name));
$this
->assertIdentical(array(
'alternate',
'collection.sub.another',
'collection.sub.new',
), $this->storage
->getAllCollectionNames());
$alt_storage
->delete($name);
$this
->assertIdentical(array(
'collection.sub.another',
'collection.sub.new',
), $this->storage
->getAllCollectionNames());
$parent_storage = $this->storage
->createCollection('collection');
$this
->assertFalse($parent_storage
->exists($name));
$this
->assertEqual(array(), $parent_storage
->listAll());
$parent_storage
->write($name, $new_data);
$this
->assertIdentical($result, TRUE);
$this
->assertIdentical($new_data, $parent_storage
->read($name));
$this
->assertEqual(array(
$name,
), $parent_storage
->listAll());
$this
->assertTrue($parent_storage
->exists($name));
$this
->assertIdentical(array(
'collection',
'collection.sub.another',
'collection.sub.new',
), $this->storage
->getAllCollectionNames());
$parent_storage
->deleteAll();
$this
->assertIdentical(array(
'collection.sub.another',
'collection.sub.new',
), $this->storage
->getAllCollectionNames());
$this
->assertIdentical($data, $this->storage
->read($name));
$this->storage
->delete($name);
$this
->assertIdentical(array(
'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);
}