You are here

public function PermissionsHashGeneratorTest::testGenerate in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest::testGenerate()
  2. 10 core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest::testGenerate()

@covers ::generate

File

core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php, line 152

Class

PermissionsHashGeneratorTest
@coversDefaultClass \Drupal\Core\Session\PermissionsHashGenerator @group Session

Namespace

Drupal\Tests\Core\Session

Code

public function testGenerate() {

  // Ensure that the super user (user 1) always gets the same hash.
  $super_user_hash = $this->permissionsHash
    ->generate($this->account1);

  // Ensure that two user accounts with the same roles generate the same hash.
  $hash_2 = $this->permissionsHash
    ->generate($this->account2);
  $hash_3 = $this->permissionsHash
    ->generate($this->account3);
  $this
    ->assertSame($hash_2, $hash_3, 'Different users with the same roles generate the same permissions hash.');
  $this
    ->assertNotSame($hash_2, $super_user_hash, 'User 1 has a different hash despite having the same roles');

  // Compare with hash for user account 1 with an additional role.
  $updated_hash_2 = $this->permissionsHash
    ->generate($this->account2Updated);
  $this
    ->assertNotSame($hash_2, $updated_hash_2, 'Same user with updated roles generates different permissions hash.');
}