You are here

public function OgMembershipRoleReferenceTest::testRoleCreate in Organic groups 8

Testing OG membership role referencing.

File

tests/src/Kernel/Entity/OgMembershipRoleReferenceTest.php, line 90

Class

OgMembershipRoleReferenceTest
Test OG membership referencing to OG role creation.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testRoleCreate() {

  // Creating a content editor role.
  $content_editor = OgRole::create();
  $content_editor
    ->setGroupType('node')
    ->setGroupBundle($this->groupBundle)
    ->setName('content_editor')
    ->setLabel('Content editor')
    ->grantPermission(OgAccess::ADMINISTER_GROUP_PERMISSION);
  $content_editor
    ->save();

  // Create a group member role.
  $group_member = OgRole::create();
  $group_member
    ->setGroupType('node')
    ->setGroupBundle($this->groupBundle)
    ->setName('group_member')
    ->setLabel('Group member');
  $group_member
    ->save();

  /** @var \Drupal\og\OgMembershipInterface $membership */
  $membership = Og::getMembership($this->group, $this->user);
  $membership
    ->setRoles([
    $content_editor,
  ])
    ->save();
  $this
    ->assertTrue($membership
    ->hasRole($content_editor
    ->id()), 'The membership has the content editor role.');

  // Adding another role to the membership.
  $membership
    ->addRole($group_member);
  $this
    ->assertTrue($membership
    ->hasRole($content_editor
    ->id()), 'The membership has the content editor role.');
  $this
    ->assertTrue($membership
    ->hasRole($group_member
    ->id()), 'The membership has the group member role.');

  // Remove a role.
  $membership
    ->revokeRole($content_editor);
  $this
    ->assertFalse($membership
    ->hasRole($content_editor
    ->id()), 'The membership does not have the content editor role after is has been revoked.');
  $this
    ->assertTrue($membership
    ->hasRole($group_member
    ->id()), 'The membership has the group member role.');

  // Check if the role has permission from the membership.
  $this
    ->assertFalse($membership
    ->hasPermission(OgAccess::ADMINISTER_GROUP_PERMISSION), 'The user has permission to administer groups.');
  $membership
    ->addRole($content_editor);
  $this
    ->assertTrue($membership
    ->hasPermission(OgAccess::ADMINISTER_GROUP_PERMISSION), 'The user has permission to administer groups.');

  // Remove a role by ID.
  $membership
    ->revokeRoleById($group_member
    ->id());
  $roles_ids = $membership
    ->getRolesIds();
  $this
    ->assertFalse(in_array($group_member
    ->id(), $roles_ids), 'The group member role has been revoked.');
}