You are here

public function GroupAccessControlHandlerTest::testViewAccess in Group 2.0.x

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

Tests the behavior of view access for groups.

@covers ::checkAccess

File

tests/src/Kernel/GroupAccessControlHandlerTest.php, line 94

Class

GroupAccessControlHandlerTest
Tests the general access behavior of group entities.

Namespace

Drupal\Tests\group\Kernel

Code

public function testViewAccess() {
  $access_control_handler = $this->entityTypeManager
    ->getAccessControlHandler('group');
  $this
    ->assertFalse($this->group
    ->access('view'), 'An outsider without the right permission has no access');
  $access_control_handler
    ->resetCache();
  $this->group
    ->addMember($this
    ->getCurrentUser());
  $this
    ->assertFalse($this->group
    ->access('view'), 'A member without the right permission has no access');
  $access_control_handler
    ->resetCache();
  $this->groupType
    ->getMemberRole()
    ->grantPermission('view group')
    ->save();
  $this
    ->assertTrue($this->group
    ->access('view'), 'A member with the right permission has access');
  $access_control_handler
    ->resetCache();
  $this->group
    ->set('status', FALSE)
    ->save();
  $this
    ->assertFalse($this->group
    ->access('view'), 'Unpublishing the group denies access');
  $access_control_handler
    ->resetCache();
  $this->groupType
    ->getMemberRole()
    ->grantPermission('view own unpublished group')
    ->save();
  $this
    ->assertTrue($this->group
    ->access('view'), 'A member and owner with the view own unpublished permission has access');
  $access_control_handler
    ->resetCache();
  $this->group
    ->set('uid', 1)
    ->save();
  $this
    ->assertFalse($this->group
    ->access('view'), 'Changing the group owner once again denies access');
  $access_control_handler
    ->resetCache();
  $this->groupType
    ->getMemberRole()
    ->grantPermission('view any unpublished group')
    ->save();
  $this
    ->assertTrue($this->group
    ->access('view'), 'A member with the view any unpublished permission has access');
  $access_control_handler
    ->resetCache();
  $this->group
    ->removeMember($this
    ->getCurrentUser());
  $this
    ->assertFalse($this->group
    ->access('view'), 'Leaving the group once again revokes access');
}