diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index 8861481..19b42f0 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -231,6 +231,9 @@ public function save($has_trusted_data = FALSE) { $this->isNew = FALSE; $this->eventDispatcher->dispatch(ConfigEvents::SAVE, new ConfigCrudEvent($this)); $this->originalData = $this->data; + // Potentially configuration schema could have changed the underlying data's + // types. + $this->resetOverriddenData(); return $this; } diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 540fa15..4802173 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -258,6 +258,18 @@ protected function doSave($id, EntityInterface $entity) { $config->setData($this->mapToStorageRecord($entity)); $config->save($entity->hasTrustedData()); + // Update the entity with the values stored in configuration. It is possible + // that configuration schema has casted some of the values. + if (!$entity->hasTrustedData()) { + $data = $this->mapFromStorageRecords(array($config->get())); + $updated_entity = current($data); + + foreach (array_keys($config->get()) as $property) { + $value = $updated_entity->get($property); + $entity->set($property, $value); + } + } + return $is_new ? SAVED_NEW : SAVED_UPDATED; } diff --git a/core/modules/config/src/Tests/ConfigCRUDTest.php b/core/modules/config/src/Tests/ConfigCRUDTest.php index 268df44..158e4a7 100644 --- a/core/modules/config/src/Tests/ConfigCRUDTest.php +++ b/core/modules/config/src/Tests/ConfigCRUDTest.php @@ -275,6 +275,14 @@ public function testDataTypes() { $this->assertIdentical($config->get(), $data); $this->assertIdentical($storage->read($name), $data); + // Test that schema type enforcement can be overridden by trusting the data. + $this->assertIdentical(99, $config->get('int')); + $config->set('int', '99')->save(TRUE); + $this->assertIdentical('99', $config->get('int')); + // Test that re-saving without testing the data enforces the schema type. + $config->save(); + $this->assertIdentical($data, $config->get()); + // Test that setting an unsupported type for a config object with a schema // fails. try { diff --git a/core/modules/config/src/Tests/ConfigEntityUnitTest.php b/core/modules/config/src/Tests/ConfigEntityUnitTest.php index 164b07d..f96a576 100644 --- a/core/modules/config/src/Tests/ConfigEntityUnitTest.php +++ b/core/modules/config/src/Tests/ConfigEntityUnitTest.php @@ -18,6 +18,15 @@ class ConfigEntityUnitTest extends KernelTestBase { /** + * Exempt from strict schema checking. + * + * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker + * + * @var bool + */ + protected $strictConfigSchema = FALSE; + + /** * Modules to enable. * * @var array @@ -89,6 +98,20 @@ public function testStorageMethods() { foreach ($entities as $entity) { $this->assertIdentical($entity->get('style'), $style, 'The loaded entity has the correct style value specified.'); } + + // Test that schema type enforcement can be overridden by trusting the data. + $entity = $this->storage->create(array( + 'id' => $this->randomMachineName(), + 'label' => $this->randomString(), + 'style' => 999 + )); + $entity->save(); + $this->assertIdentical('999', $entity->style); + $entity->style = 999; + $entity->trustData()->save(); + $this->assertIdentical(999, $entity->style); + $entity->save(); + $this->assertIdentical('999', $entity->style); } }