You are here

public function EntityQueryAlterTest::testNonMemberViewOwnAccess in Group 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Kernel/EntityQueryAlterTest.php \Drupal\Tests\group\Kernel\EntityQueryAlterTest::testNonMemberViewOwnAccess()

Tests the viewing of own entities for non-members.

File

tests/src/Kernel/EntityQueryAlterTest.php, line 227

Class

EntityQueryAlterTest
Tests that Group properly checks access for grouped entities.

Namespace

Drupal\Tests\group\Kernel

Code

public function testNonMemberViewOwnAccess() {
  $account = $this
    ->createUser([], [
    'administer entity_test_with_owner content',
  ]);
  $entity_1 = $this
    ->createTestEntity();
  $entity_2 = $this
    ->createTestEntity();
  $entity_3 = $this
    ->createTestEntity([
    'uid' => $account
      ->id(),
  ]);
  $this->groupTypeA
    ->getOutsiderRole()
    ->grantPermission('view own entity_test_as_content entity')
    ->save();
  $this->groupTypeB
    ->getOutsiderRole()
    ->grantPermission('view own entity_test_as_content entity')
    ->save();
  $group_a = $this
    ->createGroup([
    'type' => $this->groupTypeA
      ->id(),
  ]);
  $group_a
    ->addContent($entity_1, 'entity_test_as_content');
  $group_a
    ->addMember($account);
  $group_b = $this
    ->createGroup([
    'type' => $this->groupTypeB
      ->id(),
  ]);
  $group_b
    ->addContent($entity_3, 'entity_test_as_content');
  $group_b
    ->addMember($account);
  $this
    ->assertQueryAccessResult([
    $entity_1
      ->id(),
    $entity_2
      ->id(),
  ], 'Non-members can see their own test entities.');
  $this
    ->setCurrentUser($this
    ->createUser());
  $this
    ->assertQueryAccessResult([
    $entity_2
      ->id(),
  ], 'Non-members cannot see test entities they do not own.');
  $this
    ->setCurrentUser($account);
  $this
    ->assertQueryAccessResult([
    $entity_2
      ->id(),
  ], 'Only the ungrouped test entity shows up.');
}