You are here

protected function OgAccessTestBase::setUp in Organic groups 8

Overrides UnitTestCase::setUp

1 call to OgAccessTestBase::setUp()
OgAccessEntityTestBase::setUp in tests/src/Unit/OgAccessEntityTestBase.php
1 method overrides OgAccessTestBase::setUp()
OgAccessEntityTestBase::setUp in tests/src/Unit/OgAccessEntityTestBase.php

File

tests/src/Unit/OgAccessTestBase.php, line 127

Class

OgAccessTestBase
Base class for tests of the OgAccess class.

Namespace

Drupal\Tests\og\Unit

Code

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);

  // It is expected that any access check will retrieve the settings, because
  // it contains an option to give full access to to the group manager.
  $this->config = $this
    ->addCache($this
    ->prophesize(Config::class));
  $this->config
    ->get('group_manager_full_access')
    ->willReturn(FALSE);

  // Whether or not the user has access to a certain operation depends in part
  // on the 'group_manager_full_access' setting which is stored in config.
  // Since the access is cached, this means that from the point of view from
  // the caching system this access varies by the 'og.settings' config object
  // that contains this setting. It is hence expected that the cacheability
  // metadata is retrieved from the config object so it can be attached to the
  // access result object.
  $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);

  // Create a mocked test user.
  $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(),
  ]);

  // @todo Move to test.
  $this->ogRole
    ->isAdmin()
    ->willReturn(FALSE);
  $this->ogRole
    ->getPermissions()
    ->willReturn([
    'update group',
  ]);

  // Mock all dependencies for the system under test.
  $account_proxy = $this
    ->prophesize(AccountProxyInterface::class);
  $module_handler = $this
    ->prophesize(ModuleHandlerInterface::class);
  $this->permissionManager = $this
    ->prophesize(PermissionManager::class);
  $dispatcher = $this
    ->prophesize(EventDispatcherInterface::class);

  // Instantiate the system under test.
  $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());

  // This is for caching purposes only.
  $container
    ->set('current_user', $this->user
    ->reveal());
  \Drupal::setContainer($container);
}