You are here

public function DependentAccessTest::testMergeGroup in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testMergeGroup()
  2. 10 core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testMergeGroup()

Tests merging a new dependency with an existing access group dependency.

@dataProvider providerTestSetFirst

File

core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php, line 110

Class

DependentAccessTest
@coversDefaultClass \Drupal\block_content\Access\RefinableDependentAccessTrait

Namespace

Drupal\Tests\block_content\Unit\Access

Code

public function testMergeGroup($use_set_first) {
  $andGroup = new AccessGroupAnd();
  $andGroup
    ->addDependency($this->forbidden);
  $testRefinable = new RefinableDependentAccessTraitTestClass();
  if ($use_set_first) {
    $testRefinable
      ->setAccessDependency($andGroup);
  }
  else {
    $testRefinable
      ->addAccessDependency($andGroup);
  }
  $testRefinable
    ->addAccessDependency($this->neutral);

  /** @var \Drupal\block_content\Access\AccessGroupAnd $dependency */
  $dependency = $testRefinable
    ->getAccessDependency();

  // Ensure the new dependency is merged with the existing group.
  $this
    ->assertInstanceOf(AccessGroupAnd::class, $dependency);
  $dependencies = $dependency
    ->getDependencies();
  $accessResultForbidden = $dependencies[0]
    ->access('view', $this->account, TRUE);
  $this
    ->assertTrue($accessResultForbidden
    ->isForbidden());
  $this
    ->assertEquals('Because I said so', $accessResultForbidden
    ->getReason());
  $accessResultNeutral = $dependencies[1]
    ->access('view', $this->account, TRUE);
  $this
    ->assertTrue($accessResultNeutral
    ->isNeutral());
  $this
    ->assertEquals('I have no opinion', $accessResultNeutral
    ->getReason());
}