diff --git a/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php b/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php index f09dd779babab6eb361e77d42e01a87d11b9c900..b43c6a7a494094c5549229248f89324f1b0ee221 100644 --- a/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php +++ b/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php @@ -54,8 +54,9 @@ public function checkConfigSchema(TypedConfigManagerInterface $typed_config, $co } $definition = $typed_config->getDefinition($config_name); $this->schema = $typed_config->create($definition, $config_data); + $errors = array(); foreach ($config_data as $key => $value) { - $errors = $this->checkValue($key, $value); + $errors = array_merge($errors, $this->checkValue($key, $value)); } if (empty($errors)) { return TRUE; @@ -82,7 +83,7 @@ protected function checkValue($key, $value) { } catch (SchemaIncompleteException $e) { if (is_scalar($value) || $value === NULL) { - return array($error_key => 'Missing schema.'); + return array($error_key => 'Missing schema'); } } // Do not check value if it is defined to be ignored. diff --git a/core/modules/config/src/Tests/SchemaCheckTraitTest.php b/core/modules/config/src/Tests/SchemaCheckTraitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3b5793a8c372d74dcd3855c1e3fe00a273157d77 --- /dev/null +++ b/core/modules/config/src/Tests/SchemaCheckTraitTest.php @@ -0,0 +1,77 @@ +<?php + +/** + * @file + * Contains \Drupal\config\Tests\SchemaCheckTraitTest. + */ + +namespace Drupal\config\Tests; + +use Drupal\Core\Config\Schema\SchemaCheckTrait; +use Drupal\simpletest\KernelTestBase; + + +/** + * Tests \Drupal\Core\Config\Schema\SchemaCheckTrait. + */ +class SchemaCheckTraitTest extends KernelTestBase { + use SchemaCheckTrait; + + /** + * The typed config manager. + * + * @var \Drupal\Core\Config\TypedConfigManagerInterface + */ + protected $typedConfig; + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('config_test', 'config_schema_test'); + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'SchemaCheckTrait test', + 'description' => 'Tests the functionality of SchemaCheckTrait.', + 'group' => 'Configuration', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + $this->installConfig(array('config_test', 'config_schema_test')); + $this->typedConfig = \Drupal::service('config.typed'); + } + + /** + * Tests \Drupal\Core\Config\Schema\SchemaCheckTrait. + */ + public function testTrait() { + // Test a non existing schema. + $ret = $this->checkConfigSchema($this->typedConfig, 'config_schema_test.noschema', \Drupal::config('config_schema_test.noschema')->get()); + $this->assertIdentical($ret, FALSE); + + // Test an existing schema with valid data. + $config_data = \Drupal::config('config_test.types')->get(); + $ret = $this->checkConfigSchema($this->typedConfig, 'config_test.types', $config_data); + $this->assertIdentical($ret, TRUE); + + // Add a new key and new array to test the error messages. + $config_data = array('new_key' => 'new_value', 'new_array' => array()) + $config_data; + $ret = $this->checkConfigSchema($this->typedConfig, 'config_test.types', $config_data); + $expected = array( + 'config_test.types:new_key' => 'Missing schema', + 'config_test.types:new_array' => 'Non-scalar value but not defined as an array (such as mapping or sequence)', + ); + $this->assertIdentical($ret, $expected); + } + +}