You are here

public function QueryAccessTest::testEntityQuery in Entity API 8

Tests EntityQuery filtering.

File

tests/src/Kernel/QueryAccess/QueryAccessTest.php, line 102

Class

QueryAccessTest
Test query access filtering for EntityQuery and Views.

Namespace

Drupal\Tests\entity\Kernel\QueryAccess

Code

public function testEntityQuery() {

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

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

  // View (published-only).
  $user = $this
    ->createUser([], [
    'view entity_test_enhanced',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $result = $this->storage
    ->getQuery()
    ->sort('id')
    ->execute();
  $this
    ->assertEquals([
    $this->entities[1]
      ->id(),
    $this->entities[2]
      ->id(),
  ], array_values($result));

  // View $bundle (published-only).
  $user = $this
    ->createUser([], [
    'view first entity_test_enhanced',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $result = $this->storage
    ->getQuery()
    ->sort('id')
    ->execute();
  $this
    ->assertEquals([
    $this->entities[1]
      ->id(),
  ], array_values($result));
}