You are here

class PermissionsHashGeneratorTest in Drupal 10

Same name and namespace in other branches
  1. 8 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 14

Namespace

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

  /**
   * The mocked super user account.
   *
   * @var \Drupal\user\UserInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $account1;

  /**
   * A mocked account.
   *
   * @var \Drupal\user\UserInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $account2;

  /**
   * An "updated" mocked account.
   *
   * @var \Drupal\user\UserInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $account2Updated;

  /**
   * A different account.
   *
   * @var \Drupal\user\UserInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $account3;

  /**
   * The mocked private key service.
   *
   * @var \Drupal\Core\PrivateKey|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $privateKey;

  /**
   * The mocked cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $cache;

  /**
   * The mocked cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $staticCache;

  /**
   * 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',
    ]);

    // The mocked super user account, with the same roles as Account 2.
    $this->account1 = $this
      ->getMockBuilder('Drupal\\user\\Entity\\User')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getRoles',
      'id',
    ])
      ->getMock();
    $this->account1
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn(1);
    $this->account1
      ->expects($this
      ->never())
      ->method('getRoles');

    // Account 2: 'administrator' and 'authenticated' roles.
    $roles_1 = [
      'administrator',
      'authenticated',
    ];
    $this->account2 = $this
      ->getMockBuilder('Drupal\\user\\Entity\\User')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getRoles',
      'id',
    ])
      ->getMock();
    $this->account2
      ->expects($this
      ->any())
      ->method('getRoles')
      ->will($this
      ->returnValue($roles_1));
    $this->account2
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn(2);

    // Account 3: 'authenticated' and 'administrator' roles (different order).
    $roles_3 = [
      'authenticated',
      'administrator',
    ];
    $this->account3 = $this
      ->getMockBuilder('Drupal\\user\\Entity\\User')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getRoles',
      'id',
    ])
      ->getMock();
    $this->account3
      ->expects($this
      ->any())
      ->method('getRoles')
      ->will($this
      ->returnValue($roles_3));
    $this->account3
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn(3);

    // Updated account 2: now also 'editor' role.
    $roles_2_updated = [
      'editor',
      'administrator',
      'authenticated',
    ];
    $this->account2Updated = $this
      ->getMockBuilder('Drupal\\user\\Entity\\User')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getRoles',
      'id',
    ])
      ->getMock();
    $this->account2Updated
      ->expects($this
      ->any())
      ->method('getRoles')
      ->will($this
      ->returnValue($roles_2_updated));
    $this->account2Updated
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn(2);

    // Mocked private key + cache services.
    $random = Crypt::randomBytesBase64(55);
    $this->privateKey = $this
      ->getMockBuilder('Drupal\\Core\\PrivateKey')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'get',
    ])
      ->getMock();
    $this->privateKey
      ->expects($this
      ->any())
      ->method('get')
      ->will($this
      ->returnValue($random));
    $this->cache = $this
      ->getMockBuilder('Drupal\\Core\\Cache\\CacheBackendInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->staticCache = $this
      ->getMockBuilder('Drupal\\Core\\Cache\\CacheBackendInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->permissionsHash = new PermissionsHashGenerator($this->privateKey, $this->cache, $this->staticCache);
  }

  /**
   * @covers ::generate
   */
  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.');
  }

  /**
   * @covers ::generate
   */
  public function testGeneratePersistentCache() {

    // Set expectations for the mocked cache backend.
    $expected_cid = 'user_permissions_hash:administrator,authenticated';
    $mock_cache = new \stdClass();
    $mock_cache->data = 'test_hash_here';
    $this->staticCache
      ->expects($this
      ->once())
      ->method('get')
      ->with($expected_cid)
      ->will($this
      ->returnValue(FALSE));
    $this->staticCache
      ->expects($this
      ->once())
      ->method('set')
      ->with($expected_cid, $this
      ->isType('string'));
    $this->cache
      ->expects($this
      ->once())
      ->method('get')
      ->with($expected_cid)
      ->will($this
      ->returnValue($mock_cache));
    $this->cache
      ->expects($this
      ->never())
      ->method('set');
    $this->permissionsHash
      ->generate($this->account2);
  }

  /**
   * @covers ::generate
   */
  public function testGenerateStaticCache() {

    // Set expectations for the mocked cache backend.
    $expected_cid = 'user_permissions_hash:administrator,authenticated';
    $mock_cache = new \stdClass();
    $mock_cache->data = 'test_hash_here';
    $this->staticCache
      ->expects($this
      ->once())
      ->method('get')
      ->with($expected_cid)
      ->will($this
      ->returnValue($mock_cache));
    $this->staticCache
      ->expects($this
      ->never())
      ->method('set');
    $this->cache
      ->expects($this
      ->never())
      ->method('get');
    $this->cache
      ->expects($this
      ->never())
      ->method('set');
    $this->permissionsHash
      ->generate($this->account2);
  }

  /**
   * Tests the generate method with no cache returned.
   */
  public function testGenerateNoCache() {

    // Set expectations for the mocked cache backend.
    $expected_cid = 'user_permissions_hash:administrator,authenticated';
    $this->staticCache
      ->expects($this
      ->once())
      ->method('get')
      ->with($expected_cid)
      ->will($this
      ->returnValue(FALSE));
    $this->staticCache
      ->expects($this
      ->once())
      ->method('set')
      ->with($expected_cid, $this
      ->isType('string'));
    $this->cache
      ->expects($this
      ->once())
      ->method('get')
      ->with($expected_cid)
      ->will($this
      ->returnValue(FALSE));
    $this->cache
      ->expects($this
      ->once())
      ->method('set')
      ->with($expected_cid, $this
      ->isType('string'));
    $this->permissionsHash
      ->generate($this->account2);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PermissionsHashGeneratorTest::$account1 protected property The mocked super user account.
PermissionsHashGeneratorTest::$account2 protected property A mocked account.
PermissionsHashGeneratorTest::$account2Updated protected property An "updated" mocked account.
PermissionsHashGeneratorTest::$account3 protected property A different account.
PermissionsHashGeneratorTest::$cache protected property The mocked cache backend.
PermissionsHashGeneratorTest::$permissionsHash protected property The permission hash class being tested.
PermissionsHashGeneratorTest::$privateKey protected property The mocked private key service.
PermissionsHashGeneratorTest::$staticCache protected property The mocked cache backend.
PermissionsHashGeneratorTest::setUp protected function Overrides UnitTestCase::setUp
PermissionsHashGeneratorTest::testGenerate public function @covers ::generate
PermissionsHashGeneratorTest::testGenerateNoCache public function Tests the generate method with no cache returned.
PermissionsHashGeneratorTest::testGeneratePersistentCache public function @covers ::generate
PermissionsHashGeneratorTest::testGenerateStaticCache public function @covers ::generate
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.
UnitTestCase::$randomGenerator protected property The random generator.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function