From 7e29102f78acd1c9033f29448f46973f27783aba Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Mon, 21 Oct 2024 12:26:53 +0100
Subject: [PATCH] Issue #2462105 by jhedstrom, anmolgoyal74, nexusnovaz,
 borisson_, berdir, mkalkbrenner, fago: Improve failed password hashing
 exception message

---
 .../Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php | 2 +-
 .../KernelTests/Core/Field/FieldType/PasswordItemTest.php     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php
index a7c8460a7c48..a846a6f06240 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php
@@ -56,7 +56,7 @@ public function preSave() {
       $this->value = \Drupal::service('password')->hash(trim($this->value));
       // Abort if the hashing failed and returned FALSE.
       if (!$this->value) {
-        throw new EntityMalformedException('The entity does not have a password.');
+        throw new EntityMalformedException(sprintf("Failed to hash the %s password.", $entity->getEntityType()->getLabel()));
       }
     }
 
diff --git a/core/tests/Drupal/KernelTests/Core/Field/FieldType/PasswordItemTest.php b/core/tests/Drupal/KernelTests/Core/Field/FieldType/PasswordItemTest.php
index c3d6fc47392f..2eaada39c34b 100644
--- a/core/tests/Drupal/KernelTests/Core/Field/FieldType/PasswordItemTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Field/FieldType/PasswordItemTest.php
@@ -168,7 +168,7 @@ public function testPreSaveExceptionNew(): void {
     $entity = EntityTest::create();
     $entity->test_field = str_repeat('a', PasswordInterface::PASSWORD_MAX_LENGTH + 1);
     $this->expectException(EntityStorageException::class);
-    $this->expectExceptionMessage('The entity does not have a password');
+    $this->expectExceptionMessage('Failed to hash the Test entity password.');
     $entity->save();
   }
 
@@ -183,7 +183,7 @@ public function testPreSaveExceptionExisting(): void {
     $this->assertNotEquals('will_be_hashed', $entity->test_field->value);
 
     $this->expectException(EntityStorageException::class);
-    $this->expectExceptionMessage('The entity does not have a password');
+    $this->expectExceptionMessage('Failed to hash the Test entity password.');
     $entity->test_field = str_repeat('a', PasswordInterface::PASSWORD_MAX_LENGTH + 1);
     $entity->save();
   }
-- 
GitLab