function UserPermissionsTest::testAdministratorRole

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

Test assigning of permissions for the administrator role.

File

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

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/config/people/accounts');
    // Verify that the administration role is none by default.
    $this->assertOptionSelected('edit-user-admin-role', '', 'Administration role defaults to none.');
    $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->drupalPostForm('admin/config/people/accounts', $edit, t('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->drupalPostForm('admin/config/people/accounts', $edit, t('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/config/people/accounts');
    $this->assertNoFieldByName('user_admin_role');
}

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