class UserRoleEntityTest
Same name and namespace in other branches
- 11.x core/modules/user/tests/src/Kernel/UserRoleEntityTest.php \Drupal\Tests\user\Kernel\UserRoleEntityTest
- 10 core/modules/user/tests/src/Kernel/UserRoleEntityTest.php \Drupal\Tests\user\Kernel\UserRoleEntityTest
- 8.9.x core/modules/user/tests/src/Kernel/UserRoleEntityTest.php \Drupal\Tests\user\Kernel\UserRoleEntityTest
@group user
@coversDefaultClass \Drupal\user\Entity\Role
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\user\Kernel\UserRoleEntityTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of UserRoleEntityTest
File
-
core/
modules/ user/ tests/ src/ Kernel/ UserRoleEntityTest.php, line 12
Namespace
Drupal\Tests\user\KernelView 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());
}
/**
* @group legacy
*/
public function testGrantingNonExistentPermission() {
$role = Role::create([
'id' => 'test_role',
'label' => 'Test role',
]);
// A single permission that does not exist.
$this->expectDeprecation('Adding non-existent permissions to a role is deprecated in drupal:9.3.0 and triggers a runtime exception before drupal:10.0.0. The incorrect permissions are "does not exist". Permissions should be defined in a permissions.yml file or a permission callback. See https://www.drupal.org/node/3193348');
$role->grantPermission('does not exist')
->save();
// A multiple permissions that do not exist.
$this->expectDeprecation('Adding non-existent permissions to a role is deprecated in drupal:9.3.0 and triggers a runtime exception before drupal:10.0.0. The incorrect permissions are "does not exist", "also does not exist". Permissions should be defined in a permissions.yml file or a permission callback. See https://www.drupal.org/node/3193348');
$role->grantPermission('does not exist')
->grantPermission('also does not exist')
->save();
}
public function testPermissionRevokeAndConfigSync() {
$role = Role::create([
'id' => 'test_role',
'label' => 'Test role',
]);
$role->setSyncing(TRUE);
$role->grantPermission('a')
->grantPermission('b')
->grantPermission('c')
->save();
$this->assertSame([
'a',
'b',
'c',
], $role->getPermissions());
$role->revokePermission('b')
->save();
$this->assertSame([
'a',
'c',
], $role->getPermissions());
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.