You are here

public function UncacheableQueryAccessTest::testEntityQueryWithRevisions in Entity API 8

Tests EntityQuery filtering when all revisions are queried.

File

tests/src/Kernel/QueryAccess/UncacheableQueryAccessTest.php, line 196

Class

UncacheableQueryAccessTest
Test uncacheable query access filtering for EntityQuery and Views.

Namespace

Drupal\Tests\entity\Kernel\QueryAccess

Code

public function testEntityQueryWithRevisions() {

  // Admin permission, full access.
  $admin_user = $this
    ->createUser([], [
    'administer entity_test_enhanced_with_owner',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($admin_user);
  $result = $this->storage
    ->getQuery()
    ->allRevisions()
    ->sort('id')
    ->execute();
  $this
    ->assertEquals([
    '1' => $this->entities[0]
      ->id(),
    '2' => $this->entities[0]
      ->id(),
    '3' => $this->entities[1]
      ->id(),
    '4' => $this->entities[1]
      ->id(),
    '5' => $this->entities[2]
      ->id(),
    '6' => $this->entities[2]
      ->id(),
  ], $result);

  // No view permissions, no access.
  $user = $this
    ->createUser([], [
    'access content',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $result = $this->storage
    ->getQuery()
    ->allRevisions()
    ->execute();
  $this
    ->assertEmpty($result);

  // View own (published-only).
  $user = $this
    ->createUser([], [
    'view own entity_test_enhanced_with_owner',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);

  // The user_id field is not revisionable, which means that updating it
  // will modify both revisions for each entity.
  $this->entities[0]
    ->set('user_id', $user
    ->id());
  $this->entities[0]
    ->save();
  $this->entities[1]
    ->set('user_id', $user
    ->id());
  $this->entities[1]
    ->save();
  $result = $this->storage
    ->getQuery()
    ->allRevisions()
    ->sort('id')
    ->execute();
  $this
    ->assertEquals([
    '1' => $this->entities[0]
      ->id(),
    '4' => $this->entities[1]
      ->id(),
  ], $result);

  // View any (published-only).
  $user = $this
    ->createUser([], [
    'view any entity_test_enhanced_with_owner',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $result = $this->storage
    ->getQuery()
    ->allRevisions()
    ->sort('id')
    ->execute();
  $this
    ->assertEquals([
    '1' => $this->entities[0]
      ->id(),
    '4' => $this->entities[1]
      ->id(),
    '5' => $this->entities[2]
      ->id(),
    '6' => $this->entities[2]
      ->id(),
  ], $result);

  // View own unpublished.
  $user = $this
    ->createUser([], [
    'view own unpublished entity_test_enhanced_with_owner',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $this->entities[0]
    ->set('user_id', $user
    ->id());
  $this->entities[0]
    ->save();
  $this->entities[1]
    ->set('user_id', $user
    ->id());
  $this->entities[1]
    ->save();
  $result = $this->storage
    ->getQuery()
    ->allRevisions()
    ->sort('id')
    ->execute();
  $this
    ->assertEquals([
    '2' => $this->entities[0]
      ->id(),
    '3' => $this->entities[1]
      ->id(),
  ], $result);

  // View own unpublished + view any (published-only).
  $user = $this
    ->createUser([], [
    'view own unpublished entity_test_enhanced_with_owner',
    'view any entity_test_enhanced_with_owner',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $this->entities[0]
    ->set('user_id', $user
    ->id());
  $this->entities[0]
    ->save();
  $result = $this->storage
    ->getQuery()
    ->allRevisions()
    ->sort('id')
    ->execute();
  $this
    ->assertEquals([
    '1' => $this->entities[0]
      ->id(),
    '2' => $this->entities[0]
      ->id(),
    '4' => $this->entities[1]
      ->id(),
    '5' => $this->entities[2]
      ->id(),
    '6' => $this->entities[2]
      ->id(),
  ], $result);

  // View own $first_bundle + View any $second_bundle.
  $user = $this
    ->createUser([], [
    'view own first entity_test_enhanced_with_owner',
    'view any second entity_test_enhanced_with_owner',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $this->entities[1]
    ->set('user_id', $user
    ->id());
  $this->entities[1]
    ->save();
  $result = $this->storage
    ->getQuery()
    ->allRevisions()
    ->sort('id')
    ->execute();
  $this
    ->assertEquals([
    '4' => $this->entities[1]
      ->id(),
    '5' => $this->entities[2]
      ->id(),
    '6' => $this->entities[2]
      ->id(),
  ], $result);
}