From d2b22e766fa389f7ccf720fcaf338f74156f7836 Mon Sep 17 00:00:00 2001 From: nod_ <nod_@598310.no-reply.drupal.org> Date: Thu, 23 Jan 2025 15:31:28 +0100 Subject: [PATCH] Issue #3462700 by niklan, richardgaunt, smustgrave, pdureau: Update ComponentValidator to always include the component ID (cherry picked from commit 39b279217ce812e3446056140a9da98dd78e5659) --- .../Core/Theme/Component/ComponentValidator.php | 16 +++++++++++----- .../Theme/Component/ComponentValidatorTest.php | 8 ++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/lib/Drupal/Core/Theme/Component/ComponentValidator.php b/core/lib/Drupal/Core/Theme/Component/ComponentValidator.php index 357c3ab21c31..6664b2d3ec7e 100644 --- a/core/lib/Drupal/Core/Theme/Component/ComponentValidator.php +++ b/core/lib/Drupal/Core/Theme/Component/ComponentValidator.php @@ -138,7 +138,7 @@ public function validateDefinition(array $definition, bool $enforce_schemas): bo ...$message_parts, ...$missing_class_errors, ]; - $message = implode("/n", $message_parts); + $message = implode("\n", $message_parts); // Throw the exception with the error message. throw new InvalidComponentException($message); } @@ -208,7 +208,7 @@ function (array $error) use ($context): bool { return TRUE; } $message_parts = array_map( - static function (array $error): string { + static function (array $error) use ($component_id, $context): string { // We check the error message instead of values and definitions here // because it's hard to access both given the possible complexity of a // schema. Since this is a small non critical DX improvement error @@ -217,11 +217,17 @@ static function (array $error): string { $error['message'] .= '. This may be because the property is empty instead of having data present. If possible fix the source data, use the |default() twig filter, or update the schema to allow multiple types.'; } - return sprintf("[%s] %s", $error['property'], $error['message']); + // If the property value has been set, print it out for easier + // debugging. + if (isset($context[$error['property']]) && \is_scalar($context[$error['property']])) { + $error['message'] .= \sprintf('. The provided value is: "%s"', $context[$error['property']]); + } + + return sprintf('[%s/%s] %s.', $component_id, $error['property'], $error['message']); }, $errors ); - $message = implode("/n", $message_parts); + $message = implode("\n", $message_parts); throw new InvalidComponentException($message); } @@ -276,7 +282,7 @@ private function validateClassProps(array $props_schema, array $props_raw, strin } $props_schema = $this->nullifyClassPropsSchema($props_schema, $classes_per_prop); if (!empty($error_messages)) { - $message = implode("/n", $error_messages); + $message = implode("\n", $error_messages); throw new InvalidComponentException($message); } return [$props_schema, $props_raw]; diff --git a/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php b/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php index d24d2df7989c..a0ae10e47904 100644 --- a/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php +++ b/core/tests/Drupal/Tests/Core/Theme/Component/ComponentValidatorTest.php @@ -159,13 +159,14 @@ public static function dataProviderValidatePropsValid(): array { * * @throws \Drupal\Core\Render\Component\Exception\InvalidComponentException */ - public function testValidatePropsInvalid(array $context, string $component_id, array $definition): void { + public function testValidatePropsInvalid(array $context, string $component_id, array $definition, string $expected_exception_message): void { $component = new Component( ['app_root' => '/fake/path/root'], 'sdc_test:' . $component_id, $definition ); $this->expectException(InvalidComponentException::class); + $this->expectExceptionMessage($expected_exception_message); $component_validator = new ComponentValidator(); $component_validator->setValidator(); $component_validator->validateProps($context, $component); @@ -175,7 +176,7 @@ public function testValidatePropsInvalid(array $context, string $component_id, a * Data provider with invalid component props. * * @return array - * The data. + * Returns the generator with the invalid properties. */ public static function dataProviderValidatePropsInvalid(): array { return [ @@ -187,6 +188,7 @@ public static function dataProviderValidatePropsInvalid(): array { ], 'my-cta', static::loadComponentDefinitionFromFs('my-cta'), + '[sdc_test:my-cta/text] The property text is required.', ], 'attributes with invalid object class' => [ [ @@ -197,11 +199,13 @@ public static function dataProviderValidatePropsInvalid(): array { ], 'my-cta', static::loadComponentDefinitionFromFs('my-cta'), + 'Data provided to prop "attributes" for component "sdc_test:my-cta" is not a valid instance of "Drupal\Core\Template\Attribute"', ], 'ctaTarget violates the allowed properties in the enum' => [ ['ctaTarget' => 'foo'], 'my-banner', static::loadComponentDefinitionFromFs('my-banner'), + '[sdc_test:my-banner/ctaTarget] Does not have a value in the enumeration ["","_blank"]. The provided value is: "foo".', ], ]; } -- GitLab