annotate core/modules/user/src/Tests/UserEditTest.php @ 0:4c8ae668cc8c

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