3 namespace Drupal\Tests\entity\Kernel\QueryAccess;
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;
11 * Tests the uncacheable query access handler.
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.
17 * @coversDefaultClass \Drupal\entity\QueryAccess\UncacheableQueryAccessHandler
20 class UncacheableQueryAccessHandlerTest extends EntityKernelTestBase {
23 * The query access handler.
25 * @var \Drupal\entity\QueryAccess\UncacheableQueryAccessHandler
32 public static $modules = [
40 protected function setUp() {
43 $this->installEntitySchema('entity_test_enhanced_with_owner');
45 // Create uid: 1 here so that it's skipped in test cases.
46 $admin_user = $this->createUser();
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);
54 * @covers ::getConditions
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());
67 * @covers ::getConditions
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());
80 * @covers ::getConditions
82 public function testView() {
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'),
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());
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'),
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());
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',
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'])
121 new Condition('status', '1'),
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());
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'),
136 $this->assertEquals(2, $conditions->count());
137 $this->assertEquals($expected_conditions, $conditions->getConditions());
138 $this->assertEquals(['user'], $conditions->getCacheContexts());
139 $this->assertFalse($conditions->isAlwaysFalse());
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',
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']),
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());
161 * @covers ::getConditions
163 public function testUpdateDelete() {
164 foreach (['update', 'delete'] as $operation) {
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());
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()),
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());
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",
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']),
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());