You are here

class OgAccessTestBase in Organic groups 8

Base class for tests of the OgAccess class.

Hierarchy

Expanded class hierarchy of OgAccessTestBase

File

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

Namespace

Drupal\Tests\og\Unit
View source
class OgAccessTestBase extends UnitTestCase {

  /**
   * The mocked config handler.
   *
   * @var \Drupal\Core\Config\Config|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $config;

  /**
   * A mocked test user.
   *
   * @var \Drupal\user\UserInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $user;

  /**
   * The ID of the test group.
   *
   * @var string
   */
  protected $groupId;

  /**
   * The entity type ID of the test group.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The bundle ID of the test group.
   *
   * @var string
   */
  protected $bundle;

  /**
   * The mocked test group.
   *
   * @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $group;

  /**
   * The mocked group manager.
   *
   * @var \Drupal\og\GroupTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $groupTypeManager;

  /**
   * The mocked permission manager.
   *
   * @var \Drupal\og\PermissionManager|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $permissionManager;

  /**
   * The OgAccess class, this is the system under test.
   *
   * @var \Drupal\og\OgAccessInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $ogAccess;

  /**
   * The group membership manager service.
   *
   * @var \Drupal\og\MembershipManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $membershipManager;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityTypeManager;

  /**
   * The membership entity.
   *
   * @var \Drupal\og\OgMembershipInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $membership;

  /**
   * The OG role.
   *
   * @var \Drupal\og\OgRoleInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $ogRole;

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * Returns a mocked test group.
   *
   * @param bool $is_owner
   *   Whether or not this test group should be owned by the test user which is
   *   used in the test.
   *
   * @return \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy
   *   The test group.
   */
  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);

      // Our test user is hardcoded to have UID 2.
      $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);
  }

  /**
   * Mocks the cache methods.
   */
  protected function addCache($prophecy) {
    $prophecy
      ->getCacheContexts()
      ->willReturn([]);
    $prophecy
      ->getCacheTags()
      ->willReturn([]);
    $prophecy
      ->getCacheMaxAge()
      ->willReturn(0);
    return $prophecy;
  }

  /**
   * Provides group level permissions to use in access tests.
   *
   * @return array
   *   An array of test permissions.
   */
  public function permissionsProvider() {
    return [
      // In the unit tests we don't really care about the permission name - it
      // can be an arbitrary string; except for
      // OgAccessTest::testUserAccessAdminPermission test which checks for
      // "administer group" permission.
      [
        OgAccess::UPDATE_GROUP_PERMISSION,
      ],
      [
        OgAccess::ADMINISTER_GROUP_PERMISSION,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgAccessTestBase::$bundle protected property The bundle ID of the test group.
OgAccessTestBase::$config protected property The mocked config handler.
OgAccessTestBase::$entityTypeId protected property The entity type ID of the test group.
OgAccessTestBase::$entityTypeManager protected property The entity type manager service.
OgAccessTestBase::$group protected property The mocked test group.
OgAccessTestBase::$groupId protected property The ID of the test group.
OgAccessTestBase::$groupTypeManager protected property The mocked group manager.
OgAccessTestBase::$membership protected property The membership entity.
OgAccessTestBase::$membershipManager protected property The group membership manager service.
OgAccessTestBase::$ogAccess protected property The OgAccess class, this is the system under test.
OgAccessTestBase::$ogRole protected property The OG role.
OgAccessTestBase::$permissionManager protected property The mocked permission manager.
OgAccessTestBase::$user protected property A mocked test user.
OgAccessTestBase::addCache protected function Mocks the cache methods.
OgAccessTestBase::groupEntity protected function Returns a mocked test group.
OgAccessTestBase::permissionsProvider public function Provides group level permissions to use in access tests.
OgAccessTestBase::setUp protected function Overrides UnitTestCase::setUp 1
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.