annotate core/modules/user/tests/src/Functional/UserEditTest.php @ 19:fa3358dc1485 tip

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