You are here

protected function GroupMembershipManagerTest::doTestGetGroupMembershipsByRoleNames in Organic groups 8

Tests retrieval of group memberships or their IDs filtered by role names.

Contains the actual test logic of ::testGetGroupMembershipsByRoleNames() and ::testGetGroupMembershipIdsByRoleNames().

Parameters

string $method_name: The name of the method under test. Can be one of the following:

  • 'getGroupMembershipIdsByRoleNames'
  • 'getGroupMembershipsByRoleNames'.

callable $retrieve_membership_owner_id: A callable that will retrieve the ID of the owner of the membership or membership ID.

2 calls to GroupMembershipManagerTest::doTestGetGroupMembershipsByRoleNames()
GroupMembershipManagerTest::testGetGroupMembershipIdsByRoleNames in tests/src/Kernel/Entity/GroupMembershipManagerTest.php
Tests retrieval of group membership IDs filtered by role names.
GroupMembershipManagerTest::testGetGroupMembershipsByRoleNames in tests/src/Kernel/Entity/GroupMembershipManagerTest.php
Tests retrieval of group memberships filtered by role names.

File

tests/src/Kernel/Entity/GroupMembershipManagerTest.php, line 408

Class

GroupMembershipManagerTest
Tests retrieving groups associated with a given group content.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

protected function doTestGetGroupMembershipsByRoleNames($method_name, callable $retrieve_membership_owner_id) {
  $this
    ->createTestMemberships();

  // Check that an exception is thrown if no role names are passed.
  try {
    $this->membershipManager
      ->{$method_name}($this->groups['node'][0], []);
    $this
      ->fail('MembershipManager::getGroupsMembershipsByRoleNames() throws an exception when called without passing any role names.');
  } catch (\InvalidArgumentException $e) {

    // Expected result.
  }

  // Define a test matrix to iterate over. We're not using a data provider
  // because the large number of test cases would slow down the test too much.
  // The test matrix has the following structure:
  // @code
  // [
  //   // The machine name of the group entity type being tested.
  //   {entity_type_id} => [
  //     // The key of the test group as created in ::setUp().
  //     {group_key} => [ //
  //       // The roles being passed to the method.
  //       'roles' => [{role_id}],
  //       // The membership states being passed to the method.
  //       'states' => [{state_id}],
  //       // The memberships that should be returned by the method.
  //       'expected_memberships' => [{expected_membership_id}],
  //     ],
  //   ],
  // ];
  // @endcode
  $matrix = [
    'node' => [
      0 => [
        // All memberships with all possible states. The authenticated role
        // covers all memberships.
        [
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
          'states' => OgMembershipInterface::ALL_STATES,
          'expected_memberships' => [
            0,
            1,
            4,
            7,
          ],
        ],
        // All memberships with all possible states, by passing an empty
        // states array, and passing all defined roles.
        [
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
            OgRoleInterface::ADMINISTRATOR,
            'moderator',
          ],
          'states' => [],
          'expected_memberships' => [
            0,
            1,
            4,
            7,
          ],
        ],
        // Pending members.
        [
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
          ],
          'states' => [
            OgMembershipInterface::STATE_PENDING,
          ],
          'expected_memberships' => [
            4,
          ],
        ],
      ],
      1 => [
        // All administrators.
        [
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
          ],
          'states' => [],
          'expected_memberships' => [
            2,
            6,
          ],
        ],
        // Pending administrators.
        [
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
          ],
          'states' => [
            OgMembershipInterface::STATE_PENDING,
          ],
          'expected_memberships' => [
            2,
          ],
        ],
        // Blocked administrators. There are none.
        [
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
          ],
          'states' => [
            OgMembershipInterface::STATE_BLOCKED,
          ],
          'expected_memberships' => [],
        ],
        // Pending and blocked administrators and moderators. Should be the
        // same result as the pending administrators, since there are no
        // moderators or blocked users.
        [
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
            'moderator',
          ],
          'states' => [
            OgMembershipInterface::STATE_BLOCKED,
            OgMembershipInterface::STATE_PENDING,
          ],
          'expected_memberships' => [
            2,
          ],
        ],
        // Switch the order of the arguments, this should not affect the
        // result.
        [
          'roles' => [
            'moderator',
            OgRoleInterface::ADMINISTRATOR,
          ],
          'states' => [
            OgMembershipInterface::STATE_PENDING,
            OgMembershipInterface::STATE_BLOCKED,
          ],
          'expected_memberships' => [
            2,
          ],
        ],
        // There are no pending or blocked moderators.
        [
          'roles' => [
            'moderator',
          ],
          'states' => [
            OgMembershipInterface::STATE_BLOCKED,
            OgMembershipInterface::STATE_PENDING,
          ],
          'expected_memberships' => [],
        ],
      ],
    ],
    'entity_test' => [
      0 => [
        // The first test entity group doesn't have any moderators or admins.
        // Check that duplicated array values doesn't affect the result.
        [
          'roles' => [
            'moderator',
            OgRoleInterface::ADMINISTRATOR,
            'moderator',
            'moderator',
            OgRoleInterface::ADMINISTRATOR,
          ],
          'states' => [
            OgMembershipInterface::STATE_ACTIVE,
            OgMembershipInterface::STATE_BLOCKED,
            OgMembershipInterface::STATE_PENDING,
            OgMembershipInterface::STATE_PENDING,
            OgMembershipInterface::STATE_BLOCKED,
            OgMembershipInterface::STATE_ACTIVE,
          ],
          'expected_memberships' => [],
        ],
      ],
      // Check active members.
      [
        'roles' => [
          OgRoleInterface::AUTHENTICATED,
        ],
        'states' => [
          OgMembershipInterface::STATE_ACTIVE,
        ],
        'expected_memberships' => [
          0,
          3,
        ],
      ],
      1 => [
        // There are two blocked users in the second test entity group.
        [
          'roles' => [
            OgRoleInterface::AUTHENTICATED,
            OgRoleInterface::ADMINISTRATOR,
            'moderator',
          ],
          'states' => [
            OgMembershipInterface::STATE_BLOCKED,
          ],
          'expected_memberships' => [
            4,
            7,
          ],
        ],
        // There is one pending administrator, just as in the node group with
        // the same entity ID. This ensures that the correct result will be
        // returned for groups that have different entity types but the same
        // entity ID.
        [
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
          ],
          'states' => [
            OgMembershipInterface::STATE_PENDING,
          ],
          'expected_memberships' => [
            8,
          ],
        ],
        // There is one blocked moderator.
        [
          'roles' => [
            OgRoleInterface::ADMINISTRATOR,
            'moderator',
          ],
          'states' => [
            OgMembershipInterface::STATE_BLOCKED,
          ],
          'expected_memberships' => [
            4,
          ],
        ],
      ],
    ],
  ];
  foreach ($matrix as $entity_type_id => $groups) {
    foreach ($groups as $group_key => $test_cases) {
      foreach ($test_cases as $test_case) {
        $group = $this->groups[$entity_type_id][$group_key];
        $role_names = $test_case['roles'];
        $states = $test_case['states'];
        $expected_memberships = $test_case['expected_memberships'];
        $actual_memberships = $this->membershipManager
          ->{$method_name}($group, $role_names, $states);
        $this
          ->assertSameSize($expected_memberships, $actual_memberships);
        foreach ($expected_memberships as $expected_membership_key) {
          $expected_user_id = $this->users[$expected_membership_key]
            ->id();
          foreach ($actual_memberships as $actual_membership) {
            if ($retrieve_membership_owner_id($actual_membership) == $expected_user_id) {

              // Match found.
              continue 2;
            }
          }

          // The expected membership was not returned. Fail the test.
          $this
            ->fail("The membership for user {$expected_membership_key} is present in the result.");
        }
      }
    }
  }

  // Test that the correct memberships are returned when one of the users is
  // deleted. We are using the second node group as a test case. This group
  // has one pending administrator: the user with key '2'.
  $group = $this->groups['node'][1];
  $role_names = [
    OgRoleInterface::ADMINISTRATOR,
  ];
  $states = [
    OgMembershipInterface::STATE_PENDING,
  ];
  $memberships = $this->membershipManager
    ->{$method_name}($group, $role_names, $states);
  $this
    ->assertCount(1, $memberships);

  // Delete the user and check that it no longer appears in the result.
  $this->users[2]
    ->delete();
  $memberships = $this->membershipManager
    ->{$method_name}($group, $role_names, $states);
  $this
    ->assertCount(0, $memberships);
}