You are here

public function OgResolvedGroupCollectionTest::testSort in Organic groups 8

Tests if the groups can be correctly sorted according to the cast votes.

@covers ::sort

@dataProvider groupVotesProvider

Parameters

array $votes: An array of associative arrays representing voting information, with the following keys:

  • group: the ID of the group to add a vote for.
  • cache_contexts: an array of cache_contexts to associate with the group. To omit associating cache contexts, set to an empty array.
  • weight: an integer representing the vote weight. Set to NULL to omit.

array $expected_groups: The groups that are expected to be present after all votes are added, ordered by ranking.

See also

testAddGroup()

File

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

Class

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

Namespace

Drupal\Tests\og\Unit

Code

public function testSort(array $votes, array $expected_groups) {
  $collection = new OgResolvedGroupCollection();

  // Cast all votes.
  foreach ($votes as $vote) {
    $collection
      ->addGroup($this->groups[$vote['group']], $vote['cache_contexts'], $vote['weight']);
  }

  // Check that the groups can be correctly sorted.
  $collection
    ->sort();
  $info = $collection
    ->getGroupInfo();
  $actual_groups = array_values(array_map(function ($group_info) {
    return $group_info['entity']
      ->id();
  }, $info));
  $this
    ->assertEquals($expected_groups, $actual_groups);
}