From 57ad987f45761c7f6df868f716826d6b28dac8f0 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 30 Jun 2016 18:11:40 +0100
Subject: [PATCH] Issue #2756307 by Fabianx, dawehner: Provide a setting to
 disable FileCache completely for unit tests

---
 .../Component/FileCache/FileCacheFactory.php  | 10 +++++
 .../FileCache/FileCacheFactoryTest.php        | 37 ++++++++++++++-----
 core/tests/Drupal/Tests/UnitTestCase.php      |  2 +-
 3 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php
index d0860b425c37..60cfea511272 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 2b1edb23df48..327ef2750dad 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 d8fbebcb2948..085181c3a7b6 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');
 
-- 
GitLab