You are here

function OgPermissionsTestCase::testOgUserRoleChangePermissions in Organic groups 7.2

Verify proper permission changes by og_role_change_permissions().

File

./og.test, line 713

Class

OgPermissionsTestCase

Code

function testOgUserRoleChangePermissions() {

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

  // Create an entity.
  $entity = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $user1->uid,
  ));
  $wrapper = entity_metadata_wrapper('entity_test', $entity);
  $wrapper->{OG_GROUP_FIELD}
    ->set(1);
  $wrapper
    ->save();

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

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

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

  // Change permissions to authenticated member.
  $og_roles = array_flip(og_roles('entity_test', 'main', $entity->pid));

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

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