3 namespace Drupal\Tests\entity\Kernel\QueryAccess;
5 use Drupal\entity\QueryAccess\Condition;
6 use Drupal\entity\QueryAccess\QueryAccessHandler;
7 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
10 * Tests the query access handler.
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.
17 * @coversDefaultClass \Drupal\entity\QueryAccess\QueryAccessHandler
20 class QueryAccessHandlerTest extends EntityKernelTestBase {
23 * The query access handler.
25 * @var \Drupal\entity\QueryAccess\QueryAccessHandler
32 public static $modules = [
40 protected function setUp() {
43 $this->installEntitySchema('entity_test_enhanced');
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');
50 $this->handler = QueryAccessHandler::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']);
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() {
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'),
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 first entity_test_enhanced']);
96 $conditions = $this->handler->getConditions('view', $user);
97 $expected_conditions = [
98 new Condition('type', ['first']),
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.permissions'], $conditions->getCacheContexts());
105 $this->assertFalse($conditions->isAlwaysFalse());
109 * @covers ::getConditions
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());
120 // Bundle permission.
121 $user = $this->createUser([], [
122 "$operation first entity_test_enhanced",
123 "$operation second entity_test_enhanced",
125 $conditions = $this->handler->getConditions($operation, $user);
126 $expected_conditions = [
127 new Condition('type', ['first', 'second']),
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());