You are here

public function QueryAccessTest::testViewsWithRevisions in Entity API 8

Tests Views filtering when all revisions are queried.

File

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

Class

QueryAccessTest
Test query access filtering for EntityQuery and Views.

Namespace

Drupal\Tests\entity\Kernel\QueryAccess

Code

public function testViewsWithRevisions() {

  // Admin permission, full access.
  $admin_user = $this
    ->createUser([], [
    'administer entity_test_enhanced',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($admin_user);
  $view = Views::getView('entity_test_enhanced_revisions');
  $view
    ->execute();
  $this
    ->assertIdenticalResultset($view, [
    [
      'vid' => '1',
      'id' => $this->entities[0]
        ->id(),
    ],
    [
      'vid' => '2',
      'id' => $this->entities[0]
        ->id(),
    ],
    [
      'vid' => '3',
      'id' => $this->entities[1]
        ->id(),
    ],
    [
      'vid' => '4',
      'id' => $this->entities[1]
        ->id(),
    ],
    [
      'vid' => '5',
      'id' => $this->entities[2]
        ->id(),
    ],
    [
      'vid' => '6',
      'id' => $this->entities[2]
        ->id(),
    ],
  ], [
    'vid' => 'vid',
  ]);

  // No view permissions, no access.
  $user = $this
    ->createUser([], [
    'access content',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $view = Views::getView('entity_test_enhanced');
  $view
    ->execute();
  $this
    ->assertIdenticalResultset($view, []);

  // View (published-only).
  $user = $this
    ->createUser([], [
    'view entity_test_enhanced',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $view = Views::getView('entity_test_enhanced_revisions');
  $view
    ->execute();
  $this
    ->assertIdenticalResultset($view, [
    [
      'vid' => '1',
      'id' => $this->entities[0]
        ->id(),
    ],
    [
      'vid' => '4',
      'id' => $this->entities[1]
        ->id(),
    ],
    [
      'vid' => '5',
      'id' => $this->entities[2]
        ->id(),
    ],
    [
      'vid' => '6',
      'id' => $this->entities[2]
        ->id(),
    ],
  ], [
    'vid' => 'vid',
  ]);

  // View $bundle (published-only).
  $user = $this
    ->createUser([], [
    'view first entity_test_enhanced',
  ]);
  $this->container
    ->get('current_user')
    ->setAccount($user);
  $view = Views::getView('entity_test_enhanced_revisions');
  $view
    ->execute();
  $this
    ->assertIdenticalResultset($view, [
    [
      'vid' => '1',
      'id' => $this->entities[0]
        ->id(),
    ],
    [
      'vid' => '4',
      'id' => $this->entities[1]
        ->id(),
    ],
  ], [
    'vid' => 'vid',
  ]);
}