You are here

function UserEditTest::testUserEdit in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Tests/UserEditTest.php \Drupal\user\Tests\UserEditTest::testUserEdit()

Test user edit page.

File

core/modules/user/src/Tests/UserEditTest.php, line 22
Contains \Drupal\user\Tests\UserEditTest.

Class

UserEditTest
Tests user edit page.

Namespace

Drupal\user\Tests

Code

function testUserEdit() {

  // Test user edit functionality.
  $user1 = $this
    ->drupalCreateUser(array(
    'change own username',
  ));
  $user2 = $this
    ->drupalCreateUser(array());
  $this
    ->drupalLogin($user1);

  // Test that error message appears when attempting to use a non-unique user name.
  $edit['name'] = $user2
    ->getUsername();
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertRaw(t('The username %name is already taken.', array(
    '%name' => $edit['name'],
  )));

  // Check that filling out a single password field does not validate.
  $edit = array();
  $edit['pass[pass1]'] = '';
  $edit['pass[pass2]'] = $this
    ->randomMachineName();
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');
  $edit['pass[pass1]'] = $this
    ->randomMachineName();
  $edit['pass[pass2]'] = '';
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertText(t("The specified passwords do not match."), 'Typing mismatched passwords displays an error message.');

  // Test that the error message appears when attempting to change the mail or
  // pass without the current password.
  $edit = array();
  $edit['mail'] = $this
    ->randomMachineName() . '@new.example.com';
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", array(
    '%name' => t('Email'),
  )));
  $edit['current_pass'] = $user1->pass_raw;
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertRaw(t("The changes have been saved."));

  // Test that the user must enter current password before changing passwords.
  $edit = array();
  $edit['pass[pass1]'] = $new_pass = $this
    ->randomMachineName();
  $edit['pass[pass2]'] = $new_pass;
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertRaw(t("Your current password is missing or incorrect; it's required to change the %name.", array(
    '%name' => t('Password'),
  )));

  // Try again with the current password.
  $edit['current_pass'] = $user1->pass_raw;
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertRaw(t("The changes have been saved."));

  // Make sure the changed timestamp is updated.
  $this
    ->assertEqual($user1
    ->getChangedTime(), REQUEST_TIME, 'Changing a user sets "changed" timestamp.');

  // Make sure the user can log in with their new password.
  $this
    ->drupalLogout();
  $user1->pass_raw = $new_pass;
  $this
    ->drupalLogin($user1);
  $this
    ->drupalLogout();

  // Test that the password strength indicator displays.
  $config = $this
    ->config('user.settings');
  $this
    ->drupalLogin($user1);
  $config
    ->set('password_strength', TRUE)
    ->save();
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertRaw(t('Password strength:'), 'The password strength indicator is displayed.');
  $config
    ->set('password_strength', FALSE)
    ->save();
  $this
    ->drupalPostForm("user/" . $user1
    ->id() . "/edit", $edit, t('Save'));
  $this
    ->assertNoRaw(t('Password strength:'), 'The password strength indicator is not displayed.');

  // Check that the user status field has the correct value and that it is
  // properly displayed.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer users',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('user/' . $user1
    ->id() . '/edit');
  $this
    ->assertNoFieldChecked('edit-status-0');
  $this
    ->assertFieldChecked('edit-status-1');
  $edit = array(
    'status' => 0,
  );
  $this
    ->drupalPostForm('user/' . $user1
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'));
  $this
    ->assertFieldChecked('edit-status-0');
  $this
    ->assertNoFieldChecked('edit-status-1');
  $edit = array(
    'status' => 1,
  );
  $this
    ->drupalPostForm('user/' . $user1
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'));
  $this
    ->assertNoFieldChecked('edit-status-0');
  $this
    ->assertFieldChecked('edit-status-1');
}