public function GroupContentAccessControlHandlerTest::testEntityAccess in Group 8
Tests the entity operation access.
@covers ::entityAccess @dataProvider entityAccessProvider
Parameters
\Closure $expected: A closure returning the expected access result.
string $plugin_id: The plugin ID.
array $definition: The plugin definition.
bool $has_admin_permission: Whether the account has the admin permission.
bool $has_permission: Whether the account has the required permission.
bool $has_own_permission: Whether the account has the required owner permission.
string|false $permission: The operation permission.
string|false $own_permission: The owner operation permission.
bool $is_grouped: Whether the entity is grouped.
bool $is_ownable: Whether the entity can be owned.
bool $is_owner: Whether the account owns the entity.
bool $is_publishable: Whether the entity can be (un)published.
bool $is_published: Whether the entity is be published.
string $operation: The operation to check access for.
File
- tests/
src/ Unit/ GroupContentAccessControlHandlerTest.php, line 327
Class
- GroupContentAccessControlHandlerTest
- Tests the default GroupContentEnabler access handler.
Namespace
Drupal\Tests\group\UnitCode
public function testEntityAccess(\Closure $expected, $plugin_id, array $definition, $has_admin_permission, $has_permission, $has_own_permission, $permission, $own_permission, $is_grouped, $is_ownable, $is_owner, $is_publishable, $is_published, $operation) {
$storage = $this
->prophesize(GroupContentStorageInterface::class);
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager
->getStorage('group_content')
->willReturn($storage
->reveal());
$this->container
->get('entity_type.manager')
->willReturn($entity_type_manager
->reveal());
$permission_provider = $this
->prophesize(GroupContentPermissionProviderInterface::class);
$permission_provider
->getAdminPermission()
->willReturn($definition['admin_permission']);
$check_published = $operation === 'view' && $is_publishable;
if (!$check_published || $is_published) {
$permission_provider
->getPermission($operation, 'entity', 'any')
->willReturn($permission);
$permission_provider
->getPermission($operation, 'entity', 'own')
->willReturn($own_permission);
}
elseif ($check_published && !$is_published) {
$permission_provider
->getPermission("{$operation} unpublished", 'entity', 'any')
->willReturn($permission);
$permission_provider
->getPermission("{$operation} unpublished", 'entity', 'own')
->willReturn($own_permission);
}
$manager = $this
->prophesize(GroupContentEnablerManagerInterface::class);
$manager
->hasHandler($plugin_id, 'permission_provider')
->willReturn(TRUE);
$manager
->getPermissionProvider($plugin_id)
->willReturn($permission_provider
->reveal());
$this->container
->get('plugin.manager.group_content_enabler')
->willReturn($manager
->reveal());
$access_control_handler = GroupContentAccessControlHandler::createInstance($this->container
->reveal(), $plugin_id, $definition);
$account_id = rand(1, 100);
$account = $this
->prophesize(AccountInterface::class);
$account
->id()
->willReturn($account_id);
$account = $account
->reveal();
$entity_type = $this
->prophesize(EntityTypeInterface::class);
$entity_type
->entityClassImplements(EntityPublishedInterface::class)
->willReturn($is_publishable);
$entity_type
->entityClassImplements(EntityOwnerInterface::class)
->willReturn($is_ownable);
$entity_type = $entity_type
->reveal();
$entity = $this
->prophesize(ContentEntityInterface::class);
$entity
->willImplement(EntityOwnerInterface::class);
if ($is_publishable) {
$entity
->willImplement(EntityPublishedInterface::class);
$entity
->isPublished()
->willReturn($is_published);
}
$entity
->getOwnerId()
->willReturn($is_owner ? $account_id : $account_id + 1);
$entity
->getEntityType()
->willReturn($entity_type);
$entity
->getCacheContexts()
->willReturn([]);
$entity
->getCachetags()
->willReturn([
'some_entity:foo',
]);
$entity
->getCacheMaxAge()
->willReturn(9999);
$entity = $entity
->reveal();
if (!$is_grouped) {
$storage
->loadByEntity($entity)
->willReturn([]);
}
else {
$group = $this
->prophesize(GroupInterface::class);
$group_content = $this
->prophesize(GroupContentInterface::class);
$group_content
->getGroup()
->willReturn($group
->reveal());
$group_content_plugin = $this
->prophesize(GroupContentEnablerInterface::class);
$group_content_plugin
->getPluginId()
->willReturn('foo:baz');
$group_content
->getContentPlugin()
->willReturn($group_content_plugin
->reveal());
$group_content = $group_content
->reveal();
$group_content_2 = $this
->prophesize(GroupContentInterface::class);
$group_content_plugin_2 = $this
->prophesize(GroupContentEnablerInterface::class);
$group_content_plugin_2
->getPluginId()
->willReturn('cat:dog');
$group_content_2
->getContentPlugin()
->willReturn($group_content_plugin_2
->reveal());
$group_content_2 = $group_content_2
->reveal();
$storage
->loadByEntity($entity)
->willReturn([
1 => $group_content,
2 => $group_content_2,
]);
if ($definition['admin_permission']) {
$group
->hasPermission($definition['admin_permission'], $account)
->willReturn($has_admin_permission);
}
else {
$group
->hasPermission($definition['admin_permission'], $account)
->shouldNotBeCalled();
}
$checked_and_found_admin = $definition['admin_permission'] && $has_admin_permission;
if ($permission && !$checked_and_found_admin) {
$group
->hasPermission($permission, $account)
->willReturn($has_permission);
}
else {
$group
->hasPermission($permission, $account)
->shouldNotBeCalled();
}
$checked_and_found_any = $permission && $has_permission;
if ($own_permission && !$checked_and_found_admin && !$checked_and_found_any) {
$group
->hasPermission($own_permission, $account)
->willReturn($has_own_permission);
}
else {
$group
->hasPermission($own_permission, $account)
->shouldNotBeCalled();
}
}
$result = $access_control_handler
->entityAccess($entity, $operation, $account, TRUE);
$this
->assertEquals($expected(), $result);
}