function UserCreateTest::testUserAddPasswordWithNotify

Tests that the password is discarded when notify is enabled.

File

core/modules/user/tests/src/Functional/UserCreateTest.php, line 149

Class

UserCreateTest
Tests the create user administration page.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserAddPasswordWithNotify() : void {
  $admin = $this->drupalCreateUser([
    'administer users',
  ]);
  $this->drupalLogin($admin);
  $password_checker = \Drupal::service('password');
  $typed_pass = 'known_password_for_testing';
  // Create a user with notify enabled and a password filled in.
  // The typed password should be discarded.
  $name_notify = $this->randomMachineName();
  $edit = [
    'name' => $name_notify,
    'mail' => $this->randomMachineName() . '@example.com',
    'pass[pass1]' => $typed_pass,
    'pass[pass2]' => $typed_pass,
    'notify' => TRUE,
  ];
  $this->drupalGet('admin/people/create');
  $this->submitForm($edit, 'Create new account');
  $this->assertSession()
    ->pageTextContains('A welcome message with further instructions has been emailed to the new user ' . $name_notify . '.');
  $users = \Drupal::entityTypeManager()->getStorage('user')
    ->loadByProperties([
    'name' => $name_notify,
  ]);
  $user_notify = reset($users);
  // The stored password should not match the typed password.
  $this->assertFalse($password_checker->check($typed_pass, $user_notify->getPassword()), 'Typed password is not used when notify is enabled.');
  // The stored password should not be a hash of an empty string.
  $this->assertFalse($password_checker->check('', $user_notify->getPassword()), 'Password is not a hash of an empty string when notify is enabled.');
  // Create a user with notify disabled and a password filled in.
  // The typed password should be used.
  $name_no_notify = $this->randomMachineName();
  $edit = [
    'name' => $name_no_notify,
    'mail' => $this->randomMachineName() . '@example.com',
    'pass[pass1]' => $typed_pass,
    'pass[pass2]' => $typed_pass,
    'notify' => FALSE,
  ];
  $this->drupalGet('admin/people/create');
  $this->submitForm($edit, 'Create new account');
  $this->assertSession()
    ->pageTextContains('Created a new user account for ' . $name_no_notify . '. No email has been sent.');
  $users = \Drupal::entityTypeManager()->getStorage('user')
    ->loadByProperties([
    'name' => $name_no_notify,
  ]);
  $user_no_notify = reset($users);
  // The stored password should match the typed password.
  $this->assertTrue($password_checker->check($typed_pass, $user_no_notify->getPassword()), 'Typed password is used when notify is disabled.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.