diff --git a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php index d0860b425c37d34fea62da2982192cf743d42264..60cfea511272daeb190b02847f6eddac1002efb1 100644 --- a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php +++ b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php @@ -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, diff --git a/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php b/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php index 2b1edb23df481829b2aed1c08f96bd5b99451934..327ef2750dad4c2e83ca8d1a54f18e7b49fed8cf 100644 --- a/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php +++ b/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php @@ -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 diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index d8fbebcb294875081c32fd12e3ee31a1c02928a1..085181c3a7b6debbb5f76a302187ee5e1e13d89f 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -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');