function UserPermissionsTest::testAdministratorRole

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

Class

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

Namespace

Drupal\Tests\user\Functional

Code

public function testAdministratorRole() : void {
    $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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.