You are here

function CasRolesTestCase::testRoleMappingFromMultipleVariables in CAS roles 7

Same name and namespace in other branches
  1. 7.2 cas_roles.test \CasRolesTestCase::testRoleMappingFromMultipleVariables()

File

./cas_roles.test, line 146
Tests for CAS roles.

Class

CasRolesTestCase
Test case for CAS attribute setting.

Code

function testRoleMappingFromMultipleVariables() {

  // Set up cas_roles
  variable_set('cas_roles_sync_every_login', 1);
  variable_set('cas_roles_behavior', CAS_ROLES_MATCH_REGEX);
  variable_set('cas_roles_roles', '[cas:attribute:department]_[cas:attribute:position]');
  variable_set('cas_roles_require_a_role_create', FALSE);
  variable_set('cas_roles_require_a_role_login', FALSE);
  $relations = array(
    'teacher' => '/^(Physics|Math)_/',
    'student' => '/_Student$/',
    'philosopher' => '/^(Physics|IT)_Professor$/',
    'staff' => '/^Engineering_Staff$/',
  );
  variable_set('cas_roles_relations', $relations);

  // Create a new CAS user.
  $cas_name = $this
    ->randomName();
  $attributes = array(
    'department' => array(
      'Math',
      'IT',
    ),
    'position' => array(
      'Professor',
      'Staff',
    ),
  );
  $cas_user = array(
    'name' => $cas_name,
    'attributes' => $attributes,
  );
  variable_set('cas_test_cas_user', $cas_user);

  // Log in as the new CAS user.
  $this
    ->casLogin($cas_name, $attributes);

  // After login, user should have roles teacher and philosopher but not student or staff.
  $user_roles = $this->loggedInUser->roles;
  $this
    ->assertTrue(array_key_exists($this->roles['teacher'], $user_roles), "Test user has role teacher.");
  $this
    ->assertFalse(array_key_exists($this->roles['student'], $user_roles), "Test user does not have role student.");
  $this
    ->assertTrue(array_key_exists($this->roles['philosopher'], $user_roles), "Test user has role philosopher.");
  $this
    ->assertFalse(array_key_exists($this->roles['staff'], $user_roles), "Test user does not have role staff.");
  $this
    ->drupalLogout();

  // Create a new CAS user.
  $cas_name = $this
    ->randomName();
  $attributes = array(
    'department' => array(
      'Engineering',
      'IT',
    ),
    'position' => array(
      'Student',
      'Staff',
    ),
  );
  $cas_user = array(
    'name' => $cas_name,
    'attributes' => $attributes,
  );
  variable_set('cas_test_cas_user', $cas_user);

  // Log in as the new CAS user.
  $this
    ->casLogin($cas_name, $attributes);
  $this->loggedInUser = user_load($this->loggedInUser->uid, TRUE);

  // Workaround since casLogin gets cached copy.
  // After login, user should have roles teacher and philosopher but not student or staff.
  $user_roles = $this->loggedInUser->roles;
  $this
    ->assertFalse(array_key_exists($this->roles['teacher'], $user_roles), "Test user does not have role teacher.");
  $this
    ->assertTrue(array_key_exists($this->roles['student'], $user_roles), "Test user has role student.");
  $this
    ->assertFalse(array_key_exists($this->roles['philosopher'], $user_roles), "Test user does not have role philosopher.");
  $this
    ->assertTrue(array_key_exists($this->roles['staff'], $user_roles), "Test user has role staff.");
  $this
    ->drupalLogout();
}