core/lib/Drupal/Core/Config/StorableConfigBase.php | 8 +- .../language/src/Config/LanguageConfigOverride.php | 7 ++ .../src/Unit/Config/LanguageConfigOverrideTest.php | 104 +++++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Config/StorableConfigBase.php b/core/lib/Drupal/Core/Config/StorableConfigBase.php index 505a1bf..f1ef811 100644 --- a/core/lib/Drupal/Core/Config/StorableConfigBase.php +++ b/core/lib/Drupal/Core/Config/StorableConfigBase.php @@ -66,7 +66,9 @@ /** * Saves the configuration object. * - * @return \Drupal\Core\Config\Config + * Must invalidate the cache tags associated with the configuration object. + * + * @return $this * The configuration object. */ abstract public function save(); @@ -74,7 +76,9 @@ /** * Deletes the configuration object. * - * @return \Drupal\Core\Config\Config + * Must invalidate the cache tags associated with the configuration object. + * + * @return $this * The configuration object. */ abstract public function delete(); diff --git a/core/modules/language/src/Config/LanguageConfigOverride.php b/core/modules/language/src/Config/LanguageConfigOverride.php index d64dda7..5be48ad 100644 --- a/core/modules/language/src/Config/LanguageConfigOverride.php +++ b/core/modules/language/src/Config/LanguageConfigOverride.php @@ -7,6 +7,7 @@ namespace Drupal\language\Config; +use Drupal\Core\Cache\Cache; use Drupal\Core\Config\StorableConfigBase; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Config\TypedConfigManagerInterface; @@ -57,6 +58,11 @@ public function save() { $this->validateValue($key, $value); } $this->storage->write($this->name, $this->data); + // Invalidate the cache tags not only when updating, but also when creating, + // because a language config override object uses the same cache tag as the + // default configuration object. Hence creating a language override is like + // an update of configuration, but only for a specific language. + Cache::invalidateTags($this->getCacheTags()); $this->isNew = FALSE; $this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::SAVE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this)); $this->originalData = $this->data; @@ -69,6 +75,7 @@ public function save() { public function delete() { $this->data = array(); $this->storage->delete($this->name); + Cache::invalidateTags($this->getCacheTags()); $this->isNew = TRUE; $this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::DELETE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this)); $this->originalData = $this->data; diff --git a/core/modules/language/tests/src/Unit/Config/LanguageConfigOverrideTest.php b/core/modules/language/tests/src/Unit/Config/LanguageConfigOverrideTest.php new file mode 100644 index 0000000..0c979d9 --- /dev/null +++ b/core/modules/language/tests/src/Unit/Config/LanguageConfigOverrideTest.php @@ -0,0 +1,104 @@ +storage = $this->getMock('Drupal\Core\Config\StorageInterface'); + $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->typedConfig = $this->getMock('\Drupal\Core\Config\TypedConfigManagerInterface'); + $this->configTranslation = new LanguageConfigOverride('config.test', $this->storage, $this->typedConfig, $this->eventDispatcher); + $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); + + $container = new ContainerBuilder(); + $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); + \Drupal::setContainer($container); + } + + /** + * @covers ::save + */ + public function testSaveNew() { + $this->cacheTagsInvalidator->expects($this->once()) + ->method('invalidateTags') + ->with(['config:config.test']); + $this->assertTrue($this->configTranslation->isNew()); + $this->configTranslation->save(); + } + + /** + * @covers ::save + */ + public function testSaveExisting() { + $this->cacheTagsInvalidator->expects($this->once()) + ->method('invalidateTags') + ->with(['config:config.test']); + $this->configTranslation->initWithData([]); + $this->configTranslation->save(); + } + + /** + * @covers ::delete + */ + public function testDelete() { + $this->cacheTagsInvalidator->expects($this->once()) + ->method('invalidateTags') + ->with(['config:config.test']); + $this->configTranslation->initWithData([]); + $this->configTranslation->delete(); + } + +}