Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / block_content / tests / src / Unit / Access / DependentAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Unit\Access;
4
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;
11
12 /**
13  * @coversDefaultClass  \Drupal\block_content\Access\RefinableDependentAccessTrait
14  *
15  * @group block_content
16  */
17 class DependentAccessTest extends UnitTestCase {
18   use AccessibleTestingTrait;
19
20   /**
21    * An accessible object that results in forbidden access result.
22    *
23    * @var \Drupal\Core\Access\AccessibleInterface
24    */
25   protected $forbidden;
26
27   /**
28    * An accessible object that results in neutral access result.
29    *
30    * @var \Drupal\Core\Access\AccessibleInterface
31    */
32   protected $neutral;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::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'));
42   }
43
44   /**
45    * Test that the previous dependency is replaced when using set.
46    *
47    * @covers ::setAccessDependency
48    *
49    * @dataProvider providerTestSetFirst
50    */
51   public function testSetAccessDependency($use_set_first) {
52     $testRefinable = new RefinableDependentAccessTraitTestClass();
53
54     if ($use_set_first) {
55       $testRefinable->setAccessDependency($this->forbidden);
56     }
57     else {
58       $testRefinable->addAccessDependency($this->forbidden);
59     }
60     $accessResult = $testRefinable->getAccessDependency()->access('view', $this->account, TRUE);
61     $this->assertTrue($accessResult->isForbidden());
62     $this->assertEquals('Because I said so', $accessResult->getReason());
63
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());
71   }
72
73   /**
74    * Tests merging a new dependency with existing non-group access dependency.
75    *
76    * @dataProvider providerTestSetFirst
77    */
78   public function testMergeNonGroup($use_set_first) {
79     $testRefinable = new RefinableDependentAccessTraitTestClass();
80     if ($use_set_first) {
81       $testRefinable->setAccessDependency($this->forbidden);
82     }
83     else {
84       $testRefinable->addAccessDependency($this->forbidden);
85     }
86
87     $accessResult = $testRefinable->getAccessDependency()->access('view', $this->account, TRUE);
88     $this->assertTrue($accessResult->isForbidden());
89     $this->assertEquals('Because I said so', $accessResult->getReason());
90
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());
103   }
104
105   /**
106    * Tests merging a new dependency with an existing access group dependency.
107    *
108    * @dataProvider providerTestSetFirst
109    */
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);
116     }
117     else {
118       $testRefinable->addAccessDependency($andGroup);
119     }
120
121     $testRefinable->addAccessDependency($this->neutral);
122     /** @var \Drupal\block_content\Access\AccessGroupAnd $dependency */
123     $dependency = $testRefinable->getAccessDependency();
124
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());
134   }
135
136   /**
137    * Dataprovider for all test methods.
138    *
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.
142    */
143   public function providerTestSetFirst() {
144     return [
145       [TRUE],
146       [FALSE],
147     ];
148   }
149
150 }
151
152 /**
153  * Test class that implements RefinableDependentAccessInterface.
154  */
155 class RefinableDependentAccessTraitTestClass implements RefinableDependentAccessInterface {
156
157   use RefinableDependentAccessTrait;
158
159 }