You are here

function AutoassignroleUserChoiceTestCase::testUserChoiceAssignment in Auto Assign Role 6.2

Same name and namespace in other branches
  1. 6 tests/autoassignrole.test \AutoassignroleUserChoiceTestCase::testUserChoiceAssignment()

File

tests/autoassignrole.test, line 426
Autoassignrole functionality tests.

Class

AutoassignroleUserChoiceTestCase

Code

function testUserChoiceAssignment() {

  // Create a new user who can access the administration settings
  $this
    ->drupalLogin($this->admin_user);
  $edit = $this
    ->getEditValues('user_choice');
  $this
    ->drupalPost('admin/user/autoassignrole', $edit, t('Save'));

  //Create a new user normally and verify that no additional roles are added
  $new_user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($new_user);

  // check the user after login to make sure they have not been assigned our
  // roles
  foreach ($this->roles as $rid => $role) {
    $this
      ->assertFalse(array_key_exists($rid, $this->loggedInUser->roles), "New User has not been assigned the role {$role}");
  }
  $this
    ->drupalLogout();
  $user_register_page = $this
    ->drupalGet('user/register');

  // Create a new user at this page and load the user object
  $new_user = $this
    ->getNewUser();

  // right now the admin settings are set to allow multiple roles but not require
  // any to be selected.  Validate selecting all available roles.
  $new_user['user_roles[]'] = array();
  foreach ($this->roles as $rid => $role) {
    $new_user['user_roles[]'][$rid] = TRUE;
  }
  $this
    ->drupalPost('user/register', $new_user, t('Create new account'));
  $user = user_load(array(
    'name' => $new_user['name'],
    'mail' => $new_user['mail'],
  ));
  foreach ($this->roles as $rid => $role) {
    $this
      ->assertTrue(array_key_exists($rid, $user->roles), 'The user ' . $new_user['name'] . ' has been assigned the correct user selected role');
  }

  // Clear out the users selection and make sure that they can create an account
  // without selecting any roles.
  $new_user = $this
    ->getNewUser();
  $this
    ->drupalPost('user/register', $new_user, t('Create new account'));
  $user = user_load(array(
    'name' => $new_user['name'],
    'mail' => $new_user['mail'],
  ));
  foreach ($this->roles as $rid => $role) {
    $this
      ->assertFalse(array_key_exists($rid, $user->roles), 'The user ' . $new_user['name'] . ' was not assigned the ' . $role . ' role');
  }

  // use the admin form to set the role selection as required
  $this
    ->drupalLogin($this->admin_user);
  $edit['user_required'] = 1;
  $this
    ->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
  $this
    ->drupalLogout();

  // verify a new user does not get created when submitting the registration
  // form without selecting any roles
  $new_user = $this
    ->getNewUser();
  $this
    ->drupalPost('user/register', $new_user, t('Create new account'));
  $user = user_load(array(
    'name' => $new_user['name'],
    'mail' => $new_user['mail'],
  ));
  $this
    ->assertTrue(empty($user->uid), 'The user ' . $new_user['name'] . ' was not created');

  // verify that the roles displayed to the user are in ascending order
  preg_match_all('/\\<option value="\\d+"\\>([A-Za-z0-9_]+)/', $user_register_page, $matches);
  $this
    ->assertEqual($this
    ->getSortOrder($matches[1]), 'ASC', 'The roles presented to the user are in Ascending order');

  // Switch roles to descending order and verify they get displayed to the user
  // in the correct order
  // use the admin form to set the role selection as required
  $this
    ->drupalLogin($this->admin_user);
  $edit['user_sort'] = 'SORT_DESC';
  $this
    ->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
  $this
    ->drupalLogout();
  $user_register_page = $this
    ->drupalGet('user/register');

  // verify that the roles displayed to the user are in ascending order
  preg_match_all('/\\<option value="\\d+"\\>([A-Za-z0-9_]+)/', $user_register_page, $matches);
  $this
    ->assertEqual($this
    ->getSortOrder($matches[1]), 'DESC', 'The roles presented to the user are in Descending order');

  // Switch to check boxes and verify they appear correct order
  $this
    ->drupalLogin($this->admin_user);
  $edit['user_selection'] = 2;
  $this
    ->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
  $this
    ->drupalLogout();
  $user_register_page = $this
    ->drupalGet('user/register');
  preg_match_all('/\\<input type="checkbox".+\\/> ([A-Za-z0-9_]+)<\\/label\\>/', $user_register_page, $matches);
  $this
    ->assertEqual($this
    ->getSortOrder($matches[1]), 'DESC', 'The roles presented to the user are checkboxes in Descending order');

  // Switch to single selection radio buttons and verify they appear correct order
  $this
    ->drupalLogin($this->admin_user);
  $edit['user_multiple'] = 0;
  $edit['user_selection'] = 0;
  $this
    ->drupalPost('admin/user/autoassignrole', $edit, t('Save'));
  $this
    ->drupalLogout();
  $user_register_page = $this
    ->drupalGet('user/register');
  preg_match_all('/\\<input type="radio".+\\/> ([A-Za-z0-9_]+)<\\/label\\>/', $user_register_page, $matches);
  $this
    ->assertEqual($this
    ->getSortOrder($matches[1]), 'DESC', 'The roles presented to the user are radio buttons in Descending order');
}