trait OgMembershipCreationTrait in Organic groups 8
Provides a method to create memberships for testing purposes.
This trait is meant to be used only by test classes.
Hierarchy
- trait \Drupal\Tests\og\Traits\OgMembershipCreationTrait
9 files declare their use of OgMembershipCreationTrait
- AccessByOgMembershipTest.php in tests/
src/ Kernel/ Access/ AccessByOgMembershipTest.php - ActionTestBase.php in tests/
src/ Kernel/ Action/ ActionTestBase.php - GetMembershipsTest.php in tests/
src/ Kernel/ Entity/ GetMembershipsTest.php - GetUserGroupsTest.php in tests/
src/ Kernel/ Entity/ GetUserGroupsTest.php - GroupMembershipManagerTest.php in tests/
src/ Kernel/ Entity/ GroupMembershipManagerTest.php
File
- tests/
src/ Traits/ OgMembershipCreationTrait.php, line 19
Namespace
Drupal\Tests\og\TraitsView source
trait OgMembershipCreationTrait {
/**
* Creates a test membership.
*
* @param \Drupal\Core\Entity\EntityInterface $group
* The group for which to create the membership.
* @param \Drupal\Core\Session\AccountInterface $user
* The user for which to create the membership.
* @param array|null $role_names
* Optional array of role names to assign to the membership. Defaults to the
* 'member' role.
* @param string|null $state
* Optional membership state. Can be one of the following constants:
* - OgMembershipInterface::STATE_ACTIVE
* - OgMembershipInterface::STATE_PENDING
* - OgMembershipInterface::STATE_BLOCKED
* Defaults to OgMembershipInterface::STATE_ACTIVE.
* @param string|null $membership_type
* The membership type. Defaults to 'default'.
*
* @return \Drupal\og\OgMembershipInterface
* The membership.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* Thrown when the membership cannot be created.
*/
protected function createOgMembership(EntityInterface $group, AccountInterface $user, ?array $role_names = NULL, ?string $state = NULL, ?string $membership_type = NULL) {
// Provide default values.
$role_names = $role_names ?: [
OgRoleInterface::AUTHENTICATED,
];
$state = $state ?: OgMembershipInterface::STATE_ACTIVE;
$membership_type = $membership_type ?: OgMembershipInterface::TYPE_DEFAULT;
$group_entity_type = $group
->getEntityTypeId();
$group_bundle = $group
->bundle();
$roles = array_map(function ($role_name) use ($group_entity_type, $group_bundle) {
return OgRole::getRole($group_entity_type, $group_bundle, $role_name);
}, $role_names);
/** @var \Drupal\og\OgMembershipInterface $membership */
$membership = OgMembership::create([
'type' => $membership_type,
]);
$membership
->setRoles($roles)
->setState($state)
->setOwner($user)
->setGroup($group)
->save();
return $membership;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OgMembershipCreationTrait:: |
protected | function | Creates a test membership. |