View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Unit;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\MembershipManagerInterface;
use Drupal\og\OgAccessInterface;
use Drupal\og\OgMembershipInterface;
use Drupal\og\Plugin\Field\FieldFormatter\GroupSubscribeFormatter;
use Drupal\user\EntityOwnerInterface;
class GroupSubscribeFormatterTest extends UnitTestCase {
protected $entityStorage;
protected $entityTypeManager;
protected $entityTypeRepository;
protected $user;
protected $entityTypeId;
protected $bundle;
protected $group;
protected $membership;
protected $fieldItemList;
protected $groupTypeManager;
protected $fieldDefinitionInterface;
protected $ogAccess;
protected $membershipManager;
protected $accountProxy;
protected $entityId;
protected $userId;
protected $accessResult;
protected function setUp() : void {
parent::setUp();
$this->accessResult = $this
->prophesize(AccessResultInterface::class);
$this->accountProxy = $this
->prophesize(AccountProxyInterface::class);
$this->bundle = $this
->randomMachineName();
$this->entityId = rand(10, 50);
$this->entityStorage = $this
->prophesize(EntityStorageInterface::class);
$this->entityTypeId = $this
->randomMachineName();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityTypeRepository = $this
->prophesize(EntityTypeRepositoryInterface::class);
$this->fieldDefinitionInterface = $this
->prophesize(FieldDefinitionInterface::class);
$this->fieldItemList = $this
->prophesize(FieldItemListInterface::class);
$this->group = $this
->prophesize(EntityInterface::class);
$this->groupTypeManager = $this
->prophesize(GroupTypeManagerInterface::class);
$this->membershipManager = $this
->prophesize(MembershipManagerInterface::class);
$this->ogAccess = $this
->prophesize(OgAccessInterface::class);
$this->user = $this
->prophesize(AccountInterface::class);
$this->userId = rand(10, 50);
$this->fieldItemList
->getEntity()
->willReturn($this->group);
$this->group
->willImplement(EntityOwnerInterface::class);
$this->group
->getEntityTypeId()
->willReturn($this->entityTypeId);
$this->group
->bundle()
->willReturn($this->bundle);
$this->group
->id()
->willReturn($this->entityId);
$this->groupTypeManager
->isGroup($this->entityTypeId, $this->bundle)
->willReturn(TRUE);
$this->entityTypeManager
->getStorage('user')
->willReturn($this->entityStorage
->reveal());
$this->accountProxy
->id()
->willReturn($this->userId);
$this->entityTypeRepository
->getEntityTypeFromClass('Drupal\\user\\Entity\\User')
->willReturn('user');
$this->entityStorage
->load($this->userId)
->willReturn($this->user
->reveal());
$this->user
->isAuthenticated()
->willReturn(TRUE);
$this->user
->id()
->willReturn($this->userId);
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityTypeManager
->reveal());
$container
->set('entity_type.repository', $this->entityTypeRepository
->reveal());
$container
->set('og.membership_manager', $this->membershipManager
->reveal());
$container
->set('string_translation', $this
->getStringTranslationStub());
\Drupal::setContainer($container);
}
public function testGroupOwner() {
$this->group
->getOwnerId()
->willReturn($this->userId);
$elements = $this
->getElements();
$this
->assertEquals('You are the group manager', $elements[0]['#value']);
}
public function testGroupMemberActive() {
$this->group
->getOwnerId()
->willReturn(rand(100, 200));
$this->ogAccess
->userAccess($this->group
->reveal(), 'subscribe without approval', $this->user
->reveal())
->willReturn($this->accessResult
->reveal());
$this->accessResult
->isAllowed()
->willReturn(TRUE);
$elements = $this
->getElements();
$this
->assertEquals('Subscribe to group', $elements[0]['#title']);
}
public function testSubscribeWithoutApprovalPermission() {
$this->group
->getOwnerId()
->willReturn(rand(100, 200));
$this->ogAccess
->userAccess($this->group
->reveal(), 'subscribe without approval', $this->user
->reveal())
->willReturn($this->accessResult
->reveal());
$this->accessResult
->isAllowed()
->willReturn(TRUE);
$elements = $this
->getElements();
$this
->assertEquals('Subscribe to group', $elements[0]['#title']);
}
public function testSubscribeWithApprovalPermission() {
$this->group
->getOwnerId()
->willReturn(rand(100, 200));
$this->ogAccess
->userAccess($this->group
->reveal(), 'subscribe without approval', $this->user
->reveal())
->willReturn($this->accessResult
->reveal());
$this->accessResult
->isAllowed()
->willReturn(FALSE);
$access_result = $this
->prophesize(AccessResultInterface::class);
$this->ogAccess
->userAccess($this->group
->reveal(), 'subscribe', $this->user
->reveal())
->willReturn($access_result
->reveal());
$access_result
->isAllowed()
->willReturn(TRUE);
$elements = $this
->getElements();
$this
->assertEquals('Request group membership', $elements[0]['#title']);
}
public function testNoSubscribePermission() {
$this->group
->getOwnerId()
->willReturn(rand(100, 200));
foreach ([
'subscribe without approval',
'subscribe',
] as $perm) {
$this->ogAccess
->userAccess($this->group
->reveal(), $perm, $this->user
->reveal())
->willReturn($this->accessResult
->reveal());
}
$this->accessResult
->isAllowed()
->willReturn(FALSE);
$elements = $this
->getElements();
$this
->assertStringStartsWith('This is a closed group.', $elements[0]['#value']
->render());
}
public function testBlockedMember() {
$this->group
->getOwnerId()
->willReturn(rand(100, 200));
$this->membershipManager
->isMember($this->group
->reveal(), $this->user
->reveal()
->id(), [
OgMembershipInterface::STATE_BLOCKED,
])
->willReturn(TRUE);
$elements = $this
->getElements();
$this
->assertTrue(empty($elements[0]));
}
public function testMember() {
$this->group
->getOwnerId()
->willReturn(rand(100, 200));
$this->membershipManager
->isMember($this->group
->reveal(), $this->user
->reveal()
->id(), [
OgMembershipInterface::STATE_BLOCKED,
])
->willReturn(FALSE);
$this->membershipManager
->isMember($this->group
->reveal(), $this->user
->reveal()
->id(), [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
])
->willReturn(TRUE);
$elements = $this
->getElements();
$this
->assertEquals('Unsubscribe from group', $elements[0]['#title']);
}
protected function getElements() {
$formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface
->reveal(), [], '', '', [], $this->accountProxy
->reveal(), $this->ogAccess
->reveal(), $this->entityTypeManager
->reveal());
return $formatter
->viewElements($this->fieldItemList
->reveal(), $this
->randomMachineName());
}
}