1.20.x user.test | UserRoleAdminTestCase::testRoleAdministration() |
Test adding, renaming and deleting roles.
File
- modules/
user/ tests/ user.test, line 2153 - Tests for user.module.
Class
- UserRoleAdminTestCase
- Test case to test adding, editing and deleting roles.
Code
function testRoleAdministration() {
$this->backdropLogin($this->admin_user);
// Test adding a role.
$role_name = '123';
$edit = array('name' => $role_name, 'label' => $role_name);
$this->backdropPost('admin/config/people/roles/add', $edit, t('Save role'));
$this->assertText(t('The 123 role has been added.'), 'The role has been added.');
backdrop_static_reset('user_roles');
$role = user_role_load($role_name);
$this->assertTrue(is_object($role), 'The role was successfully loaded from config.');
// Try adding a duplicate role.
$this->backdropPost('admin/config/people/roles/add', $edit, t('Save role'));
$this->assertRaw(t('The machine-readable name is already in use. It must be unique.'), 'Duplicate role warning displayed.');
// Test renaming a role.
$old_label = $role->label;
$new_label = '456';
$edit = array('label' => $new_label);
$this->backdropPost("admin/config/people/roles/configure/$role_name", $edit, t('Save role'));
$this->assertText(t('The 123 role has been renamed to 456.'), 'The role has been renamed.');
backdrop_static_reset('user_roles');
$role = user_role_load($role_name);
$this->assertFalse($role->label === $old_label, 'The role has had its label changed.');
$this->assertTrue($role->label === $new_label, 'The role has the new label.');
// Make sure that the system-defined roles can still be edited, to adjust
// their labels.
$this->backdropGet('admin/config/people/roles/configure/' . BACKDROP_ANONYMOUS_ROLE);
$this->assertResponse(200, 'Access granted when trying to edit the built-in anonymous role.');
$this->backdropGet('admin/config/people/roles/configure/' . BACKDROP_AUTHENTICATED_ROLE);
$this->assertResponse(200, 'Access granted when trying to edit the built-in authenticated role.');
// Create a default role for site administrators, with all available permissions assigned.
$admin_role = new stdClass();
$admin_role->name = 'administrator';
$admin_role->label = st('Administrator');
$admin_role->weight = 2;
$admin_role->permissions = array_keys(module_invoke_all('permission'));
user_role_save($admin_role);
// Set this as the administrator role.
config_set('system.core', 'user_admin_role', $admin_role->name);
$role_name = 'administrator';
$role = user_role_load($role_name);
// Test canceling the various actions that can be performed on user roles.
$actions = array('configure', 'delete');
foreach ($actions as $action) {
$this->backdropGet("admin/config/people/roles/$action/$role_name");
$this->clickLink(t('Cancel'));
$this->assertResponse(200);
$this->assertUrl('admin/config/people/roles', array(), "Redirected to correct URL after canceling $action role.");
}
// Test deleting the default administrator role.
$this->backdropPost("admin/config/people/roles/delete/$role_name", array(), t('Delete'));
$this->assertText(t('The administrator role has been deleted.'), 'The role has been deleted');
$this->assertNoLinkByHref("admin/config/people/roles/configure/$role_name", 'Role edit link removed.');
backdrop_static_reset('user_roles');
$this->assertFalse(user_role_load($role_name), 'A deleted role can no longer be loaded.');
// Since the administrator role that was previously set as admin has been
// deleted, no role should be set as admin now.
$this->assertFalse(config_get('system.core', 'user_admin_role'), 'No role is configured as the administrator role.');
}