Chris@14: drupalCreateUser(['change own username']); Chris@14: $user2 = $this->drupalCreateUser([]); Chris@14: $this->drupalLogin($user1); Chris@14: Chris@14: // Test that error message appears when attempting to use a non-unique user name. Chris@18: $edit['name'] = $user2->getAccountName(); Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertRaw(t('The username %name is already taken.', ['%name' => $edit['name']])); Chris@14: Chris@14: // Check that the default value in user name field Chris@14: // is the raw value and not a formatted one. Chris@14: \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE); Chris@14: \Drupal::service('module_installer')->install(['user_hooks_test']); Chris@17: Cache::invalidateTags(['rendered']); Chris@14: $this->drupalGet('user/' . $user1->id() . '/edit'); Chris@14: $this->assertFieldByName('name', $user1->getAccountName()); Chris@14: Chris@17: // Ensure the formatted name is displayed when expected. Chris@17: $this->drupalGet('user/' . $user1->id()); Chris@17: $this->assertSession()->responseContains($user1->getDisplayName()); Chris@17: $this->assertSession()->titleEquals(strip_tags($user1->getDisplayName()) . ' | Drupal'); Chris@17: Chris@14: // Check that filling out a single password field does not validate. Chris@14: $edit = []; Chris@14: $edit['pass[pass1]'] = ''; Chris@14: $edit['pass[pass2]'] = $this->randomMachineName(); Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.'); Chris@14: Chris@14: $edit['pass[pass1]'] = $this->randomMachineName(); Chris@14: $edit['pass[pass2]'] = ''; Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.'); Chris@14: Chris@14: // Test that the error message appears when attempting to change the mail or Chris@14: // pass without the current password. Chris@14: $edit = []; Chris@14: $edit['mail'] = $this->randomMachineName() . '@new.example.com'; Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", ['%name' => t('Email')])); Chris@14: Chris@14: $edit['current_pass'] = $user1->passRaw; Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertRaw(t("The changes have been saved.")); Chris@14: Chris@14: // Test that the user must enter current password before changing passwords. Chris@14: $edit = []; Chris@14: $edit['pass[pass1]'] = $new_pass = $this->randomMachineName(); Chris@14: $edit['pass[pass2]'] = $new_pass; Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", ['%name' => t('Password')])); Chris@14: Chris@14: // Try again with the current password. Chris@14: $edit['current_pass'] = $user1->passRaw; Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertRaw(t("The changes have been saved.")); Chris@14: Chris@14: // Make sure the changed timestamp is updated. Chris@14: $this->assertEqual($user1->getChangedTime(), REQUEST_TIME, 'Changing a user sets "changed" timestamp.'); Chris@14: Chris@14: // Make sure the user can log in with their new password. Chris@14: $this->drupalLogout(); Chris@14: $user1->passRaw = $new_pass; Chris@14: $this->drupalLogin($user1); Chris@14: $this->drupalLogout(); Chris@14: Chris@14: // Test that the password strength indicator displays. Chris@14: $config = $this->config('user.settings'); Chris@14: $this->drupalLogin($user1); Chris@14: Chris@14: $config->set('password_strength', TRUE)->save(); Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.'); Chris@14: Chris@14: $config->set('password_strength', FALSE)->save(); Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.'); Chris@14: Chris@14: // Check that the user status field has the correct value and that it is Chris@14: // properly displayed. Chris@14: $admin_user = $this->drupalCreateUser(['administer users']); Chris@14: $this->drupalLogin($admin_user); Chris@14: Chris@14: $this->drupalGet('user/' . $user1->id() . '/edit'); Chris@14: $this->assertNoFieldChecked('edit-status-0'); Chris@14: $this->assertFieldChecked('edit-status-1'); Chris@14: Chris@14: $edit = ['status' => 0]; Chris@14: $this->drupalPostForm('user/' . $user1->id() . '/edit', $edit, t('Save')); Chris@14: $this->assertText(t('The changes have been saved.')); Chris@14: $this->assertFieldChecked('edit-status-0'); Chris@14: $this->assertNoFieldChecked('edit-status-1'); Chris@14: Chris@14: $edit = ['status' => 1]; Chris@14: $this->drupalPostForm('user/' . $user1->id() . '/edit', $edit, t('Save')); Chris@14: $this->assertText(t('The changes have been saved.')); Chris@14: $this->assertNoFieldChecked('edit-status-0'); Chris@14: $this->assertFieldChecked('edit-status-1'); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Tests setting the password to "0". Chris@14: * Chris@14: * We discovered in https://www.drupal.org/node/2563751 that logging in with a Chris@14: * password that is literally "0" was not possible. This test ensures that Chris@14: * this regression can't happen again. Chris@14: */ Chris@14: public function testUserWith0Password() { Chris@14: $admin = $this->drupalCreateUser(['administer users']); Chris@14: $this->drupalLogin($admin); Chris@14: // Create a regular user. Chris@14: $user1 = $this->drupalCreateUser([]); Chris@14: Chris@14: $edit = ['pass[pass1]' => '0', 'pass[pass2]' => '0']; Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertRaw(t("The changes have been saved.")); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Tests editing of a user account without an email address. Chris@14: */ Chris@14: public function testUserWithoutEmailEdit() { Chris@14: // Test that an admin can edit users without an email address. Chris@14: $admin = $this->drupalCreateUser(['administer users']); Chris@14: $this->drupalLogin($admin); Chris@14: // Create a regular user. Chris@14: $user1 = $this->drupalCreateUser([]); Chris@14: // This user has no email address. Chris@14: $user1->mail = ''; Chris@14: $user1->save(); Chris@14: $this->drupalPostForm("user/" . $user1->id() . "/edit", ['mail' => ''], t('Save')); Chris@14: $this->assertRaw(t("The changes have been saved.")); Chris@14: } Chris@14: Chris@14: }