|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codeception\Module; |
| 4 | + |
| 5 | +use Drupal\Core\Entity\FieldableEntityInterface; |
| 6 | +use Drupal\group\Entity\GroupInterface; |
| 7 | +use Drupal\user\Entity\User; |
| 8 | +use Drupal\user\UserInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class DrupalGroup. |
| 12 | + * |
| 13 | + * ### Example |
| 14 | + * #### Example (DrupalGroup) |
| 15 | + * modules: |
| 16 | + * - DrupalGroup: |
| 17 | + * cleanup_test: true |
| 18 | + * cleanup_failed: false |
| 19 | + * cleanup_suite: true |
| 20 | + * route_entities: |
| 21 | + * - node |
| 22 | + * - taxonomy_term. |
| 23 | + * |
| 24 | + * @package Codeception\Module |
| 25 | + */ |
| 26 | +class DrupalGroup extends DrupalEntity { |
| 27 | + |
| 28 | + /** |
| 29 | + * Wrapper method to create a group. |
| 30 | + * |
| 31 | + * Improves readbility of tests by having the method read "create group". |
| 32 | + */ |
| 33 | + public function createGroup(array $values = [], $validate = TRUE) { |
| 34 | + $type = 'group'; |
| 35 | + |
| 36 | + if (!array_key_exists('uid', $values)) { |
| 37 | + $values['uid'] = 1; |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + $entity = \Drupal::entityTypeManager() |
| 42 | + ->getStorage($type) |
| 43 | + ->create($values); |
| 44 | + if ($validate && $entity instanceof FieldableEntityInterface) { |
| 45 | + $violations = $entity->validate(); |
| 46 | + if ($violations->count() > 0) { |
| 47 | + $message = PHP_EOL; |
| 48 | + foreach ($violations as $violation) { |
| 49 | + $message .= $violation->getPropertyPath() . ': ' . $violation->getMessage() . PHP_EOL; |
| 50 | + } |
| 51 | + throw new \Exception($message); |
| 52 | + } |
| 53 | + } |
| 54 | + // Group specific entity save options. |
| 55 | + $entity->setOwner(User::load($values['uid'] ?? 1)); |
| 56 | + $entity->save(); |
| 57 | + } |
| 58 | + catch (\Exception $e) { |
| 59 | + $this->fail('Could not create group entity. Error message: ' . $e->getMessage()); |
| 60 | + } |
| 61 | + if (!empty($entity)) { |
| 62 | + $this->registerTestEntity($entity->getEntityTypeId(), $entity->id()); |
| 63 | + |
| 64 | + return $entity; |
| 65 | + } |
| 66 | + |
| 67 | + return FALSE; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Join the defined group. |
| 72 | + * |
| 73 | + * @param \Drupal\group\Entity\GroupInterface $group |
| 74 | + * Instance of a group. |
| 75 | + * @param \Drupal\user\UserInterface $user |
| 76 | + * A drupal user to add to the group. |
| 77 | + * |
| 78 | + * @return \Drupal\group\GroupMembership|false |
| 79 | + * Returns the group content entity, FALSE otherwise. |
| 80 | + */ |
| 81 | + public function joinGroup(GroupInterface $group, UserInterface $user) { |
| 82 | + $group->addMember($user); |
| 83 | + return $group->getMember($user); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Leave a group. |
| 88 | + * |
| 89 | + * @param \Drupal\group\Entity\GroupInterface $group |
| 90 | + * Instance of a group. |
| 91 | + * @param \Drupal\user\UserInterface $user |
| 92 | + * A drupal user to add to the group. |
| 93 | + * |
| 94 | + * @return bool |
| 95 | + * Returns the TRUE if the user is no longer a member of the group, |
| 96 | + * FALSE otherwise. |
| 97 | + */ |
| 98 | + public function leaveGroup(GroupInterface $group, UserInterface $user) { |
| 99 | + $group->removeMember($user); |
| 100 | + // Get member should return FALSE if the user isn't a member so we |
| 101 | + // reverse the logic. If they are still a member it'll cast to TRUE. |
| 102 | + $is_member = (bool) $group->getMember($user); |
| 103 | + return !$is_member; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments