Backup of db before drupal security update
[yaffs-website] / web / core / modules / user / src / Tests / UserEditTest.php
1 <?php
2
3 namespace Drupal\user\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests user edit page.
9  *
10  * @group user
11  */
12 class UserEditTest extends WebTestBase {
13
14   /**
15    * Test user edit page.
16    */
17   public function testUserEdit() {
18     // Test user edit functionality.
19     $user1 = $this->drupalCreateUser(['change own username']);
20     $user2 = $this->drupalCreateUser([]);
21     $this->drupalLogin($user1);
22
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']]));
27
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());
34
35     // Check that filling out a single password field does not validate.
36     $edit = [];
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.');
41
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.');
46
47     // Test that the error message appears when attempting to change the mail or
48     // pass without the current password.
49     $edit = [];
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')]));
53
54     $edit['current_pass'] = $user1->pass_raw;
55     $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
56     $this->assertRaw(t("The changes have been saved."));
57
58     // Test that the user must enter current password before changing passwords.
59     $edit = [];
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')]));
64
65     // Try again with the current password.
66     $edit['current_pass'] = $user1->pass_raw;
67     $this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
68     $this->assertRaw(t("The changes have been saved."));
69
70     // Make sure the changed timestamp is updated.
71     $this->assertEqual($user1->getChangedTime(), REQUEST_TIME, 'Changing a user sets "changed" timestamp.');
72
73     // Make sure the user can log in with their new password.
74     $this->drupalLogout();
75     $user1->pass_raw = $new_pass;
76     $this->drupalLogin($user1);
77     $this->drupalLogout();
78
79     // Test that the password strength indicator displays.
80     $config = $this->config('user.settings');
81     $this->drupalLogin($user1);
82
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.');
86
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.');
90
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);
95
96     $this->drupalGet('user/' . $user1->id() . '/edit');
97     $this->assertNoFieldChecked('edit-status-0');
98     $this->assertFieldChecked('edit-status-1');
99
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');
105
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');
111   }
112
113   /**
114    * Tests setting the password to "0".
115    *
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.
119    */
120   public function testUserWith0Password() {
121     $admin = $this->drupalCreateUser(['administer users']);
122     $this->drupalLogin($admin);
123     // Create a regular user.
124     $user1 = $this->drupalCreateUser([]);
125
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."));
129   }
130
131   /**
132    * Tests editing of a user account without an email address.
133    */
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.
141     $user1->mail = '';
142     $user1->save();
143     $this->drupalPostForm("user/" . $user1->id() . "/edit", ['mail' => ''], t('Save'));
144     $this->assertRaw(t("The changes have been saved."));
145   }
146
147 }