Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest
  2. 9 core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest

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

Hierarchy

Expanded class hierarchy of PermissionsHashGeneratorTest

File

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

Namespace

Drupal\Tests\Core\Session
View source
class PermissionsHashGeneratorTest extends UnitTestCase {

  /**
   * The mocked user 1 account.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account1;

  /**
   * The mocked user 2 account.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account2;

  /**
   * The mocked cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $staticCache;

  /**
   * The mocked access policy processor.
   *
   * @var \Drupal\Core\Session\AccessPolicyProcessorInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $processor;

  /**
   * The permission hash class being tested.
   *
   * @var \Drupal\Core\Session\PermissionsHashGeneratorInterface
   */
  protected $permissionsHash;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    new Settings([
      'hash_salt' => 'test',
    ]);
    $this->account1 = $this
      ->prophesize(AccountInterface::class);
    $this->account1
      ->id()
      ->willReturn(1);
    $this->account1 = $this->account1
      ->reveal();
    $this->account2 = $this
      ->prophesize(AccountInterface::class);
    $this->account2
      ->id()
      ->willReturn(2);
    $this->account2 = $this->account2
      ->reveal();
    $private_key = $this
      ->prophesize(PrivateKey::class);
    $private_key
      ->get()
      ->willReturn(Crypt::randomBytesBase64(55));
    $this->staticCache = $this
      ->prophesize(CacheBackendInterface::class);
    $this->staticCache
      ->get(Argument::any())
      ->willReturn(FALSE);
    $this->staticCache
      ->set(Argument::cetera())
      ->shouldBeCalled();
    $this->processor = $this
      ->prophesize(AccessPolicyProcessorInterface::class);
    $this->permissionsHash = new PermissionsHashGenerator($private_key
      ->reveal(), $this->staticCache
      ->reveal(), $this->processor
      ->reveal());
  }

  /**
   * Tests the generate method for regular accounts.
   *
   * @covers ::generate
   */
  public function testGenerateRegular() {
    $permissions = new CalculatedPermissions((new RefinableCalculatedPermissions())
      ->addItem(new CalculatedPermissionsItem([
      'permission foo',
      'permission bar',
    ])));
    $this->processor
      ->processAccessPolicies($this->account1)
      ->willReturn($permissions);
    $this->processor
      ->processAccessPolicies($this->account2)
      ->willReturn($permissions);

    // Check that two accounts with the same permissions generate the same hash.
    $hash_1 = $this->permissionsHash
      ->generate($this->account1);
    $hash_2 = $this->permissionsHash
      ->generate($this->account2);
    $this
      ->assertSame($hash_1, $hash_2, 'Different users with the same permissions generate the same permissions hash.');
  }

  /**
   * Tests the generate method for admin users.
   *
   * @covers ::generate
   */
  public function testGenerateAdmin() {
    $permissions = new CalculatedPermissions((new RefinableCalculatedPermissions())
      ->addItem(new CalculatedPermissionsItem([], TRUE)));
    $this->processor
      ->processAccessPolicies($this->account1)
      ->willReturn($permissions);
    $this->processor
      ->processAccessPolicies($this->account2)
      ->willReturn($permissions);

    // Check that two accounts with the same permissions generate the same hash.
    $hash_1 = $this->permissionsHash
      ->generate($this->account1);
    $hash_2 = $this->permissionsHash
      ->generate($this->account2);
    $this
      ->assertSame($hash_1, $hash_2, 'Different admins generate the same permissions hash.');

    // Check that the generated hash is simply 'is-admin'.
    $this
      ->assertSame('is-admin', $hash_1, 'Admins generate the string "is-admin" as their permissions hash.');
  }

  /**
   * Tests the generate method with no access policies.
   *
   * @covers ::generate
   */
  public function testGenerateNoAccessPolicies() {
    $permissions = new CalculatedPermissions(new RefinableCalculatedPermissions());
    $this->processor
      ->processAccessPolicies($this->account1)
      ->willReturn($permissions);
    $this->processor
      ->processAccessPolicies($this->account2)
      ->willReturn($permissions);

    // Check that two accounts with the same permissions generate the same hash.
    $hash_1 = $this->permissionsHash
      ->generate($this->account1);
    $hash_2 = $this->permissionsHash
      ->generate($this->account2);
    $this
      ->assertSame($hash_1, $hash_2, 'Different accounts generate the same permissions hash when there are no policies.');

    // Check that the generated hash is simply 'no-access-policies'.
    $this
      ->assertSame('no-access-policies', $hash_1, 'Accounts generate the string "is-admin" as their permissions hash when no policies are defined.');
  }

  /**
   * Tests the generate method's caching.
   *
   * @covers ::generate
   */
  public function testGenerateCache() {
    $permissions = new CalculatedPermissions(new RefinableCalculatedPermissions());
    $this->processor
      ->processAccessPolicies($this->account1)
      ->willReturn($permissions);
    $this->processor
      ->processAccessPolicies($this->account2)
      ->willReturn($permissions);

    // Test that set is called with the right cache ID.
    $this->staticCache
      ->set('permissions_hash_1', 'no-access-policies', Cache::PERMANENT, [])
      ->shouldBeCalledOnce();
    $this->staticCache
      ->set('permissions_hash_2', 'no-access-policies', Cache::PERMANENT, [])
      ->shouldBeCalledOnce();
    $this->permissionsHash
      ->generate($this->account1);
    $this->permissionsHash
      ->generate($this->account2);

    // Verify that ::set() isn't called more when ::get() returns something.
    $cache_return = new \stdClass();
    $cache_return->data = 'no-access-policies';
    $this->staticCache
      ->get('permissions_hash_1')
      ->willReturn($cache_return);
    $this->staticCache
      ->get('permissions_hash_2')
      ->willReturn($cache_return);
    $this->permissionsHash
      ->generate($this->account1);
    $this->permissionsHash
      ->generate($this->account2);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PermissionsHashGeneratorTest::$account1 protected property The mocked user 1 account.
PermissionsHashGeneratorTest::$account2 protected property The mocked user 2 account.
PermissionsHashGeneratorTest::$permissionsHash protected property The permission hash class being tested.
PermissionsHashGeneratorTest::$processor protected property The mocked access policy processor.
PermissionsHashGeneratorTest::$staticCache protected property The mocked cache backend.
PermissionsHashGeneratorTest::setUp protected function Overrides UnitTestCase::setUp
PermissionsHashGeneratorTest::testGenerateAdmin public function Tests the generate method for admin users.
PermissionsHashGeneratorTest::testGenerateCache public function Tests the generate method's caching.
PermissionsHashGeneratorTest::testGenerateNoAccessPolicies public function Tests the generate method with no access policies.
PermissionsHashGeneratorTest::testGenerateRegular public function Tests the generate method for regular accounts.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function