diff --git a/README.md b/README.md index 88109faf5448e10f9dd26eae716226ecde5855a4..6b3cb5b7d3827a93739081561be72f9ba9ec99c3 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ drush en grequest # CONFIGURATION - 1) Install Group content type "**Group Membership Request**" + 1) Install Group relationship type "**Group Membership Request**" /admin/group/types/manage/[ YOUR_GROUP_TYPE ]/content diff --git a/src/Controller/GroupMembershipRequestController.php b/src/Controller/GroupMembershipRequestController.php index 03621eaab37c66cdc42cd8a72bfd51440eb77a52..4665f17902dbe04d7a1851d796cdfaeaa852e662 100644 --- a/src/Controller/GroupMembershipRequestController.php +++ b/src/Controller/GroupMembershipRequestController.php @@ -6,10 +6,8 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Session\AccountInterface; use Drupal\grequest\MembershipRequestManager; -use Drupal\group\Entity\GroupContentInterface; use Drupal\group\Entity\GroupInterface; use Drupal\group\Entity\GroupRelationshipInterface; -use Drupal\group\Plugin\Group\Relation\GroupRelationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** diff --git a/src/Entity/Form/GroupMembershipApproveForm.php b/src/Entity/Form/GroupMembershipApproveForm.php index 2f7160bcec0bcee5047bd94b98489871c814d7c8..f3dca1471e3c9498262c5e6fe043017d9da4f01e 100644 --- a/src/Entity/Form/GroupMembershipApproveForm.php +++ b/src/Entity/Form/GroupMembershipApproveForm.php @@ -145,14 +145,7 @@ class GroupMembershipApproveForm extends ContentEntityConfirmFormBase { */ protected function getGroupRoles() { $options = []; - $properties = [ - 'group_type' => $this->getEntity()->getGroup()->getGroupType()->id(), - 'internal' => FALSE, - ]; - - $roles = $this->entityTypeManager - ->getStorage('group_role') - ->loadByProperties($properties); + $roles = $this->getEntity()->getGroup()->getGroupType()->getRoles(FALSE); foreach ($roles as $role) { $options[$role->id()] = $role->label(); } diff --git a/src/MembershipRequestManager.php b/src/MembershipRequestManager.php index 3b5cb3ad8055a8d0b6305651fda2d5ebd1e8a13b..bf8116c78cb8f319a6005eb2cc7cacc974af019d 100644 --- a/src/MembershipRequestManager.php +++ b/src/MembershipRequestManager.php @@ -122,7 +122,7 @@ class MembershipRequestManager { */ public function updateStatus(GroupRelationshipInterface $group_relationship, $transition_id) { if ($group_relationship->getPluginId() != 'group_membership_request') { - throw new \Exception('Only group content of "Group membership request" is allowed.'); + throw new \Exception('Only group relationship of "Group membership request" is allowed.'); } $state_item = $group_relationship->get(GroupMembershipRequest::STATUS_FIELD)->first(); if ($state_item->isTransitionAllowed($transition_id)) { diff --git a/src/Plugin/Group/Relation/GroupMembershipRequest.php b/src/Plugin/Group/Relation/GroupMembershipRequest.php index 5957fec82867c4c6061c73157b959d4ce3553c55..0161acdf7d22c97bb19f406b44d39f7462ff7d62 100644 --- a/src/Plugin/Group/Relation/GroupMembershipRequest.php +++ b/src/Plugin/Group/Relation/GroupMembershipRequest.php @@ -10,7 +10,7 @@ use Drupal\group\Plugin\Group\Relation\GroupRelationBase; use Drupal\Core\Session\AccountInterface; /** - * Provides a group relationship for users as group membership request. + * Provides a group relation for users as group membership request. * * @GroupRelationType( * id = "group_membership_request", diff --git a/tests/src/Functional/GroupMembershipRequestFormTest.php b/tests/src/Functional/GroupMembershipRequestFormTest.php index 1bb2eb8ad47248dcc06cbf1682e58857f1f4cc31..ed1a7653fb62973f6ddf71d227d73c4d57b1c2b4 100644 --- a/tests/src/Functional/GroupMembershipRequestFormTest.php +++ b/tests/src/Functional/GroupMembershipRequestFormTest.php @@ -7,8 +7,11 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\group\Entity\GroupContent; +use Drupal\group\Entity\GroupRelationshipInterface; use Drupal\group\GroupMembership; +use Drupal\group\PermissionScopeInterface; use Drupal\Tests\group\Functional\GroupBrowserTestBase; +use Drupal\user\RoleInterface; /** * Tests the behavior of the group type form. @@ -29,13 +32,6 @@ class GroupMembershipRequestFormTest extends GroupBrowserTestBase { 'grequest', ]; - /** - * The entity type manager service. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected $entityTypeManager; - /** * Membership request manager. * @@ -55,18 +51,21 @@ class GroupMembershipRequestFormTest extends GroupBrowserTestBase { */ protected function setUp(): void { parent::setUp(); - $this->group = $this->createGroup(); + $this->group = $this->createGroup(['type' => 'default']); + $group_type = $this->group->getGroupType(); + $plugin_id = 'group_membership_request'; $this->membershipRequestManager = $this->container->get('grequest.membership_request_manager'); - $this->entityTypeManager = $this->container->get('entity_type.manager'); - // Install group_membership_request group content. - $storage = $this->entityTypeManager->getStorage('group_content_type'); + // Install group_membership_request group relationship. + + /** @var \Drupal\group\Entity\Storage\GroupRelationshipTypeStorageInterface $group_relationship_type_storage */ + $group_relationship_type_storage = $this->entityTypeManager->getStorage('group_content_type'); $config = [ 'group_cardinality' => 0, 'entity_cardinality' => 1, ]; - $storage->createFromPlugin($this->group->getGroupType(), 'group_membership_request', $config)->save(); + $group_relationship_type_storage->createFromPlugin($group_type, $plugin_id, $config)->save(); // Add a text field to the group content type. $field_storage = FieldStorageConfig::create([ @@ -76,34 +75,38 @@ class GroupMembershipRequestFormTest extends GroupBrowserTestBase { ]); $field_storage->save(); + $relation_type_id = $group_relationship_type_storage->getRelationshipTypeId($group_type->id(), $plugin_id); FieldConfig::create([ 'field_storage' => $field_storage, - 'bundle' => $this->group - ->getGroupType() - ->getContentPlugin('group_membership_request') - ->getContentTypeConfigId(), + 'bundle' => $relation_type_id, 'label' => 'String long', ])->save(); EntityFormDisplay::create([ 'targetEntityType' => 'group_content', - 'bundle' => $this->group - ->getGroupType() - ->getContentPlugin('group_membership_request') - ->getContentTypeConfigId(), + 'bundle' => $relation_type_id, 'mode' => 'default', 'status' => TRUE, ])->setComponent('field_test_text', ['type' => 'text_textfield'])->enable()->save(); // Add permissions to the creator of the group. - $role = $this->group->getGroupType()->getMemberRole(); - $role->grantPermissions(['administer membership requests']); - $role->save(); + $this->createGroupRole([ + 'group_type' => $group_type->id(), + 'scope' => PermissionScopeInterface::INSIDER_ID, + 'global_role' => RoleInterface::AUTHENTICATED_ID, + 'permissions' => [ + 'administer membership requests', + 'leave group', + ], + ]); // Allow outsider request membership. - $role = $this->group->getGroupType()->getOutsiderRole(); - $role->grantPermissions(['request group membership']); - $role->save(); + $this->createGroupRole([ + 'group_type' => $group_type->id(), + 'scope' => PermissionScopeInterface::OUTSIDER_ID, + 'global_role' => RoleInterface::AUTHENTICATED_ID, + 'permissions' => ['request group membership'], + ]); } /** @@ -113,7 +116,13 @@ class GroupMembershipRequestFormTest extends GroupBrowserTestBase { $account = $this->createUser(); $group_membership_request = $this->membershipRequestManager->create($this->group, $account); $group_membership_request->save(); - $role_name = 'default-custom'; + + // Create a custom role. + $custom_role = $this->createGroupRole([ + 'label' => 'Custom role', + 'group_type' => $this->group->getGroupType()->id(), + 'scope' => PermissionScopeInterface::INDIVIDUAL_ID, + ]); $this->drupalGet("/group/{$this->group->id()}/content/{$group_membership_request->id()}/approve-membership"); $this->assertSession()->statusCodeEquals(200); @@ -125,7 +134,7 @@ class GroupMembershipRequestFormTest extends GroupBrowserTestBase { $this->assertSession()->pageTextContains(strip_tags($this->t('Are you sure you want to approve a request for %user?', ['%user' => $account->getDisplayName()])->render())); $edit = [ - "roles[$role_name]" => 1, + "roles[{$custom_role->id()}]" => 1, ]; $this->submitForm($edit, $submit_button); @@ -188,7 +197,7 @@ class GroupMembershipRequestFormTest extends GroupBrowserTestBase { $this->assertSession()->pageTextContains($this->t('Your request is waiting for approval')); $membership_request = $this->membershipRequestManager->getMembershipRequest($account, $this->group); - $this->assertTrue($membership_request instanceof GroupContent, 'Membership request has been successfully created.'); + $this->assertTrue($membership_request instanceof GroupRelationshipInterface, 'Membership request has been successfully created.'); $this->drupalGet("/group/{$this->group->id()}/request-membership"); $this->submitForm([], $submit_button); diff --git a/tests/src/Kernel/GroupMembershipRequestConfigTest.php b/tests/src/Kernel/GroupMembershipRequestConfigTest.php index 9a829a9bf6f5ec2e7e5db19e58048cef8f71e545..b9fa5f5912ff90a2b6c22516315631ebd3f7eaa3 100644 --- a/tests/src/Kernel/GroupMembershipRequestConfigTest.php +++ b/tests/src/Kernel/GroupMembershipRequestConfigTest.php @@ -22,6 +22,7 @@ class GroupMembershipRequestConfigTest extends EntityKernelTestBase { 'options', 'entity', 'variationcache', + 'flexible_permissions', 'state_machine', 'grequest', ]; diff --git a/tests/src/Kernel/GroupMembershipRequestTest.php b/tests/src/Kernel/GroupMembershipRequestTest.php index 830eebbf4b2e757e79cfd75e45d23e6facc5bc1f..85c9a524223e23c0be682a331652121cf9c182f6 100644 --- a/tests/src/Kernel/GroupMembershipRequestTest.php +++ b/tests/src/Kernel/GroupMembershipRequestTest.php @@ -2,9 +2,10 @@ namespace Drupal\Tests\grequest\Kernel; -use Drupal\grequest\Plugin\GroupContentEnabler\GroupMembershipRequest; -use Drupal\group\Entity\GroupContent; +use Drupal\grequest\Plugin\Group\Relation\GroupMembershipRequest; +use Drupal\group\Entity\GroupRelationship; use Drupal\group\GroupMembership; +use Drupal\group\PermissionScopeInterface; use Drupal\Tests\group\Kernel\GroupKernelTestBase; /** @@ -23,18 +24,18 @@ class GroupMembershipRequestTest extends GroupKernelTestBase { protected $membershipRequestManager; /** - * The entity type manager. + * The group we will use to test methods on. * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface + * @var \Drupal\group\Entity\Group */ - protected $entityTypeManager; + protected $group; /** * The group we will use to test methods on. * * @var \Drupal\group\Entity\Group */ - protected $group; + protected $groupRelationshipTypeStorage; /** * Modules to enable. @@ -60,12 +61,12 @@ class GroupMembershipRequestTest extends GroupKernelTestBase { ]); $this->membershipRequestManager = $this->container->get('grequest.membership_request_manager'); - $this->entityTypeManager = $this->container->get('entity_type.manager'); - $this->group = $this->createGroup(); - // Enable group membership request group content plugin. - $group_content_type_storage = $this->entityTypeManager->getStorage('group_content_type'); - $group_content_type_storage->save($group_content_type_storage->createFromPlugin($this->group->getGroupType(), 'group_membership_request')); + $group_type = $this->createGroupType(); + $this->group = $this->createGroup(['type' => $group_type->id()]); + // Enable group membership request group relationship plugin. + $this->groupRelationshipTypeStorage = $this->entityTypeManager->getStorage('group_content_type'); + $this->groupRelationshipTypeStorage->save($this->groupRelationshipTypeStorage->createFromPlugin($group_type, 'group_membership_request')); } /** @@ -133,7 +134,7 @@ class GroupMembershipRequestTest extends GroupKernelTestBase { /** * Create group membership request. * - * @return \Drupal\group\Entity\GroupContentInterface + * @return \Drupal\group\Entity\GroupRelationshipInterface * Group membership request. */ public function createRequestMembership($account) { @@ -150,16 +151,21 @@ class GroupMembershipRequestTest extends GroupKernelTestBase { */ public function testApprovalWithRoles() { $account = $this->createUser(); - $role_name = 'default-custom'; + // Create a custom role. + $custom_role = $this->createGroupRole([ + 'label' => 'Custom role', + 'group_type' => $this->group->getGroupType()->id(), + 'scope' => PermissionScopeInterface::INDIVIDUAL_ID, + ]); $group_membership_request = $this->createRequestMembership($account); - $this->membershipRequestManager->approve($group_membership_request, [$role_name]); + $this->membershipRequestManager->approve($group_membership_request, [$custom_role->id()]); $group_membership = $this->group->getMember($account); $this->assertTrue($group_membership instanceof GroupMembership, 'Group membership has been successfully created.'); - $this->assertTrue(in_array($role_name, array_keys($group_membership->getRoles())), 'Role has been found'); + $this->assertTrue(in_array($custom_role->id(), array_keys($group_membership->getRoles())), 'Role has been found'); } /** @@ -215,21 +221,19 @@ class GroupMembershipRequestTest extends GroupKernelTestBase { } /** - * Test wrong group content. + * Test wrong group relationship. */ - public function testWrongGroupContent() { + public function testWrongGroupRelationship() { $account = $this->createUser(); - $group_membership_group_content = GroupContent::create([ - 'type' => $this->group - ->getGroupType() - ->getContentPlugin('group_membership') - ->getContentTypeConfigId(), + $relation_type_id = $this->groupRelationshipTypeStorage->getRelationshipTypeId($this->group->getGroupType()->id(), 'group_membership'); + $group_membership_group_relationship = GroupRelationship::create([ + 'type' => $relation_type_id, 'gid' => $this->group->id(), 'entity_id' => $account->id(), ]); - $this->expectExceptionMessage('Only group content of "Group membership request" is allowed.'); - $this->membershipRequestManager->approve($group_membership_group_content); + $this->expectExceptionMessage('Only group relationship of "Group membership request" is allowed.'); + $this->membershipRequestManager->approve($group_membership_group_relationship); } }