Commit 5be9d3e3 authored by catch's avatar catch

Issue #2263287 by alexpott: Test the CachedStorage class using ConfigStorageTestBase.

parent e67b313d
<?php
/**
* @file
* Definition of Drupal\config\Tests\Storage\CachedStorageTest.
*/
namespace Drupal\config\Tests\Storage;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\CachedStorage;
use Drupal\Core\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder;
/**
* Tests CachedStorage operations.
*/
class CachedStorageTest extends ConfigStorageTestBase {
/**
* The cache backend the cached storage is using.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* The file storage the cached storage is using.
*
* @var \Drupal\Core\Config\FileStorage
*/
protected $filestorage;
public static function getInfo() {
return array(
'name' => 'CachedStorage operations',
'description' => 'Tests CachedStorage operations.',
'group' => 'Configuration',
);
}
function setUp() {
parent::setUp();
$this->filestorage = new FileStorage($this->configDirectories[CONFIG_ACTIVE_DIRECTORY]);
$this->cache = \Drupal::service('cache_factory')->get('config');
$this->storage = new CachedStorage($this->filestorage, $this->cache);
// ::listAll() verifications require other configuration data to exist.
$this->storage->write('system.performance', array());
}
/**
* {@inheritdoc}
*/
public function testInvalidStorage() {
// No-op as this test does not make sense.
}
/**
* {@inheritdoc}
*/
protected function read($name) {
$data = $this->cache->get($name);
// Cache misses fall through to the underlying storage.
return $data ? $data->data : $this->filestorage->read($name);
}
/**
* {@inheritdoc}
*/
protected function insert($name, $data) {
$this->filestorage->write($name, $data);
$this->cache->set($name, $data);
}
/**
* {@inheritdoc}
*/
protected function update($name, $data) {
$this->filestorage->write($name, $data);
$this->cache->set($name, $data);
}
/**
* {@inheritdoc}
*/
protected function delete($name) {
$this->cache->delete($name);
unlink($this->filestorage->getFilePath($name));
}
/**
* {@inheritdoc}
*/
public function containerBuild(ContainerBuilder $container) {
parent::containerBuild($container);
// Use the regular database cache backend to aid testing.
$container->register('cache_factory', 'Drupal\Core\Cache\DatabaseBackendFactory')
->addArgument(Database::getConnection());
}
}
......@@ -23,6 +23,11 @@
*/
abstract class ConfigStorageTestBase extends DrupalUnitTestBase {
/**
* @var \Drupal\Core\Config\StorageInterface;
*/
protected $storage;
/**
* Tests storage CRUD operations.
*
......@@ -38,17 +43,6 @@ function testCRUD() {
$data = $this->storage->read($name);
$this->assertIdentical($data, FALSE);
// Reading a name containing non-decodeable data returns FALSE.
$this->insert($name, '');
$data = $this->storage->read($name);
$this->assertIdentical($data, FALSE);
$this->update($name, 'foo');
$data = $this->storage->read($name);
$this->assertIdentical($data, FALSE);
$this->delete($name);
// Writing data returns TRUE and the data has been written.
$data = array('foo' => 'bar');
$result = $this->storage->write($name, $data);
......@@ -90,14 +84,6 @@ function testCRUD() {
$result = $this->storage->delete($name);
$this->assertIdentical($result, FALSE);
// Reading from a non-existing storage bin returns FALSE.
$result = $this->invalidStorage->read($name);
$this->assertIdentical($result, FALSE);
// Listing on a non-existing storage bin returns an empty array.
$result = $this->invalidStorage->listAll();
$this->assertIdentical($result, array());
// Deleting all names with prefix deletes the appropriate data and returns
// TRUE.
$files = array(
......@@ -114,35 +100,57 @@ function testCRUD() {
$this->assertIdentical($result, TRUE);
$this->assertIdentical($names, array());
// Deleting from a non-existing storage bin throws an exception.
// Test renaming an object that does not exist throws an exception.
try {
$this->invalidStorage->delete($name);
$this->fail('Exception not thrown upon deleting from a non-existing storage bin.');
$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 deleting from a non-existing storage bin.');
$this->pass($class . ' thrown upon renaming a nonexistent storage bin.');
}
// Test renaming an object that does not exist throws an exception.
// Test renaming to an object that already exists throws an exception.
try {
$this->storage->rename('config_test.storage_does_not_exist', 'config_test.storage_does_not_exist_rename');
$this->storage->rename('system.cron', 'system.performance');
}
catch (\Exception $e) {
$class = get_class($e);
$this->pass($class . ' thrown upon renaming a nonexistent storage bin.');
}
}
// Test renaming to an object that already exists throws an exception.
/**
* Tests an invalid storage.
*/
public function testInvalidStorage() {
$name = 'config_test.storage';
// Write something to the valid storage to prove that the storages do not
// pollute one another.
$data = array('foo' => 'bar');
$result = $this->storage->write($name, $data);
$this->assertIdentical($result, TRUE);
$raw_data = $this->read($name);
$this->assertIdentical($raw_data, $data);
// Reading from a non-existing storage bin returns FALSE.
$result = $this->invalidStorage->read($name);
$this->assertIdentical($result, FALSE);
// Deleting from a non-existing storage bin throws an exception.
try {
$this->storage->rename('system.cron', 'system.performance');
$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 renaming a nonexistent storage bin.');
$this->pass($class . ' thrown upon deleting from a non-existing storage bin.');
}
// Listing on a non-existing storage bin returns an empty array.
$result = $this->invalidStorage->listAll();
$this->assertIdentical($result, array());
// Writing to a non-existing storage bin creates the bin.
$this->invalidStorage->write($name, array('foo' => 'bar'));
$result = $this->invalidStorage->read($name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment