Verified Commit 84c6219a authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3055319 by idebr, mmbk, cilefen, quietone, catch: Constraint violations...

Issue #3055319 by idebr, mmbk, cilefen, quietone, catch: Constraint violations are not triggered for Roles on a user's account form

(cherry picked from commit 33da06fd)
parent 7b665e5a
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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',
+11 −0
Original line number Diff line number Diff line
@@ -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::keyvalue('user_form_test')->get('user_form_test_constraint_roles_edit')) {
      $fields['roles']->addConstraint('FieldWidgetConstraint');
    }
  }

}
+17 −0
Original line number Diff line number Diff line
@@ -283,4 +283,21 @@ public function testAdminSelfBlocking(): void {
    $this->assertSession()->fieldEnabled('edit-status-1');
  }

  /**
   * 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::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');
    $this->assertSession()->pageTextContains('Widget constraint has failed.');
    $this->assertSession()->pageTextNotContains('The changes have been saved.');
  }

}