You are here

protected function PermissionsHashGeneratorTest::setUp 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::setUp()
  2. 10 core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest::setUp()

Overrides UnitTestCase::setUp

File

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

Class

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

Namespace

Drupal\Tests\Core\Session

Code

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()
    ->setMethods([
    '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()
    ->setMethods([
    '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()
    ->setMethods([
    '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()
    ->setMethods([
    '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()
    ->setMethods([
    '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);
}