You are here

public function GroupContentTest::testGetCacheTagsToInvalidate in Group 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Kernel/GroupContentTest.php \Drupal\Tests\group\Kernel\GroupContentTest::testGetCacheTagsToInvalidate()

Tests that custom list cache tags are properly invalidated.

@covers ::getListCacheTagsToInvalidate

File

tests/src/Kernel/GroupContentTest.php, line 91

Class

GroupContentTest
Tests for the GroupContent entity.

Namespace

Drupal\Tests\group\Kernel

Code

public function testGetCacheTagsToInvalidate() {
  $cache = \Drupal::cache();

  // Create a group type and enable adding users as content.
  $group_type = $this
    ->createGroupType();

  /** @var \Drupal\group\Entity\Storage\GroupContentTypeStorageInterface $storage */
  $storage = $this->entityTypeManager
    ->getStorage('group_content_type');
  $storage
    ->createFromPlugin($group_type, 'user_as_content')
    ->save();

  // Create a group and user to check the cache tags for.
  $test_group = $this
    ->createGroup([
    'type' => $group_type
      ->id(),
  ]);
  $test_group_id = $test_group
    ->id();
  $test_account = $this
    ->createUser();
  $test_account_id = $test_account
    ->id();

  // Create an extra group and account to test with.
  $extra_group = $this
    ->createGroup([
    'type' => $group_type
      ->id(),
  ]);
  $extra_account = $this
    ->createUser();
  $scenarios = [
    // Create a list for specific group, any entity, any plugin.
    'group_content' => [
      "group_content_list:group:{$test_group_id}",
    ],
    // Create a list for any group, specific entity, any plugin.
    'content_groups' => [
      "group_content_list:entity:{$test_account_id}",
    ],
    // Create a list for any group, any entity, specific plugin.
    'all_memberships' => [
      "group_content_list:plugin:group_membership",
    ],
    // Create a list for specific group, any entity, specific plugin.
    'group_memberships' => [
      "group_content_list:plugin:group_membership:group:{$test_group_id}",
    ],
    // Create a list for any group, specific entity, specific plugin.
    'user_memberships' => [
      "group_content_list:plugin:group_membership:entity:{$test_account_id}",
    ],
  ];
  foreach ($scenarios as $cid => $cache_tags) {
    $cache
      ->set($cid, 'foo', CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
  }

  // Add another user to another group and verify cache entries.
  $extra_group
    ->addContent($extra_account, 'user_as_content');
  $this
    ->assertNotFalse($cache
    ->get('group_content'), 'List for specific group, any entity, any plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('content_groups'), 'List for any group, specific entity, any plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('all_memberships'), 'List for any group, any entity, specific plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('group_memberships'), 'List for specific group, any entity, specific plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('user_memberships'), 'List for any group, specific entity, specific plugin found.');

  // Add another user as content to the group and verify cache entries.
  $test_group
    ->addContent($extra_account, 'user_as_content');
  $this
    ->assertFalse($cache
    ->get('group_content'), 'List for specific group, any entity, any plugin cleared.');
  $this
    ->assertNotFalse($cache
    ->get('content_groups'), 'List for any group, specific entity, any plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('all_memberships'), 'List for any group, any entity, specific plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('group_memberships'), 'List for specific group, any entity, specific plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('user_memberships'), 'List for any group, specific entity, specific plugin found.');

  // Add the user as content to another group and verify cache entries.
  $extra_group
    ->addContent($test_account, 'user_as_content');
  $this
    ->assertFalse($cache
    ->get('content_groups'), 'List for any group, specific entity, any plugin cleared.');
  $this
    ->assertNotFalse($cache
    ->get('all_memberships'), 'List for any group, any entity, specific plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('group_memberships'), 'List for specific group, any entity, specific plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('user_memberships'), 'List for any group, specific entity, specific plugin found.');

  // Add any user as a member to any group and verify cache entries.
  $extra_group
    ->addContent($extra_account, 'group_membership');
  $this
    ->assertFalse($cache
    ->get('all_memberships'), 'List for any group, any entity, specific plugin cleared.');
  $this
    ->assertNotFalse($cache
    ->get('group_memberships'), 'List for specific group, any entity, specific plugin found.');
  $this
    ->assertNotFalse($cache
    ->get('user_memberships'), 'List for any group, specific entity, specific plugin found.');

  // Add any user as a member to the group and verify cache entries.
  $test_group
    ->addContent($extra_account, 'group_membership');
  $this
    ->assertFalse($cache
    ->get('group_memberships'), 'List for specific group, any entity, specific plugin cleared.');
  $this
    ->assertNotFalse($cache
    ->get('user_memberships'), 'List for any group, specific entity, specific plugin found.');

  // Add the user as a member to any group and verify cache entries.
  $extra_group
    ->addContent($test_account, 'group_membership');
  $this
    ->assertFalse($cache
    ->get('user_memberships'), 'List for any group, specific entity, specific plugin cleared.');

  // Set the cache again and verify if we add the user to the group.
  foreach ($scenarios as $cid => $cache_tags) {
    $cache
      ->set($cid, 'foo', CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
  }
  $test_group
    ->addContent($test_account, 'group_membership');
  $this
    ->assertFalse($cache
    ->get('group_content'), 'List for specific group, any entity, any plugin cleared.');
  $this
    ->assertFalse($cache
    ->get('content_groups'), 'List for any group, specific entity, any plugin cleared.');
  $this
    ->assertFalse($cache
    ->get('all_memberships'), 'List for any group, any entity, specific plugin cleared.');
  $this
    ->assertFalse($cache
    ->get('group_memberships'), 'List for specific group, any entity, specific plugin cleared.');
  $this
    ->assertFalse($cache
    ->get('user_memberships'), 'List for any group, specific entity, specific plugin cleared.');
}