diff --git a/src/Codeception/Module/DrupalGroup.php b/src/Codeception/Module/DrupalGroup.php new file mode 100644 index 0000000..05eb5db --- /dev/null +++ b/src/Codeception/Module/DrupalGroup.php @@ -0,0 +1,106 @@ +getStorage($type) + ->create($values); + if ($validate && $entity instanceof FieldableEntityInterface) { + $violations = $entity->validate(); + if ($violations->count() > 0) { + $message = PHP_EOL; + foreach ($violations as $violation) { + $message .= $violation->getPropertyPath() . ': ' . $violation->getMessage() . PHP_EOL; + } + throw new \Exception($message); + } + } + // Group specific entity save options. + $entity->setOwner(User::load($values['uid'] ?? 1)); + $entity->save(); + } + catch (\Exception $e) { + $this->fail('Could not create group entity. Error message: ' . $e->getMessage()); + } + if (!empty($entity)) { + $this->registerTestEntity($entity->getEntityTypeId(), $entity->id()); + + return $entity; + } + + return FALSE; + } + + /** + * Join the defined group. + * + * @param \Drupal\group\Entity\GroupInterface $group + * Instance of a group. + * @param \Drupal\user\UserInterface $user + * A drupal user to add to the group. + * + * @return \Drupal\group\GroupMembership|false + * Returns the group content entity, FALSE otherwise. + */ + public function joinGroup(GroupInterface $group, UserInterface $user) { + $group->addMember($user); + return $group->getMember($user); + } + + /** + * Leave a group. + * + * @param \Drupal\group\Entity\GroupInterface $group + * Instance of a group. + * @param \Drupal\user\UserInterface $user + * A drupal user to add to the group. + * + * @return bool + * Returns the TRUE if the user is no longer a member of the group, + * FALSE otherwise. + */ + public function leaveGroup(GroupInterface $group, UserInterface $user) { + $group->removeMember($user); + // Get member should return FALSE if the user isn't a member so we + // reverse the logic. If they are still a member it'll cast to TRUE. + $is_member = (bool) $group->getMember($user); + return !$is_member; + } + +}