function DependentAccessTest::testMergeNonGroup

Same name in other branches
  1. 9 core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testMergeNonGroup()
  2. 8.9.x core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testMergeNonGroup()
  3. 10 core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest::testMergeNonGroup()

Tests merging a new dependency with existing non-group access dependency.

@dataProvider providerTestSetFirst

File

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

Class

DependentAccessTest
@coversDefaultClass \Drupal\block_content\Access\RefinableDependentAccessTrait

Namespace

Drupal\Tests\block_content\Unit\Access

Code

public function testMergeNonGroup($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());
    $testRefinable->addAccessDependency($this->neutral);
    
    /** @var \Drupal\block_content\Access\AccessGroupAnd $dependency */
    $dependency = $testRefinable->getAccessDependency();
    // Ensure the new dependency create a new AND group when merged.
    $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());
}

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