public function OgRoleManagerTest::testLoadRoleByPermissions in Organic groups 8
Tests searching roles by a given permission list.
@covers ::getRolesByPermissions
File
- tests/src/ Kernel/ OgRoleManagerTest.php, line 116 
Class
- OgRoleManagerTest
- Kernel tests for the OG role manager service.
Namespace
Drupal\Tests\og\KernelCode
public function testLoadRoleByPermissions() {
  OgRole::create()
    ->setName(mb_strtolower($this
    ->randomMachineName()))
    ->setLabel($this
    ->randomString())
    ->setGroupType('node')
    ->setGroupBundle('node_group_type')
    ->grantPermission('access content')
    ->grantPermission('view own unpublished content')
    ->save();
  $og_role3 = OgRole::create();
  $og_role3
    ->setName(mb_strtolower($this
    ->randomMachineName()))
    ->setLabel($this
    ->randomString())
    ->setGroupType('entity_test')
    ->setGroupBundle('entity_test_group_type')
    ->grantPermission('access content')
    ->grantPermission(OgAccess::ADMINISTER_GROUP_PERMISSION)
    ->grantPermission('edit any group entity_test')
    ->save();
  $roles = $this->ogRoleManager
    ->getRolesByPermissions([
    'access content',
  ]);
  $this
    ->assertCount(3, $roles);
  // Filter based on the entity type id and bundle.
  $roles = $this->ogRoleManager
    ->getRolesByPermissions([
    OgAccess::ADMINISTER_GROUP_PERMISSION,
  ], 'entity_test', 'entity_test_group_type');
  $this
    ->assertCount(1, $roles);
  $actual_role = reset($roles);
  $this
    ->assertEquals($actual_role
    ->id(), $og_role3
    ->id());
  // By default, roles are that match all of the passed permissions.
  $roles = $this->ogRoleManager
    ->getRolesByPermissions([
    'access content',
    OgAccess::ADMINISTER_GROUP_PERMISSION,
  ]);
  $this
    ->assertCount(2, $roles);
  // Request roles that match one or more of the passed permissions.
  $roles = $this->ogRoleManager
    ->getRolesByPermissions([
    'access content',
    OgAccess::ADMINISTER_GROUP_PERMISSION,
  ], NULL, NULL, FALSE);
  $this
    ->assertCount(3, $roles);
  // Require roles with all of the passed permissions and in certain entity
  // type ID and bundle.
  $roles = $this->ogRoleManager
    ->getRolesByPermissions([
    'access content',
    OgAccess::ADMINISTER_GROUP_PERMISSION,
  ], 'entity_test', 'entity_test_group_type', TRUE);
  $this
    ->assertCount(1, $roles);
}