Commit b5b6b8fc authored by alexpott's avatar alexpott

Issue #2756175 by Fabianx: FileCacheFactory::get() should not use 'default'...

Issue #2756175 by Fabianx: FileCacheFactory::get() should not use 'default' configuration key before passed in configuration
parent d343e3aa
......@@ -44,23 +44,30 @@ public static function get($collection, $default_configuration = []) {
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']);
......
......@@ -443,11 +443,6 @@ public function boot() {
// 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')) {
......
......@@ -543,11 +543,6 @@ protected function initFileCache() {
// 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')) {
......
......@@ -81,6 +81,79 @@ public function testGetDisabledFileCache() {
$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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment