You are here

function OgGroupAudienceField::testHiddenSelectedGids in Organic groups 7

Test re-adding hidden and selected group IDs.

If a user doesn't have privileges to see a group, we make sure that if they edit the group content entity, upon save, the associations with that group isn't lost.

File

./og.test, line 1221

Class

OgGroupAudienceField
Test group audience field.

Code

function testHiddenSelectedGids() {

  // Create user.
  $permissions = array(
    'access content',
    'bypass node access',
  );
  $user1 = $this
    ->drupalCreateUser($permissions);
  $user2 = $this
    ->drupalCreateUser($permissions);
  $permissions[] = 'administer group';
  $admin_user = $this
    ->drupalCreateUser($permissions);
  $this
    ->drupalLogin($admin_user);
  $groups = array();

  // Create three entity_test group. First two belong to user1, and the third
  // one to user2.
  foreach (range(1, 3) as $id) {
    $entity = entity_create('entity_test', array(
      'name' => 'main',
      'uid' => $id < 3 ? $user1->uid : $user2->uid,
    ));
    $entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
    $entity
      ->save();
    $group = og_get_group('entity_test', $entity->pid);
    $groups[$id] = $group;
  }

  // Create a node article and assign it to the second and third groups.
  $settings = array();
  $settings['type'] = 'article';
  $settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['gid'] = $groups[2]->gid;
  $settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][1]['gid'] = $groups[3]->gid;
  $node = $this
    ->drupalCreateNode($settings);

  // Assert article is assigned to two groups.
  $this
    ->assertEqual(og_get_entity_groups('node', $node), drupal_map_assoc(array(
    $groups[2]->gid,
    $groups[3]->gid,
  )), t('Article node is assigned to two groups.'));
  $this
    ->drupalLogin($user2);
  $this
    ->drupalGet('node/' . $node->nid . '/edit');

  // TODO: Assert user can see only the third group in the audience field.
  // Unselect all (visible) options.
  $edit = array();
  $edit['group_audience[und][]'] = array();
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // FIXME: Although we have already invalidated group membersihp cache, it
  // someohow pops-up again, so we invalidate it again.
  og_group_membership_invalidate_cache();
  $this
    ->assertEqual(og_get_entity_groups('node', $node), drupal_map_assoc(array(
    $groups[2]->gid,
  )), t('Article node is still assigned to a group that is hidden from the current user.'));
}