protected function OgMembershipCreationTrait::createOgMembership in Organic groups 8
Creates a test membership.
Parameters
\Drupal\Core\Entity\EntityInterface $group: The group for which to create the membership.
\Drupal\Core\Session\AccountInterface $user: The user for which to create the membership.
array|null $role_names: Optional array of role names to assign to the membership. Defaults to the 'member' role.
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.
string|null $membership_type: The membership type. Defaults to 'default'.
Return value
\Drupal\og\OgMembershipInterface The membership.
Throws
\Drupal\Core\Entity\EntityStorageException Thrown when the membership cannot be created.
11 calls to OgMembershipCreationTrait::createOgMembership()
- AccessByOgMembershipTest::setUp in tests/
src/ Kernel/ Access/ AccessByOgMembershipTest.php - ActionTestBase::createUsers in tests/
src/ Kernel/ Action/ ActionTestBase.php - Creates test users.
- GetMembershipsTest::setUp in tests/
src/ Kernel/ Entity/ GetMembershipsTest.php - GetUserGroupsTest::testGetGroupsByRoles in tests/
src/ Kernel/ Entity/ GetUserGroupsTest.php - Tests retrieval of groups filtered by roles.
- GetUserGroupsTest::testIsMemberStates in tests/
src/ Kernel/ Entity/ GetUserGroupsTest.php - Tests member methods for states that other groups users are added to.
File
- tests/
src/ Traits/ OgMembershipCreationTrait.php, line 46
Class
- OgMembershipCreationTrait
- Provides a method to create memberships for testing purposes.
Namespace
Drupal\Tests\og\TraitsCode
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;
}