3 namespace Drupal\Tests\block_content\Unit\Access;
5 use Drupal\block_content\Access\AccessGroupAnd;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\block_content\Access\RefinableDependentAccessInterface;
8 use Drupal\block_content\Access\RefinableDependentAccessTrait;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\Tests\UnitTestCase;
13 * @coversDefaultClass \Drupal\block_content\Access\RefinableDependentAccessTrait
15 * @group block_content
17 class DependentAccessTest extends UnitTestCase {
18 use AccessibleTestingTrait;
21 * An accessible object that results in forbidden access result.
23 * @var \Drupal\Core\Access\AccessibleInterface
28 * An accessible object that results in neutral access result.
30 * @var \Drupal\Core\Access\AccessibleInterface
37 protected function setUp() {
39 $this->account = $this->prophesize(AccountInterface::class)->reveal();
40 $this->forbidden = $this->createAccessibleDouble(AccessResult::forbidden('Because I said so'));
41 $this->neutral = $this->createAccessibleDouble(AccessResult::neutral('I have no opinion'));
45 * Test that the previous dependency is replaced when using set.
47 * @covers ::setAccessDependency
49 * @dataProvider providerTestSetFirst
51 public function testSetAccessDependency($use_set_first) {
52 $testRefinable = new RefinableDependentAccessTraitTestClass();
55 $testRefinable->setAccessDependency($this->forbidden);
58 $testRefinable->addAccessDependency($this->forbidden);
60 $accessResult = $testRefinable->getAccessDependency()->access('view', $this->account, TRUE);
61 $this->assertTrue($accessResult->isForbidden());
62 $this->assertEquals('Because I said so', $accessResult->getReason());
64 // Calling setAccessDependency() replaces the existing dependency.
65 $testRefinable->setAccessDependency($this->neutral);
66 $dependency = $testRefinable->getAccessDependency();
67 $this->assertFalse($dependency instanceof AccessGroupAnd);
68 $accessResult = $dependency->access('view', $this->account, TRUE);
69 $this->assertTrue($accessResult->isNeutral());
70 $this->assertEquals('I have no opinion', $accessResult->getReason());
74 * Tests merging a new dependency with existing non-group access dependency.
76 * @dataProvider providerTestSetFirst
78 public function testMergeNonGroup($use_set_first) {
79 $testRefinable = new RefinableDependentAccessTraitTestClass();
81 $testRefinable->setAccessDependency($this->forbidden);
84 $testRefinable->addAccessDependency($this->forbidden);
87 $accessResult = $testRefinable->getAccessDependency()->access('view', $this->account, TRUE);
88 $this->assertTrue($accessResult->isForbidden());
89 $this->assertEquals('Because I said so', $accessResult->getReason());
91 $testRefinable->addAccessDependency($this->neutral);
92 /** @var \Drupal\block_content\Access\AccessGroupAnd $dependency */
93 $dependency = $testRefinable->getAccessDependency();
94 // Ensure the new dependency create a new AND group when merged.
95 $this->assertTrue($dependency instanceof AccessGroupAnd);
96 $dependencies = $dependency->getDependencies();
97 $accessResultForbidden = $dependencies[0]->access('view', $this->account, TRUE);
98 $this->assertTrue($accessResultForbidden->isForbidden());
99 $this->assertEquals('Because I said so', $accessResultForbidden->getReason());
100 $accessResultNeutral = $dependencies[1]->access('view', $this->account, TRUE);
101 $this->assertTrue($accessResultNeutral->isNeutral());
102 $this->assertEquals('I have no opinion', $accessResultNeutral->getReason());
106 * Tests merging a new dependency with an existing access group dependency.
108 * @dataProvider providerTestSetFirst
110 public function testMergeGroup($use_set_first) {
111 $andGroup = new AccessGroupAnd();
112 $andGroup->addDependency($this->forbidden);
113 $testRefinable = new RefinableDependentAccessTraitTestClass();
114 if ($use_set_first) {
115 $testRefinable->setAccessDependency($andGroup);
118 $testRefinable->addAccessDependency($andGroup);
121 $testRefinable->addAccessDependency($this->neutral);
122 /** @var \Drupal\block_content\Access\AccessGroupAnd $dependency */
123 $dependency = $testRefinable->getAccessDependency();
125 // Ensure the new dependency is merged with the existing group.
126 $this->assertTrue($dependency instanceof AccessGroupAnd);
127 $dependencies = $dependency->getDependencies();
128 $accessResultForbidden = $dependencies[0]->access('view', $this->account, TRUE);
129 $this->assertTrue($accessResultForbidden->isForbidden());
130 $this->assertEquals('Because I said so', $accessResultForbidden->getReason());
131 $accessResultNeutral = $dependencies[1]->access('view', $this->account, TRUE);
132 $this->assertTrue($accessResultNeutral->isNeutral());
133 $this->assertEquals('I have no opinion', $accessResultNeutral->getReason());
137 * Dataprovider for all test methods.
139 * Provides test cases for calling setAccessDependency() or
140 * mergeAccessDependency() first. A call to either should behave the same on a
141 * new RefinableDependentAccessInterface object.
143 public function providerTestSetFirst() {
153 * Test class that implements RefinableDependentAccessInterface.
155 class RefinableDependentAccessTraitTestClass implements RefinableDependentAccessInterface {
157 use RefinableDependentAccessTrait;