You are here

public function OgResolvedGroupCollectionTest::testRemoveGroup in Organic groups 8

Tests removing groups from the collection.

@covers ::removeGroup

File

tests/src/Unit/OgResolvedGroupCollectionTest.php, line 164

Class

OgResolvedGroupCollectionTest
Tests the collecting of resolved groups to pass as a route context.

Namespace

Drupal\Tests\og\Unit

Code

public function testRemoveGroup() {
  $collection = new OgResolvedGroupCollection();

  // Add some random votes for a random selection of groups.
  $groups = [];
  for ($i = 0; $i < 20; $i++) {

    // Pick a random group.
    $group = $this->groups[array_rand($this->groups)];

    // Add a vote for the group.
    $collection
      ->addGroup($group);

    // Keep track of the groups we've added.
    $key = $group
      ->getEntityTypeId() . '|' . $group
      ->id();
    $groups[$key] = $group;
  }

  // Check that all our groups were added correctly.
  $this
    ->assertEquals(count($groups), count($collection
    ->getGroupInfo()));

  // Loop over the added groups and delete them one by one.
  foreach ($groups as $group) {

    // Initially the group should be there.
    $this
      ->assertTrue($collection
      ->hasGroup($group));

    // When we remove it the group should no longer be there.
    $collection
      ->removeGroup($group);
    $this
      ->assertFalse($collection
      ->hasGroup($group));
  }

  // Check that all our groups were removed correctly.
  $this
    ->assertEquals(0, count($collection
    ->getGroupInfo()));
}