og-8.x-1.x-dev/tests/src/Unit/OgRoleManagerTest.php
tests/src/Unit/OgRoleManagerTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\og\Unit;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\og\Entity\OgRole;
use Drupal\og\Event\DefaultRoleEvent;
use Drupal\og\Event\DefaultRoleEventInterface;
use Drupal\og\OgRoleInterface;
use Drupal\og\OgRoleManager;
use Drupal\og\OgRoleManagerInterface;
use Drupal\og\OgRoleStorage;
use Drupal\og\PermissionManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Tests create membership helper function.
*
* @group og
* @coversDefaultClass \Drupal\og\OgRoleManager
*/
class OgRoleManagerTest extends UnitTestCase {
use ProphecyTrait;
/**
* The entity type ID of the test group.
*/
protected string $entityTypeId;
/**
* The bundle ID of the test group.
*/
protected string $bundle;
/**
* The entity type manager service.
*/
protected EntityTypeManagerInterface|ObjectProphecy $entityTypeManager;
/**
* The entity storage service.
*/
protected EntityStorageInterface|ObjectProphecy $entityStorage;
/**
* The event dispatcher service.
*/
protected EventDispatcherInterface|ObjectProphecy $eventDispatcher;
/**
* The created OG role.
*/
protected OgRole|ObjectProphecy $ogRole;
/**
* The entity storage for OgRole entities.
*/
protected EntityStorageInterface|ObjectProphecy $ogRoleStorage;
/**
* The permissions manager service.
*/
protected PermissionManagerInterface|ObjectProphecy $permissionManager;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->entityTypeId = $this->randomMachineName();
$this->bundle = $this->randomMachineName();
$this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
$this->entityStorage = $this->prophesize(EntityStorageInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$this->ogRole = $this->prophesize(OgRole::class);
$this->ogRoleStorage = $this->prophesize(OgRoleStorage::class);
$this->permissionManager = $this->prophesize(PermissionManagerInterface::class);
$this
->entityTypeManager
->getStorage('og_role')
->willReturn($this->ogRoleStorage->reveal());
}
/**
* Tests creation of per bundle roles.
*
* @covers ::createPerBundleRoles
*
* @dataProvider bundleRolesProvider
*/
public function testCreatePerBundleRoles(string $role_name): void {
$entity_type_id = $this->entityTypeId;
$bundle = $this->bundle;
// The Prophecy mocking framework uses 'promises' for dynamically generating
// mocks that return context dependent data. This works by dynamically
// setting the expected behaviors in an anonymous function. Make sure the
// mocks are available in the local scope so they can be passed to the
// anonymous functions.
$permission_manager = $this->permissionManager;
// We have to use OgRole and not OgRoleInterface, due to inheritance issues,
// where PHP doesn't allow OgRoleInterface to extend RoleInterface.
$og_role = $this->ogRole;
foreach ($this->getDefaultRoleProperties() as $properties) {
// It is expected that the role will be created with default properties.
$this->ogRoleStorage->create($properties)
->will(function () use ($entity_type_id, $bundle, $role_name, $og_role, $permission_manager) {
// It is expected that the OG permissions that need to be populated on
// the new role will be requested. We are not testing permissions here
// so we can just return an empty array.
$permission_manager->getDefaultGroupPermissions($entity_type_id, $bundle, $role_name)
->willReturn([])
->shouldBeCalled();
// For each role that is created it is expected that the role name
// will be retrieved, so that the role name can be used to filter the
// permissions.
$og_role->getName()
->willReturn($role_name)
->shouldBeCalled();
// The group type, bundle and permissions will have to be set on the
// new role.
$og_role->setGroupType($entity_type_id)
->willReturn($og_role->reveal())
->shouldBeCalled();
$og_role->setGroupBundle($bundle)
->willReturn($og_role->reveal())
->shouldBeCalled();
return $og_role->reveal();
})
->shouldBeCalled();
// The role is expected to be saved.
$og_role->save()
->willReturn(1)
->shouldBeCalled();
}
$og_role_manager = $this->getOgRoleManager();
$this->ogRoleStorage->load(Argument::any())
->willReturn(NULL)
->shouldBeCalledTimes(2);
$og_roles = $og_role_manager->createPerBundleRoles($this->entityTypeId, $this->bundle);
$this->assertCount(2, $og_roles);
}
/**
* Provides test data to test bundle roles creation.
*
* @return array
* Array with the OG Role machine names.
*/
public static function bundleRolesProvider(): array {
return [
[OgRoleInterface::ANONYMOUS],
[OgRoleInterface::AUTHENTICATED],
];
}
/**
* Returns the expected properties of the default role with the given name.
*
* @return array
* The default properties.
*/
protected function getDefaultRoleProperties(): array {
return [
OgRoleInterface::ANONYMOUS => [
'role_type' => OgRoleInterface::ROLE_TYPE_REQUIRED,
'label' => 'Non-member',
'name' => OgRoleInterface::ANONYMOUS,
],
OgRoleInterface::AUTHENTICATED => [
'role_type' => OgRoleInterface::ROLE_TYPE_REQUIRED,
'label' => 'Member',
'name' => OgRoleInterface::AUTHENTICATED,
],
];
}
/**
* Tests role removal.
*
* @covers ::removeRoles
*/
public function testRoleRemoval(): void {
$properties = [
'group_type' => $this->entityTypeId,
'group_bundle' => $this->bundle,
];
$this->ogRoleStorage->loadByProperties($properties)
->willReturn([
$this->ogRole->reveal(),
$this->ogRole->reveal(),
$this->ogRole->reveal(),
])
->shouldBeCalled();
// It is expected that all roles will be deleted, so three delete() calls
// will be made.
$this->ogRole->delete()
->shouldBeCalledTimes(3);
$og_role_manager = $this->getOgRoleManager();
$og_role_manager->removeRoles($this->entityTypeId, $this->bundle);
}
/**
* Return a new OG role manager object.
*/
protected function getOgRoleManager(): OgRoleManagerInterface {
$this->eventDispatcher
->dispatch(
Argument::type(DefaultRoleEvent::class),
DefaultRoleEventInterface::EVENT_NAME,
)->willReturnArgument();
return new OgRoleManager(
$this->entityTypeManager->reveal(),
$this->eventDispatcher->reveal(),
$this->permissionManager->reveal()
);
}
}
