3 namespace Drupal\Tests\user\Functional;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests user edit page.
13 class UserEditTest extends BrowserTestBase {
16 * Test user edit page.
18 public function testUserEdit() {
19 // Test user edit functionality.
20 $user1 = $this->drupalCreateUser(['change own username']);
21 $user2 = $this->drupalCreateUser([]);
22 $this->drupalLogin($user1);
24 // Test that error message appears when attempting to use a non-unique user name.
25 $edit['name'] = $user2->getUsername();
26 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
27 $this->assertRaw(t('The username %name is already taken.', ['%name' => $edit['name']]));
29 // Check that the default value in user name field
30 // is the raw value and not a formatted one.
31 \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
32 \Drupal::service('module_installer')->install(['user_hooks_test']);
33 Cache::invalidateTags(['rendered']);
34 $this->drupalGet('user/' . $user1->id() . '/edit');
35 $this->assertFieldByName('name', $user1->getAccountName());
37 // Ensure the formatted name is displayed when expected.
38 $this->drupalGet('user/' . $user1->id());
39 $this->assertSession()->responseContains($user1->getDisplayName());
40 $this->assertSession()->titleEquals(strip_tags($user1->getDisplayName()) . ' | Drupal');
42 // Check that filling out a single password field does not validate.
44 $edit['pass[pass1]'] = '';
45 $edit['pass[pass2]'] = $this->randomMachineName();
46 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
47 $this->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
49 $edit['pass[pass1]'] = $this->randomMachineName();
50 $edit['pass[pass2]'] = '';
51 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
52 $this->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
54 // Test that the error message appears when attempting to change the mail or
55 // pass without the current password.
57 $edit['mail'] = $this->randomMachineName() . '@new.example.com';
58 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
59 $this->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", ['%name' => t('Email')]));
61 $edit['current_pass'] = $user1->passRaw;
62 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
63 $this->assertRaw(t("The changes have been saved."));
65 // Test that the user must enter current password before changing passwords.
67 $edit['pass[pass1]'] = $new_pass = $this->randomMachineName();
68 $edit['pass[pass2]'] = $new_pass;
69 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
70 $this->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", ['%name' => t('Password')]));
72 // Try again with the current password.
73 $edit['current_pass'] = $user1->passRaw;
74 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
75 $this->assertRaw(t("The changes have been saved."));
77 // Make sure the changed timestamp is updated.
78 $this->assertEqual($user1->getChangedTime(), REQUEST_TIME, 'Changing a user sets "changed" timestamp.');
80 // Make sure the user can log in with their new password.
81 $this->drupalLogout();
82 $user1->passRaw = $new_pass;
83 $this->drupalLogin($user1);
84 $this->drupalLogout();
86 // Test that the password strength indicator displays.
87 $config = $this->config('user.settings');
88 $this->drupalLogin($user1);
90 $config->set('password_strength', TRUE)->save();
91 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
92 $this->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.');
94 $config->set('password_strength', FALSE)->save();
95 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
96 $this->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.');
98 // Check that the user status field has the correct value and that it is
99 // properly displayed.
100 $admin_user = $this->drupalCreateUser(['administer users']);
101 $this->drupalLogin($admin_user);
103 $this->drupalGet('user/' . $user1->id() . '/edit');
104 $this->assertNoFieldChecked('edit-status-0');
105 $this->assertFieldChecked('edit-status-1');
107 $edit = ['status' => 0];
108 $this->drupalPostForm('user/' . $user1->id() . '/edit', $edit, t('Save'));
109 $this->assertText(t('The changes have been saved.'));
110 $this->assertFieldChecked('edit-status-0');
111 $this->assertNoFieldChecked('edit-status-1');
113 $edit = ['status' => 1];
114 $this->drupalPostForm('user/' . $user1->id() . '/edit', $edit, t('Save'));
115 $this->assertText(t('The changes have been saved.'));
116 $this->assertNoFieldChecked('edit-status-0');
117 $this->assertFieldChecked('edit-status-1');
121 * Tests setting the password to "0".
123 * We discovered in https://www.drupal.org/node/2563751 that logging in with a
124 * password that is literally "0" was not possible. This test ensures that
125 * this regression can't happen again.
127 public function testUserWith0Password() {
128 $admin = $this->drupalCreateUser(['administer users']);
129 $this->drupalLogin($admin);
130 // Create a regular user.
131 $user1 = $this->drupalCreateUser([]);
133 $edit = ['pass[pass1]' => '0', 'pass[pass2]' => '0'];
134 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
135 $this->assertRaw(t("The changes have been saved."));
139 * Tests editing of a user account without an email address.
141 public function testUserWithoutEmailEdit() {
142 // Test that an admin can edit users without an email address.
143 $admin = $this->drupalCreateUser(['administer users']);
144 $this->drupalLogin($admin);
145 // Create a regular user.
146 $user1 = $this->drupalCreateUser([]);
147 // This user has no email address.
150 $this->drupalPostForm("user/" . $user1->id() . "/edit", ['mail' => ''], t('Save'));
151 $this->assertRaw(t("The changes have been saved."));