You are here

public function UncacheableQueryAccessHandlerTest::testView in Entity API 8

@covers ::getConditions

File

tests/src/Kernel/QueryAccess/UncacheableQueryAccessHandlerTest.php, line 82

Class

UncacheableQueryAccessHandlerTest
Tests the uncacheable query access handler.

Namespace

Drupal\Tests\entity\Kernel\QueryAccess

Code

public function testView() {

  // Any permission.
  $user = $this
    ->createUser([], [
    'view any entity_test_enhanced_with_owner',
  ]);
  $conditions = $this->handler
    ->getConditions('view', $user);
  $expected_conditions = [
    new Condition('status', '1'),
  ];
  $this
    ->assertEquals(1, $conditions
    ->count());
  $this
    ->assertEquals($expected_conditions, $conditions
    ->getConditions());
  $this
    ->assertEquals([
    'user.permissions',
  ], $conditions
    ->getCacheContexts());
  $this
    ->assertFalse($conditions
    ->isAlwaysFalse());

  // Own permission.
  $user = $this
    ->createUser([], [
    'view own entity_test_enhanced_with_owner',
  ]);
  $conditions = $this->handler
    ->getConditions('view', $user);
  $expected_conditions = [
    new Condition('user_id', $user
      ->id()),
    new Condition('status', '1'),
  ];
  $this
    ->assertEquals('AND', $conditions
    ->getConjunction());
  $this
    ->assertEquals(2, $conditions
    ->count());
  $this
    ->assertEquals($expected_conditions, $conditions
    ->getConditions());
  $this
    ->assertEquals([
    'user',
    'user.permissions',
  ], $conditions
    ->getCacheContexts());
  $this
    ->assertFalse($conditions
    ->isAlwaysFalse());

  // Any permission for the first bundle, own permission for the second.
  $user = $this
    ->createUser([], [
    'view any first entity_test_enhanced_with_owner',
    'view own second entity_test_enhanced_with_owner',
  ]);
  $conditions = $this->handler
    ->getConditions('view', $user);
  $expected_conditions = [
    (new ConditionGroup('OR'))
      ->addCacheContexts([
      'user',
      'user.permissions',
    ])
      ->addCondition('type', [
      'first',
    ])
      ->addCondition((new ConditionGroup('AND'))
      ->addCondition('user_id', $user
      ->id())
      ->addCondition('type', [
      'second',
    ])),
    new Condition('status', '1'),
  ];
  $this
    ->assertEquals('AND', $conditions
    ->getConjunction());
  $this
    ->assertEquals(2, $conditions
    ->count());
  $this
    ->assertEquals($expected_conditions, $conditions
    ->getConditions());
  $this
    ->assertEquals([
    'user',
    'user.permissions',
  ], $conditions
    ->getCacheContexts());
  $this
    ->assertFalse($conditions
    ->isAlwaysFalse());

  // View own unpublished permission.
  $user = $this
    ->createUser([], [
    'view own unpublished entity_test_enhanced_with_owner',
  ]);
  $conditions = $this->handler
    ->buildConditions('view', $user);
  $expected_conditions = [
    new Condition('user_id', $user
      ->id()),
    new Condition('status', '0'),
  ];
  $this
    ->assertEquals(2, $conditions
    ->count());
  $this
    ->assertEquals($expected_conditions, $conditions
    ->getConditions());
  $this
    ->assertEquals([
    'user',
  ], $conditions
    ->getCacheContexts());
  $this
    ->assertFalse($conditions
    ->isAlwaysFalse());

  // Both view any and view own unpublished permissions.
  $user = $this
    ->createUser([], [
    'view any entity_test_enhanced_with_owner',
    'view own unpublished entity_test_enhanced_with_owner',
  ]);
  $conditions = $this->handler
    ->buildConditions('view', $user);
  $expected_conditions = [
    new Condition('status', '1'),
    (new ConditionGroup('AND'))
      ->addCondition('user_id', $user
      ->id())
      ->addCondition('status', '0')
      ->addCacheContexts([
      'user',
    ]),
  ];
  $this
    ->assertEquals(2, $conditions
    ->count());
  $this
    ->assertEquals($expected_conditions, $conditions
    ->getConditions());
  $this
    ->assertEquals([
    'user',
    'user.permissions',
  ], $conditions
    ->getCacheContexts());
  $this
    ->assertFalse($conditions
    ->isAlwaysFalse());
}