public function OgMembershipTest::testSaveMembershipWithInvalidRoles in Organic groups 8
Tests an exception is thrown when saving a membership with invalid roles.
@covers ::save @dataProvider saveMembershipWithInvalidRolesProvider
Parameters
array $roles_metadata: An array of test role metadata.
File
- tests/
src/ Kernel/ Entity/ OgMembershipTest.php, line 448
Class
- OgMembershipTest
- Tests the OgMembership entity.
Namespace
Drupal\Tests\og\Kernel\EntityCode
public function testSaveMembershipWithInvalidRoles(array $roles_metadata) : void {
$test_groups = [
'test_group' => $this->group,
];
// Create a second test group that has the same entity type but a different
// bundle so we can test that roles created for this group will throw an
// exception.
$group = EntityTest::create([
'type' => 'a_different_group_bundle',
'name' => $this
->randomString(),
]);
$group
->save();
$this->groupTypeManager
->addGroup($group
->getEntityTypeId(), $group
->bundle());
$test_groups['different_bundle'] = $group;
// Create a third test group that has a different entity type but the same
// bundle so we can test that roles created for this group will throw an
// exception.
$group = EntityTestMul::create([
'type' => 'test_bundle',
'name' => $this
->randomString(),
]);
$group
->save();
$this->groupTypeManager
->addGroup($group
->getEntityTypeId(), $group
->bundle());
$test_groups['different_entity_type'] = $group;
// Create the roles as defined in the test case.
$roles = [];
foreach ($roles_metadata as $role_metadata) {
$group = $test_groups[$role_metadata['group']];
$role = OgRole::loadByGroupAndName($group, $role_metadata['role_name']);
// Create the role if it was not created automatically.
if (empty($role)) {
$role = OgRole::create([
'group_type' => $group
->getEntityTypeId(),
'group_bundle' => $group
->bundle(),
'name' => $role_metadata['role_name'],
]);
$role
->save();
}
$roles[] = $role;
}
// Create a membership with the test group and roles. This should throw an
// exception since the roles are invalid.
$this
->expectException(EntityStorageException::class);
OgMembership::create()
->setOwner($this->user)
->setGroup($this->group)
->setRoles($roles)
->save();
}