You are here

public function GroupMembershipManagerTest::testGetGroups in Organic groups 8

Tests retrieval of groups that are associated with a given group content.

@covers ::getGroups @dataProvider groupContentProvider

Parameters

string $group_type_id: Optional group type ID to be passed as an argument to the method under test.

string $group_bundle: Optional group bundle to be passed as an argument to the method under test.

array $expected: An array containing the expected results to be returned.

File

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

Class

GroupMembershipManagerTest
Tests retrieving groups associated with a given group content.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testGetGroups($group_type_id, $group_bundle, array $expected) {
  $result = $this->membershipManager
    ->getGroups($this->groupContent, $group_type_id, $group_bundle);

  // Check that the correct number of results is returned.
  $this
    ->assertEquals(count($expected, COUNT_RECURSIVE), count($result, COUNT_RECURSIVE));

  // Check that all expected results are returned.
  foreach ($expected as $expected_type => $expected_keys) {
    foreach ($expected_keys as $expected_key) {

      /** @var \Drupal\Core\Entity\EntityInterface $expected_group */
      $expected_group = $this->groups[$expected_type][$expected_key];

      /** @var \Drupal\Core\Entity\EntityInterface $group */
      foreach ($result[$expected_type] as $group) {
        if ($group
          ->getEntityTypeId() === $expected_group
          ->getEntityTypeId() && $group
          ->id() === $expected_group
          ->id()) {

          // The expected result was found. Continue the test.
          continue 2;
        }
      }

      // The expected result was not found.
      $this
        ->fail("The expected group of type {$expected_type} and key {$expected_key} is found.");
    }
  }
}