1 user.test UserRoleAdminTestCase::testRoleAdministration()

Test adding, renaming and deleting roles.

File

core/modules/user/tests/user.test, line 2325
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';
  $role1_description = 'Help text for 123 role.';
  $edit = array(
    'name' => $role1_name,
    'label' => $role1_name,
    'description' => $role1_description,
  );
  $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';
  $role2_description = 'Description for 456 role.';
  $edit = array(
    'name' => $role2_name,
    'label' => $role2_name,
    'description' => $role2_description,
  );
  $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';
  $new_description = 'Help text for 789 role. <strong>Bold text.</strong>. <xss>Stripped tag</xss>.';
  $edit = array(
    'label' => $new_label,
    'description' => $new_description,
  );
  $this->backdropPost("admin/config/people/roles/configure/$role1_name", $edit, t('Save role'));
  $this->assertText(t('The 789 role has been saved.'), '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.');
  $this->assertTrue($role1->description === $new_description, 'The role has the new description (help text).');

  // Confirm description (help text) is shown editing a user.
  $this->backdropGet('user/' . $this->admin_user->uid . '/edit');
  $this->assertRaw(filter_xss_admin($new_description));

  // Make sure that the system-defined roles can still be edited, to adjust
  // their labels and descriptions.
  $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->assertText(t('Description'));
  // Help text is hidden for the anonymous role, since it is never assigned.
  $this->assertNoText(t('Help text'));
  $this->backdropGet('admin/config/people/roles/configure/' . BACKDROP_AUTHENTICATED_ROLE);
  $this->assertText(t('Description'));
  $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.');
}