diff --git a/core/lib/Drupal/Core/Cache/CacheFactory.php b/core/lib/Drupal/Core/Cache/CacheFactory.php index 0a0a977..5a16d3c 100644 --- a/core/lib/Drupal/Core/Cache/CacheFactory.php +++ b/core/lib/Drupal/Core/Cache/CacheFactory.php @@ -70,10 +70,10 @@ public function __construct(Settings $settings, array $default_bin_backends = ar */ public function get($bin) { $cache_settings = $this->settings->get('cache'); - if (isset($cache_settings['bins'][$bin])) { + if (isset($cache_settings['bins'][$bin]) && $this->container->has($cache_settings['bins'][$bin])) { $service_name = $cache_settings['bins'][$bin]; } - elseif (isset($cache_settings['default'])) { + elseif (isset($cache_settings['default']) && $this->container->has($cache_settings['default'])) { $service_name = $cache_settings['default']; } elseif (isset($this->defaultBinBackends[$bin])) { diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php index 4869bea..7cbe2e1 100644 --- a/core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php @@ -106,4 +106,66 @@ public function testCacheFactoryWithSpecifiedPerBinBackend() { $this->assertSame($render_bin, $actual_bin); } + /** + * Test that the cache factory falls back to the built-in default service if specified bin service does not exist. + * + * @covers ::__construct + * @covers ::get + */ + public function testCacheFactoryBinBackendServiceExist() { + $settings = new Settings(array( + 'cache' => array( + 'bins' => array( + 'render' => 'cache.backend.custom', + ), + ), + )); + $cache_factory = new CacheFactory($settings); + + $container = new ContainerBuilder(); + $cache_factory->setContainer($container); + + $builtin_default_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface'); + $container->set('cache.backend.database', $builtin_default_backend_factory); + + $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface'); + $builtin_default_backend_factory->expects($this->once()) + ->method('get') + ->with('render') + ->will($this->returnValue($render_bin)); + + $actual_bin = $cache_factory->get('render'); + $this->assertSame($render_bin, $actual_bin); + } + + /** + * Test that the cache factory falls back to the built-in default service if specified default bin service does not exist. + * + * @covers ::__construct + * @covers ::get + */ + public function testCacheFactoryBinBackendDefaultServiceExist() { + $settings = new Settings(array( + 'cache' => array( + 'default' => 'cache.backend.custom', + ), + )); + $cache_factory = new CacheFactory($settings); + + $container = new ContainerBuilder(); + $cache_factory->setContainer($container); + + $builtin_default_backend_factory = $this->getMock('\Drupal\Core\Cache\CacheFactoryInterface'); + $container->set('cache.backend.database', $builtin_default_backend_factory); + + $render_bin = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface'); + $builtin_default_backend_factory->expects($this->once()) + ->method('get') + ->with('render') + ->will($this->returnValue($render_bin)); + + $actual_bin = $cache_factory->get('render'); + $this->assertSame($render_bin, $actual_bin); + } + }