You are here

class CreateMembershipTest in Organic groups 8

Tests create membership helper function.

@group og @coversDefaultClass \Drupal\og\Og

Hierarchy

Expanded class hierarchy of CreateMembershipTest

File

tests/src/Unit/CreateMembershipTest.php, line 26

Namespace

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

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

  /**
   * The mocked entity type repository.
   *
   * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityTypeRepository;

  /**
   * The OG group audience helper.
   *
   * @var \Drupal\og\OgGroupAudienceHelperInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $groupAudienceHelper;

  /**
   * The mocked memory cache backend.
   *
   * @var \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $staticCache;

  /**
   * The entity storage prophecy used in the test.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityStorage;

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

  /**
   * 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 test OG membership.
   *
   * @var \Drupal\og\OgMembershipInterface
   */
  protected $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->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);

    // 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() {
    $membership_manager = new MembershipManager($this->entityTypeManager
      ->reveal(), $this->groupAudienceHelper
      ->reveal(), $this->staticCache
      ->reveal());
    $membership = $membership_manager
      ->createMembership($this->group
      ->reveal(), $this->user
      ->reveal());
    $this
      ->assertInstanceOf(OgMembershipInterface::class, $membership);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CreateMembershipTest::$bundle protected property The bundle ID of the test group.
CreateMembershipTest::$entityStorage protected property The entity storage prophecy used in the test.
CreateMembershipTest::$entityTypeId protected property The entity type ID of the test group.
CreateMembershipTest::$entityTypeManager protected property The entity type manager.
CreateMembershipTest::$entityTypeRepository protected property The mocked entity type repository.
CreateMembershipTest::$group protected property The mocked test group.
CreateMembershipTest::$groupAudienceHelper protected property The OG group audience helper.
CreateMembershipTest::$membership protected property The mocked test OG membership.
CreateMembershipTest::$staticCache protected property The mocked memory cache backend.
CreateMembershipTest::$user protected property A mocked test user.
CreateMembershipTest::setUp protected function Overrides UnitTestCase::setUp
CreateMembershipTest::testNewGroup public function Tests creating membership for an un-saved group.
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.