You are here

public function GroupPermissionHashGeneratorTest::testGenerateHash in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/GroupPermissionHashGeneratorTest.php \Drupal\Tests\group\Unit\GroupPermissionHashGeneratorTest::testGenerateHash()

Tests the generation of the account's hash.

@covers ::generateHash

File

tests/src/Unit/GroupPermissionHashGeneratorTest.php, line 77

Class

GroupPermissionHashGeneratorTest
Tests the group permission hash generator service.

Namespace

Drupal\Tests\group\Unit

Code

public function testGenerateHash() {
  $scope_gt = CalculatedGroupPermissionsItemInterface::SCOPE_GROUP_TYPE;
  $scope_g = CalculatedGroupPermissionsItemInterface::SCOPE_GROUP;
  $cid = 'group_permissions_hash_24101986';
  $calculated_permissions = new RefinableCalculatedGroupPermissions();
  $this->permissionCalculator
    ->calculatePermissions($this->account)
    ->willReturn($calculated_permissions);
  $sorted_permissions = [
    'alice' => [
      'bob',
    ],
    'foo' => [
      'bar',
      'baz',
    ],
    16 => [
      'sweet',
    ],
  ];
  $expected_hash = hash('sha256', 'SALT' . serialize($sorted_permissions));
  $this->static
    ->get($cid)
    ->willReturn(FALSE);
  $this->static
    ->set($cid, $expected_hash, Cache::PERMANENT, [])
    ->shouldBeCalledTimes(1);
  $calculated_permissions
    ->addItem(new CalculatedGroupPermissionsItem($scope_gt, 'foo', [
    'baz',
    'bar',
  ]))
    ->addItem(new CalculatedGroupPermissionsItem($scope_gt, 'alice', [
    'bob',
  ]))
    ->addItem(new CalculatedGroupPermissionsItem($scope_g, 16, [
    'sweet',
  ]));
  $this
    ->assertEquals($expected_hash, $this->hashGenerator
    ->generateHash($this->account), 'The hash was generated based on the sorted calculated permissions.');
  $sorted_permissions[100] = 'is-admin';
  $expected_hash = hash('sha256', 'SALT' . serialize($sorted_permissions));
  $this->static
    ->set($cid, $expected_hash, Cache::PERMANENT, [])
    ->shouldBeCalledTimes(1);
  $calculated_permissions
    ->addItem(new CalculatedGroupPermissionsItem($scope_g, 100, [
    'irrelevant',
  ], TRUE));
  $this
    ->assertEquals($expected_hash, $this->hashGenerator
    ->generateHash($this->account), 'The hash uses a simple flag instead of permissions for admin entries.');
  $cache = (object) [
    'data' => 'foobar',
  ];
  $this->static
    ->get($cid)
    ->willReturn($cache);
  $this->static
    ->set($cid, 'foobar', Cache::PERMANENT, [])
    ->shouldNotBeCalled();
  $this
    ->assertEquals('foobar', $this->hashGenerator
    ->generateHash($this->account), 'The hash was retrieved from the static cache.');
}