You are here

class UserRoleEntityTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Kernel/UserRoleEntityTest.php \Drupal\Tests\user\Kernel\UserRoleEntityTest
  2. 9 core/modules/user/tests/src/Kernel/UserRoleEntityTest.php \Drupal\Tests\user\Kernel\UserRoleEntityTest

@group user @coversDefaultClass \Drupal\user\Entity\Role

Hierarchy

Expanded class hierarchy of UserRoleEntityTest

File

core/modules/user/tests/src/Kernel/UserRoleEntityTest.php, line 12

Namespace

Drupal\Tests\user\Kernel
View source
class UserRoleEntityTest extends KernelTestBase {
  protected static $modules = [
    'system',
    'user',
    'user_permissions_test',
  ];
  public function testOrderOfPermissions() {
    $role = Role::create([
      'id' => 'test_role',
      'label' => 'Test role',
    ]);
    $role
      ->grantPermission('b')
      ->grantPermission('a')
      ->grantPermission('c')
      ->save();
    $this
      ->assertEquals([
      'a',
      'b',
      'c',
    ], $role
      ->getPermissions());
    $role
      ->revokePermission('b')
      ->save();
    $this
      ->assertEquals([
      'a',
      'c',
    ], $role
      ->getPermissions());
    $role
      ->grantPermission('b')
      ->save();
    $this
      ->assertEquals([
      'a',
      'b',
      'c',
    ], $role
      ->getPermissions());
  }
  public function testGrantingNonExistentPermission() {
    $role = Role::create([
      'id' => 'test_role',
      'label' => 'Test role',
    ]);

    // A single permission that does not exist.
    $this
      ->expectException(\RuntimeException::class);
    $this
      ->expectExceptionMessage('Adding non-existent permissions to a role is not allowed. The incorrect permissions are "does not exist".');
    $role
      ->grantPermission('does not exist')
      ->save();

    // A multiple permissions that do not exist.
    $this
      ->expectException(\RuntimeException::class);
    $this
      ->expectExceptionMessage('Adding non-existent permissions to a role is not allowed. The incorrect permissions are "does not exist, also does not exist".');
    $role
      ->grantPermission('does not exist')
      ->grantPermission('also does not exist')
      ->save();
  }

}

Members