From c8182c9415cebe278bc2d79e736c464bd63dd542 Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Tue, 30 Aug 2022 11:53:25 -0400 Subject: [PATCH 1/2] feat(module): add group module support Create a subclass of the DrupalEntity module to make interacting with groups more convenient. --- src/Codeception/Module/DrupalGroup.php | 120 +++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/Codeception/Module/DrupalGroup.php diff --git a/src/Codeception/Module/DrupalGroup.php b/src/Codeception/Module/DrupalGroup.php new file mode 100644 index 0000000..10d0622 --- /dev/null +++ b/src/Codeception/Module/DrupalGroup.php @@ -0,0 +1,120 @@ +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->set('label', $values['label'] ?? 'Test group'); + $entity->save(); + } + catch (\Exception $e) { + $this->fail('Could not group entity. Error message: ' . $e->getMessage()); + } + if (!empty($entity)) { + $this->registerTestEntity($entity->getEntityTypeId(), $entity->id()); + + return $entity; + } + + return FALSE; + } + + /** + * Wrapper method to create a group. + * + * Improves readbility of tests by having the method read "create group". + * + * @see createEntity() + */ + public function createGroup(array $values = [], $validate = TRUE) { + if (!array_key_exists('uid', $values)) { + $values['uid'] = 1; + } + + return $this->createEntity($values, 'group', $validate); + } + + /** + * Join the defined group. + * + * @param \Drupal\group\Entity\GroupInterface $group + * Instance of a group. + * + * @return \Drupal\group\GroupMembership|false + * Returns the group content entity, FALSE otherwise. + */ + public function joinGroup(GroupInterface $group) { + $group->addMember(\Drupal::currentUser()->getAccount()); + return $group->getMember(\Drupal::currentUser()->getAccount()); + } + + /** + * Leave a group. + * + * @param \Drupal\group\Entity\GroupInterface $group + * Instance of a group. + * + * @return bool + * Returns the TRUE if the user is no longer a member of the group, + * FALSE otherwise. + */ + public function leaveGroup(GroupInterface $group) { + $group->removeMember(\Drupal::currentUser()->getAccount()); + // 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(\Drupal::currentUser()->getAccount()); + return !$is_member; + } + +} From 5742b11070c597b2e11942f6833058da5ad60bc3 Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Tue, 30 Aug 2022 13:07:44 -0400 Subject: [PATCH 2/2] fix(group): refactor to allow user selection --- src/Codeception/Module/DrupalGroup.php | 62 ++++++++++---------------- 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/src/Codeception/Module/DrupalGroup.php b/src/Codeception/Module/DrupalGroup.php index 10d0622..05eb5db 100644 --- a/src/Codeception/Module/DrupalGroup.php +++ b/src/Codeception/Module/DrupalGroup.php @@ -4,16 +4,16 @@ use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\group\Entity\GroupInterface; -use Drupal\group\GroupMembership; use Drupal\user\Entity\User; +use Drupal\user\UserInterface; /** - * Class DrupalEntity. + * Class DrupalGroup. * * ### Example - * #### Example (DrupalEntity) + * #### Example (DrupalGroup) * modules: - * - DrupalEntity: + * - DrupalGroup: * cleanup_test: true * cleanup_failed: false * cleanup_suite: true @@ -26,19 +26,17 @@ class DrupalGroup extends DrupalEntity { /** - * Create entity from values. - * - * @param array $values - * Data for creating entity. - * @param string $type - * Entity type. - * @param bool $validate - * Flag to validate entity fields.. + * Wrapper method to create a group. * - * @return \Drupal\Core\Entity\EntityInterface|bool - * Created entity. + * Improves readbility of tests by having the method read "create group". */ - public function createEntity(array $values = [], $type = 'group', $validate = TRUE) { + public function createGroup(array $values = [], $validate = TRUE) { + $type = 'group'; + + if (!array_key_exists('uid', $values)) { + $values['uid'] = 1; + } + try { $entity = \Drupal::entityTypeManager() ->getStorage($type) @@ -55,11 +53,10 @@ public function createEntity(array $values = [], $type = 'group', $validate = TR } // Group specific entity save options. $entity->setOwner(User::load($values['uid'] ?? 1)); - $entity->set('label', $values['label'] ?? 'Test group'); $entity->save(); } catch (\Exception $e) { - $this->fail('Could not group entity. Error message: ' . $e->getMessage()); + $this->fail('Could not create group entity. Error message: ' . $e->getMessage()); } if (!empty($entity)) { $this->registerTestEntity($entity->getEntityTypeId(), $entity->id()); @@ -70,33 +67,20 @@ public function createEntity(array $values = [], $type = 'group', $validate = TR return FALSE; } - /** - * Wrapper method to create a group. - * - * Improves readbility of tests by having the method read "create group". - * - * @see createEntity() - */ - public function createGroup(array $values = [], $validate = TRUE) { - if (!array_key_exists('uid', $values)) { - $values['uid'] = 1; - } - - return $this->createEntity($values, 'group', $validate); - } - /** * 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) { - $group->addMember(\Drupal::currentUser()->getAccount()); - return $group->getMember(\Drupal::currentUser()->getAccount()); + public function joinGroup(GroupInterface $group, UserInterface $user) { + $group->addMember($user); + return $group->getMember($user); } /** @@ -104,16 +88,18 @@ public function joinGroup(GroupInterface $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) { - $group->removeMember(\Drupal::currentUser()->getAccount()); + 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(\Drupal::currentUser()->getAccount()); + $is_member = (bool) $group->getMember($user); return !$is_member; }