diff --git a/core/lib/Drupal/Core/Config/ImmutableConfig.php b/core/lib/Drupal/Core/Config/ImmutableConfig.php index 6f8d6ed..4e0d7fe 100644 --- a/core/lib/Drupal/Core/Config/ImmutableConfig.php +++ b/core/lib/Drupal/Core/Config/ImmutableConfig.php @@ -45,7 +45,7 @@ public function clear($key) { * {@inheritdoc} */ public function save() { - throw new ImmutableConfigException(String::format('Can not save immutable configuration !name', ['!name' => $this->getName(), '!key' => $key])); + throw new ImmutableConfigException(String::format('Can not save immutable configuration !name', ['!name' => $this->getName()])); } /** @@ -55,7 +55,7 @@ public function save() { * The configuration object. */ public function delete() { - throw new ImmutableConfigException(String::format('Can not delete immutable configuration !name', ['!name' => $this->getName(), '!key' => $key])); + throw new ImmutableConfigException(String::format('Can not delete immutable configuration !name', ['!name' => $this->getName()])); } } diff --git a/core/lib/Drupal/Core/Form/ConfigFormBaseTrait.php b/core/lib/Drupal/Core/Form/ConfigFormBaseTrait.php index c3024b0..11249f8 100644 --- a/core/lib/Drupal/Core/Form/ConfigFormBaseTrait.php +++ b/core/lib/Drupal/Core/Form/ConfigFormBaseTrait.php @@ -49,12 +49,11 @@ protected function config($name) { if (method_exists($this, 'configFactory')) { $config_factory = $this->configFactory(); } - elseif (property_exists($this, 'configFactory') && $this->configFactory instanceof ConfigFactoryInterface) { + elseif (property_exists($this, 'configFactory')) { $config_factory = $this->configFactory; } - else { - // @todo throw a better exception - throw new \RuntimeException('No config factory available for ConfigFormBaseTrait'); + if (!isset($config_factory) || !($config_factory instanceof ConfigFactoryInterface)) { + throw new \LogicException('No config factory available for ConfigFormBaseTrait'); } if (in_array($name, $this->getEditableConfigNames())) { // Get a mutable object from the factory. diff --git a/core/tests/Drupal/Tests/Core/Config/ImmutableConfigTest.php b/core/tests/Drupal/Tests/Core/Config/ImmutableConfigTest.php new file mode 100644 index 0000000..ff17640 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/ImmutableConfigTest.php @@ -0,0 +1,70 @@ +getMock('Drupal\Core\Config\StorageInterface'); + $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $typed_config = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'); + $this->config = new ImmutableConfig('test', $storage, $event_dispatcher, $typed_config); + } + + /** + * @covers ::set + * @expectedException \Drupal\Core\Config\ImmutableConfigException + * @expectedExceptionMessage Can not set values on immutable configuration test:name + */ + public function testSet() { + $this->config->set('name', 'value'); + } + + /** + * @covers ::clear + * @expectedException \Drupal\Core\Config\ImmutableConfigException + * @expectedExceptionMessage Can not clear name key in immutable configuration test + */ + public function testClear() { + $this->config->clear('name'); + } + + /** + * @covers ::save + * @expectedException \Drupal\Core\Config\ImmutableConfigException + * @expectedExceptionMessage Can not save immutable configuration test + */ + public function testSave() { + $this->config->save(); + } + + /** + * @covers ::delete + * @expectedException \Drupal\Core\Config\ImmutableConfigException + * @expectedExceptionMessage Can not delete immutable configuration test + */ + public function testDelete() { + $this->config->delete(); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Form/ConfigFormBaseTraitTest.php b/core/tests/Drupal/Tests/Core/Form/ConfigFormBaseTraitTest.php new file mode 100644 index 0000000..443646b --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Form/ConfigFormBaseTraitTest.php @@ -0,0 +1,76 @@ +getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait'); + // Set up some configuration in a mocked config factory. + $trait->configFactory = $this->getConfigFactoryStub([ + 'editable.config' => [], + 'immutable.config' => [] + ]); + + $trait->expects($this->any()) + ->method('getEditableConfigNames') + ->willReturn(['editable.config']); + + $config_method = new \ReflectionMethod($trait, 'config'); + $config_method->setAccessible(TRUE); + + // Ensure that configuration that is expected to be mutable is. + $result = $config_method->invoke($trait, 'editable.config'); + $this->assertInstanceOf('\Drupal\Core\Config\Config', $result); + $this->assertNotInstanceOf('\Drupal\Core\Config\ImmutableConfig', $result); + + // Ensure that configuration that is expected to be immutable is. + $result = $config_method->invoke($trait, 'immutable.config'); + $this->assertInstanceOf('\Drupal\Core\Config\ImmutableConfig', $result); + } + + /** + * @covers ::config + * @expectedException \LogicException + * @expectedExceptionMessage No config factory available for ConfigFormBaseTrait + */ + public function testConfigFactoryException() { + $trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait'); + $config_method = new \ReflectionMethod($trait, 'config'); + $config_method->setAccessible(TRUE); + + // There is no config factory available this should result in an exception. + $config_method->invoke($trait, 'editable.config'); + } + + /** + * @covers ::config + * @expectedException \LogicException + * @expectedExceptionMessage No config factory available for ConfigFormBaseTrait + */ + public function testConfigFactoryExceptionInvalidProperty() { + $trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait'); + $trait->configFactory = TRUE; + $config_method = new \ReflectionMethod($trait, 'config'); + $config_method->setAccessible(TRUE); + + // There is no config factory available this should result in an exception. + $config_method->invoke($trait, 'editable.config'); + } + +}