Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / tests / src / Kernel / QueryAccess / QueryAccessHandlerTest.php
1 <?php
2
3 namespace Drupal\Tests\entity\Kernel\QueryAccess;
4
5 use Drupal\entity\QueryAccess\Condition;
6 use Drupal\entity\QueryAccess\QueryAccessHandler;
7 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
8
9 /**
10  * Tests the query access handler.
11  *
12  * Uses the "entity_test_enhanced" entity type, which has no owner.
13  * UncacheableQueryAccessHandlerTest uses the "entity_test_enhanced_with_owner"
14  * entity type, which has an owner. This ensures both sides (owner and
15  * no owner) are covered.
16  *
17  * @coversDefaultClass \Drupal\entity\QueryAccess\QueryAccessHandler
18  * @group entity
19  */
20 class QueryAccessHandlerTest extends EntityKernelTestBase {
21
22   /**
23    * The query access handler.
24    *
25    * @var \Drupal\entity\QueryAccess\QueryAccessHandler
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');
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');
50     $this->handler = QueryAccessHandler::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']);
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     // Entity type permission.
84     $user = $this->createUser([], ['view entity_test_enhanced']);
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     // Bundle permission.
95     $user = $this->createUser([], ['view first entity_test_enhanced']);
96     $conditions = $this->handler->getConditions('view', $user);
97     $expected_conditions = [
98       new Condition('type', ['first']),
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.permissions'], $conditions->getCacheContexts());
105     $this->assertFalse($conditions->isAlwaysFalse());
106   }
107
108   /**
109    * @covers ::getConditions
110    */
111   public function testUpdateDelete() {
112     foreach (['update', 'delete'] as $operation) {
113       // Entity type permission.
114       $user = $this->createUser([], ["$operation entity_test_enhanced"]);
115       $conditions = $this->handler->getConditions($operation, $user);
116       $this->assertEquals(0, $conditions->count());
117       $this->assertEquals(['user.permissions'], $conditions->getCacheContexts());
118       $this->assertFalse($conditions->isAlwaysFalse());
119
120       // Bundle permission.
121       $user = $this->createUser([], [
122         "$operation first entity_test_enhanced",
123         "$operation second entity_test_enhanced",
124       ]);
125       $conditions = $this->handler->getConditions($operation, $user);
126       $expected_conditions = [
127         new Condition('type', ['first', 'second']),
128       ];
129       $this->assertEquals('OR', $conditions->getConjunction());
130       $this->assertEquals(1, $conditions->count());
131       $this->assertEquals($expected_conditions, $conditions->getConditions());
132       $this->assertEquals(['user.permissions'], $conditions->getCacheContexts());
133       $this->assertFalse($conditions->isAlwaysFalse());
134     }
135   }
136
137 }