From 6fea54ed212548b409610c32d6d3a7f4c0d4cbf0 Mon Sep 17 00:00:00 2001
From: Ide Braakman <ide@ezcompany.nl>
Date: Thu, 27 Feb 2025 14:45:44 +0100
Subject: [PATCH 1/2] Issue #3055319: Constraint violations are not triggered
 for Roles on a user's account form

---
 core/modules/user/src/AccountForm.php           |  2 ++
 .../src/Hook/UserFormTestHooks.php              | 11 +++++++++++
 .../user/tests/src/Functional/UserEditTest.php  | 17 +++++++++++++++++
 3 files changed, 30 insertions(+)

diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php
index 921c7c967079..c8e19a5792fc 100644
--- a/core/modules/user/src/AccountForm.php
+++ b/core/modules/user/src/AccountForm.php
@@ -402,6 +402,7 @@ protected function getEditedFieldNames(FormStateInterface $form_state) {
       'name',
       'pass',
       'mail',
+      'roles',
       'timezone',
       'langcode',
       'preferred_langcode',
@@ -420,6 +421,7 @@ protected function flagViolations(EntityConstraintViolationListInterface $violat
       'name',
       'pass',
       'mail',
+      'roles',
       'timezone',
       'langcode',
       'preferred_langcode',
diff --git a/core/modules/user/tests/modules/user_form_test/src/Hook/UserFormTestHooks.php b/core/modules/user/tests/modules/user_form_test/src/Hook/UserFormTestHooks.php
index f49008597f10..1fb3c835f617 100644
--- a/core/modules/user/tests/modules/user_form_test/src/Hook/UserFormTestHooks.php
+++ b/core/modules/user/tests/modules/user_form_test/src/Hook/UserFormTestHooks.php
@@ -4,6 +4,7 @@
 
 namespace Drupal\user_form_test\Hook;
 
+use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Hook\Attribute\Hook;
 
 /**
@@ -20,4 +21,14 @@ public function formUserCancelFormAlter(&$form, &$form_state) : void {
     $form['access']['#value'] = \Drupal::currentUser()->hasPermission('cancel other accounts');
   }
 
+  /**
+   * Implements hook_entity_base_field_info_alter().
+   */
+  #[Hook('entity_base_field_info_alter')]
+  public function entityBaseFieldInfoAlter(&$fields, EntityTypeInterface $entity_type): void {
+    if ($entity_type->id() === 'user' && \Drupal::state()->get('user_form_test_constraint_roles_edit')) {
+      $fields['roles']->addConstraint('FieldWidgetConstraint');
+    }
+  }
+
 }
diff --git a/core/modules/user/tests/src/Functional/UserEditTest.php b/core/modules/user/tests/src/Functional/UserEditTest.php
index ad05af938edc..d090915ec434 100644
--- a/core/modules/user/tests/src/Functional/UserEditTest.php
+++ b/core/modules/user/tests/src/Functional/UserEditTest.php
@@ -260,4 +260,21 @@ public function testUserMailFieldAccess(): void {
     $this->assertFalse($this->getSession()->getPage()->hasField('mail'));
   }
 
+  /**
+   * Tests constraint violations are triggered on the user account form.
+   */
+  public function testRolesValidation(): void {
+    $admin_user = $this->drupalCreateUser(['administer users']);
+    $this->drupalLogin($admin_user);
+    $this->drupalGet("user/" . $admin_user->id() . "/edit");
+    $this->submitForm([], 'Save');
+    $this->assertSession()->pageTextContains('The changes have been saved.');
+    \Drupal::state()->set('user_form_test_constraint_roles_edit', TRUE);
+    \Drupal::service('module_installer')->install(['entity_test', 'user_form_test']);
+    $this->drupalGet("user/" . $admin_user->id() . "/edit");
+    $this->submitForm([], 'Save');
+    $this->assertSession()->pageTextContains('Widget constraint has failed.');
+    $this->assertSession()->pageTextNotContains('The changes have been saved.');
+  }
+
 }
-- 
GitLab


From 38c6b409c1464b7594b2e15fdfb36b72e332f8f1 Mon Sep 17 00:00:00 2001
From: Ide Braakman <ide@ezcompany.nl>
Date: Thu, 10 Apr 2025 14:21:11 +0200
Subject: [PATCH 2/2] Replace state with key/value in test coverage

---
 .../tests/modules/user_form_test/src/Hook/UserFormTestHooks.php | 2 +-
 core/modules/user/tests/src/Functional/UserEditTest.php         | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/core/modules/user/tests/modules/user_form_test/src/Hook/UserFormTestHooks.php b/core/modules/user/tests/modules/user_form_test/src/Hook/UserFormTestHooks.php
index 1fb3c835f617..106199f413fe 100644
--- a/core/modules/user/tests/modules/user_form_test/src/Hook/UserFormTestHooks.php
+++ b/core/modules/user/tests/modules/user_form_test/src/Hook/UserFormTestHooks.php
@@ -26,7 +26,7 @@ public function formUserCancelFormAlter(&$form, &$form_state) : void {
    */
   #[Hook('entity_base_field_info_alter')]
   public function entityBaseFieldInfoAlter(&$fields, EntityTypeInterface $entity_type): void {
-    if ($entity_type->id() === 'user' && \Drupal::state()->get('user_form_test_constraint_roles_edit')) {
+    if ($entity_type->id() === 'user' && \Drupal::keyvalue('user_form_test')->get('user_form_test_constraint_roles_edit')) {
       $fields['roles']->addConstraint('FieldWidgetConstraint');
     }
   }
diff --git a/core/modules/user/tests/src/Functional/UserEditTest.php b/core/modules/user/tests/src/Functional/UserEditTest.php
index d090915ec434..377357828956 100644
--- a/core/modules/user/tests/src/Functional/UserEditTest.php
+++ b/core/modules/user/tests/src/Functional/UserEditTest.php
@@ -269,7 +269,7 @@ public function testRolesValidation(): void {
     $this->drupalGet("user/" . $admin_user->id() . "/edit");
     $this->submitForm([], 'Save');
     $this->assertSession()->pageTextContains('The changes have been saved.');
-    \Drupal::state()->set('user_form_test_constraint_roles_edit', TRUE);
+    \Drupal::keyvalue('user_form_test')->set('user_form_test_constraint_roles_edit', TRUE);
     \Drupal::service('module_installer')->install(['entity_test', 'user_form_test']);
     $this->drupalGet("user/" . $admin_user->id() . "/edit");
     $this->submitForm([], 'Save');
-- 
GitLab