diff -u b/core/includes/config.inc b/core/includes/config.inc --- b/core/includes/config.inc +++ b/core/includes/config.inc @@ -243,11 +243,11 @@ * @param mixed $value * Config value to test. * @param mixed $key - * Config key to test. + * Config key. * * @throws StorageException * - * @see DatabaseStorage::validateData, FileStorage::validateData + * @see DatabaseStorage::validateData(), FileStorage::validateData() */ function config_validate_data($value, $key) { if (!is_scalar($value)) { diff -u b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php --- b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php +++ b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php @@ -112,14 +112,14 @@ } } + /** + * Tests storage controller writing and reading data preserving data type. + */ function testDataTypes() { $name = 'config_test.types'; - $data = $this->storage->read($name); - - // Test typed varaibles. - $type_array = array( + $data = array( + 'array' => array(), 'boolean' => TRUE, - 'date' => strtotime('2001-12-14t21:59:43.10-05:00'), 'exp' => 1.2e+34, 'float' => 3.14159, 'hex' => 0xC, @@ -127,14 +127,21 @@ 'octal' => 0775, 'string' => 'string', ); - $this->assertIdentical($data, $type_array); + + $result = $this->storage->write($name, $data); + $this->assertIdentical($result, TRUE); + + $read_data = $this->storage->read($name); + $this->assertIdentical($read_data, $data); } + /** + * Tests storage controller writing an invalid data type. + */ function testInvalidData() { $name = 'config_test.invalid_data'; - $invalid_key = $this->randomName(); $data = array( - $invalid_key => new \stdClass(), + 'invalid_key' => new \stdClass(), ); try { $data = $this->storage->write($name, $data); @@ -142,7 +149,7 @@ catch (StorageException $e) { $message = $e->getMessage(); } - $this->assertEqual($message, sprintf('Unallowed non-scalar value of type object in key %s in config %s', $invalid_key, $name)); + $this->assertEqual($message, sprintf('Unallowed non-scalar value of type object in key invalid_key in config %s', $name)); } abstract protected function read($name); reverted: --- b/core/modules/config/lib/Drupal/config/Tests/Storage/DatabaseStorageTest.php +++ a/core/modules/config/lib/Drupal/config/Tests/Storage/DatabaseStorageTest.php @@ -22,7 +22,7 @@ } function setUp() { + parent::setUp(); - parent::setUp('config_test'); $this->storage = new DatabaseStorage(); $this->invalidStorage = new DatabaseStorage(array('connection' => 'invalid')); } reverted: --- b/core/modules/config/lib/Drupal/config/Tests/Storage/FileStorageTest.php +++ a/core/modules/config/lib/Drupal/config/Tests/Storage/FileStorageTest.php @@ -23,17 +23,12 @@ } function setUp() { + parent::setUp(); - parent::setUp('config_test'); $this->storage = new FileStorage(); $this->invalidStorage = new FileStorage(array('directory' => $this->configFileDirectory . '/nonexisting')); // FileStorage::listAll() requires other configuration data to exist. $this->storage->write('system.performance', config('system.performance')->get()); - - // Variable type test requires config_test.types to exist.Doing this - // additionally tests the ability of FileStorage to write typed - // configuration. - $this->storage->write('config_test.types', config('config_test.types')->get()); } protected function read($name) { diff -u b/core/modules/config/tests/config_test/config/config_test.types.yml b/core/modules/config/tests/config_test/config/config_test.types.yml --- b/core/modules/config/tests/config_test/config/config_test.types.yml +++ b/core/modules/config/tests/config_test/config/config_test.types.yml @@ -1,5 +1,5 @@ +array: [] boolean: true -date: 2001-12-14t21:59:43.10-05:00 exp: 1.2e+34 float: 3.14159 hex: 0xC only in patch2: unchanged: --- a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php @@ -8,6 +8,7 @@ namespace Drupal\config\Tests; use Drupal\Core\Config\DatabaseStorage; +use Drupal\Core\Config\StorageException; use Drupal\simpletest\WebTestBase; /** @@ -22,6 +23,10 @@ class ConfigCRUDTest extends WebTestBase { ); } + function setUp() { + parent::setUp(array('config_test')); + } + /** * Tests CRUD operations. */ @@ -112,4 +117,72 @@ class ConfigCRUDTest extends WebTestBase { // their order must be identical. $this->assertIdentical($new_config->get(), $config->get()); } + + /** + * Tests data type preservation. + */ + function testDataTypePreservation() { + $config = config('config_test.types'); + + // Test typed varaibles. + $data = array( + 'array' => array(), + 'boolean' => TRUE, + 'exp' => 1.2e+34, + 'float' => 3.14159, + 'hex' => 0xC, + 'int' => 99, + 'octal' => 0775, + 'string' => 'string', + ); + + // Test data provided by config_test.types.yml. + $this->assertIdentical($config->get(), $data); + + // Set each key using config::set(). + foreach($data as $key => $value) { + $config->set($key, $value); + } + $config->save(); + $this->assertIdentical($config->get(), $data); + + // Set data using config::setData(). + $config->setData($data)->save(); + $this->assertIdentical($config->get(), $data); + } + + /** + * Tests data type validation. + */ + function testDataTypeValidation() { + $config = config('config_test.invalid_data'); + + // Config can't save resources. + $data = array( + 'valid_data_type' => 1, + 'invalid_type' => fopen(__FILE__, 'r'), + ); + // Test using config::set() + foreach ($data as $key => $value) { + $config->set($key, $value); + } + try { + $config->save(); + } + catch (StorageException $e) { + $message = $e->getMessage(); + } + $this->assertEqual($message, 'Unallowed non-scalar value of type resource in key invalid_type in config config_test.invalid_data'); + + // Config can't save objects. + $data['invalid_type'] = new \stdClass(); + // Test using config::setData(). + try { + $config->setData($data)->save(); + } + catch (StorageException $e) { + $message = $e->getMessage(); + } + $this->assertEqual($message, 'Unallowed non-scalar value of type object in key invalid_type in config config_test.invalid_data'); + } }