You are here

function UserPermissionsTest::testAdministratorRole in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserPermissionsTest.php \Drupal\user\Tests\UserPermissionsTest::testAdministratorRole()

Test assigning of permissions for the administrator role.

File

core/modules/user/src/Tests/UserPermissionsTest.php, line 100
Contains \Drupal\user\Tests\UserPermissionsTest.

Class

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

Namespace

Drupal\user\Tests

Code

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 = array();
  $edit['user_admin_role'] = $this->rid;
  $this
    ->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
  \Drupal::entityManager()
    ->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(array(
    '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 = array();
  $edit['user_admin_role'] = '';
  $this
    ->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
  \Drupal::entityManager()
    ->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');
}