Issue #1905230 by Gábor Hojtsy, fago: Improve the typed data API usage of configuration schema.

8.0.x
Dries 2013-02-18 18:49:59 -05:00
parent b2320bc9d5
commit 52c9be41a7
7 changed files with 56 additions and 48 deletions

View File

@ -1,43 +0,0 @@
<?php
/**
* @file
* Contains \Drupal\Core\Config\Schema\Parser.
*/
namespace Drupal\Core\Config\Schema;
/**
* Parser.
*/
class Parser {
/**
* Parse configuration data against schema data.
*/
static function parse($data, $definition, $name = NULL, $parent = NULL) {
// Set default type depending on data and context.
if (!isset($definition['type'])) {
if (is_array($data) || !$context) {
$definition += array('type' => 'any');
}
else {
$definition += array('type' => 'str');
}
}
// Create typed data object.
config_typed()->create($definition, $data, $name, $parent);
}
/**
* Validate configuration data against schema data.
*/
static function validate($config_data, $schema_data) {
return self::parse($config_data, $schema_data)->validate();
}
static function getDefinition($type, $data) {
return config_definition($type);
}
}

View File

@ -56,12 +56,12 @@ class TypedConfigManager extends TypedDataManager {
*/
public function create(array $definition, $value = NULL, $name = NULL, $parent = NULL) {
if (!isset($definition['type'])) {
// Set default type 'str' if possible. If not it will be 'any'.
// Set default type 'string' if possible. If not it will be 'undefined'.
if (is_string($value)) {
$definition['type'] = 'str';
$definition['type'] = 'string';
}
else {
$definition['type'] = 'any';
$definition['type'] = 'undefined';
}
}
elseif (strpos($definition['type'], ']')) {

View File

@ -20,7 +20,7 @@ class ConfigSchemaTest extends DrupalUnitTestBase {
*
* @var array
*/
public static $modules = array('system', 'locale', 'image');
public static $modules = array('system', 'locale', 'image', 'config_test');
public static function getInfo() {
return array(
@ -34,12 +34,48 @@ class ConfigSchemaTest extends DrupalUnitTestBase {
parent::setUp();
config_install_default_config('module', 'system');
config_install_default_config('module', 'image');
config_install_default_config('module', 'config_test');
}
/**
* Tests the basic metadata retrieval layer.
*/
function testSchemaMapping() {
// Nonexistent configuration key will have Unknown as metadata.
$definition = config_typed()->getDefinition('config_test.no_such_key');
$expected = array();
$expected['label'] = 'Unknown';
$expected['class'] = '\Drupal\Core\Config\Schema\Property';
$this->assertEqual($definition, $expected, 'Retrieved the right metadata for nonexistent configuration.');
// Configuration file without schema will return Unknown as well.
$definition = config_typed()->getDefinition('config_test.noschema');
$this->assertEqual($definition, $expected, 'Retrieved the right metadata for configuration with no schema.');
// Configuration file with only some schema.
$definition = config_typed()->getDefinition('config_test.someschema');
$expected = array();
$expected['label'] = 'Schema test data';
$expected['class'] = '\Drupal\Core\Config\Schema\Mapping';
$expected['mapping']['testitem'] = array('label' => 'Test item');
$expected['mapping']['testlist'] = array('label' => 'Test list');
$this->assertEqual($definition, $expected, 'Retrieved the right metadata for configuration with only some schema.');
// Check type detection on elements with undefined types.
$config = config_typed()->get('config_test.someschema');
$definition = $config['testitem']->getDefinition();
$expected = array();
$expected['label'] = 'Test item';
$expected['class'] = '\Drupal\Core\TypedData\Type\String';
$expected['type'] = 'string';
$this->assertEqual($definition, $expected, 'Automatic type detection on string item worked.');
$definition = $config['testlist']->getDefinition();
$expected = array();
$expected['label'] = 'Test list';
$expected['class'] = '\Drupal\Core\Config\Schema\Property';
$expected['type'] = 'undefined';
$this->assertEqual($definition, $expected, 'Automatic type fallback on non-string item worked.');
// Simple case, straight metadata.
$definition = config_typed()->getDefinition('system.maintenance');
$expected = array();

View File

@ -0,0 +1,4 @@
testitem: "Whatever structure there is in this file"
testlist:
- "the main file has no schema, so individual items"
- "will not have any schema information."

View File

@ -0,0 +1,8 @@
config_test.someschema:
type: mapping
label: "Schema test data"
mapping:
"testitem":
label: "Test item"
"testlist":
label: "Test list"

View File

@ -0,0 +1,4 @@
testitem: "Since this file at least has top level schema in config_test.schema.yml"
testlist:
- "Direct string items are identified and other items are"
- "recognized as undefined types."

View File

@ -23,7 +23,6 @@ contact.settings:
mapping:
"default_category":
type: string
required: yes
"flood":
type: mapping
mapping: