Loading email_registration.module +7 −1 Original line number Diff line number Diff line Loading @@ -122,7 +122,13 @@ function email_registration_cleanup_username($name) { * Implements hook_form_BASE_FORM_ID_alter(). */ function email_registration_form_user_form_alter(&$form, FormStateInterface $form_state) { $account = \Drupal::currentUser(); // Allow users to edit their own username or admins to do it if appropriate. if (!$account->hasPermission('change own username') && !$account->hasPermission('administer users')) { $form['account']['name']['#type'] = 'value'; } // Provide a random user name only if we are creating a new user, i.e. this // is a registration. if ($form_state->getFormObject()->getEntity()->isNew()) { Loading tests/src/Functional/EmailRegistrationTestCase.php +30 −0 Original line number Diff line number Diff line Loading @@ -119,4 +119,34 @@ class EmailRegistrationTestCase extends BrowserTestBase { $this->drupalLogin($user); } /** * Test the "change own username" permission and user edit save. */ public function testUsernamePermissions() { // Set login_with_username to TRUE for $this->>drupalLogin. $this->container->get('config.factory') ->getEditable('email_registration.settings') ->set('login_with_username', TRUE) ->save(TRUE); $user = $this->createUser(['change own username']); $this->drupalLogin($user); $this->drupalGet('user/' . $user->id() . '/edit'); $this->assertSession()->fieldExists('edit-name'); $this->drupalLogout($user); $user = $this->createUser(); $username = $user->getAccountName(); $this->drupalLogin($user); $this->drupalGet('user/' . $user->id() . '/edit'); // Test that the field is set to type=value. $this->assertSession()->fieldNotExists('edit-name'); $this->assertSession()->pageTextContains($username); // Make sure the email isn't changed on save. $this->drupalPostForm('user/' . $user->id() . '/edit', [], 'Save'); $this->assertSession()->pageTextContains($username); } } Loading
email_registration.module +7 −1 Original line number Diff line number Diff line Loading @@ -122,7 +122,13 @@ function email_registration_cleanup_username($name) { * Implements hook_form_BASE_FORM_ID_alter(). */ function email_registration_form_user_form_alter(&$form, FormStateInterface $form_state) { $account = \Drupal::currentUser(); // Allow users to edit their own username or admins to do it if appropriate. if (!$account->hasPermission('change own username') && !$account->hasPermission('administer users')) { $form['account']['name']['#type'] = 'value'; } // Provide a random user name only if we are creating a new user, i.e. this // is a registration. if ($form_state->getFormObject()->getEntity()->isNew()) { Loading
tests/src/Functional/EmailRegistrationTestCase.php +30 −0 Original line number Diff line number Diff line Loading @@ -119,4 +119,34 @@ class EmailRegistrationTestCase extends BrowserTestBase { $this->drupalLogin($user); } /** * Test the "change own username" permission and user edit save. */ public function testUsernamePermissions() { // Set login_with_username to TRUE for $this->>drupalLogin. $this->container->get('config.factory') ->getEditable('email_registration.settings') ->set('login_with_username', TRUE) ->save(TRUE); $user = $this->createUser(['change own username']); $this->drupalLogin($user); $this->drupalGet('user/' . $user->id() . '/edit'); $this->assertSession()->fieldExists('edit-name'); $this->drupalLogout($user); $user = $this->createUser(); $username = $user->getAccountName(); $this->drupalLogin($user); $this->drupalGet('user/' . $user->id() . '/edit'); // Test that the field is set to type=value. $this->assertSession()->fieldNotExists('edit-name'); $this->assertSession()->pageTextContains($username); // Make sure the email isn't changed on save. $this->drupalPostForm('user/' . $user->id() . '/edit', [], 'Save'); $this->assertSession()->pageTextContains($username); } }