@coversDefaultClass \Drupal\Core\Session\SuperUserAccessPolicy @group Session

Hierarchy

Expanded class hierarchy of SuperUserAccessPolicyTest

File

core/tests/Drupal/Tests/Core/Session/SuperUserAccessPolicyTest.php, line 22

Namespace

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

  /**
   * The access policy to test.
   *
   * @var \Drupal\Core\Session\SuperUserAccessPolicy
   */
  protected $accessPolicy;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->accessPolicy = new SuperUserAccessPolicy();
    $cache_context_manager = $this
      ->prophesize(CacheContextsManager::class);
    $cache_context_manager
      ->assertValidTokens(Argument::any())
      ->willReturn(TRUE);
    $container = $this
      ->prophesize(ContainerInterface::class);
    $container
      ->get('cache_contexts_manager')
      ->willReturn($cache_context_manager
      ->reveal());
    \Drupal::setContainer($container
      ->reveal());
  }

  /**
   * @covers ::applies
   */
  public function testApplies() : void {
    $this
      ->assertTrue($this->accessPolicy
      ->applies(AccessPolicyInterface::SCOPE_DRUPAL));
    $this
      ->assertFalse($this->accessPolicy
      ->applies('another scope'));
    $this
      ->assertFalse($this->accessPolicy
      ->applies($this
      ->randomString()));
  }

  /**
   * Tests the calculatePermissions method.
   *
   * @param int $uid
   *   The UID for the account the policy checks.
   * @param bool $expect_admin_rights
   *   Whether to expect admin rights to be granted.
   *
   * @covers ::calculatePermissions
   * @dataProvider calculatePermissionsProvider
   */
  public function testCalculatePermissions(int $uid, bool $expect_admin_rights) : void {
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->id()
      ->willReturn($uid);
    $calculated_permissions = $this->accessPolicy
      ->calculatePermissions($account
      ->reveal(), AccessPolicyInterface::SCOPE_DRUPAL);
    if ($expect_admin_rights) {
      $this
        ->assertCount(1, $calculated_permissions
        ->getItems(), 'Only one calculated permissions item was added.');
      $item = $calculated_permissions
        ->getItem();
      $this
        ->assertSame([], $item
        ->getPermissions());
      $this
        ->assertTrue($item
        ->isAdmin());
    }
    $this
      ->assertSame([], $calculated_permissions
      ->getCacheTags());
    $this
      ->assertSame([
      'user.is_super_user',
    ], $calculated_permissions
      ->getCacheContexts());
    $this
      ->assertSame(Cache::PERMANENT, $calculated_permissions
      ->getCacheMaxAge());
  }

  /**
   * Data provider for testCalculatePermissions.
   *
   * @return array
   *   A list of test scenarios.
   */
  public function calculatePermissionsProvider() : array {
    $cases['is-super-user'] = [
      1,
      TRUE,
    ];
    $cases['is-normal-user'] = [
      2,
      FALSE,
    ];
    return $cases;
  }

  /**
   * Tests the alterPermissions method.
   *
   * @param int $uid
   *   The UID for the account the policy checks.
   *
   * @covers ::alterPermissions
   * @dataProvider alterPermissionsProvider
   */
  public function testAlterPermissions(int $uid) : void {
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->id()
      ->willReturn($uid);
    $calculated_permissions = new RefinableCalculatedPermissions();
    $calculated_permissions
      ->addItem(new CalculatedPermissionsItem([
      'foo',
    ]));
    $calculated_permissions
      ->addCacheTags([
      'bar',
    ]);
    $calculated_permissions
      ->addCacheContexts([
      'baz',
    ]);
    $this->accessPolicy
      ->alterPermissions($account
      ->reveal(), AccessPolicyInterface::SCOPE_DRUPAL, $calculated_permissions);
    $this
      ->assertSame([
      'foo',
    ], $calculated_permissions
      ->getItem()
      ->getPermissions());
    $this
      ->assertSame([
      'bar',
    ], $calculated_permissions
      ->getCacheTags());
    $this
      ->assertSame([
      'baz',
    ], $calculated_permissions
      ->getCacheContexts());
  }

  /**
   * Data provider for testAlterPermissions.
   *
   * @return array
   *   A list of test scenarios.
   */
  public function alterPermissionsProvider() : array {
    $cases['is-super-user'] = [
      1,
    ];
    $cases['is-normal-user'] = [
      2,
    ];
    return $cases;
  }

  /**
   * Tests the getPersistentCacheContexts method.
   *
   * @covers ::getPersistentCacheContexts
   */
  public function testGetPersistentCacheContexts() : void {
    $this
      ->assertSame([
      'user.is_super_user',
    ], $this->accessPolicy
      ->getPersistentCacheContexts(AccessPolicyInterface::SCOPE_DRUPAL));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SuperUserAccessPolicyTest::$accessPolicy protected property The access policy to test.
SuperUserAccessPolicyTest::alterPermissionsProvider public function Data provider for testAlterPermissions.
SuperUserAccessPolicyTest::calculatePermissionsProvider public function Data provider for testCalculatePermissions.
SuperUserAccessPolicyTest::setUp protected function Overrides UnitTestCase::setUp
SuperUserAccessPolicyTest::testAlterPermissions public function Tests the alterPermissions method.
SuperUserAccessPolicyTest::testApplies public function @covers ::applies
SuperUserAccessPolicyTest::testCalculatePermissions public function Tests the calculatePermissions method.
SuperUserAccessPolicyTest::testGetPersistentCacheContexts public function Tests the getPersistentCacheContexts method.
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