Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / tests / src / Kernel / QueryAccess / UncacheableQueryAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\entity\Kernel\QueryAccess;
4
5 use Drupal\entity_module_test\Entity\EnhancedEntityWithOwner;
6 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
7 use Drupal\views\Tests\ViewResultAssertionTrait;
8 use Drupal\views\Views;
9
10 /**
11  * Test uncacheable query access filtering for EntityQuery and Views.
12  *
13  * @group entity
14  *
15  * @see \Drupal\entity\QueryAccess\UncacheableQueryAccessHandler
16  * @see \Drupal\entity\QueryAccess\EntityQueryAlter
17  * @see \Drupal\entity\QueryAccess\ViewsQueryAlter
18  */
19 class UncacheableQueryAccessTest extends EntityKernelTestBase {
20
21   use ViewResultAssertionTrait;
22
23   /**
24    * The test entities.
25    *
26    * @var \Drupal\Core\Entity\ContentEntityInterface[]
27    */
28   protected $entities;
29
30   /**
31    * The entity_test_enhanced storage.
32    *
33    * @var \Drupal\Core\Entity\EntityStorageInterface
34    */
35   protected $storage;
36
37   /**
38    * {@inheritdoc}
39    */
40   public static $modules = [
41     'entity',
42     'entity_module_test',
43     'user',
44     'views',
45     'system',
46   ];
47
48   /**
49    * {@inheritdoc}
50    */
51   protected function setUp() {
52     parent::setUp();
53
54     $this->installEntitySchema('entity_test_enhanced_with_owner');
55     $this->installConfig(['entity_module_test']);
56
57     // Create uid: 1 here so that it's skipped in test cases.
58     $admin_user = $this->createUser();
59
60     $first_entity = EnhancedEntityWithOwner::create([
61       'type' => 'first',
62       'name' => 'First',
63       'status' => 1,
64     ]);
65     $first_entity->save();
66
67     $first_entity->set('name', 'First!');
68     $first_entity->set('status', 0);
69     $first_entity->setNewRevision(TRUE);
70     $first_entity->save();
71
72     $second_entity = EnhancedEntityWithOwner::create([
73       'type' => 'first',
74       'name' => 'Second',
75       'status' => 0,
76     ]);
77     $second_entity->save();
78
79     $second_entity->set('name', 'Second!');
80     $second_entity->set('status', 1);
81     $second_entity->setNewRevision(TRUE);
82     $second_entity->save();
83
84     $third_entity = EnhancedEntityWithOwner::create([
85       'type' => 'second',
86       'name' => 'Third',
87       'status' => 1,
88     ]);
89     $third_entity->save();
90
91     $third_entity->set('name', 'Third!');
92     $third_entity->setNewRevision(TRUE);
93     $third_entity->save();
94
95     $this->entities = [$first_entity, $second_entity, $third_entity];
96     $this->storage = \Drupal::entityTypeManager()->getStorage('entity_test_enhanced_with_owner');
97   }
98
99   /**
100    * Tests EntityQuery filtering.
101    */
102   public function testEntityQuery() {
103     // Admin permission, full access.
104     $admin_user = $this->createUser([], ['administer entity_test_enhanced_with_owner']);
105     \Drupal::currentUser()->setAccount($admin_user);
106
107     $result = $this->storage->getQuery()->sort('id')->execute();
108     $this->assertEquals([
109       $this->entities[0]->id(),
110       $this->entities[1]->id(),
111       $this->entities[2]->id(),
112     ], array_values($result));
113
114     // No view permissions, no access.
115     $user = $this->createUser([], ['access content']);
116     \Drupal::currentUser()->setAccount($user);
117
118     $result = $this->storage->getQuery()->execute();
119     $this->assertEmpty($result);
120
121     // View own (published-only).
122     $user = $this->createUser([], ['view own entity_test_enhanced_with_owner']);
123     \Drupal::currentUser()->setAccount($user);
124
125     $this->entities[0]->set('user_id', $user->id());
126     $this->entities[0]->save();
127     $this->entities[1]->set('user_id', $user->id());
128     $this->entities[1]->save();
129
130     $result = $this->storage->getQuery()->sort('id')->execute();
131     $this->assertEquals([
132       $this->entities[1]->id(),
133     ], array_values($result));
134
135     // View any (published-only).
136     $user = $this->createUser([], ['view any entity_test_enhanced_with_owner']);
137     \Drupal::currentUser()->setAccount($user);
138
139     $result = $this->storage->getQuery()->sort('id')->execute();
140     $this->assertEquals([
141       $this->entities[1]->id(),
142       $this->entities[2]->id(),
143     ], array_values($result));
144
145     // View own unpublished.
146     $user = $this->createUser([], ['view own unpublished entity_test_enhanced_with_owner']);
147     \Drupal::currentUser()->setAccount($user);
148
149     $this->entities[0]->set('user_id', $user->id());
150     $this->entities[0]->save();
151     $this->entities[1]->set('user_id', $user->id());
152     $this->entities[1]->save();
153
154     $result = $this->storage->getQuery()->sort('id')->execute();
155     $this->assertEquals([
156       $this->entities[0]->id(),
157     ], array_values($result));
158
159     // View own unpublished + view any (published-only).
160     $user = $this->createUser([], [
161       'view own unpublished entity_test_enhanced_with_owner',
162       'view any entity_test_enhanced_with_owner',
163     ]);
164     \Drupal::currentUser()->setAccount($user);
165
166     $this->entities[0]->set('user_id', $user->id());
167     $this->entities[0]->save();
168
169     $result = $this->storage->getQuery()->sort('id')->execute();
170     $this->assertEquals([
171       $this->entities[0]->id(),
172       $this->entities[1]->id(),
173       $this->entities[2]->id(),
174     ], array_values($result));
175
176     // View own $first_bundle + View any $second_bundle.
177     $user = $this->createUser([], [
178       'view own first entity_test_enhanced_with_owner',
179       'view any second entity_test_enhanced_with_owner',
180     ]);
181     \Drupal::currentUser()->setAccount($user);
182
183     $this->entities[1]->set('user_id', $user->id());
184     $this->entities[1]->save();
185
186     $result = $this->storage->getQuery()->sort('id')->execute();
187     $this->assertEquals([
188       $this->entities[1]->id(),
189       $this->entities[2]->id(),
190     ], array_values($result));
191   }
192
193   /**
194    * Tests EntityQuery filtering when all revisions are queried.
195    */
196   public function testEntityQueryWithRevisions() {
197     // Admin permission, full access.
198     $admin_user = $this->createUser([], ['administer entity_test_enhanced_with_owner']);
199     \Drupal::currentUser()->setAccount($admin_user);
200
201     $result = $this->storage->getQuery()->allRevisions()->sort('id')->execute();
202     $this->assertEquals([
203       '1' => $this->entities[0]->id(),
204       '2' => $this->entities[0]->id(),
205       '3' => $this->entities[1]->id(),
206       '4' => $this->entities[1]->id(),
207       '5' => $this->entities[2]->id(),
208       '6' => $this->entities[2]->id(),
209     ], $result);
210
211     // No view permissions, no access.
212     $user = $this->createUser([], ['access content']);
213     \Drupal::currentUser()->setAccount($user);
214
215     $result = $this->storage->getQuery()->allRevisions()->execute();
216     $this->assertEmpty($result);
217
218     // View own (published-only).
219     $user = $this->createUser([], ['view own entity_test_enhanced_with_owner']);
220     \Drupal::currentUser()->setAccount($user);
221
222     // The user_id field is not revisionable, which means that updating it
223     // will modify both revisions for each entity.
224     $this->entities[0]->set('user_id', $user->id());
225     $this->entities[0]->save();
226     $this->entities[1]->set('user_id', $user->id());
227     $this->entities[1]->save();
228
229     $result = $this->storage->getQuery()->allRevisions()->sort('id')->execute();
230     $this->assertEquals([
231       '1' => $this->entities[0]->id(),
232       '4' => $this->entities[1]->id(),
233     ], $result);
234
235     // View any (published-only).
236     $user = $this->createUser([], ['view any entity_test_enhanced_with_owner']);
237     \Drupal::currentUser()->setAccount($user);
238
239     $result = $this->storage->getQuery()->allRevisions()->sort('id')->execute();
240     $this->assertEquals([
241       '1' => $this->entities[0]->id(),
242       '4' => $this->entities[1]->id(),
243       '5' => $this->entities[2]->id(),
244       '6' => $this->entities[2]->id(),
245     ], $result);
246
247     // View own unpublished.
248     $user = $this->createUser([], ['view own unpublished entity_test_enhanced_with_owner']);
249     \Drupal::currentUser()->setAccount($user);
250
251     $this->entities[0]->set('user_id', $user->id());
252     $this->entities[0]->save();
253     $this->entities[1]->set('user_id', $user->id());
254     $this->entities[1]->save();
255
256     $result = $this->storage->getQuery()->allRevisions()->sort('id')->execute();
257     $this->assertEquals([
258       '2' => $this->entities[0]->id(),
259       '3' => $this->entities[1]->id(),
260     ], $result);
261
262     // View own unpublished + view any (published-only).
263     $user = $this->createUser([], [
264       'view own unpublished entity_test_enhanced_with_owner',
265       'view any entity_test_enhanced_with_owner',
266     ]);
267     \Drupal::currentUser()->setAccount($user);
268
269     $this->entities[0]->set('user_id', $user->id());
270     $this->entities[0]->save();
271
272     $result = $this->storage->getQuery()->allRevisions()->sort('id')->execute();
273     $this->assertEquals([
274       '1' => $this->entities[0]->id(),
275       '2' => $this->entities[0]->id(),
276       '4' => $this->entities[1]->id(),
277       '5' => $this->entities[2]->id(),
278       '6' => $this->entities[2]->id(),
279     ], $result);
280
281     // View own $first_bundle + View any $second_bundle.
282     $user = $this->createUser([], [
283       'view own first entity_test_enhanced_with_owner',
284       'view any second entity_test_enhanced_with_owner',
285     ]);
286     \Drupal::currentUser()->setAccount($user);
287
288     $this->entities[1]->set('user_id', $user->id());
289     $this->entities[1]->save();
290
291     $result = $this->storage->getQuery()->allRevisions()->sort('id')->execute();
292     $this->assertEquals([
293       '4' => $this->entities[1]->id(),
294       '5' => $this->entities[2]->id(),
295       '6' => $this->entities[2]->id(),
296     ], $result);
297   }
298
299   /**
300    * Tests Views filtering.
301    */
302   public function testViews() {
303     // Admin permission, full access.
304     $admin_user = $this->createUser([], ['administer entity_test_enhanced_with_owner']);
305     \Drupal::currentUser()->setAccount($admin_user);
306
307     $view = Views::getView('entity_test_enhanced_with_owner');
308     $view->execute();
309     $this->assertIdenticalResultset($view, [
310       ['id' => $this->entities[0]->id()],
311       ['id' => $this->entities[1]->id()],
312       ['id' => $this->entities[2]->id()],
313     ], ['id' => 'id']);
314
315     // No view permissions, no access.
316     $user = $this->createUser([], ['access content']);
317     \Drupal::currentUser()->setAccount($user);
318
319     $view = Views::getView('entity_test_enhanced_with_owner');
320     $view->execute();
321     $this->assertIdenticalResultset($view, []);
322
323     // View own (published-only).
324     $user = $this->createUser([], ['view own entity_test_enhanced_with_owner']);
325     \Drupal::currentUser()->setAccount($user);
326
327     $this->entities[0]->set('user_id', $user->id());
328     $this->entities[0]->save();
329     $this->entities[1]->set('user_id', $user->id());
330     $this->entities[1]->save();
331
332     $view = Views::getView('entity_test_enhanced_with_owner');
333     $view->execute();
334     $this->assertIdenticalResultset($view, [
335       ['id' => $this->entities[1]->id()],
336     ], ['id' => 'id']);
337
338     // View any (published-only).
339     $user = $this->createUser([], ['view any entity_test_enhanced_with_owner']);
340     \Drupal::currentUser()->setAccount($user);
341
342     $view = Views::getView('entity_test_enhanced_with_owner');
343     $view->execute();
344     $this->assertIdenticalResultset($view, [
345       ['id' => $this->entities[1]->id()],
346       ['id' => $this->entities[2]->id()],
347     ], ['id' => 'id']);
348
349     // View own unpublished.
350     $user = $this->createUser([], ['view own unpublished entity_test_enhanced_with_owner']);
351     \Drupal::currentUser()->setAccount($user);
352
353     $this->entities[0]->set('user_id', $user->id());
354     $this->entities[0]->save();
355     $this->entities[1]->set('user_id', $user->id());
356     $this->entities[1]->save();
357
358     $view = Views::getView('entity_test_enhanced_with_owner');
359     $view->execute();
360     $this->assertIdenticalResultset($view, [
361       ['id' => $this->entities[0]->id()],
362     ], ['id' => 'id']);
363
364     // View own unpublished + view any (published-only).
365     $user = $this->createUser([], [
366       'view own unpublished entity_test_enhanced_with_owner',
367       'view any entity_test_enhanced_with_owner',
368     ]);
369     \Drupal::currentUser()->setAccount($user);
370
371     $this->entities[0]->set('user_id', $user->id());
372     $this->entities[0]->save();
373
374     $view = Views::getView('entity_test_enhanced_with_owner');
375     $view->execute();
376     $this->assertIdenticalResultset($view, [
377       ['id' => $this->entities[0]->id()],
378       ['id' => $this->entities[1]->id()],
379       ['id' => $this->entities[2]->id()],
380     ], ['id' => 'id']);
381
382     // View own $first_bundle + View any $second_bundle.
383     $user = $this->createUser([], [
384       'view own first entity_test_enhanced_with_owner',
385       'view any second entity_test_enhanced_with_owner',
386     ]);
387     \Drupal::currentUser()->setAccount($user);
388
389     $this->entities[1]->set('user_id', $user->id());
390     $this->entities[1]->save();
391
392     $view = Views::getView('entity_test_enhanced_with_owner');
393     $view->execute();
394     $this->assertIdenticalResultset($view, [
395       ['id' => $this->entities[1]->id()],
396       ['id' => $this->entities[2]->id()],
397     ], ['id' => 'id']);
398   }
399
400   /**
401    * Tests Views filtering when all revisions are queried.
402    */
403   public function testViewsWithRevisions() {
404     // Admin permission, full access.
405     $admin_user = $this->createUser([], ['administer entity_test_enhanced_with_owner']);
406     \Drupal::currentUser()->setAccount($admin_user);
407
408     $view = Views::getView('entity_test_enhanced_with_owner_revisions');
409     $view->execute();
410     $this->assertIdenticalResultset($view, [
411       ['vid' => '1', 'id' => $this->entities[0]->id()],
412       ['vid' => '2', 'id' => $this->entities[0]->id()],
413       ['vid' => '3', 'id' => $this->entities[1]->id()],
414       ['vid' => '4', 'id' => $this->entities[1]->id()],
415       ['vid' => '5', 'id' => $this->entities[2]->id()],
416       ['vid' => '6', 'id' => $this->entities[2]->id()],
417     ], ['vid' => 'vid']);
418
419     // No view permissions, no access.
420     $user = $this->createUser([], ['access content']);
421     \Drupal::currentUser()->setAccount($user);
422
423     $view = Views::getView('entity_test_enhanced_with_owner_revisions');
424     $view->execute();
425     $this->assertIdenticalResultset($view, []);
426
427     // View own (published-only).
428     $user = $this->createUser([], ['view own entity_test_enhanced_with_owner']);
429     \Drupal::currentUser()->setAccount($user);
430
431     $this->entities[0]->set('user_id', $user->id());
432     $this->entities[0]->save();
433     $this->entities[1]->set('user_id', $user->id());
434     $this->entities[1]->save();
435
436     $view = Views::getView('entity_test_enhanced_with_owner_revisions');
437     $view->execute();
438     $this->assertIdenticalResultset($view, [
439       ['vid' => '1', 'id' => $this->entities[0]->id()],
440       ['vid' => '4', 'id' => $this->entities[1]->id()],
441     ], ['vid' => 'vid']);
442
443     // View any (published-only).
444     $user = $this->createUser([], ['view any entity_test_enhanced_with_owner']);
445     \Drupal::currentUser()->setAccount($user);
446
447     $view = Views::getView('entity_test_enhanced_with_owner_revisions');
448     $view->execute();
449     $this->assertIdenticalResultset($view, [
450       ['vid' => '1', 'id' => $this->entities[0]->id()],
451       ['vid' => '4', 'id' => $this->entities[1]->id()],
452       ['vid' => '5', 'id' => $this->entities[2]->id()],
453       ['vid' => '6', 'id' => $this->entities[2]->id()],
454     ], ['vid' => 'vid']);
455
456     // View own unpublished.
457     $user = $this->createUser([], ['view own unpublished entity_test_enhanced_with_owner']);
458     \Drupal::currentUser()->setAccount($user);
459
460     $this->entities[0]->set('user_id', $user->id());
461     $this->entities[0]->save();
462     $this->entities[1]->set('user_id', $user->id());
463     $this->entities[1]->save();
464
465     $view = Views::getView('entity_test_enhanced_with_owner_revisions');
466     $view->execute();
467     $this->assertIdenticalResultset($view, [
468       ['vid' => '2', 'id' => $this->entities[0]->id()],
469       ['vid' => '3', 'id' => $this->entities[1]->id()],
470     ], ['vid' => 'vid']);
471
472     // View own unpublished + view any (published-only).
473     $user = $this->createUser([], [
474       'view own unpublished entity_test_enhanced_with_owner',
475       'view any entity_test_enhanced_with_owner',
476     ]);
477     \Drupal::currentUser()->setAccount($user);
478
479     $this->entities[0]->set('user_id', $user->id());
480     $this->entities[0]->save();
481
482     $view = Views::getView('entity_test_enhanced_with_owner_revisions');
483     $view->execute();
484     $this->assertIdenticalResultset($view, [
485       ['vid' => '1', 'id' => $this->entities[0]->id()],
486       ['vid' => '2', 'id' => $this->entities[0]->id()],
487       ['vid' => '4', 'id' => $this->entities[1]->id()],
488       ['vid' => '5', 'id' => $this->entities[2]->id()],
489       ['vid' => '6', 'id' => $this->entities[2]->id()],
490     ], ['vid' => 'vid']);
491
492     // View own $first_bundle + View any $second_bundle.
493     $user = $this->createUser([], [
494       'view own first entity_test_enhanced_with_owner',
495       'view any second entity_test_enhanced_with_owner',
496     ]);
497     \Drupal::currentUser()->setAccount($user);
498
499     $this->entities[1]->set('user_id', $user->id());
500     $this->entities[1]->save();
501
502     $view = Views::getView('entity_test_enhanced_with_owner_revisions');
503     $view->execute();
504     $this->assertIdenticalResultset($view, [
505       ['vid' => '4', 'id' => $this->entities[1]->id()],
506       ['vid' => '5', 'id' => $this->entities[2]->id()],
507       ['vid' => '6', 'id' => $this->entities[2]->id()],
508     ], ['vid' => 'vid']);
509   }
510
511 }