diff --git a/core/lib/Drupal/Core/Entity/ContentEntityForm.php b/core/lib/Drupal/Core/Entity/ContentEntityForm.php index 75bde9fe35..cada24e5c4 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityForm.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityForm.php @@ -6,6 +6,7 @@ use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Validation\ConstraintViolationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -250,7 +251,7 @@ protected function getEditedFieldNames(FormStateInterface $form_state) { * violations for those fields; e.g.: * @code * foreach ($violations->getByField('name') as $violation) { - * $form_state->setErrorByName('name', $violation->getMessage()); + * $form_state->setErrorByName('name', $violation->getOriginalMessage()); * } * parent::flagViolations($violations, $form, $form_state); * @endcode @@ -266,7 +267,8 @@ protected function flagViolations(EntityConstraintViolationListInterface $violat // Flag entity level violations. foreach ($violations->getEntityViolations() as $violation) { /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */ - $form_state->setErrorByName(str_replace('.', '][', $violation->getPropertyPath()), $violation->getMessage()); + $message = $violation instanceof ConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $form_state->setErrorByName(str_replace('.', '][', $violation->getPropertyPath()), $message); } // Let the form display flag violations of its fields. $this->getFormDisplay($form_state)->flagWidgetsErrorsFromViolations($violations, $form, $form_state); diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index 183e428ba3..ec183a30ad 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -8,7 +8,9 @@ use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Entity\EntityDisplayBase; use Drupal\Core\Form\FormStateInterface; -use Symfony\Component\Validator\ConstraintViolation; +use Drupal\Core\Validation\ConstraintViolationInterface; +use Symfony\Component\Validator\ConstraintViolation as SymfonyConstraintViolation; +use Drupal\Core\Validation\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ConstraintViolationListInterface; @@ -238,8 +240,8 @@ public function validateFormValues(FieldableEntityInterface $entity, array &$for // Flag entity level violations. foreach ($violations->getEntityViolations() as $violation) { - /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */ - $form_state->setError($form, $violation->getMessage()); + $message = $violation instanceof ConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $form_state->setError($form, $message); } $this->flagWidgetsErrorsFromViolations($violations, $form, $form_state); @@ -291,15 +293,16 @@ protected function movePropertyPathViolationsRelativeToField($field_name, Constr $cause = NULL; $parameters = []; $plural = NULL; - if ($violation instanceof ConstraintViolation) { + if ($violation instanceof SymfonyConstraintViolation) { $constraint = $violation->getConstraint(); $cause = $violation->getCause(); $parameters = $violation->getParameters(); $plural = $violation->getPlural(); } + $message = $violation instanceof ConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); $new_violation = new ConstraintViolation( - $violation->getMessage(), + $message, $violation->getMessageTemplate(), $parameters, $violation->getRoot(), diff --git a/core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php b/core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php index 6081a8de16..30c36503fd 100644 --- a/core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php +++ b/core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php @@ -5,7 +5,7 @@ use Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintBase; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Symfony\Component\Validator\ConstraintViolation; +use Drupal\Core\Validation\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationInterface; use Symfony\Component\Validator\ConstraintViolationList; diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index 63671576b2..9469005f11 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -7,7 +7,8 @@ use Drupal\Component\Utility\SortArray; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; -use Symfony\Component\Validator\ConstraintViolationInterface; +use Drupal\Core\Validation\ConstraintViolationInterface as DrupalConstraintViolationInterface; +use Symfony\Component\Validator\ConstraintViolationInterface as SymfonyConstraintViolationInterface; use Symfony\Component\Validator\ConstraintViolationListInterface; /** @@ -454,7 +455,8 @@ public function flagErrors(FieldItemListInterface $items, ConstraintViolationLis // @todo: Pass $violation->arrayPropertyPath as property path. $error_element = $this->errorElement($delta_element, $violation, $form, $form_state); if ($error_element !== FALSE) { - $form_state->setError($error_element, $violation->getMessage()); + $message = $violation instanceof DrupalConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $form_state->setError($error_element, $message); } } } @@ -463,7 +465,8 @@ public function flagErrors(FieldItemListInterface $items, ConstraintViolationLis // Pass violations to the main element without going through // errorElement() if the violations are at the ItemList level. foreach ($item_list_violations as $violation) { - $form_state->setError($element, $violation->getMessage()); + $message = $violation instanceof DrupalConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $form_state->setError($element, $message); } } } @@ -518,7 +521,7 @@ public function settingsSummary() { /** * {@inheritdoc} */ - public function errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state) { + public function errorElement(array $element, SymfonyConstraintViolationInterface $error, array $form, FormStateInterface $form_state) { return $element; } diff --git a/core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php b/core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php index bfee7f0947..a5efe7115d 100644 --- a/core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php +++ b/core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php @@ -2,9 +2,9 @@ namespace Drupal\Core\TypedData\Validation; +use Drupal\Core\Validation\ConstraintViolation; use Drupal\Core\Validation\TranslatorInterface; use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Util\PropertyPath; use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; diff --git a/core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php b/core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php index b691e715c8..e53705b2cb 100644 --- a/core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php +++ b/core/lib/Drupal/Core/TypedData/Validation/ExecutionContext.php @@ -2,9 +2,9 @@ namespace Drupal\Core\TypedData\Validation; +use Drupal\Core\Validation\ConstraintViolation; use Drupal\Core\Validation\TranslatorInterface; use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Mapping\MetadataInterface; diff --git a/core/lib/Drupal/Core/Validation/ConstraintViolation.php b/core/lib/Drupal/Core/Validation/ConstraintViolation.php new file mode 100644 index 0000000000..1df98c16da --- /dev/null +++ b/core/lib/Drupal/Core/Validation/ConstraintViolation.php @@ -0,0 +1,99 @@ +originalMessage = $message; + parent::__construct($message, $messageTemplate, $parameters, $root, $propertyPath, $invalidValue, $plural, $code, $constraint, $cause); + } + + /** + * Returns the violation message. + * + * @return \Drupal\Component\Render\MarkupInterface|string + * The violation message. + */ + public function getMessage() { + @trigger_error("Calling " . __NAMESPACE__ . "ConstraintViolation->getMessage() may cast Markup objects to strings unintentionally in the future. and is deprecated in drupal:8.8.0 and will throw an exception in drupal:9.0.0 Use ->getOriginalMessage() instead", E_USER_DEPRECATED); + return $this->getOriginalMessage(); + } + + /** + * {@inheritdoc} + */ + public function getOriginalMessage() { + return $this->originalMessage; + } + + /** + * {@inheritdoc} + */ + public function __toString() { + $root = $this->getRoot(); + if (is_object($root)) { + $class = 'Object(' . get_class($root) . ')'; + } + elseif (is_array($root)) { + $class = 'Array'; + } + else { + $class = (string) $root; + } + + $property_path = (string) $this->getPropertyPath(); + $code = $this->getCode(); + + if ('' !== $property_path && '[' !== $property_path[0] && '' !== $class) { + $class .= '.'; + } + + if (!empty($code)) { + $code = ' (code ' . $code . ')'; + } + + return $class . $property_path . ":\n " . $this->getOriginalMessage() . $code; + } + +} diff --git a/core/lib/Drupal/Core/Validation/ConstraintViolationInterface.php b/core/lib/Drupal/Core/Validation/ConstraintViolationInterface.php new file mode 100644 index 0000000000..a2f21381d8 --- /dev/null +++ b/core/lib/Drupal/Core/Validation/ConstraintViolationInterface.php @@ -0,0 +1,23 @@ +assertEqual(count($violations), 2); $this->assertEqual($violations[0]->getPropertyPath(), 'title'); - $this->assertEqual($violations[0]->getMessage(), t('A feed named %value already exists. Enter a unique title.', [ + $this->assertEqual($violations[0]->getOriginalMessage(), t('A feed named %value already exists. Enter a unique title.', [ '%value' => $feed->label(), ])); $this->assertEqual($violations[1]->getPropertyPath(), 'url'); - $this->assertEqual($violations[1]->getMessage(), t('A feed with this URL %value already exists. Enter a unique URL.', [ + $this->assertEqual($violations[1]->getOriginalMessage(), t('A feed with this URL %value already exists. Enter a unique URL.', [ '%value' => $feed->getUrl(), ])); } diff --git a/core/modules/block_content/tests/src/Functional/BlockContentValidationTest.php b/core/modules/block_content/tests/src/Functional/BlockContentValidationTest.php index bfa84082c1..6668352cb7 100644 --- a/core/modules/block_content/tests/src/Functional/BlockContentValidationTest.php +++ b/core/modules/block_content/tests/src/Functional/BlockContentValidationTest.php @@ -32,7 +32,7 @@ public function testValidation() { // Make sure the violation is on the info property $this->assertEqual($violations[0]->getPropertyPath(), 'info'); // Make sure the message is correct. - $this->assertEqual($violations[0]->getMessage(), format_string('A custom block with block description %value already exists.', [ + $this->assertEqual($violations[0]->getOriginalMessage(), format_string('A custom block with block description %value already exists.', [ '%value' => $block->label(), ])); } diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index f3a33b8b8a..ba087a3aaa 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -15,6 +15,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Validation\ConstraintViolationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -339,10 +340,12 @@ protected function getEditedFieldNames(FormStateInterface $form_state) { protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) { // Manually flag violations of fields not handled by the form display. foreach ($violations->getByField('created') as $violation) { - $form_state->setErrorByName('date', $violation->getMessage()); + $message = $violation instanceof ConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $form_state->setErrorByName('date', $message); } foreach ($violations->getByField('name') as $violation) { - $form_state->setErrorByName('name', $violation->getMessage()); + $message = $violation instanceof ConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $form_state->setErrorByName('name', $message); } parent::flagViolations($violations, $form, $form_state); } diff --git a/core/modules/comment/tests/src/Kernel/CommentValidationTest.php b/core/modules/comment/tests/src/Kernel/CommentValidationTest.php index 4c2481c984..c914e32f73 100644 --- a/core/modules/comment/tests/src/Kernel/CommentValidationTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentValidationTest.php @@ -101,7 +101,7 @@ public function testValidation() { $violations = $comment->validate(); $this->assertEqual(count($violations), 1, "Violation found on author name collision"); $this->assertEqual($violations[0]->getPropertyPath(), "name"); - $this->assertEqual($violations[0]->getMessage(), t('The name you used (%name) belongs to a registered user.', ['%name' => 'test'])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The name you used (%name) belongs to a registered user.', ['%name' => 'test'])); // Make the name valid. $comment->set('name', 'valid unused name'); @@ -109,7 +109,7 @@ public function testValidation() { $violations = $comment->validate(); $this->assertEqual(count($violations), 1, 'Violation found when email is invalid'); $this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value'); - $this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('This value is not a valid email address.')); $comment->set('mail', NULL); $comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237)); @@ -122,7 +122,7 @@ public function testValidation() { // @todo This message should be improved in // https://www.drupal.org/node/2012690. - $this->assertEqual($violations[0]->getMessage(), t('This value should be of the correct primitive type.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('This value should be of the correct primitive type.')); $comment->set('homepage', NULL); $comment->set('hostname', $this->randomString(129)); @@ -152,7 +152,7 @@ public function testValidation() { $violations = $comment->validate(); $this->assertEqual(count($violations), 1, 'Violation found when name is required, but empty and UID is anonymous.'); $this->assertEqual($violations[0]->getPropertyPath(), 'name'); - $this->assertEqual($violations[0]->getMessage(), t('You have to specify a valid author.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('You have to specify a valid author.')); // Test creating a default comment with a given user id works. $comment = $this->entityManager->getStorage('comment')->create([ @@ -177,7 +177,7 @@ public function testValidation() { $violations = $comment->validate(); $this->assertEqual(count($violations), 1, 'Violation found when author name and comment author do not match.'); $this->assertEqual($violations[0]->getPropertyPath(), 'name'); - $this->assertEqual($violations[0]->getMessage(), t('The specified author name does not match the comment author.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The specified author name does not match the comment author.')); } /** @@ -195,7 +195,7 @@ protected function assertLengthViolation(CommentInterface $comment, $field_name, $this->assertEqual(count($violations), 1, "Violation found when $field_name is too long."); $this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value"); $field_label = $comment->get($field_name)->getFieldDefinition()->getLabel(); - $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => $length])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => $length])); } } diff --git a/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php b/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php index 7b8d8dd265..05ccb9d20d 100644 --- a/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php @@ -111,7 +111,7 @@ public function testInvalidTransition() { $violations = $node->validate(); $this->assertCount(1, $violations); - $this->assertEquals('Invalid state transition from Draft to Archived', $violations->get(0)->getMessage()); + $this->assertEquals('Invalid state transition from Draft to Archived', $violations->get(0)->getOriginalMessage()); } /** @@ -134,7 +134,7 @@ public function testInvalidState() { $violations = $node->validate(); $this->assertCount(1, $violations); - $this->assertEquals('State invalid_state does not exist on Editorial workflow', $violations->get(0)->getMessage()); + $this->assertEquals('State invalid_state does not exist on Editorial workflow', $violations->get(0)->getOriginalMessage()); } /** @@ -226,7 +226,7 @@ public function testInvalidStateMultilingual() { $node->moderation_state = 'archived'; $violations = $node->validate(); $this->assertCount(1, $violations); - $this->assertEquals('Invalid state transition from Draft to Archived', $violations->get(0)->getMessage()); + $this->assertEquals('Invalid state transition from Draft to Archived', $violations->get(0)->getOriginalMessage()); // From the default french published revision, there should be none. $node_fr = Node::load($node->id())->getTranslation('fr'); @@ -350,7 +350,7 @@ public function testTransitionAccessValidation($permissions, $target_state, $mes $violations = $node->validate(); $this->assertCount(count($messages), $violations); foreach ($messages as $i => $message) { - $this->assertEquals($message, $violations->get($i)->getMessage()); + $this->assertEquals($message, $violations->get($i)->getOriginalMessage()); } } diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php index df572ebe4c..7871dfea1d 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php @@ -165,7 +165,7 @@ protected function assertEmptiedModerationFieldItemList() { // Test the empty value causes a violation in the entity. $violations = $this->testNode->validate(); $this->assertCount(1, $violations); - $this->assertEquals('This value should not be null.', $violations->get(0)->getMessage()); + $this->assertEquals('This value should not be null.', $violations->get(0)->getOriginalMessage()); // Test that incorrectly saving the entity regardless will not produce a // change in the moderation state. $this->testNode->save(); diff --git a/core/modules/content_translation/tests/src/Kernel/ContentTranslationFieldSyncRevisionTest.php b/core/modules/content_translation/tests/src/Kernel/ContentTranslationFieldSyncRevisionTest.php index 5e05bb0784..942a2ca404 100644 --- a/core/modules/content_translation/tests/src/Kernel/ContentTranslationFieldSyncRevisionTest.php +++ b/core/modules/content_translation/tests/src/Kernel/ContentTranslationFieldSyncRevisionTest.php @@ -443,7 +443,7 @@ protected function assertViolations(EntityConstraintViolationListInterface $viol $list = []; foreach ($violations as $violation) { - if ((string) $violation->getMessage() === $message) { + if ((string) $violation->getOriginalMessage() === $message) { $list[] = $violation; } } diff --git a/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php b/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php index 5c8c667f5e..d387dd5e71 100644 --- a/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php +++ b/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php @@ -424,7 +424,7 @@ public function testAutocreateValidation() { ]); $errors = $entity->validate(); $this->assertEqual(1, count($errors)); - $this->assertEqual($errors[0]->getMessage(), 'This value should not be null.'); + $this->assertEqual($errors[0]->getOriginalMessage(), 'This value should not be null.'); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_taxonomy_term.0'); // This should rectify the issue, favoring the entity over the target_id. $entity->save(); @@ -447,7 +447,7 @@ public function testAutocreateValidation() { $errors = $entity->validate(); $this->assertEqual(1, count($errors)); - $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $title])); + $this->assertEqual($errors[0]->getOriginalMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $title])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity'); // Publish the node and try again. @@ -495,9 +495,9 @@ public function testAutocreateValidation() { $errors = $entity->validate(); $this->assertEqual(2, count($errors)); - $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title])); + $this->assertEqual($errors[0]->getOriginalMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity'); - $this->assertEqual($errors[1]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $saved_unpublished_node->id()])); + $this->assertEqual($errors[1]->getOriginalMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $saved_unpublished_node->id()])); $this->assertEqual($errors[1]->getPropertyPath(), 'field_test_node.1.target_id'); // Publish one of the nodes and try again. @@ -505,7 +505,7 @@ public function testAutocreateValidation() { $saved_unpublished_node->save(); $errors = $entity->validate(); $this->assertEqual(1, count($errors)); - $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title])); + $this->assertEqual($errors[0]->getOriginalMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity'); // Publish the last invalid node and try again. @@ -529,7 +529,7 @@ public function testAutocreateValidation() { $errors = $entity->validate(); $this->assertEqual(1, count($errors)); - $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'comment', '%label' => $title])); + $this->assertEqual($errors[0]->getOriginalMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'comment', '%label' => $title])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_comment.0.entity'); // Publish the comment and try again. @@ -552,7 +552,7 @@ public function testAutocreateValidation() { $errors = $entity->validate(); $this->assertEqual(1, count($errors)); - $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'user', '%label' => $name])); + $this->assertEqual($errors[0]->getOriginalMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'user', '%label' => $name])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_user.0.entity'); // Activate the user and try again. @@ -575,7 +575,7 @@ public function testAutocreateValidation() { $errors = $entity->validate(); $this->assertEqual(1, count($errors)); - $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'file', '%label' => $filename])); + $this->assertEqual($errors[0]->getOriginalMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'file', '%label' => $filename])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_file.0.entity'); // Set the file as permanent and try again. diff --git a/core/modules/field/tests/src/Kernel/FieldCrudTest.php b/core/modules/field/tests/src/Kernel/FieldCrudTest.php index 6068d316fb..a0eb3c7501 100644 --- a/core/modules/field/tests/src/Kernel/FieldCrudTest.php +++ b/core/modules/field/tests/src/Kernel/FieldCrudTest.php @@ -180,10 +180,10 @@ protected function doFieldPropertyConstraintsTests() { $this->assertCount(2, $violations, 'Two violations found when using a null and outside the range value.'); $this->assertEquals($field_name . '.0.value', $violations[0]->getPropertyPath()); - $this->assertEquals(t('%name does not accept the value @value.', ['%name' => $field_name, '@value' => -2]), $violations[0]->getMessage()); + $this->assertEquals(t('%name does not accept the value @value.', ['%name' => $field_name, '@value' => -2]), $violations[0]->getOriginalMessage()); $this->assertEquals($field_name . '.0.value', $violations[1]->getPropertyPath()); - $this->assertEquals(t('This value should be %limit or more.', ['%limit' => 0]), $violations[1]->getMessage()); + $this->assertEquals(t('This value should be %limit or more.', ['%limit' => 0]), $violations[1]->getOriginalMessage()); // Check that a value that is not specifically restricted but outside the // range triggers the expected violation. @@ -191,7 +191,7 @@ protected function doFieldPropertyConstraintsTests() { $violations = $entity->validate(); $this->assertCount(1, $violations, 'Violations found when using value outside the range.'); $this->assertEquals($field_name . '.0.value', $violations[0]->getPropertyPath()); - $this->assertEquals(t('This value should be %limit or less.', ['%limit' => 32]), $violations[0]->getMessage()); + $this->assertEquals(t('This value should be %limit or less.', ['%limit' => 32]), $violations[0]->getOriginalMessage()); } /** diff --git a/core/modules/field/tests/src/Kernel/FieldValidationTest.php b/core/modules/field/tests/src/Kernel/FieldValidationTest.php index 92d33972f6..f3510a7d78 100644 --- a/core/modules/field/tests/src/Kernel/FieldValidationTest.php +++ b/core/modules/field/tests/src/Kernel/FieldValidationTest.php @@ -56,7 +56,7 @@ public function testCardinalityConstraint() { // Check that the expected constraint violations are reported. $this->assertEqual(count($violations), 1); $this->assertEqual($violations[0]->getPropertyPath(), ''); - $this->assertEqual($violations[0]->getMessage(), t('%name: this field cannot hold more than @count values.', ['%name' => $this->fieldTestData->field->getLabel(), '@count' => $cardinality])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('%name: this field cannot hold more than @count values.', ['%name' => $this->fieldTestData->field->getLabel(), '@count' => $cardinality])); } /** @@ -89,7 +89,7 @@ public function testFieldConstraints() { // Check that the expected constraint violations are reported. $violations_by_path = []; foreach ($violations as $violation) { - $violations_by_path[$violation->getPropertyPath()][] = $violation->getMessage(); + $violations_by_path[$violation->getPropertyPath()][] = $violation->getOriginalMessage(); } $this->assertEqual($violations_by_path, $expected_violations); } diff --git a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php index 6fbc82f39a..ad3c698547 100644 --- a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php +++ b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php @@ -102,9 +102,9 @@ public function testFileValidationConstraint($file_type) { $this->assertCount(2, $result); $this->assertEquals('field_test_file.0', $result->get(0)->getPropertyPath()); - $this->assertEquals('The file is 2.93 KB exceeding the maximum file size of 2 KB.', (string) $result->get(0)->getMessage()); + $this->assertEquals('The file is 2.93 KB exceeding the maximum file size of 2 KB.', (string) $result->get(0)->getOriginalMessage()); $this->assertEquals('field_test_file.0', $result->get(1)->getPropertyPath()); - $this->assertEquals('Only files with the following extensions are allowed: jpg|png.', (string) $result->get(1)->getMessage()); + $this->assertEquals('Only files with the following extensions are allowed: jpg|png.', (string) $result->get(1)->getOriginalMessage()); // Refer to a file that does not exist. $entity_test = EntityTest::create([ @@ -116,7 +116,7 @@ public function testFileValidationConstraint($file_type) { $result = $entity_test->validate(); $this->assertCount(1, $result); $this->assertEquals('field_test_file.0.target_id', $result->get(0)->getPropertyPath()); - $this->assertEquals('The referenced entity (file: 2) does not exist.', (string) $result->get(0)->getMessage()); + $this->assertEquals('The referenced entity (file: 2) does not exist.', (string) $result->get(0)->getOriginalMessage()); } /** diff --git a/core/modules/file/tests/src/Kernel/SaveTest.php b/core/modules/file/tests/src/Kernel/SaveTest.php index 73e8b25a53..14477a2731 100644 --- a/core/modules/file/tests/src/Kernel/SaveTest.php +++ b/core/modules/file/tests/src/Kernel/SaveTest.php @@ -71,7 +71,7 @@ public function testFileSave() { file_put_contents($uppercase_file_duplicate->getFileUri(), 'hello world'); $violations = $uppercase_file_duplicate->validate(); $this->assertEqual(count($violations), 1); - $this->assertEqual($violations[0]->getMessage(), t('The file %value already exists. Enter a unique file URI.', [ + $this->assertEqual($violations[0]->getOriginalMessage(), t('The file %value already exists. Enter a unique file URI.', [ '%value' => $uppercase_file_duplicate->getFileUri(), ])); // Ensure that file URI entity queries are case sensitive. diff --git a/core/modules/forum/tests/src/Kernel/ForumValidationTest.php b/core/modules/forum/tests/src/Kernel/ForumValidationTest.php index 9b9661b5a2..7cc455db15 100644 --- a/core/modules/forum/tests/src/Kernel/ForumValidationTest.php +++ b/core/modules/forum/tests/src/Kernel/ForumValidationTest.php @@ -46,7 +46,7 @@ public function testValidation() { $violations = $forum_post->validate(); $this->assertEqual(count($violations), 1); - $this->assertEqual($violations[0]->getMessage(), 'This value should not be null.'); + $this->assertEqual($violations[0]->getOriginalMessage(), 'This value should not be null.'); // Add the forum term. $forum_post->set('taxonomy_forums', $forum); @@ -57,7 +57,7 @@ public function testValidation() { $forum_post->set('taxonomy_forums', $container); $violations = $forum_post->validate(); $this->assertEqual(count($violations), 1); - $this->assertEqual($violations[0]->getMessage(), t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', [ + $this->assertEqual($violations[0]->getOriginalMessage(), t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', [ '%forum' => $container->label(), ])); } diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index 35a45f9021..481c2eccd2 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -6,8 +6,8 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Validation\ConstraintViolation; use Drupal\link\LinkItemInterface; -use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationListInterface; /** diff --git a/core/modules/media/tests/src/Kernel/MediaSourceFileTest.php b/core/modules/media/tests/src/Kernel/MediaSourceFileTest.php index c36f2b6397..c221fc7210 100644 --- a/core/modules/media/tests/src/Kernel/MediaSourceFileTest.php +++ b/core/modules/media/tests/src/Kernel/MediaSourceFileTest.php @@ -19,7 +19,7 @@ public function testFileExtensionConstraint() { $result = $media->validate(); $this->assertCount(1, $result); $this->assertSame('field_media_file.0', $result->get(0)->getPropertyPath()); - $this->assertContains('Only files with the following extensions are allowed:', (string) $result->get(0)->getMessage()); + $this->assertContains('Only files with the following extensions are allowed:', (string) $result->get(0)->getOriginalMessage()); // Create a random file that should pass. $media = $this->generateMedia('test.txt', $mediaType); diff --git a/core/modules/media/tests/src/Kernel/MediaSourceTest.php b/core/modules/media/tests/src/Kernel/MediaSourceTest.php index ecf1778504..ac591f99d0 100644 --- a/core/modules/media/tests/src/Kernel/MediaSourceTest.php +++ b/core/modules/media/tests/src/Kernel/MediaSourceTest.php @@ -373,7 +373,7 @@ public function testConstraints() { /** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */ $violations = $media->validate(); $this->assertCount(1, $violations, 'Expected number of validations not found.'); - $this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.'); + $this->assertEquals('Inappropriate text.', $violations->get(0)->getOriginalMessage(), 'Incorrect constraint validation message found.'); // Fix the violation and make sure it is not reported anymore. $media->setName('I love Drupal!'); @@ -405,7 +405,7 @@ public function testConstraints() { /** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */ $violations = $media->validate(); $this->assertCount(1, $violations, 'Expected number of validations not found.'); - $this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.'); + $this->assertEquals('Inappropriate text.', $violations->get(0)->getOriginalMessage(), 'Incorrect constraint validation message found.'); // Fix the violation and make sure it is not reported anymore. $media->set('field_media_test_constraints', 'I love Drupal!'); diff --git a/core/modules/node/tests/src/Kernel/NodeValidationTest.php b/core/modules/node/tests/src/Kernel/NodeValidationTest.php index e111faeaef..abe0f03ed9 100644 --- a/core/modules/node/tests/src/Kernel/NodeValidationTest.php +++ b/core/modules/node/tests/src/Kernel/NodeValidationTest.php @@ -44,13 +44,13 @@ public function testValidation() { $violations = $node->validate(); $this->assertEqual(count($violations), 1, 'Violation found when title is too long.'); $this->assertEqual($violations[0]->getPropertyPath(), 'title.0.value'); - $this->assertEqual($violations[0]->getMessage(), 'Title: may not be longer than 255 characters.'); + $this->assertEqual($violations[0]->getOriginalMessage(), 'Title: may not be longer than 255 characters.'); $node->set('title', NULL); $violations = $node->validate(); $this->assertEqual(count($violations), 1, 'Violation found when title is not set.'); $this->assertEqual($violations[0]->getPropertyPath(), 'title'); - $this->assertEqual($violations[0]->getMessage(), 'This value should not be null.'); + $this->assertEqual($violations[0]->getOriginalMessage(), 'This value should not be null.'); $node->set('title', ''); $violations = $node->validate(); @@ -66,7 +66,7 @@ public function testValidation() { $violations = $node->validate(); $this->assertEqual(count($violations), 1, 'Violation found when changed date is before the last changed date.'); $this->assertEqual($violations[0]->getPropertyPath(), ''); - $this->assertEqual($violations[0]->getMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.'); + $this->assertEqual($violations[0]->getOriginalMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.'); } } diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php b/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php index 03201922f4..6094236223 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php @@ -5,6 +5,7 @@ use Drupal\Component\Render\PlainTextOutput; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Validation\ConstraintViolationInterface; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; /** @@ -50,7 +51,8 @@ protected function validate(EntityInterface $entity, array $fields_to_validate = foreach ($violations as $violation) { // We strip every HTML from the error message to have a nicer to read // message on REST responses. - $message .= $violation->getPropertyPath() . ': ' . PlainTextOutput::renderFromHtml($violation->getMessage()) . "\n"; + $violation_message = $violation instanceof ConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $message .= $violation->getPropertyPath() . ': ' . PlainTextOutput::renderFromHtml($violation_message) . "\n"; } throw new UnprocessableEntityHttpException($message); } diff --git a/core/modules/taxonomy/tests/src/Kernel/TermValidationTest.php b/core/modules/taxonomy/tests/src/Kernel/TermValidationTest.php index 437a305f6f..d3d2f3434b 100644 --- a/core/modules/taxonomy/tests/src/Kernel/TermValidationTest.php +++ b/core/modules/taxonomy/tests/src/Kernel/TermValidationTest.php @@ -46,19 +46,19 @@ public function testValidation() { $this->assertEqual(count($violations), 1, 'Violation found when name is too long.'); $this->assertEqual($violations[0]->getPropertyPath(), 'name.0.value'); $field_label = $term->get('name')->getFieldDefinition()->getLabel(); - $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => 255])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => 255])); $term->set('name', NULL); $violations = $term->validate(); $this->assertEqual(count($violations), 1, 'Violation found when name is NULL.'); $this->assertEqual($violations[0]->getPropertyPath(), 'name'); - $this->assertEqual($violations[0]->getMessage(), t('This value should not be null.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('This value should not be null.')); $term->set('name', 'test'); $term->set('parent', 9999); $violations = $term->validate(); $this->assertEqual(count($violations), 1, 'Violation found when term parent is invalid.'); - $this->assertEqual($violations[0]->getMessage(), format_string('The referenced entity (%type: %id) does not exist.', ['%type' => 'taxonomy_term', '%id' => 9999])); + $this->assertEqual($violations[0]->getOriginalMessage(), format_string('The referenced entity (%type: %id) does not exist.', ['%type' => 'taxonomy_term', '%id' => 9999])); $term->set('parent', 0); $violations = $term->validate(); diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index 20eeabdc61..7897c1fed3 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -11,6 +11,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageManagerInterface; +use Drupal\Core\Validation\ConstraintViolationInterface; use Drupal\language\ConfigurableLanguageManagerInterface; use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser; use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin; @@ -380,7 +381,8 @@ protected function flagViolations(EntityConstraintViolationListInterface $violat ]; foreach ($violations->getByFields($field_names) as $violation) { list($field_name) = explode('.', $violation->getPropertyPath(), 2); - $form_state->setErrorByName($field_name, $violation->getMessage()); + $message = $violation instanceof ConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $form_state->setErrorByName($field_name, $message); } parent::flagViolations($violations, $form, $form_state); } diff --git a/core/modules/user/tests/src/Kernel/UserValidationTest.php b/core/modules/user/tests/src/Kernel/UserValidationTest.php index 7435b35e28..e72de4b948 100644 --- a/core/modules/user/tests/src/Kernel/UserValidationTest.php +++ b/core/modules/user/tests/src/Kernel/UserValidationTest.php @@ -91,7 +91,7 @@ public function testValidation() { $violations = $user->validate(); $this->assertEqual(count($violations), 1, 'Violation found when name is too long.'); $this->assertEqual($violations[0]->getPropertyPath(), 'name'); - $this->assertEqual($violations[0]->getMessage(), t('The username %name is too long: it must be %max characters or less.', ['%name' => $name, '%max' => 60])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The username %name is too long: it must be %max characters or less.', ['%name' => $name, '%max' => 60])); // Create a second test user to provoke a name collision. $user2 = User::create([ @@ -103,7 +103,7 @@ public function testValidation() { $violations = $user->validate(); $this->assertEqual(count($violations), 1, 'Violation found on name collision.'); $this->assertEqual($violations[0]->getPropertyPath(), 'name'); - $this->assertEqual($violations[0]->getMessage(), t('The username %name is already taken.', ['%name' => 'existing'])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The username %name is already taken.', ['%name' => 'existing'])); // Make the name valid. $user->set('name', $this->randomMachineName()); @@ -112,7 +112,7 @@ public function testValidation() { $violations = $user->validate(); $this->assertEqual(count($violations), 1, 'Violation found when email is invalid'); $this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value'); - $this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('This value is not a valid email address.')); $mail = $this->randomMachineName(Email::EMAIL_MAX_LENGTH - 11) . '@example.com'; $user->set('mail', $mail); @@ -123,21 +123,21 @@ public function testValidation() { // https://www.drupal.org/node/2023465. $this->assertEqual(count($violations), 2, 'Violations found when email is too long'); $this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value'); - $this->assertEqual($violations[0]->getMessage(), t('%name: the email address can not be longer than @max characters.', ['%name' => $user->get('mail')->getFieldDefinition()->getLabel(), '@max' => Email::EMAIL_MAX_LENGTH])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('%name: the email address can not be longer than @max characters.', ['%name' => $user->get('mail')->getFieldDefinition()->getLabel(), '@max' => Email::EMAIL_MAX_LENGTH])); $this->assertEqual($violations[1]->getPropertyPath(), 'mail.0.value'); - $this->assertEqual($violations[1]->getMessage(), t('This value is not a valid email address.')); + $this->assertEqual($violations[1]->getOriginalMessage(), t('This value is not a valid email address.')); // Provoke an email collision with an existing user. $user->set('mail', 'existing@example.com'); $violations = $user->validate(); $this->assertEqual(count($violations), 1, 'Violation found when email already exists.'); $this->assertEqual($violations[0]->getPropertyPath(), 'mail'); - $this->assertEqual($violations[0]->getMessage(), t('The email address %mail is already taken.', ['%mail' => 'existing@example.com'])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The email address %mail is already taken.', ['%mail' => 'existing@example.com'])); $user->set('mail', NULL); $violations = $user->validate(); $this->assertEqual(count($violations), 1, 'Email addresses may not be removed'); $this->assertEqual($violations[0]->getPropertyPath(), 'mail'); - $this->assertEqual($violations[0]->getMessage(), t('@name field is required.', ['@name' => $user->getFieldDefinition('mail')->getLabel()])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('@name field is required.', ['@name' => $user->getFieldDefinition('mail')->getLabel()])); $user->set('mail', 'someone@example.com'); $user->set('timezone', $this->randomString(33)); @@ -180,7 +180,7 @@ public function testValidation() { $violations = $user->validate(); $this->assertEqual(count($violations), 1); $this->assertEqual($violations[0]->getPropertyPath(), 'roles.1.target_id'); - $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%entity_type: %name) does not exist.', ['%entity_type' => 'user_role', '%name' => 'unknown_role'])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The referenced entity (%entity_type: %name) does not exist.', ['%entity_type' => 'user_role', '%name' => 'unknown_role'])); } /** @@ -202,7 +202,7 @@ protected function assertLengthViolation(EntityInterface $entity, $field_name, $ $this->assertEqual(count($violations), $count, "Violation found when $field_name is too long."); $this->assertEqual($violations[$expected_index]->getPropertyPath(), "$field_name.0.value"); $field_label = $entity->get($field_name)->getFieldDefinition()->getLabel(); - $this->assertEqual($violations[$expected_index]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => $length])); + $this->assertEqual($violations[$expected_index]->getOriginalMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => $length])); } /** @@ -217,7 +217,7 @@ protected function assertAllowedValuesViolation(EntityInterface $entity, $field_ $violations = $entity->validate(); $this->assertEqual(count($violations), 1, "Allowed values violation for $field_name found."); $this->assertEqual($violations[0]->getPropertyPath(), $field_name === 'langcode' ? "$field_name.0" : "$field_name.0.value"); - $this->assertEqual($violations[0]->getMessage(), t('The value you selected is not a valid choice.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The value you selected is not a valid choice.')); } } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index f46d61e0cb..d28b27d6d9 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -18,6 +18,7 @@ use Drupal\Core\Session\AnonymousUserSession; use Drupal\Core\Site\Settings; use Drupal\Core\Url; +use Drupal\Core\Validation\ConstraintViolationInterface; use Drupal\system\Entity\Action; use Drupal\user\Entity\Role; use Drupal\user\Entity\User; @@ -290,7 +291,7 @@ function user_validate_name($name) { $data->setValue($name); $violations = $data->validate(); if (count($violations) > 0) { - return $violations[0]->getMessage(); + return $violations[0] instanceof ConstraintViolationInterface ? $violations[0]->getOriginalMessage() : $violations[0]->getMessage(); } } diff --git a/core/modules/workspaces/src/Form/WorkspaceForm.php b/core/modules/workspaces/src/Form/WorkspaceForm.php index fe022162d7..0bc76ed24d 100644 --- a/core/modules/workspaces/src/Form/WorkspaceForm.php +++ b/core/modules/workspaces/src/Form/WorkspaceForm.php @@ -10,6 +10,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; +use Drupal\Core\Validation\ConstraintViolationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -115,7 +116,8 @@ protected function flagViolations(EntityConstraintViolationListInterface $violat ]; foreach ($violations->getByFields($field_names) as $violation) { list($field_name) = explode('.', $violation->getPropertyPath(), 2); - $form_state->setErrorByName($field_name, $violation->getMessage()); + $message = $violation instanceof ConstraintViolationInterface ? $violation->getOriginalMessage() : $violation->getMessage(); + $form_state->setErrorByName($field_name, $message); } parent::flagViolations($violations, $form, $form_state); } diff --git a/core/modules/workspaces/tests/src/Functional/WorkspaceConcurrentEditingTest.php b/core/modules/workspaces/tests/src/Functional/WorkspaceConcurrentEditingTest.php index 996e72decc..7434627f83 100644 --- a/core/modules/workspaces/tests/src/Functional/WorkspaceConcurrentEditingTest.php +++ b/core/modules/workspaces/tests/src/Functional/WorkspaceConcurrentEditingTest.php @@ -69,7 +69,7 @@ public function testSwitchingWorkspaces() { // Check that the node fails validation for API calls. $violations = $test_node->validate(); $this->assertCount(1, $violations); - $this->assertEquals('The content is being edited in the Vultures workspace. As a result, your changes cannot be saved.', $violations->get(0)->getMessage()); + $this->assertEquals('The content is being edited in the Vultures workspace. As a result, your changes cannot be saved.', $violations->get(0)->getOriginalMessage()); // Switch to the Live workspace and check that the user still can not edit // the node. @@ -83,7 +83,7 @@ public function testSwitchingWorkspaces() { // Check that the node fails validation for API calls. $violations = $test_node->validate(); $this->assertCount(1, $violations); - $this->assertEquals('The content is being edited in the Vultures workspace. As a result, your changes cannot be saved.', $violations->get(0)->getMessage()); + $this->assertEquals('The content is being edited in the Vultures workspace. As a result, your changes cannot be saved.', $violations->get(0)->getOriginalMessage()); // Deploy the changes from the 'Vultures' workspace and check that the node // can be edited again in other workspaces. diff --git a/core/modules/workspaces/tests/src/Kernel/EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php b/core/modules/workspaces/tests/src/Kernel/EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php index 5cef79df93..d6b67530d3 100644 --- a/core/modules/workspaces/tests/src/Kernel/EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php +++ b/core/modules/workspaces/tests/src/Kernel/EntityReferenceSupportedNewEntitiesConstraintValidatorTest.php @@ -75,7 +75,7 @@ public function testNewEntitiesForbiddenInNonDefaultWorkspace() { ]); $violations = $entity->validate(); $this->assertCount(1, $violations); - $this->assertEquals('Test entity entities can only be created in the default workspace.', $violations[0]->getMessage()); + $this->assertEquals('Test entity entities can only be created in the default workspace.', $violations[0]->getOriginalMessage()); } } diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php index 76c48869bb..40c5a7b54d 100644 --- a/core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php +++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php @@ -170,7 +170,7 @@ public function testDeletingWorkspaces() { ]); $violations = $workspace_3->validate(); $this->assertCount(1, $violations); - $this->assertEquals('A workspace with this ID has been deleted but data still exists for it.', $violations[0]->getMessage()); + $this->assertEquals('A workspace with this ID has been deleted but data still exists for it.', $violations[0]->getOriginalMessage()); // Running cron should delete the remaining data as well as the workspace ID // from the "workspace.delete" state entry. diff --git a/core/tests/Drupal/KernelTests/Config/TypedConfigTest.php b/core/tests/Drupal/KernelTests/Config/TypedConfigTest.php index bc9c441d51..4ea69b5fef 100644 --- a/core/tests/Drupal/KernelTests/Config/TypedConfigTest.php +++ b/core/tests/Drupal/KernelTests/Config/TypedConfigTest.php @@ -111,7 +111,7 @@ public function testSimpleConfigValidation() { $result = $typed_config->validate(); // Its not a valid llama anymore. $this->assertCount(1, $result); - $this->assertEquals('no valid llama', $result->get(0)->getMessage()); + $this->assertEquals('no valid llama', $result->get(0)->getOriginalMessage()); // Test constraints on mapping. $config->set('llama', 'llama'); @@ -129,7 +129,7 @@ public function testSimpleConfigValidation() { $typed_config = $typed_config_manager->get('config_test.validation'); $result = $typed_config->validate(); $this->assertCount(1, $result); - $this->assertEquals('no valid cat', $result->get(0)->getMessage()); + $this->assertEquals('no valid cat', $result->get(0)->getOriginalMessage()); // Test constrains on sequences elements. $config->set('cat.type', 'nyans'); @@ -138,7 +138,7 @@ public function testSimpleConfigValidation() { $typed_config = $typed_config_manager->get('config_test.validation'); $result = $typed_config->validate(); $this->assertCount(1, $result); - $this->assertEquals('Giraffes just hum', $result->get(0)->getMessage()); + $this->assertEquals('Giraffes just hum', $result->get(0)->getOriginalMessage()); // Test constrains on the sequence itself. $config->set('giraffe', ['hum', 'hum2', 'invalid-key' => 'hum']); @@ -148,7 +148,7 @@ public function testSimpleConfigValidation() { $result = $typed_config->validate(); $this->assertCount(1, $result); $this->assertEquals('giraffe', $result->get(0)->getPropertyPath()); - $this->assertEquals('Invalid giraffe key.', $result->get(0)->getMessage()); + $this->assertEquals('Invalid giraffe key.', $result->get(0)->getOriginalMessage()); // Validates mapping. $typed_config = $typed_config_manager->get('config_test.validation'); @@ -160,7 +160,7 @@ public function testSimpleConfigValidation() { $result = $typed_config->validate(); $this->assertCount(1, $result); $this->assertEquals('', $result->get(0)->getPropertyPath()); - $this->assertEquals('Unexpected keys: elephant, zebra', $result->get(0)->getMessage()); + $this->assertEquals('Unexpected keys: elephant, zebra', $result->get(0)->getOriginalMessage()); } } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/BundleConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/Entity/BundleConstraintValidatorTest.php index 613ba4e17d..264235e133 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/BundleConstraintValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/BundleConstraintValidatorTest.php @@ -64,7 +64,7 @@ protected function assertValidation($bundle) { // Make sure the information provided by a violation is correct. $violation = $violations[0]; - $this->assertEqual($violation->getMessage(), t('The entity must be of bundle %bundle.', ['%bundle' => implode(', ', (array) $bundle)]), 'The message for invalid value is correct.'); + $this->assertEqual($violation->getOriginalMessage(), t('The entity must be of bundle %bundle.', ['%bundle' => implode(', ', (array) $bundle)]), 'The message for invalid value is correct.'); $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.'); $this->assertEqual($violation->getInvalidValue(), $page_node, 'The invalid value is set correctly in the violation.'); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php index af9403c320..15baac1220 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php @@ -73,7 +73,7 @@ public function testValidate() { $violations = $adapter->validate(); $this->assertCount(1, $violations); $violation = $violations->get(0); - $this->assertEquals('This value should be of the correct primitive type.', $violation->getMessage()); + $this->assertEquals('This value should be of the correct primitive type.', $violation->getOriginalMessage()); $this->assertEquals('weight', $violation->getPropertyPath()); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php index 8fa31c32f7..a65f20eafd 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php @@ -419,7 +419,7 @@ protected function doEditStep($active_langcode, $default_revision, $untranslatab $messages = []; foreach ($violations as $violation) { /** \Symfony\Component\Validator\ConstraintViolationInterface */ - $messages[] = $violation->getMessage(); + $messages[] = $violation->getOriginalMessage(); } $this->assertEquals($valid, !$violations->count(), $this->formatMessage('Validation does not match the expected result: %s', implode(', ', $messages))); @@ -519,7 +519,7 @@ public function testMultipleTranslationChanges() { $revision->getTranslation('it')->get('name')->value = 'Test 1.3 IT'; $violations = $revision->validate(); $this->assertCount(1, $violations); - $this->assertEquals('Non-translatable fields can only be changed when updating the original language.', $violations[0]->getMessage()); + $this->assertEquals('Non-translatable fields can only be changed when updating the original language.', $violations[0]->getOriginalMessage()); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityHasFieldConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityHasFieldConstraintValidatorTest.php index eebc94f523..c5af9f70f2 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityHasFieldConstraintValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityHasFieldConstraintValidatorTest.php @@ -47,7 +47,7 @@ public function testValidation() { // field has been created. $violations = $entity->validate(); $this->assertCount(1, $violations); - $this->assertEquals($violations[0]->getMessage(), 'The entity must have the body field.'); + $this->assertEquals($violations[0]->getOriginalMessage(), 'The entity must have the body field.'); $storage->save($entity); // Create the field. diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php index 5c57a6531d..26592e372a 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php @@ -104,7 +104,7 @@ public function testEntityReferenceFieldValidation() { $entity->{$this->fieldName}->target_id = 9999; $violations = $entity->{$this->fieldName}->validate(); $this->assertEqual($violations->count(), 1, 'Validation throws a violation.'); - $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', ['%type' => $this->referencedEntityType, '%id' => 9999])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The referenced entity (%type: %id) does not exist.', ['%type' => $this->referencedEntityType, '%id' => 9999])); // Test a non-referenceable bundle. entity_test_create_bundle('non_referenceable', NULL, $this->referencedEntityType); @@ -113,7 +113,7 @@ public function testEntityReferenceFieldValidation() { $entity->{$this->fieldName}->target_id = $referenced_entity->id(); $violations = $entity->{$this->fieldName}->validate(); $this->assertEqual($violations->count(), 1, 'Validation throws a violation.'); - $this->assertEqual($violations[0]->getMessage(), t('This entity (%type: %id) cannot be referenced.', ['%type' => $this->referencedEntityType, '%id' => $referenced_entity->id()])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('This entity (%type: %id) cannot be referenced.', ['%type' => $this->referencedEntityType, '%id' => $referenced_entity->id()])); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintValidatorTest.php index 3671d6d408..acec7de2a6 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintValidatorTest.php @@ -53,7 +53,7 @@ public function testValidation() { // Make sure the information provided by a violation is correct. $violation = $violations[0]; - $this->assertEqual($violation->getMessage(), t('The entity must be of type %type.', ['%type' => $entity_type]), 'The message for invalid value is correct.'); + $this->assertEqual($violation->getOriginalMessage(), t('The entity must be of type %type.', ['%type' => $entity_type]), 'The message for invalid value is correct.'); $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.'); $this->assertEqual($violation->getInvalidValue(), $account, 'The invalid value is set correctly in the violation.'); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintsTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintsTest.php index 7dd98da1fc..214fec1ea5 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintsTest.php @@ -67,7 +67,7 @@ public function testConstraintValidation() { $entity->changed->value = REQUEST_TIME - 86400; $violations = $entity->validate(); $this->assertEqual($violations->count(), 1, 'Validation failed.'); - $this->assertEqual($violations[0]->getMessage(), t('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.')); } } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php index 4a16305362..d9c43ad780 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php @@ -135,13 +135,13 @@ protected function checkValidation($entity_type) { $test_entity->id->value = -1; $violations = $test_entity->validate(); $this->assertEqual($violations->count(), 1, 'Validation failed.'); - $this->assertEqual($violations[0]->getMessage(), t('%name: The integer must be larger or equal to %min.', ['%name' => 'ID', '%min' => 0])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('%name: The integer must be larger or equal to %min.', ['%name' => 'ID', '%min' => 0])); $test_entity = clone $entity; $test_entity->uuid->value = $this->randomString(129); $violations = $test_entity->validate(); $this->assertEqual($violations->count(), 1, 'Validation failed.'); - $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => 'UUID', '@max' => 128])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('%name: may not be longer than @max characters.', ['%name' => 'UUID', '@max' => 128])); $test_entity = clone $entity; $langcode_key = $this->entityManager->getDefinition($entity_type)->getKey('langcode'); @@ -149,20 +149,20 @@ protected function checkValidation($entity_type) { $violations = $test_entity->validate(); // This should fail on AllowedValues and Length constraints. $this->assertEqual($violations->count(), 2, 'Validation failed.'); - $this->assertEqual($violations[0]->getMessage(), t('This value is too long. It should have %limit characters or less.', ['%limit' => '12'])); - $this->assertEqual($violations[1]->getMessage(), t('The value you selected is not a valid choice.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('This value is too long. It should have %limit characters or less.', ['%limit' => '12'])); + $this->assertEqual($violations[1]->getOriginalMessage(), t('The value you selected is not a valid choice.')); $test_entity = clone $entity; $test_entity->type->value = NULL; $violations = $test_entity->validate(); $this->assertEqual($violations->count(), 1, 'Validation failed.'); - $this->assertEqual($violations[0]->getMessage(), t('This value should not be null.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('This value should not be null.')); $test_entity = clone $entity; $test_entity->name->value = $this->randomString(33); $violations = $test_entity->validate(); $this->assertEqual($violations->count(), 1, 'Validation failed.'); - $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => 'Name', '@max' => 32])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('%name: may not be longer than @max characters.', ['%name' => 'Name', '@max' => 32])); // Make sure the information provided by a violation is correct. $violation = $violations[0]; @@ -174,13 +174,13 @@ protected function checkValidation($entity_type) { $test_entity->set('user_id', 9999); $violations = $test_entity->validate(); $this->assertEqual($violations->count(), 1, 'Validation failed.'); - $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', ['%type' => 'user', '%id' => 9999])); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The referenced entity (%type: %id) does not exist.', ['%type' => 'user', '%id' => 9999])); $test_entity = clone $entity; $test_entity->field_test_text->format = $this->randomString(33); $violations = $test_entity->validate(); $this->assertEqual($violations->count(), 1, 'Validation failed.'); - $this->assertEqual($violations[0]->getMessage(), t('The value you selected is not a valid choice.')); + $this->assertEqual($violations[0]->getOriginalMessage(), t('The value you selected is not a valid choice.')); // Make sure the information provided by a violation is correct. $violation = $violations[0]; @@ -227,7 +227,7 @@ public function testEntityChangedConstraintOnConcurrentMultilingualEditing() { $entity->setChangedTime($entity->getChangedTime() - 1); $violations = $entity->validate(); $this->assertEquals(1, $violations->count()); - $this->assertEqual($violations[0]->getMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.'); + $this->assertEqual($violations[0]->getOriginalMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.'); $entity = $storage->loadUnchanged($entity->id()); $translation = $entity->addTranslation('de'); @@ -254,7 +254,7 @@ public function testEntityChangedConstraintOnConcurrentMultilingualEditing() { // previous version of the entity and thus reverting changes by other users. $violations = $entity->validate(); $this->assertEquals(1, $violations->count()); - $this->assertEqual($violations[0]->getMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.'); + $this->assertEqual($violations[0]->getOriginalMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.'); } } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ValidReferenceConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ValidReferenceConstraintValidatorTest.php index 3ac600285e..a217955751 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/ValidReferenceConstraintValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ValidReferenceConstraintValidatorTest.php @@ -76,7 +76,7 @@ public function testValidation() { // Make sure the information provided by a violation is correct. $violation = $violations[0]; - $this->assertEqual($violation->getMessage(), t('The referenced entity (%type: %id) does not exist.', [ + $this->assertEqual($violation->getOriginalMessage(), t('The referenced entity (%type: %id) does not exist.', [ '%type' => 'user', '%id' => $entity->id(), ]), 'The message for invalid value is correct.'); @@ -169,7 +169,7 @@ public function testPreExistingItemsValidation() { $this->assertEquals(t('This entity (%type: %id) cannot be referenced.', [ '%type' => 'node', '%id' => $unpublished_node->id(), - ]), $violations[0]->getMessage()); + ]), $violations[0]->getOriginalMessage()); // Now save the referencing entity which will create a pre-existing state // for it and repeat the checks. This time, the user without access should @@ -213,7 +213,7 @@ public function testPreExistingItemsValidation() { $this->assertEquals(t('This entity (%type: %id) cannot be referenced.', [ '%type' => 'node', '%id' => $different_bundle_node->id(), - ]), $violations[0]->getMessage()); + ]), $violations[0]->getOriginalMessage()); // Delete the last node and check that the pre-existing reference is not // valid anymore. @@ -224,11 +224,11 @@ public function testPreExistingItemsValidation() { $this->assertEquals(t('This entity (%type: %id) cannot be referenced.', [ '%type' => 'node', '%id' => $different_bundle_node->id(), - ]), $violations[0]->getMessage()); + ]), $violations[0]->getOriginalMessage()); $this->assertEquals(t('The referenced entity (%type: %id) does not exist.', [ '%type' => 'node', '%id' => $deleted_node->id(), - ]), $violations[1]->getMessage()); + ]), $violations[1]->getOriginalMessage()); } } diff --git a/core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php index 5dcbfccddc..939e315942 100644 --- a/core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php @@ -47,7 +47,7 @@ public function testValidation() { // Make sure the information provided by a violation is correct. $violation = $violations[0]; - $this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.'); + $this->assertEqual($violation->getOriginalMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.'); $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.'); $this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.'); diff --git a/core/tests/Drupal/KernelTests/Core/TypedData/ComplexDataConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/TypedData/ComplexDataConstraintValidatorTest.php index 54f2872ab8..4bb8e0fa84 100644 --- a/core/tests/Drupal/KernelTests/Core/TypedData/ComplexDataConstraintValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/TypedData/ComplexDataConstraintValidatorTest.php @@ -53,7 +53,7 @@ public function testValidation() { // Make sure the information provided by a violation is correct. $violation = $violations[0]; - $this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.'); + $this->assertEqual($violation->getOriginalMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.'); $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.'); $this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.'); diff --git a/core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php b/core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php index 760b2d081d..2aec67b313 100644 --- a/core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php +++ b/core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php @@ -587,7 +587,7 @@ public function testTypedDataValidation() { // Test translating violation messages. $message = t('This value should be %limit or more.', ['%limit' => 5]); - $this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.'); + $this->assertEqual($violations[0]->getOriginalMessage(), $message, 'Translated violation message retrieved.'); $this->assertEqual($violations[0]->getPropertyPath(), ''); $this->assertIdentical($violations[0]->getRoot(), $integer, 'Root object returned.'); @@ -599,7 +599,7 @@ public function testTypedDataValidation() { $violations = $this->typedDataManager->create($definition, "short")->validate(); $this->assertEqual($violations->count(), 1); $message = t('This value is too short. It should have %limit characters or more.', ['%limit' => 10]); - $this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.'); + $this->assertEqual($violations[0]->getOriginalMessage(), $message, 'Translated violation message retrieved.'); // Test having multiple violations. $definition = DataDefinition::create('integer') diff --git a/core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php b/core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php index 3fbdf4e141..447596d3cd 100644 --- a/core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php +++ b/core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php @@ -90,7 +90,7 @@ public function testEntityWithStringIdWithViolation($id) { // Check that the validation has created the appropriate violation. $this->assertCount(1, $violations); - $this->assertEquals($message, $violations[0]->getMessage()); + $this->assertEquals($message, $violations[0]->getOriginalMessage()); } /** diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php index 4afbfe18b6..18dafb24c7 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php @@ -6,8 +6,8 @@ use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Session\AccountInterface; use Drupal\entity_test\Plugin\Validation\Constraint\EntityTestCompositeConstraint; +use Drupal\Core\Validation\ConstraintViolation; use Drupal\Tests\UnitTestCase; -use Symfony\Component\Validator\ConstraintViolation; /** * @coversDefaultClass \Drupal\Core\Entity\EntityConstraintViolationList diff --git a/core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php b/core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php index 02ee137dae..79834e523b 100644 --- a/core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php +++ b/core/tests/Drupal/Tests/Core/TypedData/RecursiveContextualValidatorTest.php @@ -137,7 +137,7 @@ public function testBasicValidateWithConstraint() { $violations = $this->recursiveValidator->validate($typed_data); $this->assertCount(1, $violations); // Ensure that the right value is passed into the validator. - $this->assertEquals('test violation: foo', $violations->get(0)->getMessage()); + $this->assertEquals('test violation: foo', $violations->get(0)->getOriginalMessage()); } /** @@ -168,12 +168,12 @@ public function testPropertiesValidateWithMultipleLevels() { $violations = $this->recursiveValidator->validate($typed_data); $this->assertCount(6, $violations); - $this->assertEquals('violation: 3', $violations->get(0)->getMessage()); - $this->assertEquals('violation: value1', $violations->get(1)->getMessage()); - $this->assertEquals('violation: value2', $violations->get(2)->getMessage()); - $this->assertEquals('violation: 2', $violations->get(3)->getMessage()); - $this->assertEquals('violation: subvalue1', $violations->get(4)->getMessage()); - $this->assertEquals('violation: subvalue2', $violations->get(5)->getMessage()); + $this->assertEquals('violation: 3', $violations->get(0)->getOriginalMessage()); + $this->assertEquals('violation: value1', $violations->get(1)->getOriginalMessage()); + $this->assertEquals('violation: value2', $violations->get(2)->getOriginalMessage()); + $this->assertEquals('violation: 2', $violations->get(3)->getOriginalMessage()); + $this->assertEquals('violation: subvalue1', $violations->get(4)->getOriginalMessage()); + $this->assertEquals('violation: subvalue2', $violations->get(5)->getOriginalMessage()); $this->assertEquals('', $violations->get(0)->getPropertyPath()); $this->assertEquals('key1', $violations->get(1)->getPropertyPath()); @@ -272,9 +272,9 @@ public function testValidateProperty() { $violations = $this->recursiveValidator->validateProperty($typed_data, 'key_with_properties'); $this->assertCount(3, $violations); - $this->assertEquals('violation: 2', $violations->get(0)->getMessage()); - $this->assertEquals('violation: subvalue1', $violations->get(1)->getMessage()); - $this->assertEquals('violation: subvalue2', $violations->get(2)->getMessage()); + $this->assertEquals('violation: 2', $violations->get(0)->getOriginalMessage()); + $this->assertEquals('violation: subvalue1', $violations->get(1)->getOriginalMessage()); + $this->assertEquals('violation: subvalue2', $violations->get(2)->getOriginalMessage()); $this->assertEquals('', $violations->get(0)->getPropertyPath()); $this->assertEquals('subkey1', $violations->get(1)->getPropertyPath()); @@ -300,9 +300,9 @@ public function testValidatePropertyValue() { $violations = $this->recursiveValidator->validatePropertyValue($typed_data, 'key_with_properties', $typed_data->get('key_with_properties')); $this->assertCount(3, $violations); - $this->assertEquals('violation: 2', $violations->get(0)->getMessage()); - $this->assertEquals('violation: subvalue11', $violations->get(1)->getMessage()); - $this->assertEquals('violation: subvalue22', $violations->get(2)->getMessage()); + $this->assertEquals('violation: 2', $violations->get(0)->getOriginalMessage()); + $this->assertEquals('violation: subvalue11', $violations->get(1)->getOriginalMessage()); + $this->assertEquals('violation: subvalue22', $violations->get(2)->getOriginalMessage()); $this->assertEquals('', $violations->get(0)->getPropertyPath()); $this->assertEquals('subkey1', $violations->get(1)->getPropertyPath());