You are here

function OgUserPermissionsTestCase::testOgUserRoleChangePermissions in Organic groups 7

Verify proper permission changes by og_role_change_permissions().

File

./og.test, line 765

Class

OgUserPermissionsTestCase

Code

function testOgUserRoleChangePermissions() {

  // TODO: We need to invalidate cache as the og permissions are fired before
  // the field was added to the article content type. But this is a hack, and
  // should be removed.
  $this
    ->resetAll();

  // Create user.
  $user1 = $this
    ->drupalCreateUser();

  // Create an entity.
  $property = OG_GROUP_FIELD;
  $entity = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $user1->uid,
  ));
  $entity->{$property}[LANGUAGE_NONE][0]['value'] = 1;
  $entity
    ->save();
  $group = og_get_group('entity_test', $entity->pid);

  // Associate user to the group.
  $user2 = $this
    ->drupalCreateUser();
  $values = array(
    'entity' => $user2,
  );
  og_group($group->gid, $values);

  // Assert the user is registered to the new group.
  $this
    ->assertTrue(og_is_member($group->gid, 'user', $user2), t('User is registered to the new group.'));

  // Verify current permissions.
  $this
    ->assertFalse(og_user_access($group->gid, 'update own article content', $user2), t('User does not have "update own article content" permission.'));
  $this
    ->assertFalse(og_user_access($group->gid, 'delete own article content', $user2), t('User does not have "delete own article content" permission.'));
  $this
    ->assertFalse(og_user_access($group->gid, 'administer group', $user2), t('User does not have "administer group" permission.'));

  // Change permissions to authenticated member.
  $roles = array_flip(og_get_global_roles());

  // Authenticated role ID.
  $rid = $roles[OG_AUTHENTICATED_ROLE];
  $permissions = array(
    'delete own article content' => 1,
    'administer group' => 1,
  );
  og_role_change_permissions($rid, $permissions);

  // Verify proper permission changes.
  $this
    ->assertFalse(og_user_access($group->gid, 'update own article content', $user2), t('User still does not have "update own article content" permission.'));
  $this
    ->assertTrue(og_user_access($group->gid, 'delete own article content', $user2), t('User now has "delete own article content" permission.'));
  $this
    ->assertTrue(og_user_access($group->gid, 'administer group', $user2), t('User now has "administer group" permission.'));
  $permissions = array(
    'delete own article content' => 1,
    'administer group' => 0,
  );
  og_role_change_permissions($rid, $permissions);
  $this
    ->assertTrue(og_user_access($group->gid, 'delete own article content', $user2), t('User still has "delete own article content" permission.'));
  $this
    ->assertFalse(og_user_access($group->gid, 'administer group', $user2), t('User no longer has "administer group" permission.'));
}