3 namespace Drupal\Tests\user\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests user edit page.
12 class UserEditTest extends BrowserTestBase {
15 * Test user edit page.
17 public function testUserEdit() {
18 // Test user edit functionality.
19 $user1 = $this->drupalCreateUser(['change own username']);
20 $user2 = $this->drupalCreateUser([]);
21 $this->drupalLogin($user1);
23 // Test that error message appears when attempting to use a non-unique user name.
24 $edit['name'] = $user2->getUsername();
25 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
26 $this->assertRaw(t('The username %name is already taken.', ['%name' => $edit['name']]));
28 // Check that the default value in user name field
29 // is the raw value and not a formatted one.
30 \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
31 \Drupal::service('module_installer')->install(['user_hooks_test']);
32 $this->drupalGet('user/' . $user1->id() . '/edit');
33 $this->assertFieldByName('name', $user1->getAccountName());
35 // Check that filling out a single password field does not validate.
37 $edit['pass[pass1]'] = '';
38 $edit['pass[pass2]'] = $this->randomMachineName();
39 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
40 $this->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
42 $edit['pass[pass1]'] = $this->randomMachineName();
43 $edit['pass[pass2]'] = '';
44 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
45 $this->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
47 // Test that the error message appears when attempting to change the mail or
48 // pass without the current password.
50 $edit['mail'] = $this->randomMachineName() . '@new.example.com';
51 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
52 $this->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", ['%name' => t('Email')]));
54 $edit['current_pass'] = $user1->passRaw;
55 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
56 $this->assertRaw(t("The changes have been saved."));
58 // Test that the user must enter current password before changing passwords.
60 $edit['pass[pass1]'] = $new_pass = $this->randomMachineName();
61 $edit['pass[pass2]'] = $new_pass;
62 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
63 $this->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", ['%name' => t('Password')]));
65 // Try again with the current password.
66 $edit['current_pass'] = $user1->passRaw;
67 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
68 $this->assertRaw(t("The changes have been saved."));
70 // Make sure the changed timestamp is updated.
71 $this->assertEqual($user1->getChangedTime(), REQUEST_TIME, 'Changing a user sets "changed" timestamp.');
73 // Make sure the user can log in with their new password.
74 $this->drupalLogout();
75 $user1->passRaw = $new_pass;
76 $this->drupalLogin($user1);
77 $this->drupalLogout();
79 // Test that the password strength indicator displays.
80 $config = $this->config('user.settings');
81 $this->drupalLogin($user1);
83 $config->set('password_strength', TRUE)->save();
84 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
85 $this->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.');
87 $config->set('password_strength', FALSE)->save();
88 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
89 $this->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.');
91 // Check that the user status field has the correct value and that it is
92 // properly displayed.
93 $admin_user = $this->drupalCreateUser(['administer users']);
94 $this->drupalLogin($admin_user);
96 $this->drupalGet('user/' . $user1->id() . '/edit');
97 $this->assertNoFieldChecked('edit-status-0');
98 $this->assertFieldChecked('edit-status-1');
100 $edit = ['status' => 0];
101 $this->drupalPostForm('user/' . $user1->id() . '/edit', $edit, t('Save'));
102 $this->assertText(t('The changes have been saved.'));
103 $this->assertFieldChecked('edit-status-0');
104 $this->assertNoFieldChecked('edit-status-1');
106 $edit = ['status' => 1];
107 $this->drupalPostForm('user/' . $user1->id() . '/edit', $edit, t('Save'));
108 $this->assertText(t('The changes have been saved.'));
109 $this->assertNoFieldChecked('edit-status-0');
110 $this->assertFieldChecked('edit-status-1');
114 * Tests setting the password to "0".
116 * We discovered in https://www.drupal.org/node/2563751 that logging in with a
117 * password that is literally "0" was not possible. This test ensures that
118 * this regression can't happen again.
120 public function testUserWith0Password() {
121 $admin = $this->drupalCreateUser(['administer users']);
122 $this->drupalLogin($admin);
123 // Create a regular user.
124 $user1 = $this->drupalCreateUser([]);
126 $edit = ['pass[pass1]' => '0', 'pass[pass2]' => '0'];
127 $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
128 $this->assertRaw(t("The changes have been saved."));
132 * Tests editing of a user account without an email address.
134 public function testUserWithoutEmailEdit() {
135 // Test that an admin can edit users without an email address.
136 $admin = $this->drupalCreateUser(['administer users']);
137 $this->drupalLogin($admin);
138 // Create a regular user.
139 $user1 = $this->drupalCreateUser([]);
140 // This user has no email address.
143 $this->drupalPostForm("user/" . $user1->id() . "/edit", ['mail' => ''], t('Save'));
144 $this->assertRaw(t("The changes have been saved."));