You are here

public function EntityAccessTest::testNonMemberViewAnyAccess in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Kernel/EntityAccessTest.php \Drupal\Tests\group\Kernel\EntityAccessTest::testNonMemberViewAnyAccess()

Tests the viewing of any grouped entities for non-members.

File

tests/src/Kernel/EntityAccessTest.php, line 176

Class

EntityAccessTest
Tests that Group properly checks access for grouped entities.

Namespace

Drupal\Tests\group\Kernel

Code

public function testNonMemberViewAnyAccess() {
  $account = $this
    ->createUser([], [
    'administer entity_test_with_owner content',
  ]);
  $entity_1 = $this
    ->createTestEntity();
  $entity_2 = $this
    ->createTestEntity();
  $entity_3 = $this
    ->createTestEntity();
  $this->groupTypeA
    ->getOutsiderRole()
    ->grantPermission('view any entity_test_as_content entity')
    ->save();
  $this->groupTypeB
    ->getOutsiderRole()
    ->grantPermission('view any 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_2, 'entity_test_as_content');
  $group_b
    ->addMember($account);
  $this
    ->assertTrue($this->accessControlHandler
    ->access($entity_1, 'view'), 'Non-members can see any grouped test entities.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($entity_2, 'view'), 'Non-members can see any grouped test entities.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($entity_3, 'view'), 'The ungrouped test entity can be viewed.');
  $this
    ->setCurrentUser($this
    ->createUser([], [
    'administer entity_test_with_owner content',
  ]));
  $this
    ->assertTrue($this->accessControlHandler
    ->access($entity_1, 'view'), 'Non-members can see any grouped test entities.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($entity_2, 'view'), 'Non-members can see any grouped test entities.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($entity_3, 'view'), 'The ungrouped test entity can be viewed.');
  $this
    ->setCurrentUser($account);
  $this
    ->assertFalse($this->accessControlHandler
    ->access($entity_1, 'view'), 'Members cannot see grouped test entities.');
  $this
    ->assertFalse($this->accessControlHandler
    ->access($entity_2, 'view'), 'Members cannot see grouped test entities.');
  $this
    ->assertTrue($this->accessControlHandler
    ->access($entity_3, 'view'), 'The ungrouped test entity can be viewed.');
}