1 user.test UserRoleAdminTestCase::testRoleAdministration()

Test adding, renaming and deleting roles.

File

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

Class

UserRoleAdminTestCase
Test case to test adding, editing and deleting roles.

Code

function testRoleAdministration() {
  $this->backdropLogin($this->admin_user);

  // Visit the main roles administration page.
  $this->backdropGet('admin/config/people/roles');

  // Ensure both submit buttons exist in the "Add role" form.
  $this->clickLink(t('Add role'));
  $this->assertUrl('admin/config/people/roles/add', array(), 'Redirected to correct URL after clicking the "+ Add role" link.');
  $this->assertTitle(t('Add role | Backdrop CMS'), 'The page title on the form page is "Add role".');
  $this->assertRaw(t('Save and set permissions'), '"Save and set permissions" button found on "Add role" form.');
  $this->assertRaw(t('Save role'), '"Save role" button found on "Add role" form.');
  $this->clickLink(t('Cancel'));
  $this->assertRaw(t('Add role'), 'Redirected to the main roles listing page.');

  // Test adding a role using the 'Save role' button.
  $role1_name = '123';
  $edit = array('name' => $role1_name, 'label' => $role1_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.');
  $this->assertUrl('admin/config/people/roles', array(), 'Redirected to correct URL after clicking the "Save role" button.');
  backdrop_static_reset('user_roles');
  $role1 = user_role_load($role1_name);
  $this->assertTrue(is_object($role1), 'The role was successfully loaded from config.');

  // Ensure only one submit button exists in the "Edit role" form.
  $this->clickLink(t('Configure role'), 3);
  $this->assertUrl('admin/config/people/roles/configure/' . $role1_name, array(), 'Redirected to correct URL after clicking the "Configure role" operation.');
  $this->assertTitle(t('Configure role | Backdrop CMS'), 'The page title on the form is "Configure role".');
  $this->assertNoRaw(t('Save and set permissions'), '"Save and set permissions" button not found on "Configure role" form.');
  $this->assertRaw(t('Save role'), '"Save role" button found on "Configure role" form.');
  $this->clickLink(t('Cancel'));
  $this->assertRaw(t('Add role'), 'Redirected to the main roles listing page.');

  // Test adding a role using the 'Save and set permissions' button.
  $role2_name = '456';
  $edit = array('name' => $role2_name, 'label' => $role2_name);
  $this->backdropPost('admin/config/people/roles/add', $edit, t('Save and set permissions'));
  $this->assertText(t('The 456 role has been added.'), 'The role has been added.');
  $this->assertUrl('admin/config/people/permissions', array(), 'Redirected to correct URL after clicking the "Save and set permissions" button.');
  backdrop_static_reset('user_roles');
  $role2 = user_role_load($role2_name);
  $this->assertTrue(is_object($role2), 'The role was successfully loaded from config.');

  // Try adding a duplicate role.
  $duplicate_role_warning = t('The machine-readable name is already in use. It must be unique.');
  $this->backdropPost('admin/config/people/roles/add', $edit, t('Save and set permissions'));
  $this->assertRaw($duplicate_role_warning, 'Duplicate role warning displayed.');
  $this->backdropPost('admin/config/people/roles/add', $edit, t('Save role'));
  $this->assertRaw($duplicate_role_warning, 'Duplicate role warning displayed.');

  // Test renaming an existing role.
  $old_label = $role1->label;
  $new_label = '789';
  $edit = array('label' => $new_label);
  $this->backdropPost("admin/config/people/roles/configure/$role1_name", $edit, t('Save role'));
  $this->assertText(t('The 123 role has been renamed to 789.'), 'The role has been renamed.');
  $this->assertUrl('admin/config/people/roles', array(), 'Redirected to correct URL after clicking the renaming the role.');
  backdrop_static_reset('user_roles');
  $role1 = user_role_load($role1_name);
  $this->assertFalse($role1->label === $old_label, 'The role has had its label changed.');
  $this->assertTrue($role1->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.');
}