1 user.test UserRoleAdminTestCase::testRoleAdministration()

Test adding, renaming and deleting roles.

File

core/modules/user/tests/user.test, line 2138
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', $edit, t('Add 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(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.');
  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/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'));
  user_role_save($admin_role);
  // Set this as the administrator role.
  config_set('system.core', 'user_admin_role', $admin_role->name);

  // Test cancel deleting a role.
  $role_name = 'administrator';
  $this->backdropGet("admin/config/people/roles/delete/$role_name");
  $this->clickLink(t('Cancel'));
  $this->assertResponse(200);
  $this->assertUrl('admin/config/people/roles', array(), 'Redirected to correct URL after cancel.');

  // Test deleting the default administrator role.
  $role_name = 'administrator';
  $role = user_role_load($role_name);
  $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.');
  backdrop_static_reset('user_roles');
  $this->assertFalse(user_role_load($role_name), 'A deleted role can no longer be loaded.');
  // Make sure this role is no longer configured as the administrator role.
  $this->assertNull(config_get('system.core', 'user_admin_role'), 'The administrator role is no longer configured as the administrator role.');
}