Skip to content
Snippets Groups Projects
Commit 57ad987f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2756307 by Fabianx, dawehner: Provide a setting to disable FileCache...

Issue #2756307 by Fabianx, dawehner: Provide a setting to disable FileCache completely for unit tests
parent 9a0f369b
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,11 @@
*/
class FileCacheFactory {
/**
* The configuration key to disable FileCache completely.
*/
const DISABLE_CACHE = 'file_cache_disable';
/**
* The configuration used to create FileCache objects.
*
......@@ -34,6 +39,11 @@ class FileCacheFactory {
* The initialized FileCache object.
*/
public static function get($collection, $default_configuration = []) {
// If there is a special key in the configuration, disable FileCache completely.
if (!empty(static::$configuration[static::DISABLE_CACHE])) {
return new NullFileCache('', '');
}
$default_configuration += [
'class' => '\Drupal\Component\FileCache\FileCache',
'collection' => $collection,
......
......@@ -2,6 +2,8 @@
namespace Drupal\Tests\Component\FileCache;
use Drupal\Component\FileCache\FileCache;
use Drupal\Component\FileCache\NullFileCache;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Tests\UnitTestCase;
......@@ -17,18 +19,15 @@ class FileCacheFactoryTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$settings = [
'collection' => 'test-23',
'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend',
'cache_backend_configuration' => [
'bin' => 'dog',
$configuration = [
'test_foo_settings' => [
'collection' => 'test-23',
'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend',
'cache_backend_configuration' => [
'bin' => 'dog',
],
],
];
$configuration = FileCacheFactory::getConfiguration();
if (!$configuration) {
$configuration = [];
}
$configuration += [ 'test_foo_settings' => $settings ];
FileCacheFactory::setConfiguration($configuration);
FileCacheFactory::setPrefix('prefix');
}
......@@ -65,6 +64,24 @@ public function testGetNoPrefix() {
FileCacheFactory::get('test_foo_settings', []);
}
/**
* @covers ::get
*/
public function testGetDisabledFileCache() {
// Ensure the returned FileCache is an instance of FileCache::class.
$file_cache = FileCacheFactory::get('test_foo_settings', []);
$this->assertInstanceOf(FileCache::class, $file_cache);
$configuration = FileCacheFactory::getConfiguration();
$configuration[FileCacheFactory::DISABLE_CACHE] = TRUE;
FileCacheFactory::setConfiguration($configuration);
// Ensure the returned FileCache is now an instance of NullFileCache::class.
$file_cache = FileCacheFactory::get('test_foo_settings', []);
$this->assertInstanceOf(NullFileCache::class, $file_cache);
}
/**
* @covers ::getConfiguration
* @covers ::setConfiguration
......
......@@ -42,7 +42,7 @@ protected function setUp() {
// Ensure that the NullFileCache implementation is used for the FileCache as
// unit tests should not be relying on caches implicitly.
FileCacheFactory::setConfiguration(['default' => ['class' => '\Drupal\Component\FileCache\NullFileCache']]);
FileCacheFactory::setConfiguration([FileCacheFactory::DISABLE_CACHE => TRUE]);
// Ensure that FileCacheFactory has a prefix.
FileCacheFactory::setPrefix('prefix');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment