function DependentAccessTest::testSetAccessDependency

Same name in this branch
  1. 11.x core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testSetAccessDependency()
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::testSetAccessDependency()
  2. 8.9.x core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testSetAccessDependency()
  3. 10 core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testSetAccessDependency()

Tests that the previous dependency is replaced when using set.

@covers ::setAccessDependency

@dataProvider providerTestSetFirst

File

core/tests/Drupal/Tests/Core/Access/DependentAccessTest.php, line 53

Class

DependentAccessTest
@coversDefaultClass \Drupal\Core\Access\RefinableDependentAccessTrait[[api-linebreak]]

Namespace

Drupal\Tests\Core\Access

Code

public function testSetAccessDependency($use_set_first) : void {
  $testRefinable = new RefinableDependentAccessTraitTestClass();
  if ($use_set_first) {
    $testRefinable->setAccessDependency($this->forbidden);
  }
  else {
    $testRefinable->addAccessDependency($this->forbidden);
  }
  $accessResult = $testRefinable->getAccessDependency()
    ->access('view', $this->account, TRUE);
  $this->assertTrue($accessResult->isForbidden());
  $this->assertEquals('Because I said so', $accessResult->getReason());
  // Calling setAccessDependency() replaces the existing dependency.
  $testRefinable->setAccessDependency($this->neutral);
  $dependency = $testRefinable->getAccessDependency();
  $this->assertNotInstanceOf(AccessGroupAnd::class, $dependency);
  $accessResult = $dependency->access('view', $this->account, TRUE);
  $this->assertTrue($accessResult->isNeutral());
  $this->assertEquals('I have no opinion', $accessResult->getReason());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.