From 9df6250df85466124b3190820b8b61636139c0d8 Mon Sep 17 00:00:00 2001 From: catch Date: Mon, 12 Feb 2024 12:36:41 +0000 Subject: [PATCH] Issue #3397491 by borisson_, phenaproxima, Wim Leers, smustgrave: Add validation constraints to core.date_format. --- core/config/schema/core.data_types.schema.yml | 11 +++++++++- .../DateFormatAccessControlHandlerTest.php | 1 + .../Core/Entity/DateFormatValidationTest.php | 20 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index 390c0fd6bbb..4694cf69713 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -561,7 +561,7 @@ core.date_format.*: label: 'Date format' mapping: id: - type: string + type: machine_name label: 'ID' label: type: required_label @@ -572,6 +572,15 @@ core.date_format.*: pattern: type: core_date_format_pattern.[%parent.locked] label: 'PHP date format' + constraints: + NotBlank: [] + # A valid date format character must appear somewhere in the value. + # See https://www.php.net/manual/en/datetime.format.php + Regex: + pattern: '/[aABcdDeFgGhHiIjlLmMnNoOpPrsStTuUvwWxXyYzZ]/' + message: 'This is not a valid date format.' + constraints: + FullyValidatable: ~ # Unlocked date formats should use the translatable type. core_date_format_pattern.0: diff --git a/core/modules/system/tests/src/Kernel/DateFormatAccessControlHandlerTest.php b/core/modules/system/tests/src/Kernel/DateFormatAccessControlHandlerTest.php index 7282826400a..d63f44fccb1 100644 --- a/core/modules/system/tests/src/Kernel/DateFormatAccessControlHandlerTest.php +++ b/core/modules/system/tests/src/Kernel/DateFormatAccessControlHandlerTest.php @@ -69,6 +69,7 @@ class DateFormatAccessControlHandlerTest extends KernelTestBase { ? ['locked' => FALSE] : ['locked' => TRUE]; $entity_values['id'] = $entity_values['label'] = $this->randomMachineName(); + $entity_values['pattern'] = 'Y-m-d'; $entity = DateFormat::create($entity_values); $entity->save(); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/DateFormatValidationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/DateFormatValidationTest.php index 10e97012bbd..93195e4076b 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/DateFormatValidationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/DateFormatValidationTest.php @@ -27,4 +27,24 @@ class DateFormatValidationTest extends ConfigEntityValidationTestBase { $this->entity->save(); } + /** + * Tests that the pattern of a date format is validated. + * + * @param string $pattern + * The pattern to set. + * @param bool $locked + * Whether the date format entity is locked or not. + * @param string $expected_error + * The error message that should be flagged for the invalid pattern. + * + * @testWith ["q", true, "This is not a valid date format."] + * ["", true, "This value should not be blank."] + * ["q", false, "This is not a valid date format."] + * ["", false, "This value should not be blank."] + */ + public function testPatternIsValidated(string $pattern, bool $locked, string $expected_error): void { + $this->entity->setPattern($pattern)->set('locked', $locked); + $this->assertValidationErrors(['pattern' => $expected_error]); + } + }