You are here

function OgGroupMembership::testGroupMembershipCrud in Organic groups 7

Test group group membership create, update and delete.

File

./og.test, line 649

Class

OgGroupMembership
Test group membership handeling.

Code

function testGroupMembershipCrud() {
  module_enable(array(
    'entity_feature',
  ));

  // Add OG group field to the entity_test's "main" bundle.
  og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');

  // Add OG audience field to the node's "article" bundle.
  og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
  $web_user = $this
    ->drupalCreateUser();

  // Create group, assert group manager has group membership.
  $entity = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $web_user->uid,
  ));
  $entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
  $entity
    ->save();
  $group = og_get_group('entity_test', $entity->pid);
  $group_membership = og_get_group_membership($group->gid, 'user', $web_user->uid);
  $this
    ->assertTrue($group_membership, t('Group manager has group membership in new group.'));

  // Update group, assert group manager still has group membership.
  $entity
    ->save();
  $this
    ->assertTrue($group_membership, t('Group manager still has group membership in updated group.'));

  // Create group content not associated with a group.
  $entity = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $web_user->uid,
  ));
  $entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
  $entity
    ->save();
  $group = og_get_group('entity_test', $entity->pid);
  $node = entity_create('node', array(
    'type' => 'article',
    'title' => $this
      ->randomName(),
    'uid' => $web_user->uid,
  ));
  entity_save('node', $node);
  $this
    ->assertFalse(og_get_entity_groups('node', $node), t('Node is not assigned to any group.'));
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'og_membership')
    ->propertyCondition('entity_type', 'node', '=')
    ->propertyCondition('etid', $node->nid, '=')
    ->execute();
  $this
    ->assertTrue(empty($result['og_membership']), t('Group content not assigned to a group does not have any group membership assigned to it.'));

  // Add group association.
  $values = array(
    'entity type' => 'node',
    'entity' => $node,
  );
  og_group($group->gid, $values);
  $group_membership = og_get_group_membership($group->gid, 'node', $node->nid);
  $this
    ->assertTrue($group_membership, t('Group content has group membership in group.'));

  // Change state of group association via og_group() (i.e. not changing the
  // group membership entity directly).
  $this
    ->assertEqual($group_membership->state, OG_STATE_ACTIVE, t('Group membership state is active.'));
  $values = array(
    'entity type' => 'node',
    'entity' => $node,
    'state' => OG_STATE_PENDING,
  );
  og_group($group->gid, $values);
  $group_membership = og_get_group_membership($group->gid, 'node', $node->nid);
  $this
    ->assertEqual($group_membership->state, OG_STATE_PENDING, t('Group membership state is pending.'));

  // Remove group association.
  og_ungroup($group->gid, 'node', $node);
  $group_membership = og_get_group_membership($group->gid, 'node', $node->nid);
  $this
    ->assertFalse($group_membership, t('Group content no longer has group membership in group.'));

  // Create group content associated with a group.
  $settings = array();
  $settings['type'] = 'article';
  $settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['gid'] = $group->gid;
  $node = $this
    ->drupalCreateNode($settings);
  $group_membership = og_get_group_membership($group->gid, 'node', $node->nid);
  $this
    ->assertTrue($group_membership, t('New group content has group membership in group.'));

  // Delete group content.
  $nid = $node->nid;
  node_delete($nid);
  $group_membership = og_get_group_membership($group->gid, 'node', $nid);
  $this
    ->assertFalse($group_membership, t('Group membership was deleted for deleted group content.'));

  // Delete group.
  $gid = $group->gid;
  $group
    ->delete();
  $result = $query
    ->entityCondition('entity_type', 'og_membership')
    ->propertyCondition('entity_type', 'node', '=')
    ->propertyCondition('gid', $gid, '=')
    ->execute();
  $this
    ->assertTrue(empty($result['og_membership']), t('There are no group memberships assocaited with a deleted group.'));
}