function CasRolesTestCase::testRoleMapping in CAS roles 7.2
Same name and namespace in other branches
- 7 cas_roles.test \CasRolesTestCase::testRoleMapping()
Test the CAS role matching.
File
- ./
cas_roles.test, line 48 - Tests for CAS roles.
Class
- CasRolesTestCase
- Test case for CAS attribute setting.
Code
function testRoleMapping() {
// Set up cas_roles
variable_set('cas_roles_sync_every_login', 1);
variable_set('cas_roles_roles', '[cas:attribute:position]');
variable_set('cas_roles_require_a_role_create', FALSE);
variable_set('cas_roles_require_a_role_login', FALSE);
$relations = array(
'authenticated user' => '',
'teacher' => '/^(Professor|Teacher)$/',
'student' => '/^Student$/',
'philosopher' => '/^(Philosopher|Tenure)$/',
'staff' => '',
);
variable_set('cas_roles_relations', $relations);
// Create a new CAS user.
$cas_name = $this
->randomName();
$attributes = array(
'position' => array(
'Professor',
'Tenure',
),
);
$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();
// Do not manage philosopher role any more. and test missing relations.
$relations = array(
'teacher' => '/^(Professor|Teacher)$/',
'student' => '/^Student$/',
'philosopher' => '',
);
variable_set('cas_roles_relations', $relations);
// Change the attributes.
$attributes = array(
'position' => array(
'Student',
),
);
$cas_user = array(
'name' => $cas_name,
'attributes' => $attributes,
);
variable_set('cas_test_cas_user', $cas_user);
// Log in again.
$this
->casLogin($cas_name, $attributes);
$this->loggedInUser = user_load($this->loggedInUser->uid, TRUE);
// Workaround since casLogin gets cached copy.
// After login, user should now have roles student and philosopher, but not teacher 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
->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();
// Set synchronisation to trigger only at the first login.
variable_set('cas_roles_sync_every_login', 0);
// Change the attributes.
$attributes = array(
'position' => array(
'Teacher',
),
);
$cas_user = array(
'name' => $cas_name,
'attributes' => $attributes,
);
variable_set('cas_test_cas_user', $cas_user);
// Log in again.
$this
->casLogin($cas_name, $attributes);
$this->loggedInUser = user_load($this->loggedInUser->uid, TRUE);
// Workaround since casLogin gets cached copy.
// After login, the user should have the same roles as before.
$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
->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();
}