Commit e9d1b782 authored by Andrey Postnikov's avatar Andrey Postnikov Committed by Andrey Postnikov
Browse files

Issue #3034828 by andypost, vacho, greggles, Ben Coleman, asherry: Username is...

Issue #3034828 by andypost, vacho, greggles, Ben Coleman, asherry: Username is not available even when 'change own username' is set
parent cea92ecf
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -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()) {
+30 −0
Original line number Diff line number Diff line
@@ -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);
  }

}