function 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. 10 core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testSetAccessDependency()
  3. 11.x core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testSetAccessDependency()

Test that the previous dependency is replaced when using set.

@covers ::setAccessDependency

@dataProvider providerTestSetFirst

File

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

Class

DependentAccessTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21block_content%21src%21Access%21RefinableDependentAccessTrait.php/trait/RefinableDependentAccessTrait/8.9.x" title="Trait for \Drupal\block_content\Access\RefinableDependentAccessInterface." class="local">\Drupal\block_content\Access\RefinableDependentAccessTrait</a>

Namespace

Drupal\Tests\block_content\Unit\Access

Code

public function testSetAccessDependency($use_set_first) {
    $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.