You are here

protected function OgAccessEntityTestBase::setUp in Organic groups 8

Overrides OgAccessTestBase::setUp

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

File

tests/src/Unit/OgAccessEntityTestBase.php, line 32

Class

OgAccessEntityTestBase
OG access entity base class.

Namespace

Drupal\Tests\og\Unit

Code

protected function setUp() : void {
  parent::setUp();

  // Mock a group content entity.
  $entity_type_id = $this
    ->randomMachineName();
  $bundle = $this
    ->randomMachineName();

  // Just a random entity ID.
  $entity_id = 20;
  $entity_type = $this
    ->prophesize(EntityTypeInterface::class);
  $entity_type
    ->getListCacheTags()
    ->willReturn([]);
  $entity_type
    ->entityClassImplements(FieldableEntityInterface::class)
    ->willReturn(TRUE);
  $entity_type
    ->id()
    ->willReturn($entity_type_id);
  $this->groupContentEntity = $this
    ->prophesize(ContentEntityInterface::class);
  $this->groupContentEntity
    ->id()
    ->willReturn($entity_id);
  $this->groupContentEntity
    ->bundle()
    ->willReturn($bundle);
  $this->groupContentEntity
    ->isNew()
    ->willReturn(FALSE);
  $this->groupContentEntity
    ->getEntityType()
    ->willReturn($entity_type
    ->reveal());
  $this->groupContentEntity
    ->getEntityTypeId()
    ->willReturn($entity_type_id);
  $this
    ->addCache($this->groupContentEntity);

  // If the group type manager is asked if the group content entity is group
  // content, it is expected that this will return TRUE.
  $this->groupTypeManager
    ->isGroupContent($entity_type_id, $bundle)
    ->willReturn(TRUE);

  // It is expected that a list of entity operation permissions is retrieved
  // from the permission manager so that the passed in permission can be
  // checked against this list. Our permissions are not in the list, so it is
  // of no importance what we return here, an empty array is sufficient.
  $this->permissionManager
    ->getDefaultEntityOperationPermissions($this->entityTypeId, $this->bundle, [
    $entity_type_id => [
      $bundle,
    ],
  ])
    ->willReturn([]);

  // The group manager is expected to declare that this is not a group.
  $this->groupTypeManager
    ->isGroup($entity_type_id, $bundle)
    ->willReturn(FALSE);

  // Mock retrieval of field definitions.
  $field_definition = $this
    ->prophesize(FieldDefinitionInterface::class);
  $field_definition
    ->getType()
    ->willReturn(OgGroupAudienceHelperInterface::GROUP_REFERENCE);
  $field_definition
    ->getFieldStorageDefinition()
    ->willReturn($this
    ->prophesize(FieldStorageDefinitionInterface::class)
    ->reveal());
  $field_definition
    ->getSetting('handler_settings')
    ->willReturn([]);
  $field_definition
    ->getName()
    ->willReturn($this
    ->randomMachineName());
  $entity_field_manager = $this
    ->prophesize(EntityFieldManagerInterface::class);
  $entity_field_manager
    ->getFieldDefinitions($entity_type_id, $bundle)
    ->willReturn([
    $field_definition
      ->reveal(),
  ]);
  $group_type_id = $this->group
    ->getEntityTypeId();
  $storage = $this
    ->prophesize(EntityStorageInterface::class);
  $entity_type_manager = $this
    ->prophesize(EntityTypeManagerInterface::class);
  $entity_type_manager
    ->getDefinition($entity_type_id)
    ->willReturn($entity_type
    ->reveal());
  $entity_type_manager
    ->getStorage($group_type_id)
    ->willReturn($storage
    ->reveal());
  $container = \Drupal::getContainer();
  $container
    ->set('entity_type.manager', $entity_type_manager
    ->reveal());
  $container
    ->set('entity_field.manager', $entity_field_manager
    ->reveal());
}