public function OgResolvedGroupCollectionTest::testAddGroup in Organic groups 8
Tests adding a group to the collection, or casting an additional vote.
@covers ::addGroup
File
- tests/
src/ Unit/ OgResolvedGroupCollectionTest.php, line 51
Class
- OgResolvedGroupCollectionTest
- Tests the collecting of resolved groups to pass as a route context.
Namespace
Drupal\Tests\og\UnitCode
public function testAddGroup() {
$collection = new OgResolvedGroupCollection();
foreach ($this->groups as $group) {
$key = $group
->getEntityTypeId() . '|' . $group
->id();
// Initially the group should not exist in the collection.
$this
->assertFalse($collection
->hasGroup($group));
// Try adding the group without any optional parameters.
$collection
->addGroup($group);
// The group should now exist in the collection.
$this
->assertTrue($collection
->hasGroup($group));
$info = $collection
->getGroupInfo()[$key];
$this
->assertEquals($info['entity'], $group);
// There should not be any cache contexts associated with it.
$this
->assertArrayNotHasKey('cache_contexts', $info);
// There should be a single vote, which was cast with the default vote
// weight (0).
$this
->assertEquals(1, count($info['votes']));
$this
->assertEquals(0, $info['votes'][0]);
// Add a second vote for the group, this time passing cache contexts.
$collection
->addGroup($group, [
'route',
'url',
]);
// The cache contexts should now be associated with the group.
$info = $collection
->getGroupInfo()[$key];
$this
->assertEquals([
'route',
'url',
], array_values($info['cache_contexts']));
// There should now be two votes, and both should have been cast with the
// default vote weight.
$this
->assertEquals(2, count($info['votes']));
$this
->assertEquals(0, $info['votes'][0]);
$this
->assertEquals(0, $info['votes'][1]);
// Add a third vote, this time specifying both a cache context and a
// custom vote weight.
$weight = rand(-100, 100);
$collection
->addGroup($group, [
'user',
], $weight);
// The additional cache context should now be associated with the group.
$info = $collection
->getGroupInfo()[$key];
$this
->assertEquals([
'route',
'url',
'user',
], array_values($info['cache_contexts']));
// There should now be three votes, the last of which having the custom
// vote weight.
$this
->assertEquals(3, count($info['votes']));
$this
->assertEquals(0, $info['votes'][0]);
$this
->assertEquals(0, $info['votes'][1]);
$this
->assertEquals($weight, $info['votes'][2]);
// Adding another vote using a cache context that has been set before
// should not cause the cache context to be listed twice.
$collection
->addGroup($group, [
'url',
'user',
]);
$info = $collection
->getGroupInfo()[$key];
$this
->assertEquals([
'route',
'url',
'user',
], array_values($info['cache_contexts']));
}
}