Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / tests / src / Kernel / QueryAccess / UncacheableQueryAccessHandlerTest.php
1 <?php
2
3 namespace Drupal\Tests\entity\Kernel\QueryAccess;
4
5 use Drupal\entity\QueryAccess\Condition;
6 use Drupal\entity\QueryAccess\ConditionGroup;
7 use Drupal\entity\QueryAccess\UncacheableQueryAccessHandler;
8 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
9
10 /**
11  * Tests the uncacheable query access handler.
12  *
13  * Uses the "entity_test_enhanced_with_owner" entity type, which has an owner.
14  * QueryAccessHandlerTest uses the "entity_test_enhanced" entity type, which
15  * has no owner. This ensures both sides (owner and no owner) are covered.
16  *
17  * @coversDefaultClass \Drupal\entity\QueryAccess\UncacheableQueryAccessHandler
18  * @group entity
19  */
20 class UncacheableQueryAccessHandlerTest extends EntityKernelTestBase {
21
22   /**
23    * The query access handler.
24    *
25    * @var \Drupal\entity\QueryAccess\UncacheableQueryAccessHandler
26    */
27   protected $handler;
28
29   /**
30    * {@inheritdoc}
31    */
32   public static $modules = [
33     'entity',
34     'entity_module_test',
35   ];
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     $this->installEntitySchema('entity_test_enhanced_with_owner');
44
45     // Create uid: 1 here so that it's skipped in test cases.
46     $admin_user = $this->createUser();
47
48     $entity_type_manager = $this->container->get('entity_type.manager');
49     $entity_type = $entity_type_manager->getDefinition('entity_test_enhanced_with_owner');
50     $this->handler = UncacheableQueryAccessHandler::createInstance($this->container, $entity_type);
51   }
52
53   /**
54    * @covers ::getConditions
55    */
56   public function testNoAccess() {
57     foreach (['view', 'update', 'delete'] as $operation) {
58       $user = $this->createUser([], ['access content']);
59       $conditions = $this->handler->getConditions($operation, $user);
60       $this->assertEquals(0, $conditions->count());
61       $this->assertEquals(['user.permissions'], $conditions->getCacheContexts());
62       $this->assertTrue($conditions->isAlwaysFalse());
63     }
64   }
65
66   /**
67    * @covers ::getConditions
68    */
69   public function testAdmin() {
70     foreach (['view', 'update', 'delete'] as $operation) {
71       $user = $this->createUser([], ['administer entity_test_enhanced_with_owner']);
72       $conditions = $this->handler->getConditions($operation, $user);
73       $this->assertEquals(0, $conditions->count());
74       $this->assertEquals(['user.permissions'], $conditions->getCacheContexts());
75       $this->assertFalse($conditions->isAlwaysFalse());
76     }
77   }
78
79   /**
80    * @covers ::getConditions
81    */
82   public function testView() {
83     // Any permission.
84     $user = $this->createUser([], ['view any entity_test_enhanced_with_owner']);
85     $conditions = $this->handler->getConditions('view', $user);
86     $expected_conditions = [
87       new Condition('status', '1'),
88     ];
89     $this->assertEquals(1, $conditions->count());
90     $this->assertEquals($expected_conditions, $conditions->getConditions());
91     $this->assertEquals(['user.permissions'], $conditions->getCacheContexts());
92     $this->assertFalse($conditions->isAlwaysFalse());
93
94     // Own permission.
95     $user = $this->createUser([], ['view own entity_test_enhanced_with_owner']);
96     $conditions = $this->handler->getConditions('view', $user);
97     $expected_conditions = [
98       new Condition('user_id', $user->id()),
99       new Condition('status', '1'),
100     ];
101     $this->assertEquals('AND', $conditions->getConjunction());
102     $this->assertEquals(2, $conditions->count());
103     $this->assertEquals($expected_conditions, $conditions->getConditions());
104     $this->assertEquals(['user', 'user.permissions'], $conditions->getCacheContexts());
105     $this->assertFalse($conditions->isAlwaysFalse());
106
107     // Any permission for the first bundle, own permission for the second.
108     $user = $this->createUser([], [
109       'view any first entity_test_enhanced_with_owner',
110       'view own second entity_test_enhanced_with_owner',
111     ]);
112     $conditions = $this->handler->getConditions('view', $user);
113     $expected_conditions = [
114       (new ConditionGroup('OR'))
115         ->addCacheContexts(['user', 'user.permissions'])
116         ->addCondition('type', ['first'])
117         ->addCondition((new ConditionGroup('AND'))
118           ->addCondition('user_id', $user->id())
119           ->addCondition('type', ['second'])
120         ),
121       new Condition('status', '1'),
122     ];
123     $this->assertEquals('AND', $conditions->getConjunction());
124     $this->assertEquals(2, $conditions->count());
125     $this->assertEquals($expected_conditions, $conditions->getConditions());
126     $this->assertEquals(['user', 'user.permissions'], $conditions->getCacheContexts());
127     $this->assertFalse($conditions->isAlwaysFalse());
128
129     // View own unpublished permission.
130     $user = $this->createUser([], ['view own unpublished entity_test_enhanced_with_owner']);
131     $conditions = $this->handler->buildConditions('view', $user);
132     $expected_conditions = [
133       new Condition('user_id', $user->id()),
134       new Condition('status', '0'),
135     ];
136     $this->assertEquals(2, $conditions->count());
137     $this->assertEquals($expected_conditions, $conditions->getConditions());
138     $this->assertEquals(['user'], $conditions->getCacheContexts());
139     $this->assertFalse($conditions->isAlwaysFalse());
140
141     // Both view any and view own unpublished permissions.
142     $user = $this->createUser([], [
143       'view any entity_test_enhanced_with_owner',
144       'view own unpublished entity_test_enhanced_with_owner',
145     ]);
146     $conditions = $this->handler->buildConditions('view', $user);
147     $expected_conditions = [
148       new Condition('status', '1'),
149       (new ConditionGroup('AND'))
150         ->addCondition('user_id', $user->id())
151         ->addCondition('status', '0')
152         ->addCacheContexts(['user']),
153     ];
154     $this->assertEquals(2, $conditions->count());
155     $this->assertEquals($expected_conditions, $conditions->getConditions());
156     $this->assertEquals(['user', 'user.permissions'], $conditions->getCacheContexts());
157     $this->assertFalse($conditions->isAlwaysFalse());
158   }
159
160   /**
161    * @covers ::getConditions
162    */
163   public function testUpdateDelete() {
164     foreach (['update', 'delete'] as $operation) {
165       // Any permission.
166       $user = $this->createUser([], ["$operation any entity_test_enhanced_with_owner"]);
167       $conditions = $this->handler->getConditions($operation, $user);
168       $this->assertEquals(0, $conditions->count());
169       $this->assertEquals(['user.permissions'], $conditions->getCacheContexts());
170       $this->assertFalse($conditions->isAlwaysFalse());
171
172       // Own permission.
173       $user = $this->createUser([], ["$operation own entity_test_enhanced_with_owner"]);
174       $conditions = $this->handler->getConditions($operation, $user);
175       $expected_conditions = [
176         new Condition('user_id', $user->id()),
177       ];
178       $this->assertEquals(1, $conditions->count());
179       $this->assertEquals($expected_conditions, $conditions->getConditions());
180       $this->assertEquals(['user', 'user.permissions'], $conditions->getCacheContexts());
181       $this->assertFalse($conditions->isAlwaysFalse());
182
183       // Any permission for the first bundle, own permission for the second.
184       $user = $this->createUser([], [
185         "$operation any first entity_test_enhanced_with_owner",
186         "$operation own second entity_test_enhanced_with_owner",
187       ]);
188       $conditions = $this->handler->getConditions($operation, $user);
189       $expected_conditions = [
190         new Condition('type', ['first']),
191         (new ConditionGroup('AND'))
192           ->addCondition('user_id', $user->id())
193           ->addCondition('type', ['second']),
194       ];
195       $this->assertEquals('OR', $conditions->getConjunction());
196       $this->assertEquals(2, $conditions->count());
197       $this->assertEquals($expected_conditions, $conditions->getConditions());
198       $this->assertEquals(['user', 'user.permissions'], $conditions->getCacheContexts());
199       $this->assertFalse($conditions->isAlwaysFalse());
200     }
201   }
202
203 }