You are here

public function UserPermissionsTest::testAdministratorRole in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserPermissionsTest.php \Drupal\Tests\user\Functional\UserPermissionsTest::testAdministratorRole()
  2. 10 core/modules/user/tests/src/Functional/UserPermissionsTest.php \Drupal\Tests\user\Functional\UserPermissionsTest::testAdministratorRole()

Tests assigning of permissions for the administrator role.

File

core/modules/user/tests/src/Functional/UserPermissionsTest.php, line 109

Class

UserPermissionsTest
Verify that role permissions can be added and removed via the permissions page.

Namespace

Drupal\Tests\user\Functional

Code

public function testAdministratorRole() {
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/people/role-settings');

  // Verify that the administration role is none by default.
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('edit-user-admin-role', '')
    ->isSelected());
  $this
    ->assertFalse(Role::load($this->rid)
    ->isAdmin());

  // Set the user's role to be the administrator role.
  $edit = [];
  $edit['user_admin_role'] = $this->rid;
  $this
    ->drupalGet('admin/people/role-settings');
  $this
    ->submitForm($edit, 'Save configuration');
  \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->resetCache();
  $this
    ->assertTrue(Role::load($this->rid)
    ->isAdmin());

  // Enable aggregator module and ensure the 'administer news feeds'
  // permission is assigned by default.
  \Drupal::service('module_installer')
    ->install([
    'aggregator',
  ]);
  $this
    ->assertTrue($this->adminUser
    ->hasPermission('administer news feeds'), 'The permission was automatically assigned to the administrator role');

  // Ensure that selecting '- None -' removes the admin role.
  $edit = [];
  $edit['user_admin_role'] = '';
  $this
    ->drupalGet('admin/people/role-settings');
  $this
    ->submitForm($edit, 'Save configuration');
  \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->resetCache();
  \Drupal::configFactory()
    ->reset();
  $this
    ->assertFalse(Role::load($this->rid)
    ->isAdmin());

  // Manually create two admin roles, in that case the single select should be
  // hidden.
  Role::create([
    'id' => 'admin_role_0',
    'is_admin' => TRUE,
    'label' => 'Admin role 0',
  ])
    ->save();
  Role::create([
    'id' => 'admin_role_1',
    'is_admin' => TRUE,
    'label' => 'Admin role 1',
  ])
    ->save();
  $this
    ->drupalGet('admin/people/role-settings');
  $this
    ->assertSession()
    ->fieldNotExists('user_admin_role');
}