public function SchedulerSetupTrait::addPermissionsToUser in Scheduler 2.x
Adds a set of permissions to an existing user.
This avoids having to create new users when a test requires additional permissions, as that leads to having a list of existing permissions which has to be kept in sync with the standard user permissions.
Each test user has two roles, 'authenticated' and one other randomly-named role assigned when the user is created, and unique to that user. This is the role to which these permissions are added.
Parameters
\Drupal\Core\Session\AccountInterface $user: The user object.
array $permissions: The machine names of new permissions to add to the user's unique role.
4 calls to SchedulerSetupTrait::addPermissionsToUser()
- SchedulerDevelGenerateTest::setUp in tests/
src/ Functional/ SchedulerDevelGenerateTest.php - SchedulerFieldsDisplayTest::setUp in tests/
src/ Functional/ SchedulerFieldsDisplayTest.php - SchedulerMultilingualTest::setUp in tests/
src/ Functional/ SchedulerMultilingualTest.php - SchedulerViewsAccessTest::createScheduledItems in tests/
src/ Functional/ SchedulerViewsAccessTest.php - Create users and scheduled content for the entity type being tested.
File
- tests/
src/ Traits/ SchedulerSetupTrait.php, line 208
Class
- SchedulerSetupTrait
- Generic setup for all Scheduler tests.
Namespace
Drupal\Tests\scheduler\TraitsCode
public function addPermissionsToUser(AccountInterface $user, array $permissions) {
/** @var \Drupal\user\Entity\RoleStorageInterface $roleStorage */
$roleStorage = $this->container
->get('entity_type.manager')
->getStorage('user_role');
foreach ($user
->getRoles() as $rid) {
// The user will have two roles, 'authenticated' and one other.
if ($rid != 'authenticated') {
$role = $roleStorage
->load($rid);
foreach ($permissions as $permission) {
$role
->grantPermission($permission);
}
$role
->save();
}
}
}