You are here

public function OgRoleCacheContextTest::testMemberships in Organic groups 8

Tests that the correct cache context key is returned for group members.

Different users might have the identical roles across a number of different groups. Verify that a unique hash is returned for each combination of roles.

This tests the main implementation for SQL databases. The fallback implementation for NoSQL databases is tested in a unit test.

@covers ::getContext @dataProvider membershipsProvider

Parameters

array $group_memberships: An array that defines the roles test users have in test groups. See the data provider for a description of the format of the array.

array $expected_identical_role_groups: An array containing arrays of user IDs that are expected to have identical cache context keys, since they have identical memberships in the defined test groups.

See also

\Drupal\Tests\og\Unit\Cache\Context\OgRoleCacheContextTest::testMembershipsNoSql()

File

tests/src/Kernel/Cache/Context/OgRoleCacheContextTest.php, line 138

Class

OgRoleCacheContextTest
Tests the OG role cache context.

Namespace

Drupal\Tests\og\Kernel\Cache\Context

Code

public function testMemberships(array $group_memberships, array $expected_identical_role_groups) : void {

  // Create a node group type.
  NodeType::create([
    'name' => $this
      ->randomString(),
    'type' => 'group',
  ])
    ->save();
  $this->groupTypeManager
    ->addGroup('node', 'group');

  // The Entity Test entity doesn't have 'real' bundles, so we don't need to
  // create one, we can just add the group to the fake bundle.
  $this->groupTypeManager
    ->addGroup('entity_test', 'group');

  // Create the 'moderator' role for both group types. This is used in the
  // test as a custom role in addition to the default roles 'member',
  // 'administrator', etc.
  foreach ([
    'entity_test',
    'node',
  ] as $entity_type_id) {

    /** @var \Drupal\og\OgRoleInterface $role */
    $role = OgRole::create();
    $role
      ->setGroupType($entity_type_id)
      ->setGroupBundle('group')
      ->setName('moderator')
      ->save();
  }

  // Create the users and memberships as required by the test.
  $users = [];
  $groups = [];
  foreach ($group_memberships as $user_id => $group_entity_type_ids) {
    $users[$user_id] = $this
      ->createUser();
    foreach ($group_entity_type_ids as $group_entity_type_id => $group_ids) {
      foreach ($group_ids as $group_id => $roles) {

        // Create the group.
        if (empty($groups[$group_entity_type_id][$group_id])) {
          $groups[$group_entity_type_id][$group_id] = $this
            ->createGroup($group_entity_type_id);
        }
        $membership = OgMembership::create()
          ->setOwner($users[$user_id])
          ->setGroup($groups[$group_entity_type_id][$group_id]);
        foreach ($roles as $role_name) {
          $membership
            ->addRole(OgRole::getRole($group_entity_type_id, 'group', $role_name));
        }
        $membership
          ->save();
      }
    }
  }

  // Calculate the cache context keys for every user.
  $cache_context_ids = [];
  foreach ($users as $user_id => $user) {
    $cache_context_ids[$user_id] = $this
      ->getContextResult($user);
  }

  // Loop over the expected results and check that all users that have
  // identical roles have the same cache context key.
  foreach ($expected_identical_role_groups as $expected_identical_role_group) {

    // Check that the cache context keys for all users in the group are
    // identical.
    $cache_context_ids_subset = array_intersect_key($cache_context_ids, array_flip($expected_identical_role_group));
    $this
      ->assertTrue(count(array_unique($cache_context_ids_subset)) === 1);

    // Also check that the cache context keys for the other users are
    // different than the ones from our test group.
    $cache_context_id_from_test_group = reset($cache_context_ids_subset);
    $cache_context_ids_from_other_users = array_diff_key($cache_context_ids, array_flip($expected_identical_role_group));
    $this
      ->assertFalse(in_array($cache_context_id_from_test_group, $cache_context_ids_from_other_users));
  }
}