diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index 584feb7c53..4bd3925932 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -141,6 +141,26 @@ public function setModuleOverride(array $data) { return $this; } + /** + * Returns whether overrides apply to this configuration or not. + * + * Simply comparing the the data from getOriginal() with and without overrides + * does not help to detect overrides if the override happens to have the same + * value as the original data. + * + * @return bool + * True if overrides apply to this configuration, false otherwise. + */ + public function hasOverrides() { + if (isset($this->moduleOverrides) && !empty($this->moduleOverrides)) { + return TRUE; + } + if (isset($this->settingsOverrides) && !empty($this->settingsOverrides)) { + return TRUE; + } + return FALSE; + } + /** * Sets the current data for this configuration object. * diff --git a/core/tests/Drupal/Tests/Core/Config/ConfigTest.php b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php index 41c3e93c99..746e620917 100644 --- a/core/tests/Drupal/Tests/Core/Config/ConfigTest.php +++ b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php @@ -185,25 +185,38 @@ public function testOverrideData($data, $module_data, $setting_data) { // Save so that the original data is stored. $this->config->save(); + // Before overrides are set the configuration should indicate that. + $this->assertFalse($this->config->hasOverrides()); + // Set module override data and check value before and after save. $this->config->setModuleOverride($module_data); $this->assertConfigDataEquals($module_data); + $this->assertTrue($this->config->hasOverrides()); $this->config->save(); $this->assertConfigDataEquals($module_data); + $this->assertTrue($this->config->hasOverrides()); + + // Reset the module overrides. + $this->config->setModuleOverride([]); + $this->assertFalse($this->config->hasOverrides()); // Set setting override data and check value before and after save. $this->config->setSettingsOverride($setting_data); $this->assertConfigDataEquals($setting_data); + $this->assertTrue($this->config->hasOverrides()); $this->config->save(); $this->assertConfigDataEquals($setting_data); + $this->assertTrue($this->config->hasOverrides()); // Set module overrides again to ensure override order is correct. $this->config->setModuleOverride($module_data); // Setting data should be overriding module data. $this->assertConfigDataEquals($setting_data); + $this->assertTrue($this->config->hasOverrides()); $this->config->save(); $this->assertConfigDataEquals($setting_data); + $this->assertTrue($this->config->hasOverrides()); // Check original data has not changed. $this->assertOriginalConfigDataEquals($data, FALSE); @@ -216,6 +229,15 @@ public function testOverrideData($data, $module_data, $setting_data) { $config_value = $this->config->getOriginal($key); $this->assertEquals($value, $config_value); } + + // Check that the overrides can be completely reset. + $this->config->setModuleOverride([]); + $this->config->setSettingsOverride([]); + $this->assertConfigDataEquals($data); + $this->assertFalse($this->config->hasOverrides()); + $this->config->save(); + $this->assertConfigDataEquals($data); + $this->assertFalse($this->config->hasOverrides()); } /**