summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-07-19 16:09:04 (GMT)
committerAlex Pott2016-07-19 16:09:04 (GMT)
commitb5b6b8fc8423f8ba46021f7d21740248cd79d1fa (patch)
treeb17bced816671a640b3b18b52f6da4688836e315
parentd343e3aa8799c2d312519b065b35b8f2b3a5e1bb (diff)
Issue #2756175 by Fabianx: FileCacheFactory::get() should not use 'default' configuration key before passed in configuration
-rw-r--r--core/lib/Drupal/Component/FileCache/FileCacheFactory.php29
-rw-r--r--core/lib/Drupal/Core/DrupalKernel.php5
-rw-r--r--core/tests/Drupal/KernelTests/KernelTestBase.php5
-rw-r--r--core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php73
4 files changed, 91 insertions, 21 deletions
diff --git a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php
index 60cfea5..15b2026 100644
--- a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php
+++ b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php
@@ -44,23 +44,30 @@ class FileCacheFactory {
return new NullFileCache('', '');
}
- $default_configuration += [
+ $configuration = [];
+
+ // Check for a collection specific setting first.
+ if (isset(static::$configuration[$collection])) {
+ $configuration += static::$configuration[$collection];
+ }
+ // Then check if a default configuration has been provided.
+ if (!empty($default_configuration)) {
+ $configuration += $default_configuration;
+ }
+ // Last check if a default setting has been provided.
+ if (isset(static::$configuration['default'])) {
+ $configuration += static::$configuration['default'];
+ }
+
+ // Ensure that all properties are set.
+ $fallback_configuration = [
'class' => '\Drupal\Component\FileCache\FileCache',
'collection' => $collection,
'cache_backend_class' => NULL,
'cache_backend_configuration' => [],
];
- $configuration = [];
- if (isset(static::$configuration[$collection])) {
- $configuration = static::$configuration[$collection];
- }
- elseif (isset(static::$configuration['default'])) {
- $configuration = static::$configuration['default'];
- }
-
- // Add defaults to the configuration.
- $configuration = $configuration + $default_configuration;
+ $configuration = $configuration + $fallback_configuration;
$class = $configuration['class'];
return new $class(static::getPrefix(), $configuration['collection'], $configuration['cache_backend_class'], $configuration['cache_backend_configuration']);
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 1c8068b..3ccb2d7 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -443,11 +443,6 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
// Provide a default configuration, if not set.
if (!isset($configuration['default'])) {
- $configuration['default'] = [
- 'class' => '\Drupal\Component\FileCache\FileCache',
- 'cache_backend_class' => NULL,
- 'cache_backend_configuration' => [],
- ];
// @todo Use extension_loaded('apcu') for non-testbot
// https://www.drupal.org/node/2447753.
if (function_exists('apcu_fetch')) {
diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php
index ed3875d..ebe6b87 100644
--- a/core/tests/Drupal/KernelTests/KernelTestBase.php
+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -543,11 +543,6 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
// Provide a default configuration, if not set.
if (!isset($configuration['default'])) {
- $configuration['default'] = [
- 'class' => FileCache::class,
- 'cache_backend_class' => NULL,
- 'cache_backend_configuration' => [],
- ];
// @todo Use extension_loaded('apcu') for non-testbot
// https://www.drupal.org/node/2447753.
if (function_exists('apcu_fetch')) {
diff --git a/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php b/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php
index 327ef27..b4d8de9 100644
--- a/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php
+++ b/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php
@@ -81,6 +81,79 @@ class FileCacheFactoryTest extends UnitTestCase {
$this->assertInstanceOf(NullFileCache::class, $file_cache);
}
+ /**
+ * @covers ::get
+ *
+ * @dataProvider configurationDataProvider
+ */
+ public function testGetConfigurationOverrides($configuration, $arguments, $class) {
+ FileCacheFactory::setConfiguration($configuration);
+
+ $file_cache = FileCacheFactory::get('test_foo_settings', $arguments);
+ $this->assertInstanceOf($class, $file_cache);
+ }
+
+ /**
+ * Data provider for testGetConfigurationOverrides().
+ */
+ public function configurationDataProvider() {
+ $data = [];
+
+ // Get a unique FileCache class.
+ $file_cache = $this->getMockBuilder(FileCache::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $class = get_class($file_cache);
+
+ // Test fallback configuration.
+ $data['fallback-configuration'] = [[
+ ], [], FileCache::class];
+
+ // Test default configuration.
+ $data['default-configuration'] = [[
+ 'default' => [
+ 'class' => $class,
+ ],
+ ], [], $class];
+
+ // Test specific per collection setting.
+ $data['collection-setting'] = [[
+ 'test_foo_settings' => [
+ 'class' => $class,
+ ],
+ ], [], $class];
+
+
+ // Test default configuration plus specific per collection setting.
+ $data['default-plus-collection-setting'] = [[
+ 'default' => [
+ 'class' => '\stdClass',
+ ],
+ 'test_foo_settings' => [
+ 'class' => $class,
+ ],
+ ], [], $class];
+
+ // Test default configuration plus class specific override.
+ $data['default-plus-class-override'] = [[
+ 'default' => [
+ 'class' => '\stdClass',
+ ],
+ ], [ 'class' => $class ], $class];
+
+ // Test default configuration plus class specific override plus specific
+ // per collection setting.
+ $data['default-plus-class-plus-collection-setting'] = [[
+ 'default' => [
+ 'class' => '\stdClass',
+ ],
+ 'test_foo_settings' => [
+ 'class' => $class,
+ ],
+ ], [ 'class' => '\stdClass'], $class];
+
+ return $data;
+ }
/**
* @covers ::getConfiguration