diff --git a/core/modules/config/lib/Drupal/config/Tests/Storage/CachedStorageTest.php b/core/modules/config/lib/Drupal/config/Tests/Storage/CachedStorageTest.php new file mode 100644 index 0000000..cb00a1e --- /dev/null +++ b/core/modules/config/lib/Drupal/config/Tests/Storage/CachedStorageTest.php @@ -0,0 +1,101 @@ + '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()); + } + +} diff --git a/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php index 925ecbd..c112ccd 100644 --- a/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php +++ b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php @@ -24,6 +24,11 @@ abstract class ConfigStorageTestBase extends DrupalUnitTestBase { /** + * @var \Drupal\Core\Config\StorageInterface; + */ + protected $storage; + + /** * Tests storage CRUD operations. * * @todo Coverage: Trigger PDOExceptions / Database exceptions. @@ -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);