og-8.x-1.x-dev/tests/src/Unit/CreateMembershipTest.php

tests/src/Unit/CreateMembershipTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\og\Unit;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Prophecy\Prophecy\ObjectProphecy;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\og\MembershipManager;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\OgGroupAudienceHelperInterface;
use Drupal\og\OgMembershipInterface;
use Drupal\user\UserInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

/**
 * Tests create membership helper function.
 *
 * @group og
 * @coversDefaultClass \Drupal\og\Og
 */
class CreateMembershipTest extends UnitTestCase {

  use ProphecyTrait;

  /**
   * The entity type manager.
   */
  protected EntityTypeManagerInterface|ObjectProphecy $entityTypeManager;

  /**
   * The group type manager.
   */
  protected GroupTypeManagerInterface|ObjectProphecy $groupTypeManager;

  /**
   * The mocked entity type repository.
   */
  protected EntityTypeRepositoryInterface|ObjectProphecy $entityTypeRepository;

  /**
   * The OG group audience helper.
   */
  protected OgGroupAudienceHelperInterface|ObjectProphecy $groupAudienceHelper;

  /**
   * The mocked memory cache backend.
   */
  protected MemoryCacheInterface|ObjectProphecy $staticCache;

  /**
   * The entity storage prophecy used in the test.
   */
  protected EntityStorageInterface|ObjectProphecy $entityStorage;

  /**
   * A mocked test user.
   */
  protected AccountInterface|ObjectProphecy $user;

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

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

  /**
   * The mocked test group.
   */
  protected EntityInterface|ObjectProphecy $group;

  /**
   * The mocked test OG membership.
   */
  protected OgMembershipInterface $membership;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->entityTypeId = $this->randomMachineName();
    $this->bundle = $this->randomMachineName();

    $this->entityStorage = $this->prophesize(EntityStorageInterface::class);
    $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
    $this->groupTypeManager = $this->prophesize(GroupTypeManagerInterface::class);
    $this->entityTypeRepository = $this->prophesize(EntityTypeRepositoryInterface::class);
    $this->groupAudienceHelper = $this->prophesize(OgGroupAudienceHelperInterface::class);
    $this->staticCache = $this->prophesize(MemoryCacheInterface::class);

    $this->entityTypeManager->getStorage('og_membership')
      ->willReturn($this->entityStorage->reveal());

    $this->entityTypeRepository->getEntityTypeFromClass('Drupal\og\Entity\OgMembership')
      ->willReturn('og_membership');

    // Create a mocked Og Membership entity.
    /** @var \Drupal\og\OgMembershipInterface|\Prophecy\Prophecy\ObjectProphecy $membership_entity */
    $membership_entity = $this->prophesize(OgMembershipInterface::class);

    $this->entityStorage
      ->create(Argument::type('array'))
      ->willReturn($membership_entity->reveal());

    // Create a mocked test group.
    $this->group = $this->prophesize(ContentEntityInterface::class);
    $this->group->getEntityTypeId()->willReturn($this->entityTypeId);
    $this->group->bundle()->willReturn($this->bundle);

    $this->groupTypeManager->getGroupDefaultMembershipType($this->entityTypeId, $this->bundle)
      ->willReturn('default');

    $this->group->getEntityTypeId()
      ->willReturn($this->entityTypeId);

    $this->group->bundle()
      ->willReturn($this->bundle);

    // Create a mocked test user.
    $this->user = $this->prophesize(UserInterface::class);

    $membership_entity
      ->setOwner($this->user)
      ->willReturn($membership_entity->reveal());

    $membership_entity
      ->setGroup($this->group)
      ->willReturn($membership_entity->reveal());

    $container = new ContainerBuilder();
    $container->set('entity_type.manager', $this->entityTypeManager->reveal());
    $container->set('entity_type.repository', $this->entityTypeRepository->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * Tests creating membership for an un-saved group.
   *
   * @covers ::createMembership
   */
  public function testNewGroup(): void {
    $membership_manager = new MembershipManager($this->entityTypeManager->reveal(), $this->groupAudienceHelper->reveal(), $this->groupTypeManager->reveal(), $this->staticCache->reveal());
    $membership = $membership_manager->createMembership($this->group->reveal(), $this->user->reveal());
    $this->assertInstanceOf(OgMembershipInterface::class, $membership);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc