og-8.x-1.x-dev/tests/src/Unit/SubscriptionControllerTest.php
tests/src/Unit/SubscriptionControllerTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\og\Unit;
use Drupal\user\UserInterface;
use Prophecy\Prophecy\ObjectProphecy;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Drupal\og\Controller\SubscriptionController;
use Drupal\og\MembershipManagerInterface;
use Drupal\og\OgAccessInterface;
use Drupal\og\OgMembershipInterface;
use Drupal\user\EntityOwnerInterface;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* Tests the subscription controller.
*
* @group og
* @coversDefaultClass \Drupal\og\Controller\SubscriptionController
*/
class SubscriptionControllerTest extends UnitTestCase {
use ProphecyTrait;
/**
* The entity for builder object.
*/
protected EntityFormBuilderInterface|ObjectProphecy $entityFormBuilder;
/**
* The group entity.
*/
protected ContentEntityInterface|ObjectProphecy $group;
/**
* The membership manager service.
*/
protected MembershipManagerInterface|ObjectProphecy $membershipManager;
/**
* OG access service.
*/
protected OgAccessInterface|ObjectProphecy $ogAccess;
/**
* The mocked messenger service.
*/
protected MessengerInterface|ObjectProphecy $messenger;
/**
* The OG membership entity.
*/
protected OgMembershipInterface|ObjectProphecy $ogMembership;
/**
* The URL object.
*/
protected Url|ObjectProphecy $url;
/**
* The user entity.
*/
protected UserInterface|ObjectProphecy $user;
/**
* A user ID to use in the test.
*/
protected int $userId;
/**
* The mocked entity type manager.
*/
protected EntityTypeManagerInterface|ObjectProphecy $entityTypeManager;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$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());
$container->set('messenger', $this->messenger->reveal());
\Drupal::setContainer($container);
}
/**
* Tests non-member trying to unsubscribe from group.
*
* @covers ::unsubscribe
*/
public function testNotMember(): void {
$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(): void {
$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(string $state): void {
$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 static function memberProvider(): array {
return [
[OgMembershipInterface::STATE_ACTIVE],
[OgMembershipInterface::STATE_PENDING],
];
}
/**
* Tests group manager trying to unsubscribe from group.
*
* @covers ::unsubscribe
* @dataProvider memberProvider
*/
public function testGroupManager(string $state): void {
$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(): void {
$controller = new SubscriptionController($this->ogAccess->reveal());
$controller->unsubscribe($this->group->reveal());
}
}
