You are here

public function GetMembershipsTest::testGetMemberships in Organic groups 8

Tests retrieval of OG Membership entities associated with a given user.

@covers ::getMemberships @dataProvider membershipDataProvider

Parameters

int $index: The array index in the $this->users array of the user to test.

array $states: Array with the states to retrieve.

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

File

tests/src/Kernel/Entity/GetMembershipsTest.php, line 143

Class

GetMembershipsTest
Tests retrieving OgMembership entities associated with a given user.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testGetMemberships($index, array $states, array $expected) {
  $result = Og::getMemberships($this->users[$index], $states);

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

  // Inspect the results that were returned.
  foreach ($result as $key => $membership) {

    // Check that all result items are OgMembership objects.
    $this
      ->assertInstanceOf('Drupal\\og\\OgMembershipInterface', $membership);

    // Check that the results are keyed by OgMembership ID.
    $this
      ->assertEquals($membership
      ->id(), $key);
  }

  // Check that all expected results are returned.
  foreach ($expected as $expected_group) {
    $expected_id = $this->groups[$expected_group]
      ->id();
    foreach ($result as $membership) {
      if ($membership
        ->getGroupId() === $expected_id) {

        // Test successful: the expected result was found.
        continue 2;
      }
    }
    $this
      ->fail("The expected group with ID {$expected_id} was not found.");
  }
}