OgAccessTestBase.php in Organic groups 8
File
tests/src/Unit/OgAccessTestBase.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Unit;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\MembershipManagerInterface;
use Drupal\og\OgAccess;
use Drupal\og\OgMembershipInterface;
use Drupal\og\PermissionManager;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\RoleInterface;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class OgAccessTestBase extends UnitTestCase {
protected $config;
protected $user;
protected $groupId;
protected $entityTypeId;
protected $bundle;
protected $group;
protected $groupTypeManager;
protected $permissionManager;
protected $ogAccess;
protected $membershipManager;
protected $entityTypeManager;
protected $membership;
protected $ogRole;
protected function setUp() : void {
$this->groupId = $this
->randomMachineName();
$this->entityTypeId = $this
->randomMachineName();
$this->bundle = $this
->randomMachineName();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->membership = $this
->prophesize(OgMembershipInterface::class);
$this->ogRole = $this
->prophesize(RoleInterface::class);
$this->groupTypeManager = $this
->prophesize(GroupTypeManagerInterface::class);
$this->groupTypeManager
->isGroup($this->entityTypeId, $this->bundle)
->willReturn(TRUE);
$cache_contexts_manager = $this
->prophesize(CacheContextsManager::class);
$cache_contexts_manager
->assertValidTokens(Argument::any())
->willReturn(TRUE);
$this->config = $this
->addCache($this
->prophesize(Config::class));
$this->config
->get('group_manager_full_access')
->willReturn(FALSE);
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('og.settings')
->willReturn($this->config);
$this->config
->getCacheContexts()
->willReturn([]);
$this->config
->getCacheTags()
->willReturn([]);
$this->config
->getCacheMaxAge()
->willReturn(0);
$user_id = 2;
$this->user = $this
->prophesize(AccountInterface::class);
$this->user
->isAuthenticated()
->willReturn(TRUE);
$this->user
->id()
->willReturn($user_id);
$this->user
->hasPermission('administer organic groups')
->willReturn(FALSE);
$this->group = $this
->groupEntity()
->reveal();
$this->membershipManager = $this
->prophesize(MembershipManagerInterface::class);
$this->membershipManager
->getMembership($this->group, $user_id)
->willReturn($this->membership
->reveal());
$this->membershipManager
->getMembership($this->group, $user_id, [
OgMembershipInterface::STATE_ACTIVE,
])
->willReturn($this->membership
->reveal());
$this->membershipManager
->getGroupCount(Argument::any())
->willReturn(1);
$this->membership
->getRoles()
->willReturn([
$this->ogRole
->reveal(),
]);
$this->ogRole
->isAdmin()
->willReturn(FALSE);
$this->ogRole
->getPermissions()
->willReturn([
'update group',
]);
$account_proxy = $this
->prophesize(AccountProxyInterface::class);
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$this->permissionManager = $this
->prophesize(PermissionManager::class);
$dispatcher = $this
->prophesize(EventDispatcherInterface::class);
$this->ogAccess = new OgAccess($config_factory
->reveal(), $account_proxy
->reveal(), $module_handler
->reveal(), $this->groupTypeManager
->reveal(), $this->permissionManager
->reveal(), $this->membershipManager
->reveal(), $dispatcher
->reveal());
$container = new ContainerBuilder();
$container
->set('cache_contexts_manager', $cache_contexts_manager
->reveal());
$container
->set('config.factory', $config_factory
->reveal());
$container
->set('entity_type.manager', $this->entityTypeManager
->reveal());
$container
->set('module_handler', $this
->prophesize(ModuleHandlerInterface::class)
->reveal());
$container
->set('og.group_type_manager', $this->groupTypeManager
->reveal());
$container
->set('og.membership_manager', $this->membershipManager
->reveal());
$container
->set('current_user', $this->user
->reveal());
\Drupal::setContainer($container);
}
protected function groupEntity($is_owner = FALSE) {
$entity_type = $this
->prophesize(EntityTypeInterface::class);
$entity_type
->id()
->willReturn($this->entityTypeId);
$group_entity = $this
->prophesize(EntityInterface::class);
if ($is_owner) {
$group_entity
->willImplement(EntityOwnerInterface::class);
$group_entity
->getOwnerId()
->willReturn(2);
}
$group_entity
->getEntityType()
->willReturn($entity_type);
$group_entity
->getEntityTypeId()
->willReturn($this->entityTypeId);
$group_entity
->bundle()
->willReturn($this->bundle);
$group_entity
->id()
->willReturn($this->groupId);
return $this
->addCache($group_entity);
}
protected function addCache($prophecy) {
$prophecy
->getCacheContexts()
->willReturn([]);
$prophecy
->getCacheTags()
->willReturn([]);
$prophecy
->getCacheMaxAge()
->willReturn(0);
return $prophecy;
}
public function permissionsProvider() {
return [
[
OgAccess::UPDATE_GROUP_PERMISSION,
],
[
OgAccess::ADMINISTER_GROUP_PERMISSION,
],
];
}
}