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