1 user.test UserEditTestCase::testUserEdit()

Test user edit page.

File

core/modules/user/tests/user.test, line 2084
Tests for user.module.

Class

UserEditTestCase
Test case to test user_save() behavior.

Code

function testUserEdit() {
  // Test user edit functionality with user pictures disabled.
  $config = config('system.core');
  $config->set('user_pictures', 0)->save();
  $user1 = $this->backdropCreateUser(array('change own username'));
  $user2 = $this->backdropCreateUser(array());
  $this->backdropLogin($user1);

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

  // Repeat the test with user pictures enabled, which modifies the form.
  $config->set('user_pictures', 1)->save();

  $this->backdropPost("user/$user1->uid/edit", $edit, t('Save'));
  $this->assertRaw(t('The name %name is already taken.', array('%name' => $edit['name'])));

  // Test that the error message appears when attempting to change the mail or
  // pass without the current password.
  $edit = array();
  $edit['mail'] = $this->randomName() . '@new.example.com';
  $this->backdropPost("user/$user1->uid/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 address'))));

  $edit['current_pass'] = $user1->pass_raw;
  $this->backdropPost("user/$user1->uid/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'] = $new_pass = $this->randomName();
  $this->backdropPost("user/$user1->uid/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->backdropPost("user/$user1->uid/edit", $edit, t('Save'));
  $this->assertRaw(t("The changes have been saved."));

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