You are here

class SubscriptionControllerTest in Organic groups 8

Tests the subscription controller.

@group og @coversDefaultClass \Drupal\og\Controller\SubscriptionController

Hierarchy

Expanded class hierarchy of SubscriptionControllerTest

File

tests/src/Unit/SubscriptionControllerTest.php, line 28

Namespace

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

  /**
   * The entity for builder object.
   *
   * @var \Drupal\Core\Entity\EntityFormBuilderInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityFormBuilder;

  /**
   * The group entity.
   *
   * @var \Drupal\Core\Entity\ContentEntityInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $group;

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

  /**
   * OG access service.
   *
   * @var \Drupal\og\OgAccessInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $ogAccess;

  /**
   * The mocked messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $messenger;

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

  /**
   * The URL object.
   *
   * @var \Drupal\Core\Url|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $url;

  /**
   * The user entity.
   *
   * @var \Drupal\user\UserInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $user;

  /**
   * A user ID to use in the test.
   *
   * @var int
   */
  protected $userId;

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->entityFormBuilder = $this
      ->prophesize(EntityFormBuilderInterface::class);
    $this->group = $this
      ->prophesize(ContentEntityInterface::class);
    $this->membershipManager = $this
      ->prophesize(MembershipManagerInterface::class);
    $this->ogAccess = $this
      ->prophesize(OgAccessInterface::class);
    $this->messenger = $this
      ->prophesize(MessengerInterface::class);
    $this->ogMembership = $this
      ->prophesize(OgMembershipInterface::class);
    $this->url = $this
      ->prophesize(Url::class);
    $this->user = $this
      ->prophesize(AccountInterface::class);
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->userId = rand(20, 50);
    $this->user
      ->id()
      ->willReturn($this->userId);

    // Set the container for the string translation service.
    $container = new ContainerBuilder();
    $container
      ->set('current_user', $this->user
      ->reveal());
    $container
      ->set('entity.form_builder', $this->entityFormBuilder
      ->reveal());
    $container
      ->set('og.membership_manager', $this->membershipManager
      ->reveal());
    $container
      ->set('string_translation', $this
      ->getStringTranslationStub());
    $container
      ->set('entity_type.manager', $this->entityTypeManager
      ->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * Tests non-member trying to unsubscribe from group.
   *
   * @covers ::unsubscribe
   */
  public function testNotMember() {
    $states = [
      OgMembershipInterface::STATE_ACTIVE,
      OgMembershipInterface::STATE_PENDING,
      OgMembershipInterface::STATE_BLOCKED,
    ];
    $this->membershipManager
      ->getMembership($this->group
      ->reveal(), $this->userId, $states)
      ->willReturn(NULL);
    $this
      ->expectException(AccessDeniedHttpException::class);
    $this
      ->unsubscribe();
  }

  /**
   * Tests blocked member trying to unsubscribe from group.
   *
   * @covers ::unsubscribe
   */
  public function testBlockedMember() {
    $states = [
      OgMembershipInterface::STATE_ACTIVE,
      OgMembershipInterface::STATE_PENDING,
      OgMembershipInterface::STATE_BLOCKED,
    ];
    $this->membershipManager
      ->getMembership($this->group
      ->reveal(), $this->userId, $states)
      ->willReturn($this->ogMembership
      ->reveal());
    $this->ogMembership
      ->getState()
      ->willReturn(OgMembershipInterface::STATE_BLOCKED);
    $this
      ->expectException(AccessDeniedHttpException::class);
    $this
      ->unsubscribe();
  }

  /**
   * Tests active and pending members trying to unsubscribe from group.
   *
   * @covers ::unsubscribe
   * @dataProvider memberProvider
   */
  public function testMember($state) {
    $states = [
      OgMembershipInterface::STATE_ACTIVE,
      OgMembershipInterface::STATE_PENDING,
      OgMembershipInterface::STATE_BLOCKED,
    ];
    $this->membershipManager
      ->getMembership($this->group
      ->reveal(), $this->userId, $states)
      ->willReturn($this->ogMembership
      ->reveal());
    $this->ogMembership
      ->getState()
      ->willReturn($state);
    $this->entityFormBuilder
      ->getForm($this->ogMembership
      ->reveal(), 'unsubscribe')
      ->shouldBeCalled();
    $this
      ->unsubscribe();
  }

  /**
   * Provides test data to test members unsubscribe.
   *
   * @return array
   *   Array with the membership state.
   */
  public function memberProvider() {
    return [
      [
        OgMembershipInterface::STATE_ACTIVE,
      ],
      [
        OgMembershipInterface::STATE_PENDING,
      ],
    ];
  }

  /**
   * Tests group manager trying to unsubscribe from group.
   *
   * @covers ::unsubscribe
   * @dataProvider memberProvider
   */
  public function testGroupManager($state) {
    $states = [
      OgMembershipInterface::STATE_ACTIVE,
      OgMembershipInterface::STATE_PENDING,
      OgMembershipInterface::STATE_BLOCKED,
    ];
    $this->group
      ->willImplement(EntityOwnerInterface::class);
    $this->membershipManager
      ->getMembership($this->group
      ->reveal(), $this->userId, $states)
      ->willReturn($this->ogMembership
      ->reveal());
    $this->ogMembership
      ->getState()
      ->willReturn($state);
    $this->group
      ->getOwnerId()
      ->willReturn($this->userId);
    $this->group
      ->label()
      ->shouldBeCalled();
    $this->group
      ->toUrl()
      ->willReturn($this->url
      ->reveal());
    $this->url
      ->setAbsolute()
      ->willReturn($this->url
      ->reveal());
    $this->url
      ->toString()
      ->willReturn($this
      ->randomMachineName());
    $this->entityFormBuilder
      ->getForm($this->ogMembership
      ->reveal(), 'unsubscribe')
      ->shouldNotBeCalled();
    $this
      ->unsubscribe();
  }

  /**
   * Invoke the unsubscribe method.
   */
  protected function unsubscribe() {
    $controller = new SubscriptionController($this->ogAccess
      ->reveal(), $this->messenger
      ->reveal(), $this->entityTypeManager
      ->reveal());
    $controller
      ->unsubscribe($this->group
      ->reveal());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SubscriptionControllerTest::$entityFormBuilder protected property The entity for builder object.
SubscriptionControllerTest::$entityTypeManager protected property The mocked entity type manager.
SubscriptionControllerTest::$group protected property The group entity.
SubscriptionControllerTest::$membershipManager protected property The membership manager service.
SubscriptionControllerTest::$messenger protected property The mocked messenger service.
SubscriptionControllerTest::$ogAccess protected property OG access service.
SubscriptionControllerTest::$ogMembership protected property The OG membership entity.
SubscriptionControllerTest::$url protected property The URL object.
SubscriptionControllerTest::$user protected property The user entity.
SubscriptionControllerTest::$userId protected property A user ID to use in the test.
SubscriptionControllerTest::memberProvider public function Provides test data to test members unsubscribe.
SubscriptionControllerTest::setUp protected function Overrides UnitTestCase::setUp
SubscriptionControllerTest::testBlockedMember public function Tests blocked member trying to unsubscribe from group.
SubscriptionControllerTest::testGroupManager public function Tests group manager trying to unsubscribe from group.
SubscriptionControllerTest::testMember public function Tests active and pending members trying to unsubscribe from group.
SubscriptionControllerTest::testNotMember public function Tests non-member trying to unsubscribe from group.
SubscriptionControllerTest::unsubscribe protected function Invoke the unsubscribe method.
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.