You are here

public function UserPermissionsTest::testAdministratorRole in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserPermissionsTest.php \Drupal\Tests\user\Functional\UserPermissionsTest::testAdministratorRole()
  2. 9 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 122

Class

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

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 block module and ensure the 'administer news feeds'
  // permission is assigned by default.
  \Drupal::service('module_installer')
    ->install([
    'block',
  ]);
  $this
    ->assertTrue($this->adminUser
    ->hasPermission('administer blocks'), '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');
}