View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\og\Entity\OgRole;
use Drupal\og\Og;
use Drupal\og\OgAccess;
class OgRoleManagerTest extends KernelTestBase {
public static $modules = [
'entity_test',
'field',
'node',
'og',
'system',
'user',
];
protected $group;
protected $roleName;
protected $ogRoleManager;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'og',
]);
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('node');
$this->roleName = mb_strtolower($this
->randomMachineName());
$this->ogRoleManager = $this->container
->get('og.role_manager');
NodeType::create([
'type' => 'node_group_type',
'name' => $this
->randomString(),
])
->save();
Og::groupTypeManager()
->addGroup('node', 'node_group_type');
Og::groupTypeManager()
->addGroup('entity_test', 'entity_test_group_type');
$og_role = OgRole::create();
$og_role
->setName($this->roleName)
->setLabel($this
->randomString())
->setGroupType('node')
->setGroupBundle('node_group_type')
->grantPermission('access content')
->grantPermission(OgAccess::ADMINISTER_GROUP_PERMISSION)
->grantPermission('view own unpublished content')
->save();
}
public function testGetRolesByBundle() {
$expected_role_ids = [
'node-node_group_type-administrator',
'node-node_group_type-member',
'node-node_group_type-non-member',
'node-node_group_type-' . $this->roleName,
];
$roles = $this->ogRoleManager
->getRolesByBundle('node', 'node_group_type');
$role_ids = array_keys($roles);
sort($expected_role_ids);
sort($role_ids);
$this
->assertEquals($expected_role_ids, $role_ids);
}
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);
$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());
$roles = $this->ogRoleManager
->getRolesByPermissions([
'access content',
OgAccess::ADMINISTER_GROUP_PERMISSION,
]);
$this
->assertCount(2, $roles);
$roles = $this->ogRoleManager
->getRolesByPermissions([
'access content',
OgAccess::ADMINISTER_GROUP_PERMISSION,
], NULL, NULL, FALSE);
$this
->assertCount(3, $roles);
$roles = $this->ogRoleManager
->getRolesByPermissions([
'access content',
OgAccess::ADMINISTER_GROUP_PERMISSION,
], 'entity_test', 'entity_test_group_type', TRUE);
$this
->assertCount(1, $roles);
}
}