diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index f78d4d7..c2269ae 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -582,7 +582,7 @@ protected function getSchemaWrapper() { * @return mixed * The value cast to the type indicated in the schema. * - * @throws \Drupal\Core\Config\UnsupportedConfigDataTypeException + * @throws \Drupal\Core\Config\UnsupportedDataTypeConfigException * Exception on unsupported/undefined data type deducted. */ protected function castValue($key, $value) { @@ -621,7 +621,7 @@ protected function castValue($key, $value) { else { // Throw exception on any non-scalar or non-array value. if (!is_array($value)) { - throw new UnsupportedConfigDataTypeException(format_string('Invalid data type for config element @name:@key', array( + throw new UnsupportedDataTypeConfigException(String::format('Invalid data type for config element @name:@key', array( '@name' => $this->getName(), '@key' => $key, ))); diff --git a/core/lib/Drupal/Core/Config/FileStorage.php b/core/lib/Drupal/Core/Config/FileStorage.php index 52644a1..6ee1238 100644 --- a/core/lib/Drupal/Core/Config/FileStorage.php +++ b/core/lib/Drupal/Core/Config/FileStorage.php @@ -7,7 +7,9 @@ namespace Drupal\Core\Config; +use Drupal\Component\Utility\String; use Symfony\Component\Yaml\Dumper; +use Symfony\Component\Yaml\Exception\DumpException; use Symfony\Component\Yaml\Parser; /** @@ -105,11 +107,17 @@ public function readMultiple(array $names) { /** * Implements Drupal\Core\Config\StorageInterface::write(). * - * @throws Symfony\Component\Yaml\Exception\DumpException + * @throws \Drupal\Core\Config\UnsupportedDataTypeConfigException * @throws \Drupal\Core\Config\StorageException */ public function write($name, array $data) { - $data = $this->encode($data); + try { + $data = $this->encode($data); + } + catch(DumpException $e) { + throw new UnsupportedDataTypeConfigException(String::format('Invalid data type for used in config: @name', array('@name' => $name))); + } + $target = $this->getFilePath($name); $status = @file_put_contents($target, $data); if ($status === FALSE) { diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php index a46f1e5..7576163 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php @@ -7,10 +7,11 @@ namespace Drupal\config\Tests; +use Drupal\Component\Utility\String; use Drupal\Core\Config\ConfigNameException; use Drupal\simpletest\DrupalUnitTestBase; use Drupal\Core\Config\FileStorage; -use Drupal\Core\Config\UnsupportedConfigDataTypeException; +use Drupal\Core\Config\UnsupportedDataTypeConfigException; /** * Tests CRUD operations on configuration objects. @@ -226,12 +227,31 @@ public function testDataTypes() { $this->assertIdentical($config->get(), $data); $this->assertIdentical($storage->read($name), $data); + // Test that setting an unsupported type for a config object with a schema + // fails. try { $config->set('stream', fopen(__FILE__, 'r'))->save(); $this->fail('No Exception thrown upon saving invalid data type.'); } - catch (UnsupportedConfigDataTypeException $e) { - $this->pass(format_string('%class thrown upon saving invalid data type.', array( + catch (UnsupportedDataTypeConfigException $e) { + $this->pass(String::format('%class thrown upon saving invalid data type.', array( + '%class' => get_class($e), + ))); + } + + // Test that setting an unsupported type for a config object with no schema + // also fails. + $typed_config_manager = $this->container->get('config.typed'); + $config_name = 'config_test.no_schema'; + $config = $this->container->get('config.factory')->get($config_name); + $this->assertFalse($typed_config_manager->hasConfigSchema($config_name)); + + try { + $config->set('stream', fopen(__FILE__, 'r'))->save(); + $this->fail('No Exception thrown upon saving invalid data type.'); + } + catch (UnsupportedDataTypeConfigException $e) { + $this->pass(String::format('%class thrown upon saving invalid data type.', array( '%class' => get_class($e), ))); }