function DependentAccessTest::testSetAccessDependency

Same name 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 \Drupal\block_content\Access\RefinableDependentAccessTrait

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.