class UserPermissionsAdminTest

Same name and namespace in other branches
  1. 11.x core/modules/user/tests/src/Functional/UserPermissionsAdminTest.php \Drupal\Tests\user\Functional\UserPermissionsAdminTest
  2. 10 core/modules/user/tests/src/Functional/UserPermissionsAdminTest.php \Drupal\Tests\user\Functional\UserPermissionsAdminTest

Tests adding and removing permissions via the UI.

@group user

Hierarchy

Expanded class hierarchy of UserPermissionsAdminTest

File

core/modules/user/tests/src/Functional/UserPermissionsAdminTest.php, line 13

Namespace

Drupal\Tests\user\Functional
View source
class UserPermissionsAdminTest extends BrowserTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * Tests granting and revoking permissions via the UI sorts permissions.
   */
  public function testPermissionsSorting() {
    $role = Role::create([
      'id' => 'test_role',
      'label' => 'Test role',
    ]);
    // Start the role with a permission that is near the end of the alphabet.
    $role->grantPermission('view user email addresses');
    $role->save();
    $this->drupalLogin($this->drupalCreateUser([
      'administer permissions',
    ]));
    $this->drupalGet('admin/people/permissions');
    $this->assertSession()
      ->statusCodeEquals(200);
    // Add a permission that is near the start of the alphabet.
    $this->submitForm([
      'test_role[change own username]' => 1,
    ], 'Save permissions');
    // Check that permissions are sorted alphabetically.
    $storage = \Drupal::entityTypeManager()->getStorage('user_role');
    /** @var \Drupal\user\Entity\Role $role */
    $role = $storage->loadUnchanged($role->id());
    $this->assertEquals([
      'change own username',
      'view user email addresses',
    ], $role->getPermissions());
    // Remove the first permission, resulting in a single permission in the first
    // key of the array.
    $this->submitForm([
      'test_role[change own username]' => 0,
    ], 'Save permissions');
    /** @var \Drupal\user\Entity\Role $role */
    $role = $storage->loadUnchanged($role->id());
    $this->assertEquals([
      'view user email addresses',
    ], $role->getPermissions());
  }

}

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