class OgRoleManagerTest in Organic groups 8
Same name in this branch
- 8 tests/src/Unit/OgRoleManagerTest.php \Drupal\Tests\og\Unit\OgRoleManagerTest
- 8 tests/src/Kernel/OgRoleManagerTest.php \Drupal\Tests\og\Kernel\OgRoleManagerTest
Tests create membership helper function.
@group og @coversDefaultClass \Drupal\og\OgRoleManager
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\og\Unit\OgRoleManagerTest
Expanded class hierarchy of OgRoleManagerTest
File
- tests/
src/ Unit/ OgRoleManagerTest.php, line 22
Namespace
Drupal\Tests\og\UnitView source
class OgRoleManagerTest extends UnitTestCase {
/**
* The entity type ID of the test group.
*
* @var string
*/
protected $entityTypeId;
/**
* The bundle ID of the test group.
*
* @var string
*/
protected $bundle;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $entityTypeManager;
/**
* The entity storage service.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $entityStorage;
/**
* The event dispatcher service.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $eventDispatcher;
/**
* The created OG role.
*
* @var \Drupal\og\Entity\OgRole|\Prophecy\Prophecy\ObjectProphecy
*/
protected $ogRole;
/**
* The entity storage for OgRole entities.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $ogRoleStorage;
/**
* The permissions manager service.
*
* @var \Drupal\og\PermissionManagerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $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(EntityStorageInterface::class);
$this->permissionManager = $this
->prophesize(PermissionManagerInterface::class);
$this->entityTypeManager
->getStorage('og_role')
->willReturn($this->ogRoleStorage
->reveal());
}
/**
* Tests creation of per bundle roles.
*
* @param string $role_name
* The name of the role being created.
*
* @covers ::createPerBundleRoles
*
* @dataProvider bundleRolesProvider
*/
public function testCreatePerBundleRoles($role_name) {
$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)
->shouldBeCalled();
$og_role
->setGroupBundle($bundle)
->shouldBeCalled();
return $og_role
->reveal();
})
->shouldBeCalled();
// The role is expected to be saved.
$og_role
->save()
->willReturn(1)
->shouldBeCalled();
}
$og_role_manager = $this
->getOgRoleManager();
$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 function bundleRolesProvider() {
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() {
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() {
$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.
*
* @return \Drupal\og\OgRoleManagerInterface
* The initialized OG role manager.
*/
protected function getOgRoleManager() {
return new OgRoleManager($this->entityTypeManager
->reveal(), $this->eventDispatcher
->reveal(), $this->permissionManager
->reveal());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OgRoleManagerTest:: |
protected | property | The bundle ID of the test group. | |
OgRoleManagerTest:: |
protected | property | The entity storage service. | |
OgRoleManagerTest:: |
protected | property | The entity type ID of the test group. | |
OgRoleManagerTest:: |
protected | property | The entity type manager service. | |
OgRoleManagerTest:: |
protected | property | The event dispatcher service. | |
OgRoleManagerTest:: |
protected | property | The created OG role. | |
OgRoleManagerTest:: |
protected | property | The entity storage for OgRole entities. | |
OgRoleManagerTest:: |
protected | property | The permissions manager service. | |
OgRoleManagerTest:: |
public | function | Provides test data to test bundle roles creation. | |
OgRoleManagerTest:: |
protected | function | Returns the expected properties of the default role with the given name. | |
OgRoleManagerTest:: |
protected | function | Return a new OG role manager object. | |
OgRoleManagerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
OgRoleManagerTest:: |
public | function | Tests creation of per bundle roles. | |
OgRoleManagerTest:: |
public | function | Tests role removal. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |