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

8.0.x
Nathaniel Catchpole 2014-05-12 16:52:26 +01:00
parent e67b313df4
commit 5be9d3e379
2 changed files with 139 additions and 30 deletions

View File

@ -0,0 +1,101 @@
<?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());
}
}

View File

@ -23,6 +23,11 @@ use Drupal\simpletest\DrupalUnitTestBase;
*/
abstract class ConfigStorageTestBase extends DrupalUnitTestBase {
/**
* @var \Drupal\Core\Config\StorageInterface;
*/
protected $storage;
/**
* Tests storage CRUD operations.
*
@ -38,17 +43,6 @@ abstract class ConfigStorageTestBase extends DrupalUnitTestBase {
$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 @@ abstract class ConfigStorageTestBase extends DrupalUnitTestBase {
$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,17 +100,6 @@ abstract class ConfigStorageTestBase extends DrupalUnitTestBase {
$this->assertIdentical($result, TRUE);
$this->assertIdentical($names, array());
// Deleting from a non-existing storage bin throws an exception.
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.');
}
// Test renaming an object that does not exist throws an exception.
try {
$this->storage->rename('config_test.storage_does_not_exist', 'config_test.storage_does_not_exist_rename');
@ -142,7 +117,40 @@ abstract class ConfigStorageTestBase extends DrupalUnitTestBase {
$class = get_class($e);
$this->pass($class . ' thrown upon renaming a nonexistent storage bin.');
}
}
/**
* 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->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.');
}
// 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);