You are here

public function ChainGroupPermissionCalculatorTest::testCalculateAnonymousPermissions in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Kernel/ChainGroupPermissionCalculatorTest.php \Drupal\Tests\group\Kernel\ChainGroupPermissionCalculatorTest::testCalculateAnonymousPermissions()

Tests the calculation of the anonymous permissions.

@covers ::calculateAnonymousPermissions

File

tests/src/Kernel/ChainGroupPermissionCalculatorTest.php, line 53

Class

ChainGroupPermissionCalculatorTest
Tests the calculation of group permissions.

Namespace

Drupal\Tests\group\Kernel

Code

public function testCalculateAnonymousPermissions() {

  // @todo Use a proper set-up instead of the one from GroupKernelTestBase?
  $permissions = [
    'default' => [],
    'other' => [],
  ];
  $cache_tags = [
    'config:group.role.default-anonymous',
    'config:group.role.other-anonymous',
    'config:group_type_list',
    'group_permissions',
  ];
  sort($cache_tags);
  $calculated_permissions = $this->permissionCalculator
    ->calculateAnonymousPermissions();
  $converted = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions);
  $this
    ->assertEqualsCanonicalizing($permissions, $converted, 'Anonymous permissions are returned per group type.');
  $this
    ->assertSame([], $calculated_permissions
    ->getCacheContexts(), 'Anonymous permissions have the right cache contexts.');
  $this
    ->assertSame(-1, $calculated_permissions
    ->getCacheMaxAge(), 'Anonymous permissions have the right max cache age.');
  $this
    ->assertSame($cache_tags, $calculated_permissions
    ->getCacheTags(), 'Anonymous permissions have the right cache tags.');
  $group_role = $this->entityTypeManager
    ->getStorage('group_role')
    ->load('default-anonymous');
  $group_role
    ->grantPermission('view group')
    ->save();
  $permissions['default'][] = 'view group';
  $calculated_permissions = $this->permissionCalculator
    ->calculateAnonymousPermissions();
  $converted = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions);
  $this
    ->assertEqualsCanonicalizing($permissions, $converted, 'Updated anonymous permissions are returned per group type.');
  $this
    ->assertSame([], $calculated_permissions
    ->getCacheContexts(), 'Updated anonymous permissions have the right cache contexts.');
  $this
    ->assertSame(-1, $calculated_permissions
    ->getCacheMaxAge(), 'Updated anonymous permissions have the right max cache age.');
  $this
    ->assertSame($cache_tags, $calculated_permissions
    ->getCacheTags(), 'Updated anonymous permissions have the right cache tags.');
  $this
    ->createGroupType([
    'id' => 'test',
  ]);
  $permissions['test'] = [];
  $cache_tags[] = 'config:group.role.test-anonymous';
  sort($cache_tags);
  $calculated_permissions = $this->permissionCalculator
    ->calculateAnonymousPermissions();
  $converted = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions);
  $this
    ->assertEqualsCanonicalizing($permissions, $converted, 'Anonymous permissions are updated after introducing a new group type.');
  $this
    ->assertSame([], $calculated_permissions
    ->getCacheContexts(), 'Anonymous permissions have the right cache contexts after introducing a new group type.');
  $this
    ->assertSame(-1, $calculated_permissions
    ->getCacheMaxAge(), 'Anonymous permissions have the right max cache age after introducing a new group type.');
  $this
    ->assertSame($cache_tags, $calculated_permissions
    ->getCacheTags(), 'Anonymous permissions have the right cache tags after introducing a new group type.');
}