1 user.test UserRoleAdminTestCase::testRoleAdministration()

Test adding, renaming and deleting roles.


core/modules/user/tests/user.test, line 2138
Tests for user.module.


Test case to test adding, editing and deleting roles.


function testRoleAdministration() {

  // Test adding a role.
  $role_name = '123';
  $edit = array('name' => $role_name, 'label' => $role_name);
  $this->backdropPost('admin/config/people/roles', $edit, t('Add role'));
  $this->assertText(t('The 123 role has been added.'), 'The role has been added.');
  $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(NULL, $edit, t('Add 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/edit/$role_name", $edit, t('Save role'));
  $this->assertText(t('The 123 role has been renamed to 456.'), 'The role has been renamed.');
  $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/edit/' . BACKDROP_ANONYMOUS_ROLE);
  $this->assertResponse(200, 'Access granted when trying to edit the built-in anonymous role.');
  $this->backdropGet('admin/config/people/roles/edit/' . 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'));
  // 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('edit', 'delete');
  foreach ($actions as $action) {
    $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/edit/$role_name", 'Role edit link removed.');
  $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.');