Commit b7eb07e2 authored by alexpott's avatar alexpott

Issue #2870878 by dawehner, Wim Leers, tstoeckler, kristiaanvandeneynde: Add...

Issue #2870878 by dawehner, Wim Leers, tstoeckler, kristiaanvandeneynde: Add config validation for UUIDs
parent ca9e5201
......@@ -72,6 +72,13 @@ text:
label: 'Text'
translatable: true
# A UUID.
uuid:
type: string
label: 'UUID'
constraints:
Uuid: {}
# PHP Date format string that is translatable.
date_format:
type: string
......@@ -275,7 +282,7 @@ config_entity:
type: mapping
mapping:
uuid:
type: string
type: uuid
label: 'UUID'
langcode:
type: string
......@@ -336,8 +343,7 @@ condition.plugin:
type: boolean
label: 'Negate'
uuid:
type: string
label: 'UUID'
type: uuid
context_mapping:
type: sequence
label: 'Context assignments'
......@@ -358,8 +364,7 @@ display_variant.plugin:
type: integer
label: 'Weight'
uuid:
type: string
label: 'UUID'
type: uuid
layout_plugin.settings:
type: mapping
......@@ -602,8 +607,7 @@ field.value.entity_reference:
type: string
label: 'Value'
target_uuid:
type: string
label: 'Target UUID'
type: uuid
# Schema for the configuration of the Boolean field type.
......
<?php
namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraints\Uuid;
use Symfony\Component\Validator\Constraints\UuidValidator;
/**
* Validates a UUID.
*
* @Constraint(
* id = "Uuid",
* label = @Translation("Universally Unique Identifier", context = "Validation"),
* )
*/
class UuidConstraint extends Uuid {
/**
* {@inheritdoc}
*/
public function validatedBy() {
return UuidValidator::class;
}
}
......@@ -5,3 +5,4 @@ cat:
giraffe:
hum1: hum1
hum2: hum2
uuid: '7C30C50E-641A-4E34-A7F1-46BCFB9BE5A3'
......@@ -135,8 +135,7 @@ config_test.new:
type: string
label: 'Test setting'
uuid:
type: string
label: 'UUID'
type: uuid
config_test.old:
type: config_test.new
......@@ -194,3 +193,5 @@ config_test.validation:
constraints:
Callback:
callback: [\Drupal\config_test\ConfigValidation, validateGiraffes]
uuid:
type: uuid
......@@ -74,7 +74,7 @@ public static function validateGiraffes($string, ExecutionContextInterface $cont
* The validation execution context.
*/
public static function validateMapping($mapping, ExecutionContextInterface $context) {
if ($diff = array_diff(array_keys($mapping), ['llama', 'cat', 'giraffe', '_core'])) {
if ($diff = array_diff(array_keys($mapping), ['llama', 'cat', 'giraffe', 'uuid', '_core'])) {
$context->addViolation('Missing giraffe.');
}
}
......
......@@ -14,8 +14,7 @@ field_default_image:
type: mapping
mapping:
uuid:
type: string
label: 'Image'
type: uuid
alt:
type: label
label: 'Alternative text'
......
......@@ -21,7 +21,7 @@ image.style.*:
weight:
type: integer
uuid:
type: string
type: uuid
image.effect.*:
type: mapping
......
......@@ -5,9 +5,10 @@ system.site:
label: 'Site information'
mapping:
uuid:
type: string
type: uuid
label: 'Site UUID'
constraints:
Uuid: []
NotNull: []
name:
type: label
......
......@@ -170,7 +170,7 @@ public function testSchemaMapping() {
$expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
$expected['unwrap_for_canonical_representation'] = TRUE;
$expected['mapping']['name']['type'] = 'string';
$expected['mapping']['uuid']['type'] = 'string';
$expected['mapping']['uuid']['type'] = 'uuid';
$expected['mapping']['uuid']['label'] = 'UUID';
$expected['mapping']['langcode']['type'] = 'string';
$expected['mapping']['langcode']['label'] = 'Language code';
......@@ -186,7 +186,7 @@ public function testSchemaMapping() {
$expected['mapping']['effects']['sequence']['mapping']['id']['type'] = 'string';
$expected['mapping']['effects']['sequence']['mapping']['data']['type'] = 'image.effect.[%parent.id]';
$expected['mapping']['effects']['sequence']['mapping']['weight']['type'] = 'integer';
$expected['mapping']['effects']['sequence']['mapping']['uuid']['type'] = 'string';
$expected['mapping']['effects']['sequence']['mapping']['uuid']['type'] = 'uuid';
$expected['mapping']['third_party_settings']['type'] = 'sequence';
$expected['mapping']['third_party_settings']['label'] = 'Third party settings';
$expected['mapping']['third_party_settings']['sequence']['type'] = '[%parent.%parent.%type].third_party.[%key]';
......
<?php
namespace Drupal\KernelTests\Core\Validation;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests various low level constrains provided by core.
*
* @group Validation
*/
class ConstraintsTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['config_test'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig('config_test');
}
/**
* @see \Drupal\Core\Validation\Plugin\Validation\Constraint\UuidConstraint
*/
public function testUuid() {
$typed_config_manager = \Drupal::service('config.typed');
/** @var \Drupal\Core\Config\Schema\TypedConfigInterface $typed_config */
$typed_config = $typed_config_manager->get('config_test.validation');
$typed_config->get('uuid')
->setValue(\Drupal::service('uuid')->generate());
$this->assertCount(0, $typed_config->validate());
$typed_config->get('uuid')
->setValue(\Drupal::service('uuid')->generate() . '-invalid');
$this->assertCount(1, $typed_config->validate());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment