View source
<?php
namespace Drupal\Tests\block_content\Unit\Access;
use Drupal\block_content\Access\AccessGroupAnd;
use Drupal\Core\Access\AccessResult;
use Drupal\block_content\Access\RefinableDependentAccessInterface;
use Drupal\block_content\Access\RefinableDependentAccessTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
class DependentAccessTest extends UnitTestCase {
use AccessibleTestingTrait;
protected $forbidden;
protected $neutral;
protected function setUp() : void {
parent::setUp();
$this->account = $this
->prophesize(AccountInterface::class)
->reveal();
$this->forbidden = $this
->createAccessibleDouble(AccessResult::forbidden('Because I said so'));
$this->neutral = $this
->createAccessibleDouble(AccessResult::neutral('I have no opinion'));
}
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());
$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());
}
public function testMergeNonGroup($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());
$testRefinable
->addAccessDependency($this->neutral);
$dependency = $testRefinable
->getAccessDependency();
$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());
}
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);
$dependency = $testRefinable
->getAccessDependency();
$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());
}
public function providerTestSetFirst() {
return [
[
TRUE,
],
[
FALSE,
],
];
}
}
class RefinableDependentAccessTraitTestClass implements RefinableDependentAccessInterface {
use RefinableDependentAccessTrait;
}