View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Unit;
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 Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class SubscriptionControllerTest extends UnitTestCase {
protected $entityFormBuilder;
protected $group;
protected $membershipManager;
protected $ogAccess;
protected $messenger;
protected $ogMembership;
protected $url;
protected $user;
protected $userId;
protected $entityTypeManager;
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);
$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);
}
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();
}
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();
}
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();
}
public function memberProvider() {
return [
[
OgMembershipInterface::STATE_ACTIVE,
],
[
OgMembershipInterface::STATE_PENDING,
],
];
}
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();
}
protected function unsubscribe() {
$controller = new SubscriptionController($this->ogAccess
->reveal(), $this->messenger
->reveal(), $this->entityTypeManager
->reveal());
$controller
->unsubscribe($this->group
->reveal());
}
}