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);

  // Test that multiple failed password validations trigger flood control.
  $config = config('user.flood');
  $config->set('flood_user_limit', 2);
  $config->save();
  flood_clear_event('failed_pass_validation_user', $user1->uid);
  $edit = array();
  $edit['mail'] = $this->randomName() . '@new.example.com';
  $edit['current_pass'] = $this->randomName();
  // For the first and second attempt the password is validated.
  $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'))));
  $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'))));
  // For the third attempt error from the flood control is received.
  $this->backdropPost("user/$user1->uid/edit", $edit, t('Save'));
  $this->assertRaw(t('Sorry, you have entered the incorrect password more than @count times. Changes to fields that require current password are temporarily blocked. Try again later.', array('@count' => $config->get('flood_user_limit'))));

  // Test that multiple failed password validations with an empty password
  // does not trigger flood control.
  flood_clear_event('failed_pass_validation_user', $user1->uid);
  $edit = array();
  $edit['mail'] = $this->randomName() . '@new.example.com';
  // For the first and second attempt the password is validated.
  $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'))));
  $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'))));
  // For the third attempt the password is still validated as no password was
  // entered.
  $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'))));
  $this->assertNoRaw(t('Sorry, you have entered incorrect password more than @count times. Changes to fields that require current password are temporarily blocked. Try again later.', array('@count' => $config->get('flood_user_limit'))));
  $this->backdropLogout();
}